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

The following examples show how to use org.eclipse.xtext.resource.XtextResource#getContents() . 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: N4JSDirtyStateEditorSupport.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void modelChanged(XtextResource resource) {
	if (resource == null || !getDirtyResource().isInitialized())
		return;
	resource.getContents(); // trigger init
	super.modelChanged(resource);
}
 
Example 2
Source File: JSONHierarchicalSymbolService.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public List<Either<SymbolInformation, DocumentSymbol>> getSymbols(XtextResource resource,
		CancelIndicator cancelIndicator) {
	List<Either<SymbolInformation, DocumentSymbol>> result = new ArrayList<>();
	for (EObject content : resource.getContents()) {
		if (content instanceof JSONDocument) {
			JSONDocument document = (JSONDocument) content;
			JSONValue rootValue = document.getContent();
			getSymbols(rootValue, symbol -> result.add(Either.forRight(symbol)), cancelIndicator);
		}
	}
	return result;
}
 
Example 3
Source File: DefaultImportsConfiguration.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public XImportSection getImportSection(XtextResource resource) {
	EList<EObject> contents = resource.getContents();
	if (!contents.isEmpty())
	{
		for (Iterator<EObject> i = contents.get(0).eAllContents(); i.hasNext();) {
			EObject next = i.next();
			if (next instanceof XImportSection)
				return (XImportSection) next;
		}
	}
	return null;
}
 
Example 4
Source File: RailroadSynchronizer.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private IFigure createFigure(XtextResource state) {
	EList<EObject> contents = state.getContents();
	if (!contents.isEmpty()) {
		EObject rootObject = contents.get(0);
		return transformer.transform(rootObject);
	}
	return null;
}
 
Example 5
Source File: ArithmeticsCodeMiningProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void createCodeMinings(IDocument document, XtextResource resource, CancelIndicator indicator,
	IAcceptor<? super ICodeMining> acceptor) throws BadLocationException {

	EList<EObject> contents = resource.getContents();
	if (contents.isEmpty()) {
		return;
	}

	// get all evaluations contained by the open document
	List<Evaluation> allEvaluations = EcoreUtil2.eAllOfType(contents.get(0), Evaluation.class);

	// get keyword for ';'
	Keyword semicolon = grammar.getEvaluationAccess().getSemicolonKeyword_1();

	for (Evaluation evaluation : allEvaluations) {
		ICompositeNode node = NodeModelUtils.findActualNodeFor(evaluation);
		for (Iterator<INode> it = node.getAsTreeIterable().iterator(); it.hasNext();) {
			INode child = it.next();
			if (semicolon.equals(child.getGrammarElement())) {
				int annotationOffset = child.getTotalOffset();
				String annotationText = getAnnotationText(evaluation);
				acceptor.accept(createNewLineContentCodeMining(annotationOffset, annotationText));
			}
		}
	}
}
 
Example 6
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 7
Source File: HighlightingReconciler.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.8
 */
protected void beforeRefresh(XtextResource resource, CancelIndicator cancelIndicator) {
	if (resource instanceof DerivedStateAwareResource)
		resource.getContents(); // trigger derived state computation
	if (resource instanceof IBatchLinkableResource)
		((IBatchLinkableResource) resource).linkBatched(cancelIndicator);
}
 
