Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Leherbauer2010-01-08 11:11:22 +0000
committerAnton Leherbauer2010-01-08 11:11:22 +0000
commit647c85c5c0a36b3401851c6f5790b498dc022c81 (patch)
treea21b12bc42f1311169b3f39aa9515c9ffc2d757f
parent1e262f08ea2af40192c64138b75727e1be97795b (diff)
downloadorg.eclipse.cdt-647c85c5c0a36b3401851c6f5790b498dc022c81.tar.gz
org.eclipse.cdt-647c85c5c0a36b3401851c6f5790b498dc022c81.tar.xz
org.eclipse.cdt-647c85c5c0a36b3401851c6f5790b498dc022c81.zip
Bug 298554 - Code templates should be proposed if line is completely selected except for whitespaces
Patch by Marc-Andre Laperle
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/template/TemplateEngine.java27
1 files changed, 19 insertions, 8 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 7db1cfcc144..3f87f9c9823 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, 2008 IBM Corporation and others.
+ * Copyright (c) 2000, 2010 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
@@ -17,6 +17,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.text.BadLocationException;
@@ -61,6 +63,8 @@ public class TemplateEngine {
private ArrayList<ICompletionProposal> fProposals= new ArrayList<ICompletionProposal>();
/** Positions created on the key documents to remove in reset. */
private final Map<IDocument, Position> fPositions= new HashMap<IDocument, Position>();
+ /** Pattern to match the start of a line content */
+ private final Pattern fStartOfLineContentPattern = Pattern.compile("[^ \t]"); //$NON-NLS-1$
public class CTemplateProposal extends TemplateProposal implements ICCompletionProposal {
@@ -201,12 +205,12 @@ public class TemplateEngine {
}
}
-
+
/**
- * Returns <code>true</code> if one line is completely selected or if multiple lines are selected.
- * Being completely selected means that all characters except the new line characters are
- * selected.
- *
+ * Returns <code>true</code> if one line is completely selected or if multiple lines are selected. Being
+ * completely selected means that all characters are selected except the new line characters and
+ * leading/trailing spaces.
+ *
* @return <code>true</code> if one or multiple lines are selected
*/
private boolean areMultipleLinesSelected(ITextViewer viewer) {
@@ -221,9 +225,16 @@ public class TemplateEngine {
IDocument document= viewer.getDocument();
int startLine= document.getLineOfOffset(s.x);
- int endLine= document.getLineOfOffset(s.x + s.y);
IRegion line= document.getLineInformation(startLine);
- return startLine != endLine || (s.x == line.getOffset() && s.y == line.getLength());
+
+ String lineContent = document.get(line.getOffset(), line.getLength());
+ Matcher m = fStartOfLineContentPattern.matcher(lineContent);
+ int lineContentStart = 0;
+ if(m.find())
+ lineContentStart = m.start() + line.getOffset();
+ int lineContentLength = lineContent.trim().length();
+
+ return s.x <= lineContentStart && s.x + s.y >= lineContentStart + lineContentLength;
} catch (BadLocationException x) {
return false;

Back to the top