Search in sources :

Example 1 with Codetemplates

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

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

the class TemplateValidator method checkParameters.

@Check
public void checkParameters(Variable variable) {
    Codetemplate template = EcoreUtil2.getContainerOfType(variable, Codetemplate.class);
    Codetemplates templates = EcoreUtil2.getContainerOfType(template, Codetemplates.class);
    if (templates != null && template != null) {
        Grammar language = templates.getLanguage();
        AbstractRule rule = template.getContext();
        ContextTypeIdHelper helper = languageRegistry.getContextTypeIdHelper(language);
        if (helper != null && rule != null && !rule.eIsProxy() && rule instanceof ParserRule) {
            String contextTypeId = helper.getId(rule);
            ContextTypeRegistry contextTypeRegistry = languageRegistry.getContextTypeRegistry(language);
            TemplateContextType contextType = contextTypeRegistry.getContextType(contextTypeId);
            if (contextType != null) {
                Iterator<TemplateVariableResolver> resolvers = Iterators.filter(contextType.resolvers(), TemplateVariableResolver.class);
                String type = variable.getType();
                if (type == null)
                    type = variable.getName();
                while (resolvers.hasNext()) {
                    final TemplateVariableResolver resolver = resolvers.next();
                    if (resolver.getType().equals(type)) {
                        IInspectableTemplateVariableResolver inspectableResolver = registry.toInspectableResolver(resolver);
                        if (inspectableResolver != null) {
                            inspectableResolver.validateParameters(variable, this);
                        }
                    }
                }
            }
        }
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Codetemplates(org.eclipse.xtext.ui.codetemplates.templates.Codetemplates) Codetemplate(org.eclipse.xtext.ui.codetemplates.templates.Codetemplate) ContextTypeRegistry(org.eclipse.jface.text.templates.ContextTypeRegistry) Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) ContextTypeIdHelper(org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper) IInspectableTemplateVariableResolver(org.eclipse.xtext.ui.codetemplates.ui.resolvers.IInspectableTemplateVariableResolver) IInspectableTemplateVariableResolver(org.eclipse.xtext.ui.codetemplates.ui.resolvers.IInspectableTemplateVariableResolver) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver) Check(org.eclipse.xtext.validation.Check)

Example 3 with Codetemplates

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

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

the class CodetemplatesProposalProvider method completeCodetemplate_KeywordContext.

@Override
public void completeCodetemplate_KeywordContext(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
    Codetemplate template = EcoreUtil2.getContainerOfType(model, Codetemplate.class);
    if (template != null) {
        Codetemplates templates = EcoreUtil2.getContainerOfType(template, Codetemplates.class);
        if (templates != null) {
            Grammar language = templates.getLanguage();
            if (language != null && !language.eIsProxy()) {
                Set<String> keywords = GrammarUtil.getAllKeywords(language);
                for (String keyword : keywords) {
                    String proposalText = keyword;
                    proposalText = getValueConverter().toString(proposalText, ((RuleCall) assignment.getTerminal()).getRule().getName());
                    StyledString displayText = new StyledString(proposalText).append(" - Keyword", StyledString.QUALIFIER_STYLER);
                    ICompletionProposal proposal = createCompletionProposal(proposalText, displayText, null, context);
                    getPriorityHelper().adjustCrossReferencePriority(proposal, context.getPrefix());
                    if (proposal instanceof ConfigurableCompletionProposal) {
                        ((ConfigurableCompletionProposal) proposal).setPriority(((ConfigurableCompletionProposal) proposal).getPriority() - 1);
                    }
                    acceptor.accept(proposal);
                }
            }
        }
    }
}
Also used : Codetemplates(org.eclipse.xtext.ui.codetemplates.templates.Codetemplates) ConfigurableCompletionProposal(org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal) Codetemplate(org.eclipse.xtext.ui.codetemplates.templates.Codetemplate) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Grammar(org.eclipse.xtext.Grammar) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString)

Example 5 with Codetemplates

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

Codetemplates (org.eclipse.xtext.ui.codetemplates.templates.Codetemplates)6 Codetemplate (org.eclipse.xtext.ui.codetemplates.templates.Codetemplate)5 Grammar (org.eclipse.xtext.Grammar)4 ParserRule (org.eclipse.xtext.ParserRule)4 Variable (org.eclipse.xtext.ui.codetemplates.templates.Variable)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 AbstractRule (org.eclipse.xtext.AbstractRule)2 Action (org.eclipse.xtext.Action)2 Parameter (org.eclipse.xtext.Parameter)2 Dollar (org.eclipse.xtext.ui.codetemplates.templates.Dollar)2 Literal (org.eclipse.xtext.ui.codetemplates.templates.Literal)2 TemplateBody (org.eclipse.xtext.ui.codetemplates.templates.TemplateBody)2 ContextTypeIdHelper (org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper)2 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)1 StyledString (org.eclipse.jface.viewers.StyledString)1 Keyword (org.eclipse.xtext.Keyword)1 IHighlightedPositionAcceptor (org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor)1