Search in sources :

Example 1 with TemplateContext

use of org.eclipse.jface.text.templates.TemplateContext in project linuxtools by eclipse.

the class SpecfileCompletionProcessor method computeTemplateProposals.

/**
 * Compute the templates proposals, these proposals are contextual on
 * sections. Return an array of template proposals for the given viewer,
 * region, specfile, prefix.
 *
 * @param viewer
 *            the viewer for which the context is created
 * @param region
 *            the region into <code>document</code> for which the context is
 *            created
 * @param specfile
 *            the specfile element
 * @param prefix
 *            the prefix string
 * @return a ICompletionProposal[]
 */
private List<? extends ICompletionProposal> computeTemplateProposals(ITextViewer viewer, IRegion region, Specfile specfile, String prefix) {
    TemplateContext context = createContext(viewer, region, specfile);
    List<TemplateProposal> matches = new ArrayList<>();
    if (context == null) {
        return matches;
    }
    ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
    // $NON-NLS-1$
    context.setVariable("selection", selection.getText());
    String id = context.getContextType().getId();
    Template[] templates = Activator.getDefault().getTemplateStore().getTemplates(id);
    for (Template template : templates) {
        try {
            context.getContextType().validate(template.getPattern());
        } catch (TemplateException e) {
            continue;
        }
        int relevance = getRelevance(template, prefix);
        if (relevance > 0) {
            matches.add(new TemplateProposal(template, context, region, Activator.getDefault().getImage(TEMPLATE_ICON), relevance));
        }
    }
    Collections.sort(matches, (t1, t2) -> (t2.getRelevance() - t1.getRelevance()));
    return matches;
}
Also used : TemplateProposal(org.eclipse.jface.text.templates.TemplateProposal) TemplateException(org.eclipse.jface.text.templates.TemplateException) ArrayList(java.util.ArrayList) DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) TemplateContext(org.eclipse.jface.text.templates.TemplateContext) ITextSelection(org.eclipse.jface.text.ITextSelection) Template(org.eclipse.jface.text.templates.Template)

Example 2 with TemplateContext

use of org.eclipse.jface.text.templates.TemplateContext in project liferay-ide by liferay.

the class AddJSFPortletOperation method createModeJSPFiles.

@Override
protected IStatus createModeJSPFiles() {
    IDataModel dm = getDataModel();
    StringBuffer jsfNamespaces = new StringBuffer();
    if (getDataModel().getBooleanProperty(ICE_FACES)) {
        jsfNamespaces.append("\txmlns:ace=\"http://www.icefaces.org/icefaces/components\"\n");
        jsfNamespaces.append("\txmlns:icecore=\"http://www.icefaces.org/icefaces/core\"\n");
    }
    if (getDataModel().getBooleanProperty(LIFERAY_FACES_ALLOY)) {
        jsfNamespaces.append("\txmlns:aui=\"http://liferay.com/faces/aui\"\n");
    }
    if (getDataModel().getBooleanProperty(PRIME_FACES)) {
        jsfNamespaces.append("\txmlns:p=\"http://primefaces.org/ui\"\n");
    }
    if (getDataModel().getBooleanProperty(RICH_FACES)) {
        jsfNamespaces.append("\txmlns:rich=\"http://richfaces.org/rich\"\n");
    }
    if (getDataModel().getBooleanProperty(STANDARD_JSF)) {
        jsfNamespaces.append("");
    }
    TemplateContext context = new DocumentTemplateContext(portletContextType, new Document(), 0, 0);
    context.setVariable("portlet_name", getDataModel().getStringProperty(PORTLET_NAME));
    context.setVariable("jsf_namespaces", jsfNamespaces.toString());
    if (dm.getBooleanProperty(VIEW_MODE)) {
        createResourceForMode("javax.portlet.faces.defaultViewId.view", JSF_VIEW_MODE_TEMPLATE, context);
    }
    if (dm.getBooleanProperty(EDIT_MODE)) {
        createResourceForMode("javax.portlet.faces.defaultViewId.edit", JSF_EDIT_MODE_TEMPLATE, context);
    }
    if (dm.getBooleanProperty(HELP_MODE)) {
        createResourceForMode("javax.portlet.faces.defaultViewId.help", JSF_HELP_MODE_TEMPLATE, context);
    }
    return Status.OK_STATUS;
}
Also used : DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) 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 3 with TemplateContext

use of org.eclipse.jface.text.templates.TemplateContext in project liferay-ide by liferay.

the class StructuresContentAssistProcessor method getTemplateProposals.

private ICompletionProposal[] getTemplateProposals(IProject eclipseprj, ITextViewer viewer, int offset, String contextTypeId, Node currentNode, String prefix) {
    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, contextTypeId);
    if (context == null) {
        return new ICompletionProposal[0];
    }
    // name of the selection variables {line, word}_selection
    // $NON-NLS-1$
    context.setVariable("selection", selection.getText());
    StructuresTemplateContext templateContext = StructuresTemplateContext.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 = PortalImages.IMG_USER_TEMPLATE;
    List<TemplateProposal> matches = new ArrayList<TemplateProposal>();
    TemplateStore store = PortalUI.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);
            }
        }
    }
    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()]);
}
Also used : TemplateProposal(org.eclipse.jface.text.templates.TemplateProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) TemplateContext(org.eclipse.jface.text.templates.TemplateContext) Image(org.eclipse.swt.graphics.Image) TemplateStore(org.eclipse.jface.text.templates.persistence.TemplateStore) ITextSelection(org.eclipse.jface.text.ITextSelection) Template(org.eclipse.jface.text.templates.Template)

Example 4 with TemplateContext

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

Example 5 with TemplateContext

use of org.eclipse.jface.text.templates.TemplateContext in project webtools.sourceediting by eclipse.

the class HTMLTemplateCompletionProcessor 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()]);
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ArrayList(java.util.ArrayList) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) ArrayList(java.util.ArrayList) List(java.util.List) TemplateContext(org.eclipse.jface.text.templates.TemplateContext) ReplaceNameTemplateContext(org.eclipse.wst.xml.ui.internal.contentassist.ReplaceNameTemplateContext) ITextSelection(org.eclipse.jface.text.ITextSelection) Template(org.eclipse.jface.text.templates.Template)

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