Java Code Examples for org.eclipse.xtext.resource.XtextResource#getParseResult()

The following examples show how to use org.eclipse.xtext.resource.XtextResource#getParseResult() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RenameService2.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected EObject getElementWithIdentifierAt(XtextResource xtextResource, int offset) {
	if (offset >= 0) {
		if (xtextResource != null) {
			IParseResult parseResult = xtextResource.getParseResult();
			if (parseResult != null) {
				ICompositeNode rootNode = parseResult.getRootNode();
				if (rootNode != null) {
					ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset);
					if (leaf != null && isIdentifier(leaf)) {
						return eObjectAtOffsetHelper.resolveElementAt(xtextResource, offset);
					}
				}
			}
		}
	}
	return null;
}
 
Example 2
Source File: XtendHoverSerializer.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public Pair<String, String> computePreAndSuffix(EObject element) {
	ICompositeNode node = NodeModelUtils.getNode(element);
	if (node != null) {
		XtextResource resource = (XtextResource) element.eResource();
		if (resource != null) {
			IParseResult parseResult = resource.getParseResult();
			if (parseResult != null) {
				String model = parseResult.getRootNode().getText();
				return Tuples.create(model.substring(0, node.getTotalOffset()) + "\n",
						"\n" + model.substring(node.getTotalEndOffset()));
			}
		}
	}
	return Tuples.create("", "");

}
 
Example 3
Source File: FormatterTester.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected ArrayList<TextReplacement> createMissingEditReplacements(XtextResource res, Collection<TextReplacement> edits, int offset,
		int length) {
	Set<Integer> offsets = IterableExtensions
			.toSet(Iterables.transform(edits, (TextReplacement it) -> Integer.valueOf(it.getOffset())));
	ArrayList<TextReplacement> result = new ArrayList<>();
	int lastOffset = 0;
	IParseResult parseResult = res.getParseResult();
	if (parseResult != null) {
		ICompositeNode rootNode = parseResult.getRootNode();
		if (rootNode != null) {
			for (ILeafNode leaf : rootNode.getLeafNodes()) {
				if (!leaf.isHidden() || !StringExtensions.isNullOrEmpty(leaf.getText().trim())) {
					ITextRegion leafRegion = leaf.getTextRegion();
					if (lastOffset >= offset && leafRegion.getOffset() <= offset + length && !offsets.contains(Integer.valueOf(lastOffset))) {
						result.add(new TextReplacement(lastOffset, leafRegion.getOffset() - lastOffset, "!!"));
					}
					lastOffset = leafRegion.getOffset() + leafRegion.getLength();
				}
			}
		}
	}
	return result;
}
 
Example 4
Source File: XbaseHighlightingCalculator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@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;
	if (highlightedIdentifiers == null) {
		highlightedIdentifiers = initializeHighlightedIdentifiers();
		idLengthsToHighlight = new BitSet();
		for (String s : highlightedIdentifiers.keySet()) {
			idLengthsToHighlight.set(s.length());
		}
	}
	//TODO remove this check when the typesystem works without a java project
	if (resource.isValidationDisabled()) {
		highlightSpecialIdentifiers(acceptor, parseResult.getRootNode());
		return;
	}
	doProvideHighlightingFor(resource, acceptor, cancelIndicator);
}
 
Example 5
Source File: XtextStyledTextSelectionProvider.java    From statecharts with Eclipse Public License 1.0 6 votes vote down vote up
public ISelection getSelection() {
	if (styledText.isDisposed())
		return StructuredSelection.EMPTY;
	int offset = Math.max(styledText.getCaretOffset() - 1, 0);
	XtextResource fakeResource = xtextResource;
	IParseResult parseResult = fakeResource.getParseResult();
	if (parseResult == null)
		return StructuredSelection.EMPTY;
	ICompositeNode rootNode = parseResult.getRootNode();
	ILeafNode selectedNode = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset);
	final EObject selectedObject = NodeModelUtils.findActualSemanticObjectFor(selectedNode);
	if (selectedObject == null) {
		return StructuredSelection.EMPTY;
	}
	return new StructuredSelection(selectedObject);
}
 
Example 6
Source File: FormatHyperlinkHelper.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void createHyperlinksByOffset(final XtextResource resource, final int offset, final IHyperlinkAcceptor acceptor) {
  final IParseResult parseResult = resource.getParseResult();
  if (parseResult == null || parseResult.getRootNode() == null) {
    return; // Return, no need to call in super.createAdditionalHyperlinks
  }

  // Check if the current parse tree node represents an override keyword, in which case we want to link
  // to the overridden rule
  INode node = NodeModelUtils.findLeafNodeAtOffset(parseResult.getRootNode(), offset);
  if (node != null && isOverrideKeyword(node.getGrammarElement())) {
    Rule rule = (Rule) eObjectAtOffsetHelper.resolveElementAt(resource, offset);
    Region region = new Region(node.getOffset(), node.getLength());
    List<Rule> extendedRules = getExtendedRules(rule);
    for (Rule extendedRule : extendedRules) {
      createHyperlinksTo(resource, region, extendedRule, acceptor);
    }
  }
  super.createHyperlinksByOffset(resource, offset, acceptor);
}
 
