Search in sources :

Example 1 with TextDocumentContentChangeEvent

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));
}
Also used : DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) Test(org.junit.Test)

Example 2 with TextDocumentContentChangeEvent

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);
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) InsertEdit(org.eclipse.text.edits.InsertEdit) JavaModelException(org.eclipse.jdt.core.JavaModelException) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Range(org.eclipse.lsp4j.Range) DeleteEdit(org.eclipse.text.edits.DeleteEdit) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 3 with TextDocumentContentChangeEvent

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());
        }
    }
}
Also used : TextDocumentItem(org.eclipse.lsp4j.TextDocumentItem) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent)

Example 4 with TextDocumentContentChangeEvent

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;
}
Also used : TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent)

Example 5 with TextDocumentContentChangeEvent

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);
    }
}
Also used : Path(java.nio.file.Path) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) IFileSpecification(org.apache.flex.compiler.filespecs.IFileSpecification) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent)

Aggregations

TextDocumentContentChangeEvent (org.eclipse.lsp4j.TextDocumentContentChangeEvent)15 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)9 DidChangeTextDocumentParams (org.eclipse.lsp4j.DidChangeTextDocumentParams)7 Range (org.eclipse.lsp4j.Range)6 TextDocumentItem (org.eclipse.lsp4j.TextDocumentItem)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 Position (org.eclipse.lsp4j.Position)2 TextDocument (org.springframework.ide.vscode.commons.util.text.TextDocument)2 Path (java.nio.file.Path)1 List (java.util.List)1 IFileSpecification (org.apache.flex.compiler.filespecs.IFileSpecification)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)1 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)1 CompletionList (org.eclipse.lsp4j.CompletionList)1 ExecuteCommandParams (org.eclipse.lsp4j.ExecuteCommandParams)1