Skip to content

Commit

Permalink
Show "Use ..." quick fixes before others
Browse files Browse the repository at this point in the history
  • Loading branch information
valentjn committed Apr 22, 2021
1 parent 2f99550 commit 5b76ff2
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 158 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

## 12.1.0 (upcoming)

- Show `Use ...` quick fixes before other quick fixes (fixes [vscode-ltex#297](https://github.com/valentjn/vscode-ltex/issues/297))
- Limit number of `Use ...` quick fixes to 5 (see [vscode-ltex#297](https://github.com/valentjn/vscode-ltex/issues/297))

## 12.0.0 (April 18, 2021)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,13 @@ public List<Either<Command, CodeAction>> generate(
List<Either<Command, CodeAction>> result =
new ArrayList<Either<Command, CodeAction>>();

Map<String, List<LanguageToolRuleMatch>> acceptSuggestionsMatchesMap = new LinkedHashMap<>();
List<LanguageToolRuleMatch> addToDictionaryMatches = new ArrayList<>();
List<LanguageToolRuleMatch> hideFalsePositivesMatches = new ArrayList<>();
List<LanguageToolRuleMatch> disableRulesMatches = new ArrayList<>();
Map<String, List<LanguageToolRuleMatch>> acceptSuggestionsMatchesMap = new LinkedHashMap<>();

for (LanguageToolRuleMatch match : checkingResult.getKey()) {
if (match.isIntersectingWithRange(params.getRange(), document)) {
if (match.isUnknownWordRule()) addToDictionaryMatches.add(match);
if (match.getSentence() != null) hideFalsePositivesMatches.add(match);
disableRulesMatches.add(match);

for (String newWord : match.getSuggestedReplacements()) {
if (!acceptSuggestionsMatchesMap.containsKey(newWord)) {
if (acceptSuggestionsMatchesMap.size() >= maximumNumberOfAcceptSuggestionsCodeActions) {
Expand All @@ -137,9 +133,19 @@ public List<Either<Command, CodeAction>> generate(

acceptSuggestionsMatchesMap.get(newWord).add(match);
}

if (match.isUnknownWordRule()) addToDictionaryMatches.add(match);
if (match.getSentence() != null) hideFalsePositivesMatches.add(match);
disableRulesMatches.add(match);
}
}

for (Map.Entry<String, List<LanguageToolRuleMatch>> entry
: acceptSuggestionsMatchesMap.entrySet()) {
result.add(Either.forRight(getAcceptSuggestionsCodeAction(
document, entry.getKey(), entry.getValue())));
}

if (!addToDictionaryMatches.isEmpty()
&& this.settingsManager.getSettings().getLanguageToolHttpServerUri().isEmpty()) {
result.add(Either.forRight(getAddWordToDictionaryCodeAction(document,
Expand All @@ -156,13 +162,32 @@ public List<Either<Command, CodeAction>> generate(
disableRulesMatches, annotatedTextFragments)));
}

for (Map.Entry<String, List<LanguageToolRuleMatch>> entry
: acceptSuggestionsMatchesMap.entrySet()) {
result.add(Either.forRight(getAcceptSuggestionsCodeAction(
document, entry.getKey(), entry.getValue())));
return result;
}

private CodeAction getAcceptSuggestionsCodeAction(LtexTextDocumentItem document, String newWord,
List<LanguageToolRuleMatch> acceptSuggestionsMatches) {
VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier(
document.getUri(), document.getVersion());
List<Diagnostic> diagnostics = new ArrayList<>();
List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = new ArrayList<>();

for (LanguageToolRuleMatch match : acceptSuggestionsMatches) {
Diagnostic diagnostic = createDiagnostic(match, document);
Range range = diagnostic.getRange();

diagnostics.add(diagnostic);
documentChanges.add(Either.forLeft(new TextDocumentEdit(textDocument,
Collections.singletonList(new TextEdit(range, newWord)))));
}

return result;
CodeAction codeAction = new CodeAction((acceptSuggestionsMatches.size() == 1)
? Tools.i18n("useWord", newWord) : Tools.i18n("useWordAllSelectedMatches", newWord));
codeAction.setKind(acceptSuggestionsCodeActionKind);
codeAction.setDiagnostics(diagnostics);
codeAction.setEdit(new WorkspaceEdit(documentChanges));

return codeAction;
}

private CodeAction getAddWordToDictionaryCodeAction(
Expand Down Expand Up @@ -335,31 +360,6 @@ private CodeAction getDisableRulesCodeAction(
return codeAction;
}

private CodeAction getAcceptSuggestionsCodeAction(LtexTextDocumentItem document, String newWord,
List<LanguageToolRuleMatch> acceptSuggestionsMatches) {
VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier(
document.getUri(), document.getVersion());
List<Diagnostic> diagnostics = new ArrayList<>();
List<Either<TextDocumentEdit, ResourceOperation>> documentChanges = new ArrayList<>();

for (LanguageToolRuleMatch match : acceptSuggestionsMatches) {
Diagnostic diagnostic = createDiagnostic(match, document);
Range range = diagnostic.getRange();

diagnostics.add(diagnostic);
documentChanges.add(Either.forLeft(new TextDocumentEdit(textDocument,
Collections.singletonList(new TextEdit(range, newWord)))));
}

CodeAction codeAction = new CodeAction((acceptSuggestionsMatches.size() == 1)
? Tools.i18n("useWord", newWord) : Tools.i18n("useWordAllSelectedMatches", newWord));
codeAction.setKind(acceptSuggestionsCodeActionKind);
codeAction.setDiagnostics(diagnostics);
codeAction.setEdit(new WorkspaceEdit(documentChanges));

return codeAction;
}

public static List<String> getCodeActions() {
return Collections.singletonList(acceptSuggestionsCodeActionKind);
}
Expand Down
Loading

0 comments on commit 5b76ff2

Please sign in to comment.