Java Code Examples for org.eclipse.xtext.nodemodel.ICompositeNode#getTextRegion()

The following examples show how to use org.eclipse.xtext.nodemodel.ICompositeNode#getTextRegion() . 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: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void checkNoJavaStyleTypeCasting(INode node) {
	BidiTreeIterator<INode> iterator = node.getAsTreeIterable().reverse().iterator();
	ILeafNode child = getFirstLeafNode(iterator);
	if (child != null && child.getGrammarElement() == grammarAccess.getXParenthesizedExpressionAccess().getRightParenthesisKeyword_2()) {
		INode expressionNode = getNode(iterator, grammarAccess.getXParenthesizedExpressionAccess().getXExpressionParserRuleCall_1());
		EObject semanticObject = NodeModelUtils.findActualSemanticObjectFor(expressionNode);
		if (semanticObject instanceof XFeatureCall || semanticObject instanceof XMemberFeatureCall) {
			XAbstractFeatureCall featureCall = (XAbstractFeatureCall) semanticObject;
			if (featureCall.isTypeLiteral()) {
				ICompositeNode parenthesizedNode = child.getParent();
				ITextRegion parenthesizedRegion = parenthesizedNode.getTextRegion();
				addIssue("Use 'as' keyword for type casting.", featureCall, parenthesizedRegion.getOffset(), parenthesizedRegion.getLength(), JAVA_STYLE_TYPE_CAST);
			}
		}
	}
}
 
Example 2
Source File: DefaultNodeModelFormatter.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public IFormattedRegion format(ICompositeNode root, int offset, int length) {
	String indent = getIndentation(root, offset);
	TokenStringBuffer buf = new TokenStringBuffer();
	ITokenStream out = offset == 0 ? buf : new FilterFirstWhitespaceStream(buf);
	ITokenStream fmt;
	if (formatter instanceof IFormatterExtension) {
		EObject semanticElement = NodeModelUtils.findActualSemanticObjectFor(root);
		if (semanticElement != null)
			fmt = ((IFormatterExtension) formatter).createFormatterStream(semanticElement, indent, out, false);
		else {
			// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=380406
			ITextRegion rootRegion = root.getTextRegion();
			return new FormattedRegion(rootRegion.getOffset(), rootRegion.getLength(), root.getText());
		}
	} else
		fmt = formatter.createFormatterStream(indent, out, false);
	try {
		ITextRegion range = nodeModelStreamer.feedTokenStream(fmt, root, offset, length);
		return new FormattedRegion(range.getOffset(), range.getLength(), buf.toString());
	} catch (IOException e) {
		// this should never happen since TokenStringBuffer doesn't throw IOEs.
		throw new RuntimeException(e);
	}
}
 
Example 3
Source File: RewritableImportSection.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public List<ReplaceRegion> rewrite() {
	removeObsoleteStaticImports();
	final List<ReplaceRegion> replaceRegions = newArrayList();
	if (isSort) {
		List<XImportDeclaration> allImportDeclarations = newArrayList();
		allImportDeclarations.addAll(originalImportDeclarations);
		allImportDeclarations.addAll(addedImportDeclarations);
		allImportDeclarations.removeAll(removedImportDeclarations);
		String newImportSection = serializeImports(allImportDeclarations);
		importRegion = regionUtil.addLeadingWhitespace(importRegion, resource);
		importRegion = regionUtil.addTrailingWhitespace(importRegion, resource);
		return singletonList(new ReplaceRegion(importRegion, newImportSection));
	} else {
		for (XImportDeclaration removedImportDeclaration : removedImportDeclarations) {
			ICompositeNode node = NodeModelUtils.findActualNodeFor(removedImportDeclaration);
			if (node != null) {
				ITextRegion textRegion = node.getTextRegion();
				textRegion = regionUtil.addTrailingSingleWhitespace(textRegion, lineSeparator, resource);
				replaceRegions.add(new ReplaceRegion(textRegion, ""));
			}
		}
		addSectionToAppend(new IAcceptor<ReplaceRegion>() {
			@Override
			public void accept(ReplaceRegion t) {
				replaceRegions.add(t);
			}
		});
	}
	return replaceRegions;
}
 
Example 4
Source File: Xtext2RailroadFactory.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected Region getTextRegion(EObject eObject) {
	ICompositeNode parseTreeNode = NodeModelUtils.getNode(eObject);
	if (parseTreeNode != null) {
		ITextRegion parseTreeRegion = parseTreeNode.getTextRegion();
		return new Region(parseTreeRegion.getOffset(), parseTreeRegion.getLength());
	}
	else
		return null;
}
 
Example 5
Source File: DotOutlineTreeProvider.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected EObjectNode createEObjectNode(IOutlineNode parentNode,
		EObject modelElement, Image image, Object text, boolean isLeaf) {
	if (EcoreUtil2.getContainerOfType(modelElement,
			HtmlLabel.class) != null) {
		// in case of a html-like label addition offset should be calculated
		EObjectNode eObjectNode = new EObjectNode(modelElement, parentNode,
				image, text, isLeaf);
		ICompositeNode parserNode = NodeModelUtils.getNode(modelElement);
		if (parserNode != null) {
			ITextRegion parserNodeTextRegion = parserNode.getTextRegion();
			ITextRegion newTextRegion = new TextRegion(
					parserNodeTextRegion.getOffset()
							+ attributeValueStartOffset,
					parserNodeTextRegion.getLength());
			eObjectNode.setTextRegion(newTextRegion);
		}
		if (isLocalElement(parentNode, modelElement)) {
			ITextRegion significantTextRegion = locationInFileProvider
					.getSignificantTextRegion(modelElement);
			ITextRegion shortTextRegion = new TextRegion(
					significantTextRegion.getOffset()
							+ attributeValueStartOffset,
					significantTextRegion.getLength());
			eObjectNode.setShortTextRegion(shortTextRegion);
		}
		return eObjectNode;
	} else {
		return super.createEObjectNode(parentNode, modelElement, image,
				text, isLeaf);
	}

}
 
