From f79150a34e36db566bf8c6b186797f01ebe2bf24 Mon Sep 17 00:00:00 2001 From: Nikita Gryzlov Date: Fri, 13 Nov 2020 18:06:11 +0300 Subject: [PATCH 1/2] =?UTF-8?q?#1375=20=D0=A4=D0=B8=D0=BB=D1=8C=D1=82?= =?UTF-8?q?=D1=80=20=D0=BF=D0=BE=20=D1=82=D0=B8=D0=BF=D0=B0=D0=BC=20=D0=BA?= =?UTF-8?q?=D0=BE=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 From 73914a9078a050002bab0f2e439462104df766d0 Mon Sep 17 00:00:00 2001 From: Nikita Gryzlov Date: Mon, 16 Nov 2020 10:34:52 +0300 Subject: [PATCH 2/2] =?UTF-8?q?#1434=20=D0=97=D0=B0=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20isPreferred=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=BA=D0=B2=D0=B8=D0=BA-=D1=84=D0=B8=D0=BA=D1=81?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bsl/languageserver/providers/CodeActionProvider.java | 3 +++ .../languageserver/providers/CodeActionProviderTest.java | 9 +++++++++ 2 files changed, 12 insertions(+) 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 fc9c8d91120..e4dca5e0282 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 @@ -73,6 +73,9 @@ public static List createCodeActions( codeAction.setDiagnostics(diagnostics); codeAction.setEdit(edit); codeAction.setKind(CodeActionKind.QuickFix); + if (diagnostics.size() == 1) { + codeAction.setIsPreferred(Boolean.TRUE); + } return Collections.singletonList(codeAction); 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 9e8581d60bb..36ae7ae92a9 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 @@ -42,6 +42,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.DirtiesContext; +import javax.annotation.CheckForNull; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -102,6 +103,7 @@ void testGetCodeActions() { .anyMatch(codeAction -> codeAction.getDiagnostics().contains(diagnostics.get(0))) .anyMatch(codeAction -> codeAction.getDiagnostics().contains(diagnostics.get(1))) .anyMatch(codeAction -> codeAction.getKind().equals(CodeActionKind.QuickFix)) + .allMatch(codeAction -> (codeAction.getDiagnostics().size() == 1) == toBoolean(codeAction.getIsPreferred())) ; } @@ -163,4 +165,11 @@ void testOnly() { .containsOnly(CodeActionKind.Refactor) ; } + + private static boolean toBoolean(@CheckForNull Boolean value) { + if (value == null) { + return false; + } + return value; + } } \ No newline at end of file