Java Code Examples for org.eclipse.xtext.ide.server.Document#getContents()

The following examples show how to use org.eclipse.xtext.ide.server.Document#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: WorkspaceManagerTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testDoRead() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("type Test {");
  _builder.newLine();
  _builder.append("    ");
  _builder.append("string foo");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final URI path = this.operator_mappedTo("MyType1.testlang", _builder);
  this.workspaceManger.doBuild(Collections.<URI>unmodifiableList(CollectionLiterals.<URI>newArrayList(path)), CollectionLiterals.<URI>emptyList(), null);
  StringConcatenation _builder_1 = new StringConcatenation();
  _builder_1.append("type Test {");
  _builder_1.newLine();
  _builder_1.append("    ");
  _builder_1.append("Test foo");
  _builder_1.newLine();
  _builder_1.append("}");
  _builder_1.newLine();
  final String inMemContents = _builder_1.toString();
  this.workspaceManger.didOpen(path, Integer.valueOf(1), inMemContents).build(null);
  final Function2<Document, XtextResource, String> _function = (Document $0, XtextResource $1) -> {
    return $0.getContents();
  };
  Assert.assertEquals(inMemContents, this.workspaceManger.<String>doRead(path, _function));
}
 
Example 2
Source File: SemanticChangeProvider.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns IChange to remove annotation from element. Only removes exisiting annotations.
 *
 * @param document
 *            Document to modify
 * @param element
 *            Element to modify
 * @param annotation
 *            Annotation to remove
 */
TextEdit removeAnnotation(Document document, AnnotableElement element, String annotation) {
	AnnotableElement annotatedElement = element;
	Collection<Annotation> sourceList = null;
	Annotation targetAnnotation = null;

	// Search for given annotation in the elements annotation list as well as
	// in the containing export declaration as far as it exists.
	targetAnnotation = getAnnotationWithName(annotatedElement, annotation);
	if (targetAnnotation != null) {
		sourceList = annotatedElement.getAnnotations();

	} else if (element.eContainer() instanceof ExportDeclaration) {

		ExportDeclaration expDecl = (ExportDeclaration) element.eContainer();
		targetAnnotation = getAnnotationWithName(expDecl, annotation);
		if (targetAnnotation != null) {
			sourceList = expDecl.getAnnotations();
		}
	}

	if (sourceList == null || targetAnnotation == null) {
		return null;
	}

	ICompositeNode node = NodeModelUtils.findActualNodeFor(targetAnnotation);
	int offset = node.getOffset();
	int length = node.getLength();

	// Workaround the fact that the offset of the first annotation starts
	// after the '@' symbol. This is grammar caused. (The annotation can be first in the annotations list of the
	// export declaration or of the element itself)
	if (targetAnnotation == IterableExtensions.head(sourceList)) {
		var containerNode = NodeModelUtils.findActualNodeFor(targetAnnotation.eContainer());
		offset = containerNode.getOffset();
		length = node.getOffset() + node.getLength() - containerNode.getOffset();
	}

	// If the annotation is in the same line as the following element modifiers
	// also remove the additional whitespace.
	// Also consider chained one line annotations without separator
	if (element instanceof ModifiableElement) {
		ModifiableElement mElement = (ModifiableElement) element;
		Position offsetPosition = document.getPosition(offset);
		Position mOffsetPosition = document.getPosition(modifierOffset(mElement));

		if (offsetPosition.getLine() == mOffsetPosition.getLine()) {
			// Only delete trailing white space if there is white space in front of the annotation
			// Also check if there even is a trailing white space.

			String contents = document.getContents();
			char trailingChar = contents.charAt(offset + length);
			char leadingChar = contents.charAt(offset - 1);
			if (Character.isWhitespace(leadingChar) && Character.isWhitespace(trailingChar)) {
				length++;
			}
		}
	}

	return ChangeProvider.removeText(document, offset, length, true);
}