use of org.eclipse.jdt.internal.corext.template.java.CompilationUnitContextType in project che by eclipse.
the class TemplateEngine method complete.
/**
* Inspects the context of the compilation unit around <code>completionPosition</code>
* and feeds the collector with proposals.
* @param viewer the text viewer
* @param completionPosition the context position in the document of the text viewer
* @param compilationUnit the compilation unit (may be <code>null</code>)
*/
public void complete(ITextViewer viewer, int completionPosition, ICompilationUnit compilationUnit) {
IDocument document = viewer.getDocument();
if (!(fContextType instanceof CompilationUnitContextType))
return;
Point selection = viewer.getSelectedRange();
Position position = new Position(completionPosition, selection.y);
// remember selected text
String selectedText = null;
if (selection.y != 0) {
try {
selectedText = document.get(selection.x, selection.y);
document.addPosition(position);
fPositions.put(document, position);
} catch (BadLocationException e) {
}
}
CompilationUnitContext context = ((CompilationUnitContextType) fContextType).createContext(document, position, compilationUnit);
//$NON-NLS-1$
context.setVariable("selection", selectedText);
int start = context.getStart();
int end = context.getEnd();
IRegion region = new Region(start, end - start);
Template[] templates = JavaPlugin.getDefault().getTemplateStore().getTemplates();
if (selection.y == 0) {
for (int i = 0; i != templates.length; i++) {
Template template = templates[i];
if (context.canEvaluate(template)) {
fProposals.add(new TemplateProposal(template, context, region, getImage()));
}
}
} else {
if (context.getKey().length() == 0)
context.setForceEvaluation(true);
boolean multipleLinesSelected = areMultipleLinesSelected(viewer);
for (int i = 0; i != templates.length; i++) {
Template template = templates[i];
if (context.canEvaluate(template) && (!multipleLinesSelected && template.getPattern().indexOf($_WORD_SELECTION) != -1 || (multipleLinesSelected && template.getPattern().indexOf($_LINE_SELECTION) != -1))) {
fProposals.add(new TemplateProposal(templates[i], context, region, getImage()));
}
}
}
}
Aggregations