use of org.eclipse.jface.text.templates.TemplateContext in project liferay-ide by liferay.
the class AddPortletOperation method createModeJSPFiles.
@SuppressWarnings("unchecked")
protected IStatus createModeJSPFiles() {
IDataModel dm = getDataModel();
TemplateContext context = new DocumentTemplateContext(portletContextType, new Document(), 0, 0);
context.setVariable("portlet_display_name", getDataModel().getStringProperty(DISPLAY_NAME));
List<ParamValue> initParams = (List<ParamValue>) getDataModel().getProperty(INIT_PARAMS);
String initParamSuffix = null;
if (initNames61[0].equals(initParams.get(0).getName())) {
initParamSuffix = "template";
} else {
initParamSuffix = "jsp";
}
if (dm.getBooleanProperty(ABOUT_MODE)) {
createResourceForMode("about-" + initParamSuffix, ABOUT_MODE_TEMPLATE, context);
}
if (dm.getBooleanProperty(CONFIG_MODE)) {
createResourceForMode("config-" + initParamSuffix, CONFIG_MODE_TEMPLATE, context);
}
if (dm.getBooleanProperty(EDIT_MODE)) {
createResourceForMode("edit-" + initParamSuffix, EDIT_MODE_TEMPLATE, context);
}
if (dm.getBooleanProperty(EDITDEFAULTS_MODE)) {
createResourceForMode("edit-defaults-" + initParamSuffix, EDITDEFAULTS_MODE_TEMPLATE, context);
}
if (dm.getBooleanProperty(EDITGUEST_MODE)) {
createResourceForMode("edit-guest-" + initParamSuffix, EDITGUEST_MODE_TEMPLATE, context);
}
if (dm.getBooleanProperty(HELP_MODE)) {
createResourceForMode("help-" + initParamSuffix, HELP_MODE_TEMPLATE, context);
}
if (dm.getBooleanProperty(PREVIEW_MODE)) {
createResourceForMode("preview-" + initParamSuffix, PREVIEW_MODE_TEMPLATE, context);
}
if (dm.getBooleanProperty(PRINT_MODE)) {
createResourceForMode("print-" + initParamSuffix, PRINT_MODE_TEMPLATE, context);
}
if (dm.getBooleanProperty(VIEW_MODE)) {
createResourceForMode("view-" + initParamSuffix, VIEW_MODE_TEMPLATE, context);
}
return Status.OK_STATUS;
}
use of org.eclipse.jface.text.templates.TemplateContext in project mylyn.docs by eclipse.
the class MarkupTemplateCompletionProcessor method computeCompletionProposals.
/**
* Override to improve matching accuracy
*/
@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);
if (context == null) {
return new ICompletionProposal[0];
}
Region selectionRegion = new Region(selection.getOffset(), selection.getLength());
TemplateContext selectionContext = createContext(viewer, selectionRegion);
int lineOffset = 0;
try {
IRegion lineInformationOfOffset = viewer.getDocument().getLineInformationOfOffset(offset);
lineOffset = offset - lineInformationOfOffset.getOffset();
} catch (BadLocationException e1) {
// ignore
}
String selectionText = selection.getText();
// $NON-NLS-1$
context.setVariable("selection", selectionText);
// $NON-NLS-1$
selectionContext.setVariable("selection", selectionText);
// $NON-NLS-1$
context.setVariable("text", selectionText);
// $NON-NLS-1$
selectionContext.setVariable("text", selectionText);
Template[] templates = getTemplates(context.getContextType().getId());
List<ICompletionProposal> matches = new ArrayList<>(templates.length);
for (Template template : templates) {
try {
context.getContextType().validate(template.getPattern());
} catch (TemplateException e) {
continue;
}
if (!template.matches(prefix, context.getContextType().getId())) {
continue;
}
boolean selectionBasedMatch = isSelectionBasedMatch(template, context);
if (template.getName().startsWith(prefix) || selectionBasedMatch) {
int relevance = getRelevance(template, lineOffset, prefix);
if (selectionBasedMatch) {
matches.add(createProposal(template, selectionContext, (IRegion) selectionRegion, relevance));
} else {
matches.add(createProposal(template, context, (IRegion) region, relevance));
}
}
}
Collections.sort(matches, proposalComparator);
return matches.toArray(new ICompletionProposal[matches.size()]);
}
Aggregations