use of org.eclipse.jface.text.templates.TemplateContext in project webtools.sourceediting by eclipse.
the class JSPTemplateCompletionProcessor method computeCompletionProposals.
/*
* Copied from super class except instead of calling createContext(viewer,
* region) call createContext(viewer, region, offset) instead
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
// adjust offset to end of normalized selection
if (selection.getOffset() == offset)
offset = selection.getOffset() + selection.getLength();
String prefix = extractPrefix(viewer, offset);
Region region = new Region(offset - prefix.length(), prefix.length());
// If there's no prefix, check if we're in a tag open region
if (prefix.trim().length() == 0) {
IndexedRegion treeNode = ContentAssistUtils.getNodeAt(viewer, offset);
if (treeNode instanceof IDOMText) {
IDOMNode node = (IDOMNode) treeNode;
// Check each region in the node, if the offset is after a tag region, replace it with the template
IStructuredDocumentRegion cursor = node.getFirstStructuredDocumentRegion();
IStructuredDocumentRegion end = node.getLastStructuredDocumentRegion();
do {
if (cursor != null && DOMRegionContext.XML_TAG_OPEN.equals(cursor.getType()) && cursor.getStartOffset() == offset - 1) {
// We have a tag to replace
offset = cursor.getStartOffset();
region = new Region(cursor.getStartOffset(), cursor.getLength());
break;
}
} while (cursor != end && (cursor = cursor.getNext()) != null);
}
}
TemplateContext context = createContext(viewer, region, offset);
if (context == null)
return new ICompletionProposal[0];
// name of the selection variables {line, word}_selection
// //$NON-NLS-1$
context.setVariable("selection", selection.getText());
Template[] templates = getTemplates(context.getContextType().getId());
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()))
matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
}
Collections.sort(matches, fgProposalComparator);
return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
}
use of org.eclipse.jface.text.templates.TemplateContext in project webtools.sourceediting by eclipse.
the class NewDTDTemplatesWizardPage method getTemplateString.
/**
* Returns template string to insert.
*
* @return String to insert or null if none is to be inserted
*/
String getTemplateString() {
String templateString = null;
Template template = getSelectedTemplate();
if (template != null) {
TemplateContextType contextType = DTDUIPlugin.getDefault().getTemplateContextRegistry().getContextType(TemplateContextTypeIdsDTD.NEW);
IDocument document = new Document();
TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0);
try {
TemplateBuffer buffer = context.evaluate(template);
templateString = buffer.getString();
} catch (Exception e) {
// $NON-NLS-1$
Logger.log(Logger.WARNING_DEBUG, "Could not create template for new dtd", e);
}
}
return templateString;
}
use of org.eclipse.jface.text.templates.TemplateContext in project webtools.sourceediting by eclipse.
the class NewXMLTemplatesWizardPage method getTemplateString.
/**
* Returns template string to insert.
*
* @return String to insert or null if none is to be inserted
*/
String getTemplateString() {
String templateString = null;
Template template = getSelectedTemplate();
if (template != null) {
TemplateContextType contextType = XMLUIPlugin.getDefault().getTemplateContextRegistry().getContextType(TemplateContextTypeIdsXML.NEW);
IDocument document = new Document();
TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0);
try {
TemplateBuffer buffer = context.evaluate(template);
templateString = buffer.getString();
} catch (Exception e) {
// $NON-NLS-1$
Logger.log(Logger.WARNING_DEBUG, "Could not create template for new xml", e);
}
}
return templateString;
}
use of org.eclipse.jface.text.templates.TemplateContext in project webtools.sourceediting by eclipse.
the class XPathTemplateCompletionProcessor method computeCompletionProposals.
/*
* Copied from super class except instead of calling createContext(viewer,
* region) call createContext(viewer, region, offset) instead
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
// adjust offset to end of normalized selection
if (selection.getOffset() == offset) {
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];
}
// name of the selection variables {line, word}_selection
// $NON-NLS-1$
context.setVariable("selection", selection.getText());
Template[] templates = getTemplates(context.getContextType().getId());
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())) {
matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
}
}
Collections.sort(matches, fgProposalComparator);
return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
}
use of org.eclipse.jface.text.templates.TemplateContext in project syncope by apache.
the class HTMLTemplateAssistProcessor method computeCompletionProposals.
public ICompletionProposal[] computeCompletionProposals(final ITextViewer viewer, final int offsetinp) {
int offset = offsetinp;
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
// adjust offset to end of normalized selection
if (selection.getOffset() == offset) {
offset = selection.getOffset() + selection.getLength();
}
String prefix = extractPrefix(viewer, offset);
Region region = new Region(offset - prefix.length(), prefix.length());
TemplateContext context = createContext(viewer, region);
if (context == null) {
return new ICompletionProposal[0];
}
context.setVariable("selection", selection.getText());
Template[] templates = getTemplates(context.getContextType().getId());
List<ICompletionProposal> matches = new ArrayList<ICompletionProposal>();
for (int i = 0; i < templates.length; i++) {
Template template = templates[i];
try {
context.getContextType().validate(template.getPattern());
} catch (final TemplateException e) {
continue;
}
if (template.getName().startsWith(prefix) && template.matches(prefix, context.getContextType().getId())) {
matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
}
}
return matches.toArray(new ICompletionProposal[matches.size()]);
}
Aggregations