diff --git a/src/lsp/providers/codeActionProvider/index.ts b/src/lsp/providers/codeActionProvider/index.ts index 959411af30b8194c3b3cf7531771774e91424ead..28b51444d56fb61000f2e34b32affdfdd19e4f01 100644 --- a/src/lsp/providers/codeActionProvider/index.ts +++ b/src/lsp/providers/codeActionProvider/index.ts @@ -22,10 +22,7 @@ DiagnosticKind, isInvalidApplyDiagnostic, AugmentedDiagnostic, InvalidApplyDiagnostic, - isUtilityConflictsDiagnostic, - UtilityConflictsDiagnostic, } from '../diagnostics/types' -import { flatten, dedupeBy } from '../../../util/array' async function getDiagnosticsFromCodeActionParams( state: State, @@ -38,11 +35,7 @@ return params.context.diagnostics .map((diagnostic) => { return diagnostics.find((d) => { - return ( - d.code === diagnostic.code && - d.message === diagnostic.message && - rangesEqual(d.range, diagnostic.range) - ) + return rangesEqual(d.range, diagnostic.range) }) }) .filter(Boolean) @@ -62,46 +55,40 @@ params, codes ) - let actions = diagnostics.map((diagnostic) => { - if (isInvalidApplyDiagnostic(diagnostic)) { - return provideInvalidApplyCodeActions(state, params, diagnostic) - } + return Promise.all( + diagnostics + .map((diagnostic) => { + if (isInvalidApplyDiagnostic(diagnostic)) { + return provideInvalidApplyCodeAction(state, params, diagnostic) + } - if (isUtilityConflictsDiagnostic(diagnostic)) { - return provideUtilityConflictsCodeActions(state, params, diagnostic) - } + let match = findLast( + / Did you mean (?:something like )?'(?[^']+)'\?$/g, + diagnostic.message + ) - let match = findLast( - / Did you mean (?:something like )?'(?[^']+)'\?$/g, - diagnostic.message - ) + if (!match) { + return null + } - if (!match) { - return [] - } - - return [ - { - title: `Replace with '${match.groups.replacement}'`, - kind: CodeActionKind.QuickFix, - diagnostics: [diagnostic], - edit: { - changes: { - [params.textDocument.uri]: [ - { - range: diagnostic.range, - newText: match.groups.replacement, - }, - ], + return { + title: `Replace with '${match.groups.replacement}'`, + kind: CodeActionKind.QuickFix, + diagnostics: [diagnostic], + edit: { + changes: { + [params.textDocument.uri]: [ + { + range: diagnostic.range, + newText: match.groups.replacement, + }, + ], + }, }, - }, - }, - ] - }) - - return Promise.all(actions) - .then(flatten) - .then((x) => dedupeBy(x, (item) => JSON.stringify(item.edit))) + } + }) + .filter(Boolean) + ) } function classNameToAst( @@ -167,56 +154,11 @@ return cssObjToAst(obj, state.modules.postcss) } -async function provideUtilityConflictsCodeActions( - state: State, - params: CodeActionParams, - diagnostic: UtilityConflictsDiagnostic -): Promise { - return [ - { - title: `Delete '${diagnostic.className.className}'`, - kind: CodeActionKind.QuickFix, - diagnostics: [diagnostic], - edit: { - changes: { - [params.textDocument.uri]: [ - { - range: diagnostic.className.classList.range, - newText: removeRangeFromString( - diagnostic.className.classList.classList, - diagnostic.className.relativeRange - ), - }, - ], - }, - }, - }, - { - title: `Delete '${diagnostic.otherClassName.className}'`, - kind: CodeActionKind.QuickFix, - diagnostics: [diagnostic], - edit: { - changes: { - [params.textDocument.uri]: [ - { - range: diagnostic.className.classList.range, - newText: removeRangeFromString( - diagnostic.className.classList.classList, - diagnostic.otherClassName.relativeRange - ), - }, - ], - }, - }, - }, - ] -} - -async function provideInvalidApplyCodeActions( +async function provideInvalidApplyCodeAction( state: State, params: CodeActionParams, diagnostic: InvalidApplyDiagnostic -): Promise { +): Promise { let document = state.editor.documents.get(params.textDocument.uri) let documentText = document.getText() const { postcss } = state.modules @@ -308,32 +250,30 @@ }), ]).process(documentText, { from: undefined }) if (!change) { - return [] + return null } - return [ - { - title: 'Extract to new rule', - kind: CodeActionKind.QuickFix, - diagnostics: [diagnostic], - edit: { - changes: { - [params.textDocument.uri]: [ - ...(totalClassNamesInClassList > 1 - ? [ - { - range: diagnostic.className.classList.range, - newText: removeRangeFromString( - diagnostic.className.classList.classList, - diagnostic.className.relativeRange - ), - }, - ] - : []), - change, - ], - }, + return { + title: 'Extract to new rule.', + kind: CodeActionKind.QuickFix, + diagnostics: [diagnostic], + edit: { + changes: { + [params.textDocument.uri]: [ + ...(totalClassNamesInClassList > 1 + ? [ + { + range: diagnostic.className.classList.range, + newText: removeRangeFromString( + diagnostic.className.classList.classList, + diagnostic.className.relativeRange + ), + }, + ] + : []), + change, + ], }, }, - ] + } } diff --git a/src/util/array.ts b/src/util/array.ts index 869eb9f6cd346528cd554b115bfb374952032e48..b40dd245fe57a3f1e1ddeb24fc75343041b530db 100644 --- a/src/util/array.ts +++ b/src/util/array.ts @@ -2,16 +2,6 @@ export function dedupe(arr: Array): Array { return arr.filter((value, index, self) => self.indexOf(value) === index) } -export function dedupeBy( - arr: Array, - transform: (item: T) => any -): Array { - return arr.filter( - (value, index, self) => - self.map(transform).indexOf(transform(value)) === index - ) -} - export function ensureArray(value: T | T[]): T[] { return Array.isArray(value) ? value : [value] }