use of org.eclipse.lsp4j.TextDocumentContentChangeEvent in project sonarlint-core by SonarSource.
the class ServerMainTest method analyzeSimpleJsFileOnChange.
@Test
public void analyzeSimpleJsFileOnChange() throws Exception {
String uri = getUri("foo.js");
VersionedTextDocumentIdentifier docId = new VersionedTextDocumentIdentifier(1);
docId.setUri(uri);
lsProxy.getTextDocumentService().didChange(new DidChangeTextDocumentParams(docId, Collections.singletonList(new TextDocumentContentChangeEvent("function foo() {\n alert('toto');\n}"))));
assertThat(waitForDiagnostics(uri)).extracting("range.start.line", "range.start.character", "range.end.line", "range.end.character", "code", "source", "message", "severity").containsExactly(tuple(1, 2, 1, 15, "javascript:S1442", "sonarlint", "Remove this usage of alert(...). (javascript:S1442)", DiagnosticSeverity.Information));
}
use of org.eclipse.lsp4j.TextDocumentContentChangeEvent in project eclipse.jdt.ls by eclipse.
the class DocumentLifeCycleHandler method handleChanged.
public void handleChanged(DidChangeTextDocumentParams params) {
ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
if (unit == null || !unit.isWorkingCopy() || params.getContentChanges().isEmpty()) {
return;
}
try {
if (unit.equals(sharedASTProvider.getActiveJavaElement())) {
sharedASTProvider.disposeAST();
}
List<TextDocumentContentChangeEvent> contentChanges = params.getContentChanges();
for (TextDocumentContentChangeEvent changeEvent : contentChanges) {
Range range = changeEvent.getRange();
int length;
if (range != null) {
length = changeEvent.getRangeLength().intValue();
} else {
// range is optional and if not given, the whole file content is replaced
length = unit.getSource().length();
range = JDTUtils.toRange(unit, 0, length);
}
int startOffset = JsonRpcHelpers.toOffset(unit.getBuffer(), range.getStart().getLine(), range.getStart().getCharacter());
TextEdit edit = null;
String text = changeEvent.getText();
if (length == 0) {
edit = new InsertEdit(startOffset, text);
} else if (text.isEmpty()) {
edit = new DeleteEdit(startOffset, length);
} else {
edit = new ReplaceEdit(startOffset, length, text);
}
IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
edit.apply(document, TextEdit.NONE);
}
triggerValidation(unit);
} catch (JavaModelException | MalformedTreeException | BadLocationException e) {
JavaLanguageServerPlugin.logException("Error while handling document change", e);
}
}
use of org.eclipse.lsp4j.TextDocumentContentChangeEvent in project freemarker-languageserver by angelozerr.
the class TextDocuments method onDidChangeTextDocument.
public void onDidChangeTextDocument(DidChangeTextDocumentParams params) {
List<TextDocumentContentChangeEvent> changes = params.getContentChanges();
// like vscode does, get the last changes
// see
// https://github.com/Microsoft/vscode-languageserver-node/blob/master/server/src/main.ts
TextDocumentContentChangeEvent last = changes.size() > 0 ? changes.get(changes.size() - 1) : null;
if (last != null) {
TextDocumentItem document = get(params.getTextDocument().getUri());
if (document != null) {
document.setVersion(params.getTextDocument().getVersion());
document.setText(last.getText());
}
}
}
use of org.eclipse.lsp4j.TextDocumentContentChangeEvent in project xtext-core by eclipse.
the class Document method applyTextDocumentChanges.
/**
* 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;
}
use of org.eclipse.lsp4j.TextDocumentContentChangeEvent in project vscode-nextgenas by BowlerHatLLC.
the class ActionScriptTextDocumentService method didChange.
/**
* Called when a change is made to a file open for editing in Visual Studio
* Code. Receives incremental changes that need to be applied to the
* in-memory String that we store for this file.
*/
@Override
public void didChange(DidChangeTextDocumentParams params) {
VersionedTextDocumentIdentifier textDocument = params.getTextDocument();
String textDocumentUri = textDocument.getUri();
if (!textDocumentUri.endsWith(AS_EXTENSION) && !textDocumentUri.endsWith(MXML_EXTENSION)) {
return;
}
Path path = LanguageServerUtils.getPathFromLanguageServerURI(textDocumentUri);
if (path != null) {
for (TextDocumentContentChangeEvent change : params.getContentChanges()) {
if (change.getRange() == null) {
sourceByPath.put(path, change.getText());
} else {
String existingText = sourceByPath.get(path);
String newText = patch(existingText, change);
sourceByPath.put(path, newText);
}
}
if (currentWorkspace != null) {
IFileSpecification fileSpec = fileSpecGetter.getFileSpecification(path.toAbsolutePath().toString());
currentWorkspace.fileChanged(fileSpec);
}
//we do a quick check of the current file on change for better
//performance while typing. we'll do a full check when we save the
//file later
checkFilePathForProblems(path, true);
}
}
Aggregations