Java Code Examples for org.eclipse.xtext.util.ITextRegion#contains()

The following examples show how to use org.eclipse.xtext.util.ITextRegion#contains() . 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: ImportsCollector.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Collects import declarations in XtextResource for the given range (selectedRegion)
 */
public void collectImports(final XtextResource state, final ITextRegion selectedRegion, final ImportsAcceptor acceptor) {
  ICompositeNode rootNode = state.getParseResult().getRootNode();
  final EObject selectedSemanticObj = this.findActualSemanticObjectFor(rootNode, selectedRegion);
  final Iterable<ILeafNode> contentsIterator = NodeModelUtils.findActualNodeFor(selectedSemanticObj).getLeafNodes();
  for (final ILeafNode node : contentsIterator) {
    {
      final ITextRegion nodeRegion = node.getTotalTextRegion();
      boolean _contains = selectedRegion.contains(nodeRegion);
      if (_contains) {
        final EObject semanticElement = node.getSemanticElement();
        if ((semanticElement != null)) {
          this.visit(semanticElement, NodeModelUtils.findActualNodeFor(semanticElement), acceptor);
        }
      }
      if ((node.isHidden() && this.grammarAccess.getML_COMMENTRule().equals(node.getGrammarElement()))) {
        this.addJavaDocReferences(node, selectedRegion, acceptor);
      }
    }
  }
}
 
Example 2
Source File: OutlineWithEditorLinker.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected IOutlineNode findBestNode(IOutlineNode input, ITextRegion selectedTextRegion) {
	ITextRegion textRegion = input.getFullTextRegion();
	if (textRegion == null || textRegion.contains(selectedTextRegion)) {
		IOutlineNode currentBestNode = input;
		for (IOutlineNode child : input.getChildren()) {
			IOutlineNode candidate = findBestNode(child, selectedTextRegion);
			if (candidate != null
					&& (currentBestNode.getFullTextRegion() == null || candidate.getFullTextRegion() != null
							&& currentBestNode.getFullTextRegion().getLength() >= candidate.getFullTextRegion().getLength())) {
				currentBestNode = candidate;
			}
		}
		return currentBestNode;
	}
	return null;
}
 
Example 3
Source File: ImportsCollector.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private void addJavaDocReferences(final INode documentationNode, final ITextRegion selectedRegion, final ImportsAcceptor acceptor) {
  List<ReplaceRegion> _computeTypeRefRegions = this.javaDocTypeReferenceProvider.computeTypeRefRegions(documentationNode);
  for (final ReplaceRegion docTypeReference : _computeTypeRefRegions) {
    {
      int _offset = docTypeReference.getOffset();
      int _length = docTypeReference.getLength();
      final TextRegion referenceRange = new TextRegion(_offset, _length);
      boolean _contains = selectedRegion.contains(referenceRange);
      if (_contains) {
        String docTypeText = docTypeReference.getText();
        final EObject element = NodeModelUtils.findActualSemanticObjectFor(documentationNode);
        IScope scope = this.scopeProvider.getScope(element, 
          TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE);
        IEObjectDescription singleElement = scope.getSingleElement(QualifiedName.create(docTypeText));
        JvmType referencedType = null;
        if ((singleElement != null)) {
          EObject _eObjectOrProxy = singleElement.getEObjectOrProxy();
          referencedType = ((JvmType) _eObjectOrProxy);
        }
        if (((referencedType instanceof JvmDeclaredType) && (!referencedType.eIsProxy()))) {
          JvmDeclaredType casted = ((JvmDeclaredType) referencedType);
          boolean _equals = casted.getQualifiedName().equals(docTypeText);
          boolean _not = (!_equals);
          if (_not) {
            acceptor.acceptTypeImport(casted);
          }
        }
      }
    }
  }
}
 
Example 4
Source File: FormatterTester.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertReplacementsAreInRegion(List<ITextReplacement> rep, Collection<ITextRegion> regions,
		String doc) {
	Set<ITextReplacement> invalid = Sets.newHashSet();
	ALLOWED: for (ITextRegion allowed : regions)
		for (ITextReplacement r : rep) {
			if (allowed.contains(r))
				continue ALLOWED;
			invalid.add(r);
		}
	if (!invalid.isEmpty()) {
		String visualized = new TextRegionsToString().addAllReplacements(invalid).toString();
		fail("One or more TextReplacements are outside of the allowed region. Region: " + regions, visualized);
	}
}
 
