Search in sources :

Example 6 with TemplateProposal

use of org.eclipse.jface.text.templates.TemplateProposal 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()]);
}
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 7 with TemplateProposal

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

the class CheckCfgTemplateProposalProvider method addConfiguredCheckTemplates.

/**
 * Adds template proposals for all checks which may be referenced in current catalog configuration. Only proposals for checks
 * which have not yet been configured are provided.
 *
 * @param templateContext
 *          the template context
 * @param context
 *          the context
 * @param acceptor
 *          the acceptor
 */
private void addConfiguredCheckTemplates(final TemplateContext templateContext, final ContentAssistContext context, final ITemplateAcceptor acceptor) {
    // NOPMD
    ConfiguredCatalog configuredCatalog = EcoreUtil2.getContainerOfType(context.getCurrentModel(), ConfiguredCatalog.class);
    Iterable<String> alreadyConfiguredCheckNames = Iterables.filter(Iterables.transform(configuredCatalog.getCheckConfigurations(), new Function<ConfiguredCheck, String>() {

        @Override
        public String apply(final ConfiguredCheck from) {
            if (from.getCheck() != null) {
                return from.getCheck().getName();
            }
            return null;
        }
    }), Predicates.notNull());
    final CheckCatalog catalog = configuredCatalog.getCatalog();
    for (final Check check : catalog.getAllChecks()) {
        // create a template on the fly
        final String checkName = check.getName();
        if (!Iterables.contains(alreadyConfiguredCheckNames, checkName)) {
            // check if referenced check has configurable parameters
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            final StringJoiner paramsJoiner = new StringJoiner(", ", " (", ")");
            // $NON-NLS-1$
            paramsJoiner.setEmptyValue("");
            for (final FormalParameter param : check.getFormalParameters()) {
                final String paramName = param.getName();
                final Object defaultValue = interpreter.evaluate(param.getRight()).getResult();
                final String valuePlaceholder = helper.createLiteralValuePattern(paramName, defaultValue);
                // $NON-NLS-1$
                paramsJoiner.add(paramName + " = " + valuePlaceholder);
            }
            // $NON-NLS-1$ //$NON-NLS-2$
            final String severity = (catalog.isFinal() || check.isFinal()) ? "default " : "${default:Enum('SeverityKind')} ";
            // $NON-NLS-1$ //$NON-NLS-2$
            final String description = "Configures the check \"" + check.getLabel() + "\"";
            // $NON-NLS-1$
            final String contextTypeId = "com.avaloq.tools.ddk.checkcfg.CheckCfg.ConfiguredCheck." + checkName;
            // $NON-NLS-1$
            final String pattern = severity + qualifiedNameValueConverter.toString(checkName) + paramsJoiner + "${cursor}";
            Template t = new Template(checkName, description, contextTypeId, pattern, true);
            TemplateProposal tp = createProposal(t, templateContext, context, images.forConfiguredCheck(check.getDefaultSeverity()), getRelevance(t));
            acceptor.accept(tp);
        }
    }
}
Also used : FormalParameter(com.avaloq.tools.ddk.check.check.FormalParameter) TemplateProposal(org.eclipse.jface.text.templates.TemplateProposal) Function(com.google.common.base.Function) ConfiguredCatalog(com.avaloq.tools.ddk.checkcfg.checkcfg.ConfiguredCatalog) ConfiguredCheck(com.avaloq.tools.ddk.checkcfg.checkcfg.ConfiguredCheck) Check(com.avaloq.tools.ddk.check.check.Check) ConfiguredCheck(com.avaloq.tools.ddk.checkcfg.checkcfg.ConfiguredCheck) CheckCatalog(com.avaloq.tools.ddk.check.check.CheckCatalog) StringJoiner(java.util.StringJoiner) Template(org.eclipse.jface.text.templates.Template)

Example 8 with TemplateProposal

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

the class TemplatesFirstCompletionProposalComparator method compare.

/**
 * {@inheritDoc}
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public int compare(final ICompletionProposal o1, final ICompletionProposal o2) {
    // Template sorting by relevance and then alphabetically by name
    if (o1 instanceof TemplateProposal && o2 instanceof TemplateProposal) {
        TemplateProposal t1 = (TemplateProposal) o1;
        TemplateProposal t2 = (TemplateProposal) o2;
        if (t1.getRelevance() == t2.getRelevance()) {
            return o1.getDisplayString().compareTo(o2.getDisplayString());
        }
        return ((Integer) t1.getRelevance()).compareTo(t1.getRelevance());
    }
    // Templates always first
    if (o1 instanceof TemplateProposal) {
        return -1;
    } else if (o2 instanceof TemplateProposal) {
        return 1;
    }
    // Fallback
    if ((o1 instanceof Comparable) && (o2 instanceof Comparable)) {
        return ((Comparable) o1).compareTo(o2);
    }
    return o1.getDisplayString().compareTo(o2.getDisplayString());
}
Also used : TemplateProposal(org.eclipse.jface.text.templates.TemplateProposal)

Example 9 with TemplateProposal

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

the class AbstractContentAssistUiTest method assertTemplateProposalExistsAndSuccessful.

/**
 * Assert that application of the target {@link TemplateProposal} was successful and the text of the resulting document equals the expected text.
 * Creates a test source with the given {@code sourceFileName} and {@code sourceContent}.
 *
 * @param sourceFileName
 *          the name of the source being tested, must not be {@code null}
 * @param sourceContent
 *          the source content, may be {@code null}
 * @param contentassistProposal
 *          the display string of the content assist proposal, must not be {@code null}
 * @param expectedContent
 *          the expected document content after application of the {@link TemplateProposal}, must not be {@code null}
 * @param offset
 *          offset in test file
 */
@SuppressWarnings("PMD.UseObjectForClearerAPI")
protected void assertTemplateProposalExistsAndSuccessful(final String sourceFileName, final String sourceContent, final String contentassistProposal, final String expectedContent, final int offset) {
    if (sourceContent == null) {
        Assert.assertNotNull(String.format("There must be an existing test source with the file name '%s'.", sourceFileName), getTestSource(sourceFileName));
    } else {
        createTestSource(sourceFileName, sourceContent);
    }
    openEditor(sourceFileName);
    evaluateCompletionProposals(offset);
    ICompletionProposal[] completionProposals = getCompletionProposals();
    TemplateProposal templateProposal = null;
    for (int i = 0; i < completionProposals.length && templateProposal == null; i++) {
        ICompletionProposal proposal = completionProposals[i];
        String displayString = proposal.getDisplayString();
        if (proposal instanceof TemplateProposal && contentassistProposal.equals(displayString)) {
            templateProposal = (TemplateProposal) proposal;
        }
    }
    Assert.assertNotNull(String.format("Template proposal '%s' must be found.", contentassistProposal), templateProposal);
    String actualContent = applyTemplateProposal(templateProposal, offset);
    Assert.assertEquals("Editor content must match expected result.", expectedContent.replaceAll(CR_LF, LF), actualContent.replaceAll(CR_LF, LF));
    closeEditor(getEditor(), false);
}
Also used : TemplateProposal(org.eclipse.jface.text.templates.TemplateProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal)

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