use of org.eclipse.jface.text.templates.TemplateContext in project webtools.sourceediting by eclipse.
the class NewHTMLTemplatesWizardPage 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 = HTMLUIPlugin.getDefault().getTemplateContextRegistry().getContextType(TemplateContextTypeIdsHTML.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 html", e);
}
}
return templateString;
}
use of org.eclipse.jface.text.templates.TemplateContext in project webtools.sourceediting by eclipse.
the class NewJSPTemplatesWizardPage 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 = JSPUIPlugin.getDefault().getTemplateContextRegistry().getContextType(TemplateContextTypeIdsJSP.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 jsp", e);
}
}
return templateString;
}
use of org.eclipse.jface.text.templates.TemplateContext in project webtools.sourceediting by eclipse.
the class NewTagTemplatesWizardPage 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 = JSPUIPlugin.getDefault().getTemplateContextRegistry().getContextType(TemplateContextTypeIdsJSP.NEW_TAG);
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 jsp tag", e);
}
}
return templateString;
}
use of org.eclipse.jface.text.templates.TemplateContext in project webtools.sourceediting by eclipse.
the class XSLTemplateCompletionProcessor method computeCompletionProposals.
/*
* Copied from super class except instead of calling createContext(viewer,
* region) call createContext(viewer, region, offset) instead
*/
@Override
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 liferay-ide by liferay.
the class KaleoContentAssistProcessor method _getTemplateProposals.
private ICompletionProposal[] _getTemplateProposals(IProject eclipseprj, ITextViewer viewer, int offset, String contextTypeId, Node currentNode, String prefix) {
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
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, contextTypeId);
if (context == null) {
return new ICompletionProposal[0];
}
// name of the selection variables {line, word}_selection
context.setVariable("selection", selection.getText());
KaleoTemplateContext templateContext = KaleoTemplateContext.fromId(contextTypeId);
/*
* add the user defined templates - separate them from the rest of the
* templates so that we know what they are and can assign proper icon to
* them.
*/
Image image = KaleoImages.IMG_USER_TEMPLATE;
List<TemplateProposal> matches = new ArrayList<>();
TemplateStore store = KaleoUI.getDefault().getTemplateStore();
if (store != null) {
Template[] templates = store.getTemplates(contextTypeId);
for (Template template : templates) {
TemplateProposal proposal = _createProposalForTemplate(prefix, region, context, image, template, true);
if (proposal != null) {
matches.add(proposal);
}
}
}
/*
* if( templateContext == KaleoTemplateContext.CONFIGURATION ) { image =
* KaleoImages.IMG_PARAMETER; } else { other suggestions from the
* templatecontext are to be text inside the element, not actual
* elements..
*/
image = null;
// }
Template[] templates = templateContext.getTemplates(eclipseprj, currentNode, prefix);
for (Template template : templates) {
TemplateProposal proposal = _createProposalForTemplate(prefix, region, context, image, template, false);
if (proposal != null) {
matches.add(proposal);
}
}
return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
}
Aggregations