Search in sources :

Example 1 with Variable

use of org.eclipse.xtext.ui.codetemplates.templates.Variable in project xtext-eclipse by eclipse.

the class SingleCodetemplateSemanticSequencer method sequence.

@Override
public void sequence(ISerializationContext context, EObject semanticObject) {
    EPackage epackage = semanticObject.eClass().getEPackage();
    ParserRule rule = context.getParserRule();
    Action action = context.getAssignedAction();
    Set<Parameter> parameters = context.getEnabledBooleanParameters();
    if (epackage == TemplatesPackage.eINSTANCE)
        switch(semanticObject.eClass().getClassifierID()) {
            case TemplatesPackage.CODETEMPLATE:
                sequence_Codetemplate(context, (Codetemplate) semanticObject);
                return;
            case TemplatesPackage.CODETEMPLATES:
                sequence_Codetemplates(context, (Codetemplates) semanticObject);
                return;
            case TemplatesPackage.DOLLAR:
                sequence_Dollar(context, (Dollar) semanticObject);
                return;
            case TemplatesPackage.LITERAL:
                sequence_Literal(context, (Literal) semanticObject);
                return;
            case TemplatesPackage.TEMPLATE_BODY:
                sequence_TemplateBody(context, (TemplateBody) semanticObject);
                return;
            case TemplatesPackage.VARIABLE:
                sequence_Variable(context, (Variable) semanticObject);
                return;
        }
    if (errorAcceptor != null)
        errorAcceptor.accept(diagnosticProvider.createInvalidContextOrTypeDiagnostic(semanticObject, context));
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) Codetemplates(org.eclipse.xtext.ui.codetemplates.templates.Codetemplates) Variable(org.eclipse.xtext.ui.codetemplates.templates.Variable) Dollar(org.eclipse.xtext.ui.codetemplates.templates.Dollar) Codetemplate(org.eclipse.xtext.ui.codetemplates.templates.Codetemplate) Literal(org.eclipse.xtext.ui.codetemplates.templates.Literal) Parameter(org.eclipse.xtext.Parameter) TemplateBody(org.eclipse.xtext.ui.codetemplates.templates.TemplateBody) EPackage(org.eclipse.emf.ecore.EPackage)

Example 2 with Variable

use of org.eclipse.xtext.ui.codetemplates.templates.Variable in project xtext-eclipse by eclipse.

the class CodetemplatesSemanticSequencer method sequence.

@Override
public void sequence(ISerializationContext context, EObject semanticObject) {
    EPackage epackage = semanticObject.eClass().getEPackage();
    ParserRule rule = context.getParserRule();
    Action action = context.getAssignedAction();
    Set<Parameter> parameters = context.getEnabledBooleanParameters();
    if (epackage == TemplatesPackage.eINSTANCE)
        switch(semanticObject.eClass().getClassifierID()) {
            case TemplatesPackage.CODETEMPLATE:
                sequence_Codetemplate(context, (Codetemplate) semanticObject);
                return;
            case TemplatesPackage.CODETEMPLATES:
                sequence_Codetemplates(context, (Codetemplates) semanticObject);
                return;
            case TemplatesPackage.DOLLAR:
                sequence_Dollar(context, (Dollar) semanticObject);
                return;
            case TemplatesPackage.LITERAL:
                sequence_Literal(context, (Literal) semanticObject);
                return;
            case TemplatesPackage.TEMPLATE_BODY:
                sequence_TemplateBody(context, (TemplateBody) semanticObject);
                return;
            case TemplatesPackage.VARIABLE:
                sequence_Variable(context, (Variable) semanticObject);
                return;
        }
    if (errorAcceptor != null)
        errorAcceptor.accept(diagnosticProvider.createInvalidContextOrTypeDiagnostic(semanticObject, context));
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) Codetemplates(org.eclipse.xtext.ui.codetemplates.templates.Codetemplates) Variable(org.eclipse.xtext.ui.codetemplates.templates.Variable) Dollar(org.eclipse.xtext.ui.codetemplates.templates.Dollar) Codetemplate(org.eclipse.xtext.ui.codetemplates.templates.Codetemplate) Literal(org.eclipse.xtext.ui.codetemplates.templates.Literal) Parameter(org.eclipse.xtext.Parameter) TemplateBody(org.eclipse.xtext.ui.codetemplates.templates.TemplateBody) EPackage(org.eclipse.emf.ecore.EPackage)