Example 5
Source File: ElementAtOffsetUtil.java    From xtext-web with Eclipse Public License 2.0 5 votes vote down vote up
public EObject getElementAt(XtextResource resource, int offset) {
	EObject crossLinkedEObject = eObjectAtOffsetHelper.resolveCrossReferencedElementAt(resource, offset);
	if (crossLinkedEObject != null) {
		return crossLinkedEObject;
	} else {
		EObject containedEObject = eObjectAtOffsetHelper.resolveContainedElementAt(resource, offset);
		if (containedEObject != null) {
			ITextRegion nameRegion = locationInFileProvider.getSignificantTextRegion(containedEObject);
			if (nameRegion.contains(offset)) {
				return containedEObject;
			}
		}
	}
	return null;
}
 
Example 6
Source File: XtextHyperlinkHelper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void createHyperlinksByOffset(XtextResource resource, int offset, IHyperlinkAcceptor acceptor) {
	super.createHyperlinksByOffset(resource, offset, acceptor);
	EObject objectAtOffset = eObjectAtOffsetHelper.resolveElementAt(resource, offset);
	if (objectAtOffset instanceof AbstractRule) {
		ITextRegion nameLocation = locationInFileProvider.getSignificantTextRegion(objectAtOffset, XtextPackage.Literals.ABSTRACT_RULE__NAME, 0);
		if (nameLocation != null && nameLocation.contains(offset)) {
			AbstractRule rule = (AbstractRule) objectAtOffset;
			createLinksToBase(nameLocation, rule, acceptor);
			if (rule.getType() != null && rule.getType().getClassifier() != null && NodeModelUtils.getNode(rule.getType()) == null) {
				createHyperlinksTo(resource, toRegion(nameLocation), rule.getType().getClassifier(), acceptor);
			}
		}
	}
}
 
Example 7
Source File: DefaultRenameElementHandler.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean isRefactoringEnabled(IRenameElementContext renameElementContext, XtextResource resource) {
	ResourceSet resourceSet = resource.getResourceSet();
	if (renameElementContext != null && resourceSet != null) {
		EObject targetElement = resourceSet.getEObject(renameElementContext.getTargetElementURI(), true);
		if (targetElement != null && !targetElement.eIsProxy()) {
			if(targetElement.eResource() == resource && renameElementContext.getTriggeringEditorSelection() instanceof ITextSelection) {
				ITextSelection textSelection = (ITextSelection) renameElementContext.getTriggeringEditorSelection();
				ITextRegion selectedRegion = new TextRegion(textSelection.getOffset(), textSelection.getLength());
				INode crossReferenceNode = eObjectAtOffsetHelper.getCrossReferenceNode(resource, selectedRegion);
				if(crossReferenceNode == null) {
					// selection is on the declaration. make sure it's the name
					ITextRegion significantRegion = locationInFileProvider.getSignificantTextRegion(targetElement);
					if(!significantRegion.contains(selectedRegion)) 
						return false;
				}
			}
			IRenameStrategy.Provider renameStrategyProvider = globalServiceProvider.findService(targetElement,
					IRenameStrategy.Provider.class);
			try {
				if (renameStrategyProvider.get(targetElement, renameElementContext) != null) {
					return true;
				} else {
					IRenameStrategy2 strategy2 = globalServiceProvider.findService(targetElement, IRenameStrategy2.class); 
					ISimpleNameProvider simpleNameProvider = globalServiceProvider.findService(targetElement, ISimpleNameProvider.class); 
					return strategy2 != null && simpleNameProvider.canRename(targetElement);
				}
					
			} catch (NoSuchStrategyException e) {
				MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Cannot rename element",
						e.getMessage());
			}
		}
	}
	return false;
}
 
Example 8
Source File: XbaseHyperLinkHelper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
private ITextRegion getTextRegion(final XImportDeclaration it, final int offset) {
	final List<INode> nodes = NodeModelUtils.findNodesForFeature(it,
			XtypePackage.Literals.XIMPORT_DECLARATION__MEMBER_NAME);
	for (final INode node : nodes) {
		final ITextRegion textRegion = node.getTextRegion();
		if (textRegion.contains(offset)) {
			return textRegion;
		}
	}
	return null;
}
 
Example 9
Source File: FormatterTester.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertReplacementsAreInRegion(List<ITextReplacement> rep, Collection<ITextRegion> regions,
		String doc) {
	Set<ITextReplacement> invalid = Sets.newHashSet();
	ALLOWED: for (ITextRegion allowed : regions)
		for (ITextReplacement r : rep) {
			if (allowed.contains(r))
				continue ALLOWED;
			invalid.add(r);
		}
	if (!invalid.isEmpty()) {
		String visualized = new TextRegionsToString().addAllReplacements(invalid).toString();
		fail("One or more TextReplacements are outside of the allowed region. Region: " + regions, visualized);
	}
}
 
