Search in sources :

Example 1 with DidChangeTextDocumentParams

use of org.eclipse.lsp4j.DidChangeTextDocumentParams 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 DidChangeTextDocumentParams

use of org.eclipse.lsp4j.DidChangeTextDocumentParams in project sonarlint-core by SonarSource.

the class ServerMainTest method testCodeActionRuleDescription.

@Test
public void testCodeActionRuleDescription() 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}"))));
    List<? extends Command> codeActions = lsProxy.getTextDocumentService().codeAction(new CodeActionParams(new TextDocumentIdentifier(uri), new Range(new Position(1, 4), new Position(1, 4)), new CodeActionContext(waitForDiagnostics(uri)))).get();
    assertThat(codeActions).hasSize(1);
    String ruleKey = (String) codeActions.get(0).getArguments().get(0);
    assertThat(ruleKey).isEqualTo("javascript:S1442");
    lsProxy.getWorkspaceService().executeCommand(new ExecuteCommandParams(codeActions.get(0).getCommand(), codeActions.get(0).getArguments())).get();
    assertThat(client.ruleDescs).hasSize(1);
    assertThat(client.ruleDescs.get(0).getKey()).isEqualTo("javascript:S1442");
    assertThat(client.ruleDescs.get(0).getName()).contains("\"alert(...)\" should not be used");
    assertThat(client.ruleDescs.get(0).getHtmlDescription()).contains("can be useful for debugging during development");
    assertThat(client.ruleDescs.get(0).getType()).isEqualTo("VULNERABILITY");
    assertThat(client.ruleDescs.get(0).getSeverity()).isEqualTo("MINOR");
}
Also used : DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Position(org.eclipse.lsp4j.Position) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) Range(org.eclipse.lsp4j.Range) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) Test(org.junit.Test)

Example 3 with DidChangeTextDocumentParams

use of org.eclipse.lsp4j.DidChangeTextDocumentParams in project eclipse.jdt.ls by eclipse.

the class CompletionHandlerTest method changeDocument.

private void changeDocument(ICompilationUnit unit, String content, int version) throws JavaModelException {
    DidChangeTextDocumentParams changeParms = new DidChangeTextDocumentParams();
    VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier();
    textDocument.setUri(JDTUtils.toURI(unit));
    textDocument.setVersion(version);
    changeParms.setTextDocument(textDocument);
    TextDocumentContentChangeEvent event = new TextDocumentContentChangeEvent();
    event.setText(content);
    List<TextDocumentContentChangeEvent> contentChanges = new ArrayList<>();
    contentChanges.add(event);
    changeParms.setContentChanges(contentChanges);
    lifeCycleHandler.didChange(changeParms);
}
Also used : DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) ArrayList(java.util.ArrayList) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent)

Example 4 with DidChangeTextDocumentParams

use of org.eclipse.lsp4j.DidChangeTextDocumentParams in project eclipse.jdt.ls by eclipse.

the class DocumentLifeCycleHandlerTest method changeDocument.

private void changeDocument(ICompilationUnit cu, String content, int version, Range range, int length) throws JavaModelException {
    DidChangeTextDocumentParams changeParms = new DidChangeTextDocumentParams();
    VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier();
    textDocument.setUri(JDTUtils.toURI(cu));
    textDocument.setVersion(version);
    changeParms.setTextDocument(textDocument);
    TextDocumentContentChangeEvent event = new TextDocumentContentChangeEvent();
    if (range != null) {
        event.setRange(range);
        event.setRangeLength(length);
    }
    event.setText(content);
    List<TextDocumentContentChangeEvent> contentChanges = new ArrayList<>();
    contentChanges.add(event);
    changeParms.setContentChanges(contentChanges);
    lifeCycleHandler.didChange(changeParms);
}
Also used : DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) ArrayList(java.util.ArrayList) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent)

Example 5 with DidChangeTextDocumentParams

use of org.eclipse.lsp4j.DidChangeTextDocumentParams in project sts4 by spring-projects.

the class LanguageServerHarness method changeDocument.

public synchronized TextDocumentInfo changeDocument(String uri, int start, int end, String replaceText) {
    TextDocumentInfo oldDoc = documents.get(uri);
    String oldContent = oldDoc.getText();
    String newContent = oldContent.substring(0, start) + replaceText + oldContent.substring(end);
    TextDocumentItem textDocument = setDocumentContent(uri, newContent);
    DidChangeTextDocumentParams didChange = new DidChangeTextDocumentParams();
    VersionedTextDocumentIdentifier version = new VersionedTextDocumentIdentifier();
    version.setUri(uri);
    version.setVersion(textDocument.getVersion());
    didChange.setTextDocument(version);
    switch(getDocumentSyncMode()) {
        case None:
            // nothing todo
            break;
        case Incremental:
            {
                TextDocumentContentChangeEvent change = new TextDocumentContentChangeEvent();
                change.setRange(new Range(oldDoc.toPosition(start), oldDoc.toPosition(end)));
                change.setRangeLength(end - start);
                change.setText(replaceText);
                didChange.setContentChanges(Collections.singletonList(change));
                break;
            }
        case Full:
            {
                TextDocumentContentChangeEvent change = new TextDocumentContentChangeEvent();
                change.setText(newContent);
                didChange.setContentChanges(Collections.singletonList(change));
                break;
            }
        default:
            throw new IllegalStateException("Unkown SYNC mode: " + getDocumentSyncMode());
    }
    if (getServer() != null) {
        getServer().getTextDocumentService().didChange(didChange);
    }
    return documents.get(uri);
}
Also used : DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) TextDocumentItem(org.eclipse.lsp4j.TextDocumentItem) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) Range(org.eclipse.lsp4j.Range) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent)

Aggregations

DidChangeTextDocumentParams (org.eclipse.lsp4j.DidChangeTextDocumentParams)7 TextDocumentContentChangeEvent (org.eclipse.lsp4j.TextDocumentContentChangeEvent)7 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)7 Range (org.eclipse.lsp4j.Range)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 Position (org.eclipse.lsp4j.Position)2 TextDocumentItem (org.eclipse.lsp4j.TextDocumentItem)2 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)1 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)1 ExecuteCommandParams (org.eclipse.lsp4j.ExecuteCommandParams)1 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)1