Example 3 with Variable

use of org.eclipse.xtext.ui.codetemplates.templates.Variable in project xtext-eclipse by eclipse.

the class AbstractTemplateProposalConflictHelper method initTokenSource.

@Override
protected void initTokenSource(String text, TokenSource tokenSource, ContentAssistContext context) {
    EObject currentModel = context.getLastCompleteNode().getSemanticElement();
    Variable variable = currentModel != null ? EcoreUtil2.getContainerOfType(currentModel, Variable.class) : null;
    TemplateBody body = currentModel != null ? EcoreUtil2.getContainerOfType(currentModel, TemplateBody.class) : null;
    Lexer lexer = (Lexer) tokenSource;
    CharStream stream = new ANTLRStringStream(text);
    lexer.setCharStream(stream);
    initLexer(lexer, body != null, variable != null);
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) Lexer(org.eclipse.xtext.parser.antlr.Lexer) Variable(org.eclipse.xtext.ui.codetemplates.templates.Variable) EObject(org.eclipse.emf.ecore.EObject) TemplateBody(org.eclipse.xtext.ui.codetemplates.templates.TemplateBody) CharStream(org.antlr.runtime.CharStream)

Example 4 with Variable

use of org.eclipse.xtext.ui.codetemplates.templates.Variable in project xtext-eclipse by eclipse.

the class CodetemplatesProposalProvider method completeVariable_Name.

