Java Code Examples for org.eclipse.xtext.util.ReplaceRegion#getLength()

The following examples show how to use org.eclipse.xtext.util.ReplaceRegion#getLength() . 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: DefaultTextEditComposer.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected List<TextEdit> getObjectEdits() {
	final Collection<EObject> modifiedObjects = getModifiedObjects();
	Collection<EObject> topLevelObjects = EcoreUtil.filterDescendants(modifiedObjects);
	Iterable<EObject> containedModifiedObjects = Collections.emptyList();
	if (!resource.getContents().isEmpty()) {
		final EObject root = resource.getContents().get(0);
		containedModifiedObjects = Iterables.filter(topLevelObjects, new Predicate<EObject>() {
			@Override
			public boolean apply(EObject input) {
				return EcoreUtil.isAncestor(root, input);
			}
		});
	}
	List<TextEdit> edits = Lists.newArrayListWithExpectedSize(Iterables.size(containedModifiedObjects));
	for (EObject modifiedObject : containedModifiedObjects) {
		ReplaceRegion replaceRegion = serializer.serializeReplacement(modifiedObject, getSaveOptions());
		TextEdit edit = new ReplaceEdit(replaceRegion.getOffset(), replaceRegion.getLength(), replaceRegion.getText());
		edits.add(edit);
	}
	return edits;
}
 
Example 2
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 3
Source File: ReplacingAppendable.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public int insertNewImports() throws BadLocationException {
	List<ReplaceRegion> importChanges = getImportSection().rewrite();
	int lengthDelta = 0;
	if(!importChanges.isEmpty()) {
		for(ReplaceRegion change: importChanges) {
			lengthDelta = lengthDelta - change.getLength() + change.getText().length(); 
		}
		replaceConverter.convertToTextEdit(importChanges).apply(getDocument());
	}
	return lengthDelta;
}
 
Example 4
Source File: ReplaceConverter.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public int getReplaceLengthDelta(List<ReplaceRegion> changes, int caretPosition) {
	int delta = 0;
	if(changes != null && !changes.isEmpty()) {
		for(ReplaceRegion change: changes) {
			if(change.getEndOffset() < caretPosition)
				delta += change.getText().length() - change.getLength();
		}
	}
	return delta;
}
 
Example 5
Source File: PartialParsingHelper.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isNullEdit(INode oldRootNode, ReplaceRegion replaceRegion) {
	if (replaceRegion.getLength() == replaceRegion.getText().length()) {
		String replacedText = oldRootNode.getText().substring(replaceRegion.getOffset(), replaceRegion.getEndOffset());
		if (replaceRegion.getText().equals(replacedText)) {
			return true;
		}
	}
	return false;
}
 
Example 6
Source File: TokenRegionProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public ReplaceRegion getTokenReplaceRegion(String changedText, ReplaceRegion replaceRegion) {
	int lengthDelta = replaceRegion.getText().length() - replaceRegion.getLength();
	ITextRegion tokenRegion = getTokenRegion(changedText, new TextRegion(replaceRegion.getOffset(), replaceRegion
			.getText().length()));
	if (tokenRegion.getOffset() == replaceRegion.getOffset()
			&& tokenRegion.getLength() == replaceRegion.getText().length())
		return replaceRegion;
	else 
		return new ReplaceRegion(tokenRegion.getOffset(), tokenRegion.getLength() - lengthDelta,
				changedText.substring(tokenRegion.getOffset(), tokenRegion.getOffset() + tokenRegion.getLength()));
}
 
Example 7
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 8
Source File: FixedPartialParsingHelper.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isNullEdit(final INode oldRootNode, final ReplaceRegion replaceRegion) {
  if (replaceRegion.getLength() == replaceRegion.getText().length()) {
    String replacedText = oldRootNode.getText().substring(replaceRegion.getOffset(), replaceRegion.getEndOffset());
    if (replaceRegion.getText().equals(replacedText)) {
      return true;
    }
  }
  return false;
}
 
Example 9
Source File: TokenSequencePreservingPartialParsingHelper.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public IParseResult reparse(IParser parser, IParseResult previousParseResult, ReplaceRegion changedRegion) {
	if (isBrokenPreviousState(previousParseResult, changedRegion.getOffset())) {
		return fullyReparse(parser, previousParseResult, changedRegion);
	}
	ICompositeNode oldRootNode = previousParseResult.getRootNode();
	Iterator<ILeafNode> leafNodes = oldRootNode.getLeafNodes().iterator();
	ILeafNode leftNode = getLeftNode(leafNodes, changedRegion.getOffset());
	if (leftNode == null) {
		return fullyReparse(parser, previousParseResult, changedRegion);
	}
	ILeafNode rightNode = getRightNode(leafNodes, changedRegion.getEndOffset());
	if (rightNode == null) {
		return fullyReparse(parser, previousParseResult, changedRegion);
	}
	while(leafNodes.hasNext()) {
		if (leafNodes.next().getSyntaxErrorMessage() != null) {
			return fullyReparse(parser, previousParseResult, changedRegion);
		}
	}
	String originalText = oldRootNode.getText().substring(leftNode.getTotalOffset());
	StringBuilder newTextBuilder = new StringBuilder(originalText);
	changedRegion.shiftBy(-leftNode.getTotalOffset()).applyTo(newTextBuilder);
	String newText = newTextBuilder.toString();
	if (originalText.equals(newText)) {
		// nothing to do
		return previousParseResult;
	}
	int originalLength = rightNode.getTotalEndOffset() - leftNode.getTotalOffset();
	int expectedLength = originalLength - changedRegion.getLength() + changedRegion.getText().length();
	if (!isSameTokenSequence(originalText.substring(0, originalLength), newText, expectedLength)) {
		// different token sequence, cannot perform a partial parse run
		return fullyReparse(parser, previousParseResult, changedRegion);
	}
	
	PartialParsingPointers parsingPointers = calculatePartialParsingPointers(oldRootNode, leftNode, rightNode);
	ICompositeNode replaceMe = getReplacedNode(parsingPointers);
	if (replaceMe == null || replaceMe == oldRootNode || replaceMe.getOffset() == 0 && replaceMe.getEndOffset() == oldRootNode.getLength()) {
		return fullyReparse(parser, previousParseResult, changedRegion);
	}
	String reparseRegion = insertChangeIntoReplaceRegion(replaceMe, changedRegion);
	
	EObject oldSemanticElement = getOldSemanticElement(replaceMe, parsingPointers);
	if (oldSemanticElement == null)
		return fullyReparse(parser, previousParseResult, changedRegion);
	if (oldSemanticElement == replaceMe.getParent().getSemanticElement()) {
		throw new IllegalStateException("oldParent == oldElement");
	}
	
	IParseResult newParseResult = doParseRegion(parser, parsingPointers, replaceMe, reparseRegion);
	if (newParseResult == null) {
		throw new IllegalStateException("Could not perform a partial parse operation");
	}
	
	replaceOldSemanticElement(oldSemanticElement, previousParseResult, newParseResult);
	nodeModelBuilder.replaceAndTransferLookAhead(replaceMe, newParseResult.getRootNode());
	((ParseResult) newParseResult).setRootNode(oldRootNode);
	StringBuilder builder = new StringBuilder(oldRootNode.getText());
	changedRegion.applyTo(builder);
	nodeModelBuilder.setCompleteContent(oldRootNode, builder.toString());
	return newParseResult;
}