Search in sources :

Example 86 with TextDocumentIdentifier

use of org.eclipse.lsp4j.TextDocumentIdentifier in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method codeAction.

/**
     * Can be used to "quick fix" an error or warning.
     */
@Override
public CompletableFuture<List<? extends Command>> codeAction(CodeActionParams params) {
    List<? extends Diagnostic> diagnostics = params.getContext().getDiagnostics();
    TextDocumentIdentifier textDocument = params.getTextDocument();
    Path path = LanguageServerUtils.getPathFromLanguageServerURI(textDocument.getUri());
    if (path == null || !sourceByPath.containsKey(path)) {
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    ArrayList<Command> commands = new ArrayList<>();
    for (Diagnostic diagnostic : diagnostics) {
        //I don't know why this can be null
        String code = diagnostic.getCode();
        if (code == null) {
            continue;
        }
        switch(code) {
            case //AccessUndefinedPropertyProblem
            "1120":
                {
                    //see if there's anything we can import
                    createCodeActionsForImport(diagnostic, commands);
                    break;
                }
            case //UnknownTypeProblem
            "1046":
                {
                    //see if there's anything we can import
                    createCodeActionsForImport(diagnostic, commands);
                    break;
                }
            case //InaccessiblePropertyReferenceProblem
            "1178":
                {
                    //see if there's anything we can import
                    createCodeActionsForImport(diagnostic, commands);
                    break;
                }
            case //CallUndefinedMethodProblem
            "1180":
                {
                    //see if there's anything we can import
                    createCodeActionsForImport(diagnostic, commands);
                    break;
                }
        }
    }
    return CompletableFuture.completedFuture(commands);
}
Also used : Path(java.nio.file.Path) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Command(org.eclipse.lsp4j.Command) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic)

Example 87 with TextDocumentIdentifier

use of org.eclipse.lsp4j.TextDocumentIdentifier in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method didClose.

/**
     * Called when a file is closed in Visual Studio Code. We should no longer
     * store the file as a String, and we can load the contents from the file
     * system.
     */
@Override
public void didClose(DidCloseTextDocumentParams params) {
    TextDocumentIdentifier 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) {
        sourceByPath.remove(path);
    }
}
Also used : Path(java.nio.file.Path) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier)

Example 88 with TextDocumentIdentifier

use of org.eclipse.lsp4j.TextDocumentIdentifier in project vscode-nextgenas by BowlerHatLLC.

the class ActionScriptTextDocumentService method documentSymbol.

/**
     * Searches by name for a symbol in a specific document (not the whole
     * workspace)
     */
@Override
public CompletableFuture<List<? extends SymbolInformation>> documentSymbol(DocumentSymbolParams params) {
    TextDocumentIdentifier textDocument = params.getTextDocument();
    Path path = LanguageServerUtils.getPathFromLanguageServerURI(textDocument.getUri());
    if (path == null) {
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    ICompilationUnit unit = getCompilationUnit(path);
    if (unit == null) {
        //we couldn't find a compilation unit with the specified path
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    IASScope[] scopes;
    try {
        scopes = unit.getFileScopeRequest().get().getScopes();
    } catch (Exception e) {
        return CompletableFuture.completedFuture(Collections.emptyList());
    }
    List<SymbolInformation> result = new ArrayList<>();
    for (IASScope scope : scopes) {
        scopeToSymbols(scope, result);
    }
    return CompletableFuture.completedFuture(result);
}
Also used : Path(java.nio.file.Path) ICompilationUnit(org.apache.flex.compiler.units.ICompilationUnit) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) IASScope(org.apache.flex.compiler.scopes.IASScope) ArrayList(java.util.ArrayList) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) ConcurrentModificationException(java.util.ConcurrentModificationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 89 with TextDocumentIdentifier

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

the class ServerMainTest method analyzeSimpleJsFileOnSave.

@Test
public void analyzeSimpleJsFileOnSave() throws Exception {
    String uri = getUri("foo.js");
    lsProxy.getTextDocumentService().didSave(new DidSaveTextDocumentParams(new TextDocumentIdentifier(uri), "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 : TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) DidSaveTextDocumentParams(org.eclipse.lsp4j.DidSaveTextDocumentParams) Test(org.junit.Test)

Example 90 with TextDocumentIdentifier

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

Aggregations

TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)172 Test (org.junit.Test)113 Position (org.eclipse.lsp4j.Position)102 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)56 Range (org.eclipse.lsp4j.Range)47 TextDocumentPositionParams (org.eclipse.lsp4j.TextDocumentPositionParams)37 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)35 URI (java.net.URI)34 AbstractProjectsManagerBasedTest (org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest)33 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)32 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)32 List (java.util.List)26 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)25 Location (org.eclipse.lsp4j.Location)23 AbstractQuickFixTest (org.eclipse.jdt.ls.core.internal.correction.AbstractQuickFixTest)22 Command (org.eclipse.lsp4j.Command)22 PrepareRenameParams (org.eclipse.lsp4j.PrepareRenameParams)20 FormattingOptions (org.eclipse.lsp4j.FormattingOptions)19 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)18 CodeAction (org.eclipse.lsp4j.CodeAction)18