-
-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(language-service): auto insert
const props =
with props
comp…
…letion (#4942) Co-authored-by: Johnson Chu <[email protected]>
- Loading branch information
1 parent
06166e2
commit 82d27ce
Showing
6 changed files
with
153 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/language-service/lib/plugins/vue-complete-define-assignment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import type { LanguageServicePlugin, LanguageServicePluginInstance } from '@volar/language-service'; | ||
import { TextRange, tsCodegen, VueVirtualCode } from '@vue/language-core'; | ||
import type * as vscode from 'vscode-languageserver-protocol'; | ||
import { URI } from 'vscode-uri'; | ||
import { isTsDocument } from './vue-autoinsert-dotvalue'; | ||
|
||
export function create(): LanguageServicePlugin { | ||
return { | ||
name: 'vue-complete-define-assignment', | ||
capabilities: { | ||
completionProvider: {}, | ||
}, | ||
create(context): LanguageServicePluginInstance { | ||
return { | ||
isAdditionalCompletion: true, | ||
async provideCompletionItems(document) { | ||
if (!isTsDocument(document)) { | ||
return; | ||
} | ||
|
||
const enabled = await context.env.getConfiguration?.<boolean>('vue.complete.defineAssignment') ?? true; | ||
if (!enabled) { | ||
return; | ||
} | ||
|
||
const result: vscode.CompletionItem[] = []; | ||
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 (!sourceScript || !virtualCode) { | ||
return; | ||
} | ||
|
||
const root = sourceScript?.generated?.root; | ||
if (!(root instanceof VueVirtualCode)) { | ||
return; | ||
} | ||
|
||
const codegen = tsCodegen.get(root._sfc); | ||
const scriptSetup = root._sfc.scriptSetup; | ||
const scriptSetupRanges = codegen?.scriptSetupRanges.get(); | ||
if (!scriptSetup || !scriptSetupRanges) { | ||
return; | ||
} | ||
|
||
const mappings = [...context.language.maps.forEach(virtualCode)]; | ||
|
||
addDefineCompletionItem(scriptSetupRanges.props.define && { | ||
exp: scriptSetupRanges.props.withDefaults ?? scriptSetupRanges.props.define.exp, | ||
statement: scriptSetupRanges.props.define.statement | ||
}, 'props'); | ||
addDefineCompletionItem(scriptSetupRanges.emits.define, 'emit'); | ||
addDefineCompletionItem(scriptSetupRanges.slots.define, 'slots'); | ||
|
||
return { | ||
isIncomplete: false, | ||
items: result | ||
}; | ||
|
||
function addDefineCompletionItem( | ||
define: { | ||
exp: TextRange, | ||
statement: TextRange; | ||
} | undefined, | ||
name: string | ||
) { | ||
if (!define || define.exp.start !== define.statement.start) { | ||
return; | ||
} | ||
|
||
let offset; | ||
for (const [, map] of mappings) { | ||
for (const [generatedOffset] of map.toGeneratedLocation(scriptSetup!.startTagEnd + define.exp.start)) { | ||
offset = generatedOffset; | ||
break; | ||
} | ||
} | ||
if (offset === undefined) { | ||
return; | ||
} | ||
|
||
const pos = document.positionAt(offset); | ||
result.push({ | ||
label: name, | ||
kind: 6 satisfies typeof vscode.CompletionItemKind.Variable, | ||
commitCharacters: ['.', ',', ';'], | ||
additionalTextEdits: [{ | ||
newText: `const ${name} = `, | ||
range: { | ||
start: pos, | ||
end: pos | ||
} | ||
}] | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; | ||
} |