1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 304482 - [code templates] word_selection templates are not proposed

when selected word is the only word on line
This commit is contained in:
Simon Ducharme Boutin 2012-01-16 00:56:30 -05:00 committed by Marc-Andre Laperle
parent cbdaff33ff
commit 1e12841459

View file

@ -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 * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -131,8 +131,10 @@ public class TemplateEngine {
IDocument document= viewer.getDocument(); IDocument document= viewer.getDocument();
Point selection= viewer.getSelectedRange(); Point selection= viewer.getSelectedRange();
boolean multipleLinesSelected= areMultipleLinesSelected(viewer); boolean linesSelected= areLinesSelected(viewer);
if (multipleLinesSelected) { boolean showLineSelectionTemplates = linesSelected;
boolean showWordSelectionTemplates = !linesSelected || isOnlyWordOnLine(viewer);
if (linesSelected) {
// adjust line selection to start at column 1 and end at line delimiter // adjust line selection to start at column 1 and end at line delimiter
try { try {
IRegion startLine = document.getLineInformationOfOffset(selection.x); IRegion startLine = document.getLineInformationOfOffset(selection.x);
@ -170,14 +172,15 @@ public class TemplateEngine {
} else { } else {
if (multipleLinesSelected || context.getKey().length() == 0) if (linesSelected || context.getKey().length() == 0)
context.setForceEvaluation(true); context.setForceEvaluation(true);
for (int i= 0; i != templates.length; i++) { for (int i= 0; i != templates.length; i++) {
Template template= templates[i]; Template template= templates[i];
if (context.canEvaluate(template) && if (context.canEvaluate(template) &&
template.getContextTypeId().equals(context.getContextType().getId()) && 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)); 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 * @return <code>true</code> if one or multiple lines are selected
*/ */
private boolean areMultipleLinesSelected(ITextViewer viewer) { private boolean areLinesSelected(ITextViewer viewer) {
if (viewer == null) if (viewer == null)
return false; return false;
@ -220,5 +223,33 @@ public class TemplateEngine {
return false; 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;
}
}
} }