Example 7
Source File: DotFoldingRegionProvider.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
protected void computeObjectFolding(XtextResource xtextResource,
		IFoldingRegionAcceptor<ITextRegion> foldingRegionAcceptor) {
	acceptedRegions.clear();

	IParseResult parseResult = xtextResource.getParseResult();
	if (parseResult != null) {
		EObject rootASTElement = parseResult.getRootASTElement();
		if (rootASTElement != null) {
			TreeIterator<EObject> allContents = rootASTElement
					.eAllContents();
			while (allContents.hasNext()) {
				EObject eObject = allContents.next();
				if (isHandled(eObject)) {
					computeObjectFolding(eObject, foldingRegionAcceptor);
				}
				if (eObject instanceof Attribute) {
					computeDotAttributeValueFolding((Attribute) eObject,
							foldingRegionAcceptor);
				}
				if (!shouldProcessContent(eObject)) {
					allContents.prune();
				}
			}
		}
	}
}
 
Example 8
Source File: DefaultSemanticHighlightingCalculator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@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);
}
 
Example 9
Source File: MultiLineJavaDocTypeReferenceProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public EObjectInComment computeEObjectReferencedInComment(XtextResource resource, int offset) {
	IParseResult parseResult = resource.getParseResult();
	if(parseResult != null) {
		INode rootNode = parseResult.getRootNode();
		INode node = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset);
		EObject semanticObject = NodeModelUtils.findActualSemanticObjectFor(node);
		if(semanticObject != null) {
			EReference reference = getEReference(semanticObject, node, offset);
			if(reference != null) {
				IScope scope = getScope(semanticObject, reference, node, offset);
				List<ReplaceRegion> eObjectReferences = computeTypeRefRegions(node);
				for(ReplaceRegion eObjectReference : eObjectReferences) {
					if(eObjectReference.getOffset() <= offset && offset <= eObjectReference.getEndOffset()) {
						String eObjectReferenceText = eObjectReference.getText();
						if(!Strings.isNullOrEmpty(eObjectReferenceText)) {
							ITextRegion region = new TextRegion(eObjectReference.getOffset(), eObjectReference.getLength());
							IEObjectDescription candidate = getElementFromScope(scope, node, region, eObjectReferenceText);
							if(candidate != null) {
								EObject eObject = candidate.getEObjectOrProxy();
								if(eObject != null) {
									return new EObjectInComment(eObject, region);
								}
							}
						}
					}
				}
			}
		}
	}
	return null;
}
 
Example 10
Source File: ContentFormatterFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ReplaceRegion exec(XtextResource state) throws Exception {
	IParseResult parseResult = state.getParseResult();
	if (parseResult == null)
		return null;
	IFormattedRegion r = formatter.format(parseResult.getRootNode(), region.getOffset(), region.getLength());
	return new ReplaceRegion(r.getOffset(), r.getLength(), r.getFormattedText());
}
 
Example 11
Source File: TestLanguageRenameService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected EObject getElementWithIdentifierAt(XtextResource xtextResource, int offset) {
	if (offset >= 0) {
		if (xtextResource != null) {
			IParseResult parseResult = xtextResource.getParseResult();
			if (parseResult != null) {
				ICompositeNode rootNode = parseResult.getRootNode();
				if (rootNode != null) {
					ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset);
					if (leaf != null && isIdentifier(leaf)) {
						EObject element = eObjectAtOffsetHelper.resolveElementAt(xtextResource, offset);
						if (element != null) {
							IQualifiedNameProvider nameProvider = xtextResource.getResourceServiceProvider()
									.get(IQualifiedNameProvider.class);
							QualifiedName fqn = nameProvider.getFullyQualifiedName(element);
							if (fqn != null) {
								String leafText = NodeModelUtils.getTokenText(leaf);
								if (fqn.getSegmentCount() == 1 && Objects.equal(fqn.toString(), leafText)
										|| Objects.equal(fqn.getLastSegment(), leafText)) {
									return element;
								}
							}
						}
					}
				}
			}
		}
	}
	return null;
}
 
Example 12
Source File: AbstractXtextTests.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void checkNodeModel(XtextResource resource) {
	IParseResult parseResult = resource.getParseResult();
	if(parseResult != null) {
		ICompositeNode rootNode = parseResult.getRootNode();
		getInvariantChecker().checkInvariant(rootNode);
		new LookAheadInfo(rootNode).checkConsistency();
	}
}
 
