Search in sources :

Example 1 with EvaluatedTemplate

use of org.eclipse.xtext.ui.codetemplates.ui.evaluator.EvaluatedTemplate in project xtext-eclipse by eclipse.

the class CodetemplatesProposalProvider method complete_Literal.

@Override
public void complete_Literal(EObject model, RuleCall ruleCall, final ContentAssistContext context, final ICompletionProposalAcceptor acceptor) {
    if ((mode & NESTED) != 0) {
        final TemplateData data = new TemplateData(model);
        if (data.doCreateProposals()) {
            final EvaluatedTemplate evaluatedTemplate = new EvaluatedTemplate(data.template);
            temporaryResourceProvider.useTemporaryResource(data.template.eResource().getResourceSet(), data.language, data.rule, evaluatedTemplate.getMappedString(), new IUnitOfWork.Void<XtextResource>() {

                @Override
                public void process(XtextResource resource) throws Exception {
                    IPartialEditingContentAssistContextFactory delegateFactory = languageRegistry.getPartialContentAssistContextFactory(data.language);
                    delegateFactory.initializeFor(data.rule);
                    String mappedInput = evaluatedTemplate.getMappedString();
                    int mappedOffset = Math.min(mappedInput.length(), evaluatedTemplate.getMappedOffset(context.getOffset()));
                    DummyDocument document = new DummyDocument(mappedInput);
                    DummyTextViewer dummyViewer = new DummyTextViewer(TextSelection.emptySelection(), document);
                    ContentAssistContext[] contexts = delegateFactory.create(dummyViewer, mappedOffset, resource);
                    ICompletionProposalAcceptor mappingAcceptor = new ProjectionAwareProposalAcceptor(acceptor, evaluatedTemplate);
                    createNestedProposals(contexts, context.getViewer(), mappingAcceptor, data);
                }
            });
        }
    }
}
Also used : XtextResource(org.eclipse.xtext.resource.XtextResource) StyledString(org.eclipse.jface.viewers.StyledString) ICompletionProposalAcceptor(org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor) IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) EvaluatedTemplate(org.eclipse.xtext.ui.codetemplates.ui.evaluator.EvaluatedTemplate) IPartialEditingContentAssistContextFactory(org.eclipse.xtext.ui.codetemplates.ui.partialEditing.IPartialEditingContentAssistContextFactory) ProjectionAwareProposalAcceptor(org.eclipse.xtext.ui.codetemplates.ui.projectedEditing.ProjectionAwareProposalAcceptor)

Example 2 with EvaluatedTemplate

use of org.eclipse.xtext.ui.codetemplates.ui.evaluator.EvaluatedTemplate in project xtext-eclipse by eclipse.

the class SemanticHighlighter method provideHighlightingFor.

