Java Code Examples for org.eclipse.lsp4j.TextDocumentContentChangeEvent#getText()

The following examples show how to use org.eclipse.lsp4j.TextDocumentContentChangeEvent#getText() . 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: XDocument.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * As opposed to {@link TextEdit}[] the positions in the edits of a {@link DidChangeTextDocumentParams} refer to the
 * state after applying the preceding edits. See
 * https://microsoft.github.io/language-server-protocol/specification#textedit-1 and
 * https://github.com/microsoft/vscode/issues/23173#issuecomment-289378160 for details.
 *
 * @return a new document with an incremented version and the text document changes applied.
 * @since 2.18
 */
@Override
public XDocument applyTextDocumentChanges(Iterable<? extends TextDocumentContentChangeEvent> changes) {
	XDocument currentDocument = this;
	int newVersion = currentDocument.version + 1;
	for (TextDocumentContentChangeEvent change : changes) {
		String newContent;

		Range range = change.getRange();
		if (range == null) {
			newContent = change.getText();
		} else {
			int start = currentDocument.getOffSet(range.getStart());
			int end = currentDocument.getOffSet(range.getEnd());
			String oldContents = currentDocument.contents;
			newContent = oldContents.substring(0, start) + change.getText() + oldContents.substring(end);
		}
		currentDocument = new XDocument(newVersion, newContent, printSourceOnError, true);
	}
	return currentDocument;
}
 
Example 2
Source File: Document.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * As opposed to {@link TextEdit}[] the positions in the edits of a {@link DidChangeTextDocumentParams} refer to the
 * state after applying the preceding edits. See
 * https://microsoft.github.io/language-server-protocol/specification#textedit-1 and
 * https://github.com/microsoft/vscode/issues/23173#issuecomment-289378160 for details.
 * 
 * @return a new document with an incremented version and the text document changes applied.
 * @since 2.18
 */
public Document applyTextDocumentChanges(Iterable<? extends TextDocumentContentChangeEvent> changes) {
	Document currentDocument = this;
	Integer newVersion = null;
	if (currentDocument.version != null) {
		newVersion = Integer.valueOf(currentDocument.version.intValue() + 1);
	}
	for (TextDocumentContentChangeEvent change : changes) {
		final String newContent;
		if (change.getRange() == null) {
			newContent = change.getText();
		} else {
			int start = currentDocument.getOffSet(change.getRange().getStart());
			int end = currentDocument.getOffSet(change.getRange().getEnd());
			newContent = currentDocument.contents.substring(0, start) + change.getText()
					+ currentDocument.contents.substring(end);
		}
		currentDocument = new Document(newVersion, newContent, printSourceOnError);
	}
	return currentDocument;
}
 
Example 3
Source File: OpenFileContext.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
public void refreshOpenFile(@SuppressWarnings("unused") int version,
		Iterable<? extends TextDocumentContentChangeEvent> changes, CancelIndicator cancelIndicator) {

	if (mainResource == null) {
		throw new IllegalStateException("trying to refresh a resource that was not yet initialized: " + mainURI);
	}
	if (!mainResource.isLoaded()) {
		throw new IllegalStateException("trying to refresh a resource that is not yet loaded: " + mainURI);
	}

	// TODO GH-1774 the following is only necessary for changed files (could be moved to #updateDirtyState())
	ResourceSet resSet = getResourceSet();
	for (Resource res : new ArrayList<>(resSet.getResources())) {
		if (res != mainResource) {
			res.unload(); // TODO GH-1774 better way to do this? (unload is expensive due to re-proxyfication)
			resSet.getResources().remove(res);
		}
	}

	for (TextDocumentContentChangeEvent change : changes) {
		Range range = change.getRange();
		int start = range != null ? document.getOffSet(range.getStart()) : 0;
		int end = range != null ? document.getOffSet(range.getEnd()) : document.getContents().length();
		String replacement = change.getText();

		document = document.applyTextDocumentChanges(Collections.singletonList(change));

		mainResource.update(start, end - start, replacement);
	}

	resolveAndValidateOpenFile(cancelIndicator);
}