Example 13
Source File: AstSelectionProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected EObject getEObjectAtOffset(XtextResource resource, ITextRegion currentEditorSelection) {
	IParseResult parseResult = resource.getParseResult();
	if (parseResult != null) {
		ICompositeNode rootNode = parseResult.getRootNode();
		INode nodeAtOffset = findLeafNodeAtOffset(rootNode, currentEditorSelection.getOffset());
		return findSemanticObjectFor(nodeAtOffset);
	}
	return null;
}
 
Example 14
Source File: N4JSASTUtils.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Computes an MD5 hash from the given resource's source code (the actual source text), as stored in
 * {@link TModule#getAstMD5()}. Will fail with an exception if the given resource does not have a valid
 * {@link XtextResource#getParseResult() parse result}, as created by Xtext during parsing.
 */
public static String md5Hex(XtextResource resource) {
	final IParseResult parseResult = resource.getParseResult();
	final INode rootNode = parseResult != null ? parseResult.getRootNode() : null;
	final String source = rootNode != null ? rootNode.getText() : null;
	if (source == null) {
		throw new IllegalStateException("resource does not have a valid parse result: " + resource.getURI());
	}
	return Hashing.murmur3_128(SEED).hashString(source, Charsets.UTF_8).toString();
}
 
Example 15
Source File: HoverService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected HoverContext createContext(Document document, XtextResource resource, int offset) {
	EObject crossLinkedEObject = eObjectAtOffsetHelper.resolveCrossReferencedElementAt(resource, offset);
	if (crossLinkedEObject != null) {
		if (crossLinkedEObject.eIsProxy()) {
			return null;
		}
		IParseResult parseResult = resource.getParseResult();
		if (parseResult == null) {
			return 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) {
			return null;
		}
		ITextRegion leafRegion = leafNode.getTextRegion();
		return new HoverContext(document, resource, offset, leafRegion, crossLinkedEObject);
	}
	EObject element = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
	if (element == null) {
		return null;
	}
	ITextRegion region = locationInFileProvider.getSignificantTextRegion(element);
	return new HoverContext(document, resource, offset, region, element);
}
 
Example 16
Source File: ParserBasedContentAssistContextFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public ContentAssistContext[] create(ITextViewer viewer, int offset, XtextResource resource)
		throws BadLocationException {
	this.viewer = viewer;
	this.resource = resource;
	//This is called to make sure late initialization is done.
	if (resource instanceof DerivedStateAwareResource) {
		resource.getContents();
	}
	this.parseResult = resource.getParseResult();
	if (parseResult == null)
		throw new NullPointerException("parseResult is null");
	return doCreateContexts(offset);
}
 
Example 17
Source File: XbaseHighlightingCalculator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void doProvideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor,
		CancelIndicator cancelIndicator) {
	IParseResult parseResult = resource.getParseResult();
	if (parseResult == null)
		throw new IllegalStateException("resource#parseResult may not be null");
	ICompositeNode node = parseResult.getRootNode();
	highlightSpecialIdentifiers(acceptor, node);
	super.doProvideHighlightingFor(resource, acceptor, cancelIndicator);
}
 
Example 18
Source File: JSONSemanticHighlightingCalculator.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor,
		CancelIndicator cancelIndicator) {
	if (resource == null || resource.getParseResult() == null) {
		// skip invalid resources
		return;
	}

	// obtain root node
	INode root = resource.getParseResult().getRootNode();

	for (INode node : root.getAsTreeIterable()) {
		EObject grammarElement = node.getGrammarElement();

		// special handling of the names of name-value-pairs in order to differentiate
		// keys and values
		if (grammarElement instanceof RuleCall && grammarElement.eContainer() instanceof Assignment
				&& ((RuleCall) grammarElement).getRule() == grammarAccess.getSTRINGRule()) {
			final Assignment assignment = ((Assignment) grammarElement.eContainer());

			// if the STRING value is assigned to the feature 'name' of NameValuePair
			if (assignment.getFeature().equals(JSONPackage.Literals.NAME_VALUE_PAIR__NAME.getName())) {
				// enable PROPERTY_NAME highlighting
				acceptor.addPosition(node.getOffset(), node.getLength(),
						JSONHighlightingConfiguration.PROPERTY_NAME_ID);
			} else {
				// otherwise enable string literal highlighting
				acceptor.addPosition(node.getOffset(), node.getLength(), JSONHighlightingConfiguration.STRING_ID);
			}
		}
	}
}
 
Example 19
Source File: AbstractXtextTests.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void checkNodeModel(XtextResource resource) {
	IParseResult parseResult = resource.getParseResult();
	if(parseResult != null)
		getInvariantChecker().checkInvariant(parseResult.getRootNode());
}
 
Example 20
Source File: AbstractXtextTests.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected void checkNodeModel(XtextResource resource) {
	IParseResult parseResult = resource.getParseResult();
	if(parseResult != null)
		getInvariantChecker().checkInvariant(parseResult.getRootNode());
}