summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Ducharme Boutin2012-01-16 00:56:30 (EST)
committerMarc-Andre Laperle2012-01-16 01:32:44 (EST)
commit065c2fc46f9e67a4cc31d38c87b855c9e155d5e0 (patch)
tree44ace6e7e504045b00422ab27b136b61865ddad1
parent6fd8c80d5bd0388e656a65aaad92b63515d7a89e (diff)
downloadorg.eclipse.cdt-065c2fc46f9e67a4cc31d38c87b855c9e155d5e0.zip
org.eclipse.cdt-065c2fc46f9e67a4cc31d38c87b855c9e155d5e0.tar.gz
org.eclipse.cdt-065c2fc46f9e67a4cc31d38c87b855c9e155d5e0.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 9f55a72..bfebbde 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
@@ -129,8 +129,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);
@@ -168,14 +170,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));
}
@@ -191,7 +194,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;
@@ -218,5 +221,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;
+ }
+ }
}