Skip to content

Commit

Permalink
Merge pull request #1454 from 1c-syntax/feature/quick-fix-is-preferred
Browse files Browse the repository at this point in the history
  • Loading branch information
nixel2007 authored Nov 16, 2020
2 parents dfa204c + 73914a9 commit 46c099a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,6 +73,9 @@ public static List<CodeAction> createCodeActions(
codeAction.setDiagnostics(diagnostics);
codeAction.setEdit(edit);
codeAction.setKind(CodeActionKind.QuickFix);
if (diagnostics.size() == 1) {
codeAction.setIsPreferred(Boolean.TRUE);
}

return Collections.singletonList(codeAction);

Expand All @@ -82,8 +86,12 @@ public List<Either<Command, CodeAction>> getCodeActions(
DocumentContext documentContext
) {

List<String> 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::<Command, CodeAction>forRight)
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
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;
import org.springframework.test.annotation.DirtiesContext;

import javax.annotation.CheckForNull;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -56,13 +58,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
Expand Down Expand Up @@ -96,15 +103,13 @@ 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()))
;
}

@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());

Expand All @@ -124,4 +129,47 @@ 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<Diagnostic> 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<Either<Command, CodeAction>> codeActions = codeActionProvider.getCodeActions(params, documentContext);

// then
assertThat(codeActions)
.extracting(Either::getRight)
.extracting(CodeAction::getKind)
.containsOnly(CodeActionKind.Refactor)
;
}

private static boolean toBoolean(@CheckForNull Boolean value) {
if (value == null) {
return false;
}
return value;
}
}

0 comments on commit 46c099a

Please sign in to comment.