Search in sources :

Example 1 with QuickFixProvider

use of com.github._1c_syntax.bsl.languageserver.diagnostics.QuickFixProvider in project bsl-language-server by 1c-syntax.

the class QuickFixSupplierTest method testGetQuickFixClass.

@Test
void testGetQuickFixClass() {
    Optional<Class<? extends QuickFixProvider>> quickFixClass = quickFixSupplier.getQuickFixClass(new DiagnosticCode("NON_EXISTING"));
    assertThat(quickFixClass).isEmpty();
    quickFixClass = quickFixSupplier.getQuickFixClass(new DiagnosticCode("CommitTransactionOutsideTryCatch"));
    assertThat(quickFixClass).isEmpty();
    quickFixClass = quickFixSupplier.getQuickFixClass(new DiagnosticCode("CommentedCode"));
    assertThat(quickFixClass).hasValue(CommentedCodeDiagnostic.class);
}
Also used : QuickFixProvider(com.github._1c_syntax.bsl.languageserver.diagnostics.QuickFixProvider) DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with QuickFixProvider

use of com.github._1c_syntax.bsl.languageserver.diagnostics.QuickFixProvider in project bsl-language-server by 1c-syntax.

the class QuickFixCodeActionSupplier method getCodeActionsByDiagnostic.

private List<CodeAction> getCodeActionsByDiagnostic(Diagnostic diagnostic, CodeActionParams params, DocumentContext documentContext) {
    Optional<Class<? extends QuickFixProvider>> quickFixClass = quickFixSupplier.getQuickFixClass(diagnostic.getCode());
    if (quickFixClass.isEmpty()) {
        return Collections.emptyList();
    }
    Class<? extends QuickFixProvider> quickFixProviderClass = quickFixClass.get();
    QuickFixProvider quickFixInstance = quickFixSupplier.getQuickFixInstance(quickFixProviderClass);
    return quickFixInstance.getQuickFixes(Collections.singletonList(diagnostic), params, documentContext);
}
Also used : QuickFixProvider(com.github._1c_syntax.bsl.languageserver.diagnostics.QuickFixProvider)

Example 3 with QuickFixProvider

use of com.github._1c_syntax.bsl.languageserver.diagnostics.QuickFixProvider in project bsl-language-server by 1c-syntax.

the class FixAllCodeActionSupplier method getFixAllCodeAction.

private List<CodeAction> getFixAllCodeAction(Either<String, Integer> diagnosticCode, CodeActionParams params, DocumentContext documentContext) {
    Optional<Class<? extends QuickFixProvider>> quickFixClass = quickFixSupplier.getQuickFixClass(diagnosticCode);
    if (quickFixClass.isEmpty()) {
        return Collections.emptyList();
    }
    List<Diagnostic> suitableDiagnostics = documentContext.getComputedDiagnostics().stream().filter(diagnostic -> diagnosticCode.equals(diagnostic.getCode())).collect(Collectors.toList());
    // if incomingDiagnostics list has size = 1 - it will be displayed as regular quick fix
    if (suitableDiagnostics.size() < ADD_FIX_ALL_DIAGNOSTICS_THRESHOLD) {
        return Collections.emptyList();
    }
    CodeActionContext fixAllContext = new CodeActionContext();
    fixAllContext.setDiagnostics(suitableDiagnostics);
    fixAllContext.setOnly(Collections.singletonList(CodeActionKind.QuickFix));
    CodeActionParams fixAllParams = new CodeActionParams();
    fixAllParams.setTextDocument(params.getTextDocument());
    fixAllParams.setRange(params.getRange());
    fixAllParams.setContext(fixAllContext);
    Class<? extends QuickFixProvider> quickFixProviderClass = quickFixClass.get();
    QuickFixProvider quickFixInstance = quickFixSupplier.getQuickFixInstance(quickFixProviderClass);
    return quickFixInstance.getQuickFixes(suitableDiagnostics, fixAllParams, documentContext);
}
Also used : CodeAction(org.eclipse.lsp4j.CodeAction) QuickFixProvider(com.github._1c_syntax.bsl.languageserver.diagnostics.QuickFixProvider) Diagnostic(org.eclipse.lsp4j.Diagnostic) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Collectors(java.util.stream.Collectors) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) Component(org.springframework.stereotype.Component) List(java.util.List) Stream(java.util.stream.Stream) CodeActionKind(org.eclipse.lsp4j.CodeActionKind) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) Optional(java.util.Optional) Collections(java.util.Collections) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) QuickFixProvider(com.github._1c_syntax.bsl.languageserver.diagnostics.QuickFixProvider) Diagnostic(org.eclipse.lsp4j.Diagnostic)

Aggregations

QuickFixProvider (com.github._1c_syntax.bsl.languageserver.diagnostics.QuickFixProvider)3 DocumentContext (com.github._1c_syntax.bsl.languageserver.context.DocumentContext)1 DiagnosticCode (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 CodeAction (org.eclipse.lsp4j.CodeAction)1 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)1 CodeActionKind (org.eclipse.lsp4j.CodeActionKind)1 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)1 Diagnostic (org.eclipse.lsp4j.Diagnostic)1 Either (org.eclipse.lsp4j.jsonrpc.messages.Either)1 Test (org.junit.jupiter.api.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 Component (org.springframework.stereotype.Component)1