From e042a8c563d70aba69c2876b7c39dcadeb07c23e Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Sat, 20 Jul 2024 00:52:51 +0800 Subject: [PATCH] feat(language-service): remove `v-bind` code action --- packages/language-service/index.ts | 2 - .../plugins/vue-toggle-v-bind-codeaction.ts | 151 ------------------ 2 files changed, 153 deletions(-) delete mode 100644 packages/language-service/lib/plugins/vue-toggle-v-bind-codeaction.ts diff --git a/packages/language-service/index.ts b/packages/language-service/index.ts index f32460d04d..e837df7c6e 100644 --- a/packages/language-service/index.ts +++ b/packages/language-service/index.ts @@ -24,7 +24,6 @@ import { create as createVueDocumentLinksPlugin } from './lib/plugins/vue-docume import { create as createVueExtractFilePlugin } from './lib/plugins/vue-extract-file'; import { create as createVueSfcPlugin } from './lib/plugins/vue-sfc'; import { create as createVueTemplatePlugin } from './lib/plugins/vue-template'; -import { create as createVueToggleVBindPlugin } from './lib/plugins/vue-toggle-v-bind-codeaction'; import { create as createVueTwoslashQueriesPlugin } from './lib/plugins/vue-twoslash-queries'; import { create as createVueVisualizeHiddenCallbackParamPlugin } from './lib/plugins/vue-visualize-hidden-callback-param'; @@ -200,7 +199,6 @@ function getCommonLanguageServicePlugins( createVueVisualizeHiddenCallbackParamPlugin(), createVueDirectiveCommentsPlugin(), createVueExtractFilePlugin(ts, getTsPluginClient), - createVueToggleVBindPlugin(ts), createEmmetPlugin({ mappedLanguages: { 'vue': 'html', diff --git a/packages/language-service/lib/plugins/vue-toggle-v-bind-codeaction.ts b/packages/language-service/lib/plugins/vue-toggle-v-bind-codeaction.ts deleted file mode 100644 index e21100e593..0000000000 --- a/packages/language-service/lib/plugins/vue-toggle-v-bind-codeaction.ts +++ /dev/null @@ -1,151 +0,0 @@ -import type { LanguageServicePlugin, LanguageServicePluginInstance } from '@volar/language-service'; -import { VueVirtualCode, forEachElementNode, type CompilerDOM } from '@vue/language-core'; -import type * as vscode from 'vscode-languageserver-protocol'; -import { URI } from 'vscode-uri'; - -export function create(ts: typeof import('typescript')): LanguageServicePlugin { - return { - name: 'vue-toggle-v-bind-codeaction', - capabilities: { - codeActionProvider: { - codeActionKinds: ['refactor'], - }, - }, - create(context): LanguageServicePluginInstance { - return { - provideCodeActions(document, range, _context) { - - const startOffset = document.offsetAt(range.start); - const endOffset = document.offsetAt(range.end); - const decoded = context.decodeEmbeddedDocumentUri(URI.parse(document.uri)); - const sourceScript = decoded && context.language.scripts.get(decoded[0]); - const virtualCode = decoded && sourceScript?.generated?.embeddedCodes.get(decoded[1]); - if (!(virtualCode instanceof VueVirtualCode)) { - return; - } - - const { template } = virtualCode.sfc; - if (!template?.ast) { - return; - } - - const templateStartOffset = template.startTagEnd; - const result: vscode.CodeAction[] = []; - - for (const node of forEachElementNode(template.ast)) { - if (startOffset > templateStartOffset + node.loc.end.offset || endOffset < templateStartOffset + node.loc.start.offset) { - return; - } - for (const prop of node.props) { - if ( - startOffset - templateStartOffset >= prop.loc.start.offset - && endOffset - templateStartOffset <= prop.loc.end.offset - ) { - if (prop.type === 7 satisfies CompilerDOM.NodeTypes.DIRECTIVE && prop.exp) { - - const sourceFile = ts.createSourceFile('/a.ts', prop.exp.loc.source, ts.ScriptTarget.Latest, true); - const firstStatement = sourceFile.statements[0]; - - if (sourceFile.statements.length === 1 && ts.isExpressionStatement(firstStatement) && ts.isStringLiteralLike(firstStatement.expression)) { - const stringNode = sourceFile.statements[0]; - const removeTextRanges: [number, number][] = [ - [prop.loc.start.offset, prop.loc.start.offset + 1], - // Work correctly with trivias for cases like - [prop.exp.loc.start.offset, prop.exp.loc.start.offset + stringNode.pos + stringNode.getLeadingTriviaWidth() + 1], - [prop.exp.loc.start.offset + stringNode.end - 1, prop.exp.loc.end.offset], - ]; - result.push({ - title: 'Remove v-bind from attribute', - kind: 'refactor.rewrite.removeVBind', - edit: { - changes: { - [document.uri]: removeTextRanges.map(range => ({ - newText: '', - range: { - start: document.positionAt(templateStartOffset + range[0]), - end: document.positionAt(templateStartOffset + range[1]), - } - })) - }, - }, - }); - } - } - if (prop.type === 6 satisfies CompilerDOM.NodeTypes.ATTRIBUTE) { - - const edits: vscode.TextEdit[] = []; - const addVBindPos = document.positionAt(templateStartOffset + prop.loc.start.offset); - edits.push({ - newText: ':', - range: { - start: addVBindPos, - end: addVBindPos, - }, - }); - - let newPosition: vscode.Position | undefined; - - if (prop.value) { - const valueStart = document.positionAt(templateStartOffset + prop.value.loc.start.offset); - const valueEnd = document.positionAt(templateStartOffset + prop.value.loc.end.offset); - - if (prop.value.loc.end.offset - prop.value.loc.start.offset !== prop.value.content.length) { - valueStart.character++; - valueEnd.character--; - } - - edits.push({ - newText: "'", - range: { - start: valueStart, - end: valueStart, - }, - }); - edits.push({ - newText: "'", - range: { - start: valueEnd, - end: valueEnd, - }, - }); - } - else { - const addValuePos = document.positionAt(templateStartOffset + prop.loc.end.offset); - - newPosition = { - line: addValuePos.line, - character: addValuePos.character + ':'.length + '="'.length, - }; - - edits.push({ - newText: '=""', - range: { - start: addValuePos, - end: addValuePos - }, - }); - } - - result.push({ - title: 'Add v-bind to attribute', - kind: 'refactor.rewrite.addVBind', - edit: { - changes: { [document.uri]: edits }, - }, - command: newPosition ? context?.commands.setSelection.create(newPosition) : undefined, - }); - } - } - } - } - - return result; - }, - - transformCodeAction(item) { - return item; // ignore mapping - }, - }; - }, - }; -}