Example 8
Source File: OverrideIndicatorModelListener.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected Map<Annotation, Position> createOverrideIndicatorAnnotationMap(XtextResource xtextResource, CancelIndicator cancelIndicator) {
	List<EObject> contents = xtextResource.getContents();
	if (contents.isEmpty())
		return Maps.newHashMap();
	EObject eObject = contents.get(0);
	if (!(eObject instanceof XtendFile)) {
		return Maps.newHashMap();
	}
	XtendFile xtendFile = (XtendFile) eObject;
	Map<Annotation, Position> annotationToPosition = Maps.newHashMap();
	for (XtendFunction xtendFunction : getXtendFunctions(xtendFile)) {
		if(cancelIndicator.isCanceled())
			throw new OperationCanceledException();
		if (xtendFunction.isOverride()) {
			typeResolver.resolveTypes(xtendFunction);
			INode node = NodeModelUtils.getNode(xtendFunction);
			JvmOperation inferredOperation = associations.getDirectlyInferredOperation(xtendFunction);
			if (inferredOperation != null) {
				JvmOperation jvmOperation = overrideHelper.findOverriddenOperation(inferredOperation);
				if (node != null && jvmOperation != null) {
					boolean overwriteIndicator = isOverwriteIndicator(jvmOperation);
					String text = (overwriteIndicator ? "overrides " : "implements ") + jvmOperation.getQualifiedName(); //$NON-NLS-1$ //$NON-NLS-2$
					node = getFirst(findNodesForFeature(xtendFunction, XtendPackage.eINSTANCE.getXtendFunction_Name()),
							node);
					annotationToPosition.put(
							new OverrideIndicatorAnnotation(overwriteIndicator, text, xtextResource
									.getURIFragment(xtendFunction)), new Position(node.getOffset()));
				}
			}
		}
	}
	return annotationToPosition;
}
 
Example 9
Source File: AbstractFormatter2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * For {@link XtextResource resources}, assume we want to format the first EObject from the contents list only.
 * Because that's where the parser puts the semantic model.
 */
protected void _format(XtextResource resource, IFormattableDocument document) {
	List<EObject> contents = resource.getContents();
	if (!contents.isEmpty()) {
		EObject model = contents.get(0);
		document.format(model);
	}
}
 
Example 10
Source File: XtextLinkerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testExplicitRuleCallsAreTracked() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar test.Lang with org.eclipse.xtext.common.Terminals");
  _builder.newLine();
  _builder.append("generate test \'http://test\'");
  _builder.newLine();
  _builder.append("Rule: name=super::ID name=ID;");
  _builder.newLine();
  _builder.append("terminal ID: super;");
  _builder.newLine();
  _builder.append("terminal _super: \'s\';");
  _builder.newLine();
  final String grammarAsString = _builder.toString();
  final XtextResource resource = this.getResourceFromString(grammarAsString);
  EObject _get = resource.getContents().get(0);
  Grammar grammar = ((Grammar) _get);
  final AbstractRule firstRule = IterableExtensions.<AbstractRule>head(grammar.getRules());
  final RuleCall firstRuleCall = IteratorExtensions.<RuleCall>head(Iterators.<RuleCall>filter(firstRule.eAllContents(), RuleCall.class));
  Assert.assertTrue(firstRuleCall.isExplicitlyCalled());
  final RuleCall secondRuleCall = IteratorExtensions.<RuleCall>last(Iterators.<RuleCall>filter(firstRule.eAllContents(), RuleCall.class));
  Assert.assertFalse(secondRuleCall.isExplicitlyCalled());
  final RuleCall thirdRuleCall = IteratorExtensions.<RuleCall>head(Iterators.<RuleCall>filter(grammar.getRules().get(1).eAllContents(), RuleCall.class));
  Assert.assertTrue(thirdRuleCall.isExplicitlyCalled());
  resource.update(grammarAsString.indexOf("_super"), 1, " ");
  Assert.assertEquals(resource, firstRuleCall.eResource());
  Assert.assertEquals(resource, secondRuleCall.eResource());
  Assert.assertEquals(resource, thirdRuleCall.eResource());
  resource.getContents();
  Assert.assertFalse(thirdRuleCall.isExplicitlyCalled());
  Assert.assertEquals(IterableExtensions.<AbstractRule>last(grammar.getRules()), thirdRuleCall.getRule());
}
 
Example 11
Source File: ContentAssistContextFactory.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public ContentAssistContext[] create(String document, ITextRegion selection, int offset, XtextResource resource) {
	this.document = document;
	this.selection = selection;
	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 12
Source File: FixedHighlightingReconciler.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @since 2.8
 */
@Override
protected void beforeRefresh(final XtextResource resource, final CancelIndicator cancelIndicator) {
  if (resource instanceof DerivedStateAwareResource) {
    resource.getContents(); // trigger derived state computation
  }
  if (resource instanceof IBatchLinkableResource) {
    ((IBatchLinkableResource) resource).linkBatched(cancelIndicator);
  }
}