Skip to content

Commit fd3c8f0

Browse files
authored
(feat) lang="..." completions for script/style/template (#413)
Starting with - "ts" for script - "less", "scss" for style - "pug" for template
1 parent f5dd7d6 commit fd3c8f0

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

packages/language-server/src/plugins/html/HTMLPlugin.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { getEmmetCompletionParticipants } from 'vscode-emmet-helper';
22
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';
410
import { DocumentManager, Document, isInTag } from '../../lib/documents';
511
import { LSConfigManager, LSHTMLConfig } from '../../ls-config';
612
import { svelteHtmlDataProvider } from './dataProvider';
@@ -59,12 +65,46 @@ export class HTMLPlugin implements HoverProvider, CompletionsProvider {
5965
]);
6066
const results = this.lang.doComplete(document, position, html);
6167
return CompletionList.create(
62-
[...results.items, ...emmetResults.items],
68+
[...results.items, ...this.getLangCompletions(results.items), ...emmetResults.items],
6369
// Emmet completions change on every keystroke, so they are never complete
6470
emmetResults.items.length > 0,
6571
);
6672
}
6773

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+
68108
doTagComplete(document: Document, position: Position): string | null {
69109
if (!this.featureEnabled('tagComplete')) {
70110
return null;

0 commit comments

Comments
 (0)