@Override
public void provideHighlightingFor(XtextResource resource, final IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) {
    if (resource == null || resource.getContents().isEmpty())
        return;
    Codetemplates templates = (Codetemplates) resource.getContents().get(0);
    Grammar grammar = templates.getLanguage();
    if (grammar != null && !grammar.eIsProxy()) {
        TemplateBodyHighlighter highlighter = getHighlighter(grammar);
        if (highlighter != null) {
            ContextTypeIdHelper helper = registry.getContextTypeIdHelper(grammar);
            ContextTypeRegistry contextTypeRegistry = registry.getContextTypeRegistry(grammar);
            for (Codetemplate template : templates.getTemplates()) {
                operationCanceledManager.checkCanceled(cancelIndicator);
                if (template.getBody() != null) {
                    final EvaluatedTemplate evaluatedTemplate = new EvaluatedTemplate(template);
                    highlighter.provideHighlightingFor(evaluatedTemplate.getMappedString(), new IHighlightedPositionAcceptor() {

                        @Override
                        public void addPosition(int offset, int length, String... id) {
                            int beginOffset = evaluatedTemplate.getOriginalOffset(offset);
                            int endOffset = evaluatedTemplate.getOriginalOffset(offset + length);
                            int fixedLength = endOffset - beginOffset;
                            acceptor.addPosition(beginOffset, fixedLength, id);
                        }
                    });
                    String id = null;
                    TemplateContextType contextType = null;
                    if (template.getContext() != null) {
                        id = helper.getId(template.getContext());
                        if (id != null)
                            contextType = contextTypeRegistry.getContextType(id);
                    }
                    Set<String> defaultResolvers = Sets.newHashSet();
                    if (contextType != null) {
                        Iterator<TemplateVariableResolver> resolvers = Iterators.filter(contextType.resolvers(), TemplateVariableResolver.class);
                        while (resolvers.hasNext()) {
                            TemplateVariableResolver resolver = resolvers.next();
                            defaultResolvers.add(resolver.getType());
                        }
                    }
                    for (TemplatePart part : template.getBody().getParts()) {
                        if (part instanceof Variable) {
                            Variable variable = (Variable) part;
                            ICompositeNode node = NodeModelUtils.findActualNodeFor(variable);
                            if (node != null) {
                                for (ILeafNode leafNode : node.getLeafNodes()) {
                                    if (leafNode.getGrammarElement() instanceof Keyword) {
                                        acceptor.addPosition(leafNode.getTotalOffset(), leafNode.getTotalLength(), TemplatesHighlightingConfiguration.TEMPLATE_VARIABLE);
                                    }
                                }
                                List<INode> typeNodes = NodeModelUtils.findNodesForFeature(variable, TemplatesPackage.Literals.VARIABLE__TYPE);
                                if (typeNodes.isEmpty()) {
                                    if (defaultResolvers.contains(variable.getName())) {
                                        List<INode> nameNodes = NodeModelUtils.findNodesForFeature(variable, TemplatesPackage.Literals.VARIABLE__NAME);
                                        for (INode nameNode : nameNodes) {
                                            highlightNode(nameNode, TemplatesHighlightingConfiguration.TEMPLATE_VARIABLE, acceptor);
                                        }
                                    }
                                } else {
                                    for (INode typeNode : typeNodes) {
                                        highlightNode(typeNode, TemplatesHighlightingConfiguration.TEMPLATE_VARIABLE, acceptor);
                                    }
                                }
                                List<INode> parameterNodes = NodeModelUtils.findNodesForFeature(variable, TemplatesPackage.Literals.VARIABLE__PARAMETERS);
                                for (INode parameterNode : parameterNodes) {
                                    highlightNode(parameterNode, TemplatesHighlightingConfiguration.TEMPLATE_VARIABLE_ARGUMENT, acceptor);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) Variable(org.eclipse.xtext.ui.codetemplates.templates.Variable) Keyword(org.eclipse.xtext.Keyword) ContextTypeRegistry(org.eclipse.jface.text.templates.ContextTypeRegistry) Grammar(org.eclipse.xtext.Grammar) IHighlightedPositionAcceptor(org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor) ContextTypeIdHelper(org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper) TemplatePart(org.eclipse.xtext.ui.codetemplates.templates.TemplatePart) Codetemplates(org.eclipse.xtext.ui.codetemplates.templates.Codetemplates) EvaluatedTemplate(org.eclipse.xtext.ui.codetemplates.ui.evaluator.EvaluatedTemplate) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Codetemplate(org.eclipse.xtext.ui.codetemplates.templates.Codetemplate) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver)

Aggregations

EvaluatedTemplate (org.eclipse.xtext.ui.codetemplates.ui.evaluator.EvaluatedTemplate)2 ContextTypeRegistry (org.eclipse.jface.text.templates.ContextTypeRegistry)1 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)1 TemplateVariableResolver (org.eclipse.jface.text.templates.TemplateVariableResolver)1 StyledString (org.eclipse.jface.viewers.StyledString)1 Grammar (org.eclipse.xtext.Grammar)1 Keyword (org.eclipse.xtext.Keyword)1 IHighlightedPositionAcceptor (org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor)1 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)1 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)1 INode (org.eclipse.xtext.nodemodel.INode)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 Codetemplate (org.eclipse.xtext.ui.codetemplates.templates.Codetemplate)1 Codetemplates (org.eclipse.xtext.ui.codetemplates.templates.Codetemplates)1 TemplatePart (org.eclipse.xtext.ui.codetemplates.templates.TemplatePart)1 Variable (org.eclipse.xtext.ui.codetemplates.templates.Variable)1 IPartialEditingContentAssistContextFactory (org.eclipse.xtext.ui.codetemplates.ui.partialEditing.IPartialEditingContentAssistContextFactory)1 ProjectionAwareProposalAcceptor (org.eclipse.xtext.ui.codetemplates.ui.projectedEditing.ProjectionAwareProposalAcceptor)1 ICompletionProposalAcceptor (org.eclipse.xtext.ui.editor.contentassist.ICompletionProposalAcceptor)1 ContextTypeIdHelper (org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper)1