Example 6
Source File: DotHtmlLabelOutlineTreeProvider.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected EObjectNode createEObjectNode(IOutlineNode parentNode,
		EObject modelElement, Image image, Object text, boolean isLeaf) {
	EObjectNode eObjectNode = new EObjectNode(modelElement, parentNode,
			image, text, isLeaf) {
		@Override
		public IXtextDocument getDocument() {
			return xtextDocument != null ? xtextDocument
					: super.getDocument();
		}
	};
	ICompositeNode parserNode = NodeModelUtils.getNode(modelElement);
	if (parserNode != null) {
		ITextRegion parserNodeTextRegion = parserNode.getTextRegion();
		ITextRegion newTextRegion = new TextRegion(
				parserNodeTextRegion.getOffset() + offset,
				parserNodeTextRegion.getLength());
		eObjectNode.setTextRegion(newTextRegion);
	}
	/* if (isLocalElement(parentNode, modelElement)) { */
	ITextRegion significantTextRegion = locationInFileProvider
			.getSignificantTextRegion(modelElement);
	ITextRegion shortTextRegion = new TextRegion(
			significantTextRegion.getOffset() + offset,
			significantTextRegion.getLength());
	eObjectNode.setShortTextRegion(shortTextRegion);
	// }
	return eObjectNode;
}
 
Example 7
Source File: XtendFoldingRegionProvider.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void computeImportFolding(XtendFile xtendFile, IFoldingRegionAcceptor<ITextRegion> foldingRegionAcceptor) {
	if(xtendFile.getImportSection() != null 
			&& xtendFile.getImportSection().getImportDeclarations().size() >1) {
		ICompositeNode node = NodeModelUtils.findActualNodeFor(xtendFile.getImportSection());
		if(node != null) {
			ITextRegion textRegion = node.getTextRegion();
			((IFoldingRegionAcceptorExtension<ITextRegion>)foldingRegionAcceptor).accept(textRegion.getOffset(), textRegion.getLength(), foldingPreferences.isFoldImports());
		}
	}
}
 
Example 8
Source File: XbaseHighlightingCalculator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void highlightNumberLiterals(XNumberLiteral literal, IHighlightedPositionAcceptor acceptor) {
	ICompositeNode node = NodeModelUtils.findActualNodeFor(literal);
	ITextRegion textRegion = node.getTextRegion();
	acceptor.addPosition(textRegion.getOffset(), textRegion.getLength(), NUMBER_ID);
}
 
Example 9
Source File: ParameterContextInformationProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void getContextInformation(ContentAssistContext context, IContextInformationAcceptor acceptor) {
	XExpression containerCall = getContainerCall(eObjectAtOffsetHelper.resolveContainedElementAt(context.getResource(), context.getOffset()));
	LightweightTypeReferenceFactory factory = proposalProvider.getTypeConverter(context.getResource());
	if (containerCall != null) {
		ICompositeNode containerCallNode = NodeModelUtils.findActualNodeFor(containerCall);
		ITextRegion containerCallRegion = containerCallNode.getTextRegion();
		if(containerCallRegion.getOffset() > context.getOffset()
				|| containerCallRegion.getOffset() + containerCallRegion.getLength() < context.getOffset()) 
			return;
		JvmIdentifiableElement calledFeature = getCalledFeature(containerCall);
		if (calledFeature instanceof JvmExecutable) {
			if(getParameterListOffset(containerCall) > context.getOffset()) 
				return;
			ParameterData parameterData = new ParameterData();
			IScope scope = getScope(containerCall);
			QualifiedName qualifiedName = QualifiedName.create(getCalledFeatureName(containerCall));
			boolean candidatesFound = false;
			for (IEObjectDescription element : scope.getElements(qualifiedName)) {
				if (element instanceof IIdentifiableElementDescription) {
					IIdentifiableElementDescription featureDescription = (IIdentifiableElementDescription) element;
					JvmIdentifiableElement featureCandidate = featureDescription.getElementOrProxy();
					if (featureCandidate instanceof JvmExecutable) {
						JvmExecutable executable = (JvmExecutable) featureCandidate;
						if(!executable.getParameters().isEmpty()) {
							StyledString styledString = new StyledString();
							proposalProvider.appendParameters(styledString, executable,
									featureDescription.getNumberOfIrrelevantParameters(), factory);
							parameterData.addOverloaded(styledString.toString(), executable.isVarArgs());
							candidatesFound = true;
						}
					}
				}
			}
			if (candidatesFound) {
				StyledString displayString = proposalProvider.getStyledDisplayString((JvmExecutable) calledFeature, true, 0, 
						qualifiedNameConverter.toString(qualifiedNameProvider.getFullyQualifiedName(calledFeature)), 
						calledFeature.getSimpleName(), factory);
				ParameterContextInformation parameterContextInformation = new ParameterContextInformation(
						parameterData, displayString.toString(), getParameterListOffset(containerCall), context.getOffset());
				acceptor.accept(parameterContextInformation);
			}
		}
	}
}