Example 10
Source File: XtendOutlineWithEditorLinker.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void findNodesInRange(final IOutlineNode input, final ITextRegion selectedTextRegion, final List<IOutlineNode> nodes) {
  final ITextRegion textRegion = input.getFullTextRegion();
  if (((textRegion == null) || textRegion.contains(selectedTextRegion))) {
    nodes.add(input);
  }
  List<IOutlineNode> _children = input.getChildren();
  for (final IOutlineNode child : _children) {
    this.findNodesInRange(child, selectedTextRegion, nodes);
  }
}
 
Example 11
Source File: AbstractTextSegment.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public ITextSegment merge(ITextRegion other) {
	if (contains(other))
		return this;
	if (other instanceof ITextSegment && other.contains(this))
		return (ITextSegment) other;
	int offset = getOffset();
	int length = getLength();
	int newOffset = Math.min(offset, other.getOffset());
	int newLength = Math.max(offset + length, other.getOffset() + other.getLength()) - newOffset;
	return new TextSegment(getTextRegionAccess(), newOffset, newLength);
}
 
Example 12
Source File: FormatterTestHelper.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void assertReplacementsAreInRegion(List<ITextReplacement> rep, Collection<ITextRegion> regions,
		String doc) {
	Set<ITextReplacement> invalid = Sets.newHashSet();
	ALLOWED: for (ITextRegion allowed : regions)
		for (ITextReplacement r : rep) {
			if (allowed.contains(r))
				continue ALLOWED;
			invalid.add(r);
		}
	if (!invalid.isEmpty()) {
		String visualized = new TextRegionsToString().addAllReplacements(invalid).toString();
		fail("One or more TextReplacements are outside of the allowed region. Region: " + regions, visualized);
	}
}
 
Example 13
Source File: DefaultDocumentHighlightService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns with {@code true} if the AST element selected from the resource
 * can provide document highlights, otherwise returns with {@code false}.
 * 
 * <p>
 * Clients may override this method to change the default behavior.
 * 
 * @param selectedElement
 *            the selected element resolved via the offset from the
 *            resource. Can be {@code null}.
 * @param resource
 *            the resource for the document.
 * @param offset
 *            the offset of the selection.
 * 
 * @return {@code true} if the document highlight is available for the
 *         selected element, otherwise {@code false}.
 *
 */
protected boolean isDocumentHighlightAvailableFor(final EObject selectedElement, final XtextResource resource,
		final int offset) {
	if (selectedElement == null || !getSelectedElementFilter().apply(selectedElement)) {
		return false;
	}

	// This code handles the special case where your language has constructs that can refer to
	// themselves. For example "function MyFunction begin ... end MyFunction" defines the function "MyFunction" and
	// terminates its implementation block with an additional repetition of the word "MyFunction". Normally, when
	// you are positioned on a selected element, you only want to highlight that selected element when you are
	// positioned directly on top of the name of the selected element. However, when the selected element can refer
	// to itself then there are references inside the element that must trigger highlighting.  In the example,
	// we also want to highlight "MyFunction" when we are positioned on the "end MyFunction".
	
	INode crossReferenceNode = offsetHelper.getCrossReferenceNode(resource, new TextRegion(offset, 0));
	
	if (crossReferenceNode != null) {
		EObject crossReferencedElement = offsetHelper.getCrossReferencedElement(crossReferenceNode);

		if (crossReferencedElement != null && crossReferencedElement == selectedElement) {
			return true;
		}
	}

	final EObject containedElement = offsetHelper.resolveContainedElementAt(resource, offset);
	// When the cursor is positioned in the selected element, then we only want to highlight the selected element
	// when we are directly on top of the name (the significant text region) of that element.
	
		if (selectedElement == containedElement) {
		final ITextRegion region = locationInFileProvider.getSignificantTextRegion(containedElement);
		return !isNullOrEmpty(region)
				// Region is comparable to a selection in an editor,
				// therefore the end position is exclusive.
				&& (region.contains(offset) || (region.getOffset() + region.getLength()) == offset);
	}

	return true;
}
 
Example 14
Source File: AbstractTraceRegionToString.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean shouldInclude(AbstractTraceRegion region) {
	ITextRegion frame = getLocalFrame();
	return frame == null ? true : frame.contains(region.getMyRegion());
}
 
Example 15
Source File: AbstractTraceRegionToString.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean shouldInclude(AbstractTraceRegion region, ILocationData location) {
	SourceRelativeURI srcRelativePath = location.getSrcRelativePath();
	ITextRegion frame = getRemoteFrame(
			srcRelativePath == null ? region.getAssociatedSrcRelativePath() : srcRelativePath);
	return frame == null ? true : frame.contains(location);
}