Search in sources :

Example 1 with TemplateProposal

use of org.eclipse.jface.text.templates.TemplateProposal 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 TemplateProposal

use of org.eclipse.jface.text.templates.TemplateProposal 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 3 with TemplateProposal

use of org.eclipse.jface.text.templates.TemplateProposal in project dsl-devkit by dsldevkit.

the class CheckCfgTemplateProposalProvider method addCatalogConfigurations.

/**
 * Adds the populated check configuration.
 *
 * @param templateContext
 *          the template context
 * @param context
 *          the context
 * @param acceptor
 *          the acceptor
 */
@SuppressWarnings("all")
private void addCatalogConfigurations(final TemplateContext templateContext, final ContentAssistContext context, final ITemplateAcceptor acceptor) {
    // $NON-NLS-1$
    final String templateName = "Add all registered catalogs";
    // $NON-NLS-1$
    final String templateDescription = "configures all missing catalogs";
    final String contextTypeId = templateContext.getContextType().getId();
    if (context.getRootModel() instanceof CheckConfiguration) {
        final CheckConfiguration conf = (CheckConfiguration) context.getRootModel();
        List<IEObjectDescription> allElements = Lists.newArrayList(scopeProvider.getScope(conf, CheckcfgPackage.Literals.CONFIGURED_CATALOG__CATALOG).getAllElements());
        StringBuilder builder = new StringBuilder();
        for (IEObjectDescription description : allElements) {
            if (description.getEObjectOrProxy() instanceof CheckCatalog) {
                CheckCatalog catalog = (CheckCatalog) description.getEObjectOrProxy();
                if (catalog.eIsProxy()) {
                    catalog = (CheckCatalog) EcoreUtil.resolve(catalog, conf);
                }
                if (isCatalogConfigured(conf, catalog)) {
                    continue;
                } else if (allElements.indexOf(description) > 0) {
                    builder.append(Strings.newLine());
                }
                final String catalogName = qualifiedNameValueConverter.toString(description.getQualifiedName().toString());
                // $NON-NLS-1$ //$NON-NLS-2$
                builder.append("catalog ").append(catalogName).append(" {}").append(Strings.newLine());
            }
        }
        if (builder.length() > 0) {
            // $NON-NLS-1$
            builder.append("${cursor}");
            Template t = new Template(templateName, templateDescription, contextTypeId, builder.toString(), true);
            TemplateProposal tp = createProposal(t, templateContext, context, images.forConfiguredCatalog(), getRelevance(t));
            acceptor.accept(tp);
        }
    }
}
Also used : TemplateProposal(org.eclipse.jface.text.templates.TemplateProposal) CheckConfiguration(com.avaloq.tools.ddk.checkcfg.checkcfg.CheckConfiguration) CheckCatalog(com.avaloq.tools.ddk.check.check.CheckCatalog) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Template(org.eclipse.jface.text.templates.Template)

Example 4 with TemplateProposal

use of org.eclipse.jface.text.templates.TemplateProposal in project statecharts by Yakindu.

the class SGenTemplateProposalProvider method createFeatureConfigurationTemplates.

private void createFeatureConfigurationTemplates(TemplateContext templateContext, ContentAssistContext context, ITemplateAcceptor acceptor) {
    GeneratorModel model = (GeneratorModel) EcoreUtil2.getRootContainer(context.getCurrentModel());
    IGeneratorDescriptor generatorDescriptor = GeneratorExtensions.getGeneratorDescriptor(model.getGeneratorId());
    Iterable<ILibraryDescriptor> libraryDescriptor = LibraryExtensions.getLibraryDescriptors(generatorDescriptor.getLibraryIDs());
    for (ILibraryDescriptor desc : libraryDescriptor) {
        ResourceSet set = new ResourceSetImpl();
        Resource resource = set.getResource(desc.getURI(), true);
        FeatureTypeLibrary lib = (FeatureTypeLibrary) resource.getContents().get(0);
        EList<FeatureType> types = lib.getTypes();
        for (FeatureType featureType : types) {
            Template template = new Template(featureType.getName() + " feature", "Creates feature " + featureType.getName(), featureType.getName(), creator.createProposal(featureType, desc.createFeatureValueProvider(SGenActivator.getInstance().getInjector(SGenActivator.ORG_YAKINDU_SCT_GENERATOR_GENMODEL_SGEN)), context.getCurrentModel()), false);
            TemplateProposal proposal = createProposal(template, templateContext, context, getImage(template), getRelevance(template));
            acceptor.accept(proposal);
        }
    }
}
Also used : TemplateProposal(org.eclipse.jface.text.templates.TemplateProposal) FeatureType(org.yakindu.sct.model.sgen.FeatureType) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) FeatureTypeLibrary(org.yakindu.sct.model.sgen.FeatureTypeLibrary) Resource(org.eclipse.emf.ecore.resource.Resource) GeneratorModel(org.yakindu.sct.model.sgen.GeneratorModel) IGeneratorDescriptor(org.yakindu.sct.generator.core.extensions.IGeneratorDescriptor) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ILibraryDescriptor(org.yakindu.sct.generator.core.extensions.ILibraryDescriptor) Template(org.eclipse.jface.text.templates.Template)

Example 5 with TemplateProposal

use of org.eclipse.jface.text.templates.TemplateProposal in project titan.EclipsePlug-ins by eclipse.

the class ProposalCollector method addTemplateProposal.

/**
 * Adds a new TemplateProposal to the ones already know.
 *
 * @param prefixString
 *                the prefix of TemplateProposal, using which we can
 *                decide if the proposal is valid in the given context.
 * @param candidate
 *                the proposal candidate to be added
 * @param image
 *                the image to use for this proposal in the completion
 *                popup
 */
public void addTemplateProposal(final String prefixString, final Template candidate, final Image image) {
    int prefixLength = prefixLength(prefixString);
    if (prefixLength != -1) {
        Region region = new Region(replacementOffset, 0);
        proposalList.add(new TemplateProposal(candidate, templatecontext, region, image));
    }
}
Also used : TemplateProposal(org.eclipse.jface.text.templates.TemplateProposal) Region(org.eclipse.jface.text.Region)

Aggregations

TemplateProposal (org.eclipse.jface.text.templates.TemplateProposal)9 Template (org.eclipse.jface.text.templates.Template)6 ArrayList (java.util.ArrayList)3 ITextSelection (org.eclipse.jface.text.ITextSelection)3 Region (org.eclipse.jface.text.Region)3 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)3 DocumentTemplateContext (org.eclipse.jface.text.templates.DocumentTemplateContext)3 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)3 CheckCatalog (com.avaloq.tools.ddk.check.check.CheckCatalog)2 IRegion (org.eclipse.jface.text.IRegion)2 TemplateStore (org.eclipse.jface.text.templates.persistence.TemplateStore)2 Image (org.eclipse.swt.graphics.Image)2 Check (com.avaloq.tools.ddk.check.check.Check)1 FormalParameter (com.avaloq.tools.ddk.check.check.FormalParameter)1 CheckConfiguration (com.avaloq.tools.ddk.checkcfg.checkcfg.CheckConfiguration)1 ConfiguredCatalog (com.avaloq.tools.ddk.checkcfg.checkcfg.ConfiguredCatalog)1 ConfiguredCheck (com.avaloq.tools.ddk.checkcfg.checkcfg.ConfiguredCheck)1 Function (com.google.common.base.Function)1 StringJoiner (java.util.StringJoiner)1 Resource (org.eclipse.emf.ecore.resource.Resource)1