Skip to content

Commit

Permalink
Add isPreferred to single suggestions for autofixing
Browse files Browse the repository at this point in the history
Closes GH-47.

Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
Reviewed-by: Titus Wormer <tituswormer@gmail.com>
  • Loading branch information
remcohaszing committed Jul 27, 2022
1 parent 9c30eb3 commit 753fa20
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
34 changes: 18 additions & 16 deletions lib/index.js
Expand Up @@ -475,23 +475,25 @@ export function createUnifiedLanguageServer({
continue
}

codeActions.push(
CodeAction.create(
replacement
? start.line === end.line && start.character === end.character
? 'Insert `' + replacement + '`'
: 'Replace `' + actual + '` with `' + replacement + '`'
: 'Remove `' + actual + '`',
{
changes: {
[document.uri]: [
TextEdit.replace(diagnostic.range, replacement)
]
}
},
CodeActionKind.QuickFix
)
const codeAction = CodeAction.create(
replacement
? start.line === end.line && start.character === end.character
? 'Insert `' + replacement + '`'
: 'Replace `' + actual + '` with `' + replacement + '`'
: 'Remove `' + actual + '`',
{
changes: {
[document.uri]: [TextEdit.replace(diagnostic.range, replacement)]
}
},
CodeActionKind.QuickFix
)

if (expected.length === 1) {
codeAction.isPreferred = true
}

codeActions.push(codeAction)
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/code-actions.js
Expand Up @@ -24,6 +24,12 @@ function warn() {
end: {line: 1, column: 7}
}).expected = ['']

// Insert
file.message('', {
start: {line: 1, column: 1},
end: {line: 1, column: 7}
}).expected = ['alternative a', 'alternative b']

// @ts-expect-error We are deliberately testing invalid types here, because
// the expected field used to be untyped for a long time.
file.message('', {line: 1, column: 1}).expected = 'insert me'
Expand Down
41 changes: 39 additions & 2 deletions test/index.js
Expand Up @@ -469,7 +469,8 @@ test('`textDocument/codeAction` (and diagnostics)', async (t) => {
]
}
},
kind: 'quickfix'
kind: 'quickfix',
isPreferred: true
},
{
title: 'Replace `actual` with `replacement`',
Expand All @@ -486,7 +487,8 @@ test('`textDocument/codeAction` (and diagnostics)', async (t) => {
]
}
},
kind: 'quickfix'
kind: 'quickfix',
isPreferred: true
},
{
title: 'Remove `actual`',
Expand All @@ -503,6 +505,41 @@ test('`textDocument/codeAction` (and diagnostics)', async (t) => {
]
}
},
kind: 'quickfix',
isPreferred: true
},
{
title: 'Replace `actual` with `alternative a`',
edit: {
changes: {
[uri]: [
{
range: {
start: {line: 0, character: 0},
end: {line: 0, character: 6}
},
newText: 'alternative a'
}
]
}
},
kind: 'quickfix'
},
{
title: 'Replace `actual` with `alternative b`',
edit: {
changes: {
[uri]: [
{
range: {
start: {line: 0, character: 0},
end: {line: 0, character: 6}
},
newText: 'alternative b'
}
]
}
},
kind: 'quickfix'
}
],
Expand Down

0 comments on commit 753fa20

Please sign in to comment.