use of org.eclipse.xtext.parser.IParseResult in project xtext-core by eclipse.
the class DefaultSemanticHighlightingCalculator method provideHighlightingFor.
@Override
public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) {
if (resource == null)
return;
IParseResult parseResult = resource.getParseResult();
if (parseResult == null || parseResult.getRootASTElement() == null)
return;
doProvideHighlightingFor(resource, acceptor, cancelIndicator);
}
use of org.eclipse.xtext.parser.IParseResult in project statecharts by Yakindu.
the class AbstractSCTResource method parse.
protected IParseResult parse(SpecificationElement element, String rule) {
ParserRule parserRule = XtextFactory.eINSTANCE.createParserRule();
parserRule.setName(rule);
String specification = element.getSpecification();
IParseResult result = parser.parse(parserRule, new StringReader(specification != null ? specification : ""));
createDiagnostics(result, element);
return result;
}
use of org.eclipse.xtext.parser.IParseResult in project xtext-eclipse by eclipse.
the class ContentFormatter method exec.
protected TextEdit exec(IXtextDocument document, IRegion region, XtextResource resource) throws Exception {
try {
IParseResult parseResult = resource.getParseResult();
if (parseResult != null && parseResult.getRootASTElement() != null) {
FormatterRequest request = requestProvider.get();
initRequest(document, region, resource, request);
IFormatter2 formatter = formatterProvider.get();
List<ITextReplacement> replacements = formatter.format(request);
final TextEdit mte = createTextEdit(replacements);
return mte;
}
} catch (Exception e) {
LOG.error("Error formatting " + resource.getURI() + ": " + e.getMessage(), e);
}
return new MultiTextEdit();
}
use of org.eclipse.xtext.parser.IParseResult in project xtext-eclipse by eclipse.
the class ExpressionUtil method findSelectedExpression.
/**
* @return the smallest single expression containing the selection.
*/
public XExpression findSelectedExpression(XtextResource resource, ITextSelection selection) {
IParseResult parseResult = resource.getParseResult();
if (parseResult != null) {
ICompositeNode rootNode = parseResult.getRootNode();
INode node = NodeModelUtils.findLeafNodeAtOffset(rootNode, selection.getOffset());
if (node == null) {
return null;
}
if (isHidden(node)) {
if (selection.getLength() > node.getLength()) {
node = NodeModelUtils.findLeafNodeAtOffset(rootNode, node.getEndOffset());
} else {
node = NodeModelUtils.findLeafNodeAtOffset(rootNode, selection.getOffset() - 1);
}
} else if (node.getOffset() == selection.getOffset() && !isBeginOfExpression(node)) {
node = NodeModelUtils.findLeafNodeAtOffset(rootNode, selection.getOffset() - 1);
}
if (node != null) {
EObject currentSemanticElement = NodeModelUtils.findActualSemanticObjectFor(node);
while (!(contains(currentSemanticElement, node, selection) && currentSemanticElement instanceof XExpression)) {
node = nextNodeForFindSelectedExpression(currentSemanticElement, node, selection);
if (node == null)
return null;
currentSemanticElement = NodeModelUtils.findActualSemanticObjectFor(node);
}
return (XExpression) currentSemanticElement;
}
}
return null;
}
use of org.eclipse.xtext.parser.IParseResult in project xtext-eclipse by eclipse.
the class AbstractEObjectHover method getXtextElementAt.
/**
* Call this method only from within an IUnitOfWork
*/
protected Pair<EObject, IRegion> getXtextElementAt(XtextResource resource, final int offset) {
// check for cross reference
EObject crossLinkedEObject = eObjectAtOffsetHelper.resolveCrossReferencedElementAt(resource, offset);
if (crossLinkedEObject != null) {
if (!crossLinkedEObject.eIsProxy()) {
IParseResult parseResult = resource.getParseResult();
if (parseResult != null) {
ILeafNode leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset);
if (leafNode != null && leafNode.isHidden() && leafNode.getOffset() == offset) {
leafNode = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset - 1);
}
if (leafNode != null) {
ITextRegion leafRegion = leafNode.getTextRegion();
return Tuples.create(crossLinkedEObject, (IRegion) new Region(leafRegion.getOffset(), leafRegion.getLength()));
}
}
}
} else {
EObject o = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
if (o != null) {
ITextRegion region = locationInFileProvider.getSignificantTextRegion(o);
final IRegion region2 = new Region(region.getOffset(), region.getLength());
if (TextUtilities.overlaps(region2, new Region(offset, 0)))
return Tuples.create(o, region2);
}
}
return null;
}
Aggregations