int length = selectionText.length() + prefix.length();
Region region = new Region(offset, length);
String templatePattern;
Boolean hasSelectionVariable = false;
IDocument doc = viewer.getDocument();
TemplateContext context = createContext(viewer, region);
if (context == null || doc.getLength() < 1)
return new ICompletionProposal[0];
Template[] templates = getTemplates(context.getContextType().getId());
// TODO: move these variables to the CFTemplateContext class
context.setVariable("selection", selectionText); // name of the selection variables {line, word}_selection //$NON-NLS-1$
if(((ICFDocument)doc).getCFDocument() != null) {
context.setVariable("currentfile", ((ICFDocument)doc).getCFDocument().getFilename());
}
boolean inChevron = false;
try {
if (doc.getLength() > offset + 1 && (doc.getChar(offset - 1) == '<' || doc.getChar(offset + 1) == '>')) {
inChevron = true;
}
} catch (BadLocationException e1) {
e1.printStackTrace();
}
List matches = new ArrayList();
for (int i = 0; i < templates.length; i++) {
Template template = templates[i];
hasSelectionVariable = false;
try {
templatePattern = template.getPattern();
context.getContextType().validate(templatePattern);
if((templatePattern.indexOf("${word_selection}") > 0 || templatePattern.indexOf("${line_selection}") > 0 )
&& selectionText.length() > 0){
hasSelectionVariable = true;
}
} catch (TemplateException e) {
continue;
}
relavance = getRelevance(template, prefix);
if (template.matches(prefix, context.getContextType().getId()) && relavance > 0 && selectionText.length() == 0 || hasSelectionVariable && template.matches("", context.getContextType().getId())) {
if(inChevron && templatePattern.startsWith("<") && templatePattern.endsWith(">")){
region = getRegionNoChevrons(doc, offset, length);
context = createContext(viewer, region);
context.setVariable("selection", selectionText);
}
matches.add(createProposal(template, context, (IRegion) region, relavance));
}
}