@Override
public void completeVariable_Name(EObject model, Assignment assignment, final ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    if ((mode & NORMAL) != 0) {
        {
            String proposalText = "variable";
            StyledString displayText = new StyledString(proposalText).append(" - Create a new template variable", StyledString.QUALIFIER_STYLER);
            ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
            if (proposal instanceof ConfigurableCompletionProposal) {
                ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
                configurable.setSelectionStart(configurable.getReplacementOffset());
                configurable.setSelectionLength(proposalText.length());
                configurable.setAutoInsertable(false);
                configurable.setSimpleLinkedMode(context.getViewer(), '\t', ' ');
            }
            acceptor.accept(proposal);
        }
        TemplateData data = new TemplateData(model);
        if (data.doCreateProposals()) {
            ContextTypeIdHelper helper = languageRegistry.getContextTypeIdHelper(data.language);
            if (helper != null) {
                String contextTypeId = helper.getId(data.rule);
                ContextTypeRegistry contextTypeRegistry = languageRegistry.getContextTypeRegistry(data.language);
                TemplateContextType contextType = contextTypeRegistry.getContextType(contextTypeId);
                if (contextType != null) {
                    Iterator<TemplateVariableResolver> resolvers = Iterators.filter(contextType.resolvers(), TemplateVariableResolver.class);
                    while (resolvers.hasNext()) {
                        final TemplateVariableResolver resolver = resolvers.next();
                        IInspectableTemplateVariableResolver inspectableResolver = templateVariableResolverRegistry.toInspectableResolver(resolver);
                        if (inspectableResolver != null) {
                            if (!inspectableResolver.hasMandatoryParameters()) {
                                String type = resolver.getType();
                                StyledString displayString = new StyledString(type, StyledString.DECORATIONS_STYLER).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
                                acceptor.accept(createCompletionProposal(type, displayString, null, context));
                            }
                            String proposalText = "variable:" + resolver.getType();
                            StyledString displayText = new StyledString("variable").append(":" + resolver.getType(), StyledString.DECORATIONS_STYLER).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
                            if (inspectableResolver.hasMandatoryParameters())
                                proposalText = proposalText + "()";
                            Builder builder = context.copy();
                            PrefixMatcher newMatcher = new PrefixMatcher() {

                                @Override
                                public boolean isCandidateMatchingPrefix(String name, String prefix) {
                                    return context.getMatcher().isCandidateMatchingPrefix(name, prefix) || context.getMatcher().isCandidateMatchingPrefix("variable:" + resolver.getType(), prefix) || context.getMatcher().isCandidateMatchingPrefix(resolver.getType(), prefix);
                                }
                            };
                            builder.setMatcher(newMatcher);
                            ContentAssistContext myContext = builder.toContext();
                            ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, myContext);
                            if (proposal instanceof ConfigurableCompletionProposal) {
                                ConfigurableCompletionProposal configurable = (ConfigurableCompletionProposal) proposal;
                                configurable.setSelectionStart(configurable.getReplacementOffset());
                                configurable.setSelectionLength("variable".length());
                                configurable.setAutoInsertable(false);
                                if (inspectableResolver.hasMandatoryParameters()) {
                                    configurable.setCursorPosition(proposalText.length() - 1);
                                }
                                configurable.setSimpleLinkedMode(myContext.getViewer(), '\t');
                            }
                            acceptor.accept(proposal);
                        } else {
                            String type = resolver.getType();
                            StyledString displayString = new StyledString(type, StyledString.DECORATIONS_STYLER).append(" - " + resolver.getDescription(), StyledString.QUALIFIER_STYLER);
                            acceptor.accept(createCompletionProposal(type, displayString, null, context));
                        }
                    }
                }
            }
            if (data.template.getBody() != null) {
                for (Variable variable : Iterables.filter(data.template.getBody().getParts(), Variable.class)) {
                    if (variable != model && variable.getName() != null) {
                        String proposalText = variable.getName();
                        StyledString displayText = new StyledString(proposalText).append(" - existing variable", StyledString.QUALIFIER_STYLER);
                        if (variable.getType() != null)
                            displayText = displayText.append(" of type " + variable.getType(), StyledString.QUALIFIER_STYLER);
                        ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
                        acceptor.accept(proposal);
                    }
                }
            }
        }
    }
}
Also used : PrefixMatcher(org.eclipse.xtext.ui.editor.contentassist.PrefixMatcher) Variable(org.eclipse.xtext.ui.codetemplates.templates.Variable) Builder(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext.Builder) ContextTypeRegistry(org.eclipse.jface.text.templates.ContextTypeRegistry) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) ContextTypeIdHelper(org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper) IInspectableTemplateVariableResolver(org.eclipse.xtext.ui.codetemplates.ui.resolvers.IInspectableTemplateVariableResolver) ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) ContentAssistContext(org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) IInspectableTemplateVariableResolver(org.eclipse.xtext.ui.codetemplates.ui.resolvers.IInspectableTemplateVariableResolver) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver)

Example 5 with Variable

use of org.eclipse.xtext.ui.codetemplates.templates.Variable 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

Variable (org.eclipse.xtext.ui.codetemplates.templates.Variable)6 Codetemplate (org.eclipse.xtext.ui.codetemplates.templates.Codetemplate)3 Codetemplates (org.eclipse.xtext.ui.codetemplates.templates.Codetemplates)3 TemplateBody (org.eclipse.xtext.ui.codetemplates.templates.TemplateBody)3 EPackage (org.eclipse.emf.ecore.EPackage)2 ContextTypeRegistry (org.eclipse.jface.text.templates.ContextTypeRegistry)2 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)2 TemplateVariableResolver (org.eclipse.jface.text.templates.TemplateVariableResolver)2 Action (org.eclipse.xtext.Action)2 Parameter (org.eclipse.xtext.Parameter)2 ParserRule (org.eclipse.xtext.ParserRule)2 Dollar (org.eclipse.xtext.ui.codetemplates.templates.Dollar)2 Literal (org.eclipse.xtext.ui.codetemplates.templates.Literal)2 TemplatePart (org.eclipse.xtext.ui.codetemplates.templates.TemplatePart)2 ContextTypeIdHelper (org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper)2 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1 CharStream (org.antlr.runtime.CharStream)1 EObject (org.eclipse.emf.ecore.EObject)1 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 StyledString (org.eclipse.jface.viewers.StyledString)1