|
1 | 1 | import { getEmmetCompletionParticipants } from 'vscode-emmet-helper'; |
2 | 2 | import { getLanguageService, HTMLDocument } from 'vscode-html-languageservice'; |
3 | | -import { CompletionList, Hover, Position, SymbolInformation } from 'vscode-languageserver'; |
| 3 | +import { |
| 4 | + CompletionList, |
| 5 | + Hover, |
| 6 | + Position, |
| 7 | + SymbolInformation, |
| 8 | + CompletionItem, |
| 9 | +} from 'vscode-languageserver'; |
4 | 10 | import { DocumentManager, Document, isInTag } from '../../lib/documents'; |
5 | 11 | import { LSConfigManager, LSHTMLConfig } from '../../ls-config'; |
6 | 12 | import { svelteHtmlDataProvider } from './dataProvider'; |
@@ -59,12 +65,46 @@ export class HTMLPlugin implements HoverProvider, CompletionsProvider { |
59 | 65 | ]); |
60 | 66 | const results = this.lang.doComplete(document, position, html); |
61 | 67 | return CompletionList.create( |
62 | | - [...results.items, ...emmetResults.items], |
| 68 | + [...results.items, ...this.getLangCompletions(results.items), ...emmetResults.items], |
63 | 69 | // Emmet completions change on every keystroke, so they are never complete |
64 | 70 | emmetResults.items.length > 0, |
65 | 71 | ); |
66 | 72 | } |
67 | 73 |
|
| 74 | + private getLangCompletions(completions: CompletionItem[]): CompletionItem[] { |
| 75 | + const styleScriptTemplateCompletions = completions.filter((completion) => |
| 76 | + ['template', 'style', 'script'].includes(completion.label), |
| 77 | + ); |
| 78 | + const langCompletions: CompletionItem[] = []; |
| 79 | + addLangCompletion('script', ['ts']); |
| 80 | + addLangCompletion('style', ['less', 'scss']); |
| 81 | + addLangCompletion('template', ['pug']); |
| 82 | + return langCompletions; |
| 83 | + |
| 84 | + function addLangCompletion(tag: string, languages: string[]) { |
| 85 | + const existingCompletion = styleScriptTemplateCompletions.find( |
| 86 | + (completion) => completion.label === tag, |
| 87 | + ); |
| 88 | + if (!existingCompletion) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + languages.forEach((lang) => |
| 93 | + langCompletions.push({ |
| 94 | + ...existingCompletion, |
| 95 | + label: `${tag} (lang="${lang}")`, |
| 96 | + insertText: |
| 97 | + existingCompletion.insertText && |
| 98 | + `${existingCompletion.insertText} lang="${lang}"`, |
| 99 | + textEdit: existingCompletion.textEdit && { |
| 100 | + range: existingCompletion.textEdit.range, |
| 101 | + newText: `${existingCompletion.textEdit.newText} lang="${lang}"`, |
| 102 | + }, |
| 103 | + }), |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + |
68 | 108 | doTagComplete(document: Document, position: Position): string | null { |
69 | 109 | if (!this.featureEnabled('tagComplete')) { |
70 | 110 | return null; |
|
0 commit comments