From f79150a34e36db566bf8c6b186797f01ebe2bf24 Mon Sep 17 00:00:00 2001 From: Nikita Gryzlov Date: Fri, 13 Nov 2020 18:06:11 +0300 Subject: [PATCH] =?UTF-8?q?#1375=20=D0=A4=D0=B8=D0=BB=D1=8C=D1=82=D1=80=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D1=82=D0=B8=D0=BF=D0=B0=D0=BC=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=B4=20=D1=8D=D0=BA=D1=88=D0=B5=D0=BD=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../providers/CodeActionProvider.java | 5 ++ .../providers/CodeActionProviderTest.java | 51 ++++++++++++++++--- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/providers/CodeActionProvider.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/providers/CodeActionProvider.java index 441e4690ebc..fc9c8d91120 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/languageserver/providers/CodeActionProvider.java +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/providers/CodeActionProvider.java @@ -39,6 +39,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; @Component @@ -82,8 +83,12 @@ public List> getCodeActions( DocumentContext documentContext ) { + List only = Optional.ofNullable(params.getContext().getOnly()) + .orElse(Collections.emptyList()); + return codeActionSuppliers.stream() .flatMap(codeActionSupplier -> codeActionSupplier.getCodeActions(params, documentContext).stream()) + .filter(codeAction -> only.isEmpty() || only.contains(codeAction.getKind())) .map(Either::forRight) .collect(Collectors.toList()); } diff --git a/src/test/java/com/github/_1c_syntax/bsl/languageserver/providers/CodeActionProviderTest.java b/src/test/java/com/github/_1c_syntax/bsl/languageserver/providers/CodeActionProviderTest.java index ea659abfc26..9e8581d60bb 100644 --- a/src/test/java/com/github/_1c_syntax/bsl/languageserver/providers/CodeActionProviderTest.java +++ b/src/test/java/com/github/_1c_syntax/bsl/languageserver/providers/CodeActionProviderTest.java @@ -36,6 +36,7 @@ import org.eclipse.lsp4j.Range; import org.eclipse.lsp4j.TextDocumentIdentifier; import org.eclipse.lsp4j.jsonrpc.messages.Either; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -56,13 +57,18 @@ class CodeActionProviderTest { @Autowired private LanguageServerConfiguration configuration; + private DocumentContext documentContext; + + @BeforeEach + void init() { + String filePath = "./src/test/resources/providers/codeAction.bsl"; + documentContext = TestUtils.getDocumentContextFromFile(filePath); + } + @Test void testGetCodeActions() { // given - String filePath = "./src/test/resources/providers/codeAction.bsl"; - DocumentContext documentContext = TestUtils.getDocumentContextFromFile(filePath); - DiagnosticInfo diagnosticInfo = new DiagnosticInfo( CanonicalSpellingKeywordsDiagnostic.class, configuration @@ -102,9 +108,6 @@ void testGetCodeActions() { @Test void testEmptyDiagnosticList() { // given - String filePath = "./src/test/resources/providers/codeAction.bsl"; - DocumentContext documentContext = TestUtils.getDocumentContextFromFile(filePath); - CodeActionParams params = new CodeActionParams(); TextDocumentIdentifier textDocumentIdentifier = new TextDocumentIdentifier(documentContext.getUri().toString()); @@ -124,4 +127,40 @@ void testEmptyDiagnosticList() { .filteredOn(codeAction -> codeAction.getRight().getKind().equals(CodeActionKind.QuickFix)) .isEmpty(); } + + @Test + void testOnly() { + // given + CodeActionParams params = new CodeActionParams(); + TextDocumentIdentifier textDocumentIdentifier = new TextDocumentIdentifier(documentContext.getUri().toString()); + + DiagnosticInfo diagnosticInfo = new DiagnosticInfo( + CanonicalSpellingKeywordsDiagnostic.class, + configuration + ); + DiagnosticCode diagnosticCode = diagnosticInfo.getCode(); + + List diagnostics = documentContext.getDiagnostics().stream() + .filter(diagnostic -> diagnostic.getCode().equals(diagnosticCode)) + .collect(Collectors.toList()); + + CodeActionContext codeActionContext = new CodeActionContext(); + + codeActionContext.setOnly(List.of(CodeActionKind.Refactor)); + codeActionContext.setDiagnostics(diagnostics); + + params.setRange(new Range()); + params.setTextDocument(textDocumentIdentifier); + params.setContext(codeActionContext); + + // when + List> codeActions = codeActionProvider.getCodeActions(params, documentContext); + + // then + assertThat(codeActions) + .extracting(Either::getRight) + .extracting(CodeAction::getKind) + .containsOnly(CodeActionKind.Refactor) + ; + } } \ No newline at end of file