Skip to content

Commit

Permalink
fix: #916 workaround not working for code actions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Feb 3, 2023
1 parent 99b16d5 commit 2a5ca1b
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions vue-language-tools/vue-language-service/src/languageService.ts
Expand Up @@ -80,8 +80,15 @@ export function getLanguageServicePlugins(
return result;
},
async resolve(item) {

item = await base.complete!.resolve!(item);

const itemData = item.data as { uri?: string; } | undefined;

if (itemData?.uri && item.additionalTextEdits) {
patchAdditionalTextEdits(itemData.uri, item.additionalTextEdits);
}

if (
item.textEdit?.newText && /\w*Vue$/.test(item.textEdit.newText)
&& item.additionalTextEdits?.length === 1 && item.additionalTextEdits[0].newText.indexOf('import ' + item.textEdit.newText + ' from ') >= 0
Expand Down Expand Up @@ -182,6 +189,28 @@ export function getLanguageServicePlugins(
const result = await base.codeAction?.on?.(document, range, context);
return result?.filter(codeAction => codeAction.title.indexOf('__VLS_') === -1);
},
async resolve(item) {

const result = await base.codeAction?.resolve?.(item);

if (result?.edit?.changes) {
for (const uri in result.edit.changes) {
const edits = result.edit.changes[uri];
if (edits) {
patchAdditionalTextEdits(uri, edits);
}
}
}
if (result?.edit?.documentChanges) {
for (const documentChange of result.edit.documentChanges) {
if (vscode.TextDocumentEdit.is(documentChange)) {
patchAdditionalTextEdits(documentChange.textDocument.uri, documentChange.edits);
}
}
}

return result;
},
}
};
};
Expand Down Expand Up @@ -231,6 +260,27 @@ export function getLanguageServicePlugins(
};
}

// fix https://github.com/johnsoncodehk/volar/issues/916
function patchAdditionalTextEdits(uri: string, edits: vscode.TextEdit[]) {
if (
uri.endsWith('.vue.js')
|| uri.endsWith('.vue.ts')
|| uri.endsWith('.vue.jsx')
|| uri.endsWith('.vue.tsx')
) {
for (const edit of edits) {
if (
edit.range.start.line === 0
&& edit.range.start.character === 0
&& edit.range.end.line === 0
&& edit.range.end.character === 0
) {
edit.newText = '\n' + edit.newText;
}
}
}
}

export function createLanguageService(
host: VueLanguageServiceHost,
config: Config, // volar.config.js
Expand Down

0 comments on commit 2a5ca1b

Please sign in to comment.