Search in sources :

Example 21 with TemplateContext

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;
}
Also used : DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) ParamValue(org.eclipse.jst.j2ee.common.ParamValue) List(java.util.List) IDataModel(org.eclipse.wst.common.frameworks.datamodel.IDataModel) DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) TemplateContext(org.eclipse.jface.text.templates.TemplateContext) Document(org.eclipse.jface.text.Document)

Example 22 with TemplateContext

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()]);
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) ArrayList(java.util.ArrayList) TemplateContext(org.eclipse.jface.text.templates.TemplateContext) ITextSelection(org.eclipse.jface.text.ITextSelection) IRegion(org.eclipse.jface.text.IRegion) Template(org.eclipse.jface.text.templates.Template) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

TemplateContext (org.eclipse.jface.text.templates.TemplateContext)22 Template (org.eclipse.jface.text.templates.Template)19 DocumentTemplateContext (org.eclipse.jface.text.templates.DocumentTemplateContext)12 ArrayList (java.util.ArrayList)10 ITextSelection (org.eclipse.jface.text.ITextSelection)10 TemplateException (org.eclipse.jface.text.templates.TemplateException)10 Document (org.eclipse.jface.text.Document)9 IRegion (org.eclipse.jface.text.IRegion)9 Region (org.eclipse.jface.text.Region)9 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)9 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)9 IDocument (org.eclipse.jface.text.IDocument)7 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)7 List (java.util.List)6 ReplaceNameTemplateContext (org.eclipse.wst.xml.ui.internal.contentassist.ReplaceNameTemplateContext)4 BadLocationException (org.eclipse.jface.text.BadLocationException)3 TemplateProposal (org.eclipse.jface.text.templates.TemplateProposal)3 TemplateStore (org.eclipse.jface.text.templates.persistence.TemplateStore)2 Image (org.eclipse.swt.graphics.Image)2 IDataModel (org.eclipse.wst.common.frameworks.datamodel.IDataModel)2