summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Ducharme Boutin2012-01-16 00:56:30 (EST)
committer Marc-Andre Laperle2012-01-16 00:56:30 (EST)
commit1e1284145943a748f676748cc5169d4da5bfafa8 (patch)
treed33ac394351241b83237cb58949191b9470f93df
parentcbdaff33ffc7eef7c4ce7dc5e43683fc05cf7a19 (diff)
downloadorg.eclipse.cdt-1e1284145943a748f676748cc5169d4da5bfafa8.zip
org.eclipse.cdt-1e1284145943a748f676748cc5169d4da5bfafa8.tar.gz
org.eclipse.cdt-1e1284145943a748f676748cc5169d4da5bfafa8.tar.bz2
Bug 304482 - [code templates] word_selection templates are not proposed
when selected word is the only word on line
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/template/TemplateEngine.java43
1 files changed, 37 insertions, 6 deletions
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/template/TemplateEngine.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/template/TemplateEngine.java
index ed0430b..486873a 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/template/TemplateEngine.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/template/TemplateEngine.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2010 IBM Corporation and others.
+ * Copyright (c) 2000, 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -131,8 +131,10 @@ public class TemplateEngine {
IDocument document= viewer.getDocument();
Point selection= viewer.getSelectedRange();
- boolean multipleLinesSelected= areMultipleLinesSelected(viewer);
- if (multipleLinesSelected) {
+ boolean linesSelected= areLinesSelected(viewer);
+ boolean showLineSelectionTemplates = linesSelected;
+ boolean showWordSelectionTemplates = !linesSelected || isOnlyWordOnLine(viewer);
+ if (linesSelected) {
// adjust line selection to start at column 1 and end at line delimiter
try {
IRegion startLine = document.getLineInformationOfOffset(selection.x);
@@ -170,14 +172,15 @@ public class TemplateEngine {
} else {
- if (multipleLinesSelected || context.getKey().length() == 0)
+ if (linesSelected || context.getKey().length() == 0)
context.setForceEvaluation(true);
for (int i= 0; i != templates.length; i++) {
Template template= templates[i];
if (context.canEvaluate(template) &&
template.getContextTypeId().equals(context.getContextType().getId()) &&
- (!multipleLinesSelected && template.getPattern().indexOf($_WORD_SELECTION) != -1 || (multipleLinesSelected && template.getPattern().indexOf($_LINE_SELECTION) != -1)))
+ ((showWordSelectionTemplates && template.getPattern().indexOf($_WORD_SELECTION) != -1 ||
+ (showLineSelectionTemplates && template.getPattern().indexOf($_LINE_SELECTION) != -1))))
{
fProposals.add(new CTemplateProposal(templates[i], context, region, image));
}
@@ -193,7 +196,7 @@ public class TemplateEngine {
*
* @return <code>true</code> if one or multiple lines are selected
*/
- private boolean areMultipleLinesSelected(ITextViewer viewer) {
+ private boolean areLinesSelected(ITextViewer viewer) {
if (viewer == null)
return false;
@@ -220,5 +223,33 @@ public class TemplateEngine {
return false;
}
}
+
+ /**
+ * Returns <code>true</code> if there's only one word on the line
+ *
+ * @return <code>true</code> if only one word is on the line
+ */
+ private boolean isOnlyWordOnLine(ITextViewer viewer) {
+ if (viewer == null)
+ return false;
+
+ Point s= viewer.getSelectedRange();
+ if (s.y == 0)
+ return false;
+
+ try {
+
+ IDocument document= viewer.getDocument();
+ int startLine= document.getLineOfOffset(s.x);
+ IRegion line= document.getLineInformation(startLine);
+
+ String lineContent = document.get(line.getOffset(), line.getLength());
+
+ return lineContent.trim().lastIndexOf(' ', s.x) == -1;
+
+ } catch (BadLocationException x) {
+ return false;
+ }
+ }
}