use of org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper 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);
}
}
}
}
}
}
}
}
}
Aggregations