offset = selection.getOffset() + selection.getLength();
}
String prefix = extractPrefix(viewer, offset);
Region region = new Region(offset - prefix.length(), prefix.length());
TemplateContext context = createContext(viewer, region, offset);
if (context == null) {
return new ICompletionProposal[0];
}
IndexedRegion treeNode = ContentAssistUtils.getNodeAt(viewer, offset);
currentTapestryComponent = (Node) treeNode;
char preChar=0,preChar2=0;
//In situation user input <, we should store the char before cursor into preChar and even preChar2
if(currentTapestryComponent.getNodeValue() != null){
for(int i=offset-treeNode.getStartOffset()-1; i>=0; i--){
char temp = currentTapestryComponent.getNodeValue().charAt(i);
if(temp != 9 && temp != 10 && temp != 32){
if(preChar == 0)
preChar = temp;
else{
preChar2 = temp;
break;
}
}
}
}
while ((currentTapestryComponent != null) && (currentTapestryComponent.getNodeType() == Node.TEXT_NODE) && (currentTapestryComponent.getParentNode() != null)) {
currentTapestryComponent = currentTapestryComponent.getParentNode();
}
// name of the selection variables {line, word}_selection
context.setVariable("selection", selection.getText()); //$NON-NLS-1$
System.out.println(">>>>> Get template list by context id:" + context.getContextType().getId() + " selection:" + selection.getText());
Template[] templates = getTemplates((IDOMNode) treeNode, offset, context.getContextType().getId(), preChar, preChar2);
List matches = new ArrayList();
for (int i = 0; i < templates.length; i++) {
Template template = templates[i];
try {
context.getContextType().validate(template.getPattern());
}
catch (TemplateException e) {
continue;
}
if (template.matches(prefix, context.getContextType().getId())) {
int place = getRelevance(template, prefix);
matches.add(createProposal(template, context, (IRegion) region, place));
}
}