Skip to content

Commit

Permalink
fix: auto import component tag name including "Vue" on import
Browse files Browse the repository at this point in the history
close #1461
  • Loading branch information
johnsoncodehk committed Jun 16, 2022
1 parent 2f6a112 commit 714fd49
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/vue-code-gen/src/generators/template.ts
Expand Up @@ -9,7 +9,7 @@ import { colletVars, walkInterpolationFragment } from '../transform';
const capabilitiesSet = {
all: { basic: true, diagnostic: true, references: true, definitions: true, rename: true, completion: true, semanticTokens: true },
noDiagnostic: { basic: true, references: true, definitions: true, rename: true, completion: true, semanticTokens: true },
diagnosticOnly: { diagnostic: true, completion: true },
diagnosticOnly: { diagnostic: true },
tagHover: { basic: true },
event: { basic: true, diagnostic: true },
tagReference: { references: true, definitions: true, rename: { in: false, out: true } },
Expand Down
30 changes: 7 additions & 23 deletions packages/vue-language-service/src/plugins/vue-template.ts
Expand Up @@ -323,15 +323,9 @@ export default function useVueTemplateLanguagePlugin<T extends ReturnType<typeof
const componentName = capitalize(camelize(item.label.replace(/\./g, '-')));
const textDoc = vueDocument.getDocument();
const compiledVue = vueDocument.file.getCompiledVue()!;
let insertText = '';
const planAResult = await planAInsertText();
if (planAResult) {
insertText = planAResult.insertText;
item.detail = planAResult.description + '\n\n' + rPath;
}
else {
insertText = planBInsertText();
item.detail = `Auto import from '${importPath}'\n\n${rPath}`;
const insert = await getInsert();
if (insert) {
item.detail = insert.description + '\n\n' + rPath;
}
if (descriptor.scriptSetup) {
const startTagEnd = compiledVue.getSourceRange(descriptor.scriptSetup.startTagEnd)?.[0].start;
Expand All @@ -341,7 +335,7 @@ export default function useVueTemplateLanguagePlugin<T extends ReturnType<typeof
item.additionalTextEdits = [
vscode.TextEdit.insert(
editPosition,
'\n' + insertText,
'\n' + insert?.insertText,
),
];
}
Expand All @@ -354,7 +348,7 @@ export default function useVueTemplateLanguagePlugin<T extends ReturnType<typeof
item.additionalTextEdits = [
vscode.TextEdit.insert(
editPosition,
'\n' + insertText,
'\n' + insert?.insertText,
),
];
const scriptRanges = parseScriptRanges(options.ts, scriptAst, !!descriptor.scriptSetup, true, true);
Expand Down Expand Up @@ -407,7 +401,7 @@ export default function useVueTemplateLanguagePlugin<T extends ReturnType<typeof
}
return item;

async function planAInsertText() {
async function getInsert() {
const embeddedScriptUri = shared.fsPathToUri(vueDocument.file.getScriptFileName());
const tsImportName = camelize(path.basename(importFile).replace(/\./g, '-'));
const [formatOptions, preferences] = await Promise.all([
Expand All @@ -430,16 +424,6 @@ export default function useVueTemplateLanguagePlugin<T extends ReturnType<typeof
}
}
}
function planBInsertText() {
const anyImport = scriptSetupImport ?? scriptImport;
let withSemicolon = true;
let quote = '"';
if (anyImport) {
withSemicolon = anyImport.text.endsWith(';');
quote = anyImport.text.includes("'") ? "'" : '"';
}
return `import ${componentName} from ${quote}${importPath}${quote}${withSemicolon ? ';' : ''}`;
}
}

async function provideHtmlData(vueDocument: VueDocument) {
Expand Down Expand Up @@ -546,7 +530,7 @@ export default function useVueTemplateLanguagePlugin<T extends ReturnType<typeof

if (enabledComponentAutoImport && (descriptor.script || descriptor.scriptSetup)) {
for (const vueDocument of options.vueDocuments.getAll()) {
let baseName = path.basename(vueDocument.uri);
let baseName = path.removeExt(path.basename(vueDocument.uri), '.vue');
if (baseName.toLowerCase() === 'index') {
baseName = path.basename(path.dirname(vueDocument.uri));
}
Expand Down

0 comments on commit 714fd49

Please sign in to comment.