Skip to content

Commit 01fbe14

Browse files
authored
feat: add runes mode indicator (#2554)
adds a runes mode indicator atop of the file via a code lens. Will show "runes mode" if the components is in that mode, "legacy mode" if not, and nothing if it's not Svelte 5 #2418
1 parent 8e7d7ef commit 01fbe14

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

packages/language-server/src/plugins/svelte/SvelteDocument.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ export class SvelteDocument {
4848
public get config() {
4949
return this.parent.configPromise;
5050
}
51+
public get isSvelte5() {
52+
return this.getSvelteVersion()[0] > 4;
53+
}
5154

5255
constructor(private parent: Document) {
5356
this.script = this.parent.scriptInfo;
@@ -70,11 +73,7 @@ export class SvelteDocument {
7073

7174
async getTranspiled(): Promise<ITranspiledSvelteDocument> {
7275
if (!this.transpiledDoc) {
73-
if (!this.svelteVersion) {
74-
const { major, minor } = getPackageInfo('svelte', this.getFilePath()).version;
75-
this.svelteVersion = [major, minor];
76-
}
77-
const [major, minor] = this.svelteVersion;
76+
const [major, minor] = this.getSvelteVersion();
7877

7978
if (major > 3 || (major === 3 && minor >= 32)) {
8079
this.transpiledDoc = await TranspiledSvelteDocument.create(
@@ -103,6 +102,14 @@ export class SvelteDocument {
103102
const svelte = importSvelte(this.getFilePath());
104103
return svelte.compile((await this.getTranspiled()).getText(), options);
105104
}
105+
106+
private getSvelteVersion() {
107+
if (!this.svelteVersion) {
108+
const { major, minor } = getPackageInfo('svelte', this.getFilePath()).version;
109+
this.svelteVersion = [major, minor];
110+
}
111+
return this.svelteVersion;
112+
}
106113
}
107114

108115
export interface ITranspiledSvelteDocument extends PositionMapper {

packages/language-server/src/plugins/svelte/SveltePlugin.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CancellationToken,
44
CodeAction,
55
CodeActionContext,
6+
CodeLens,
67
CompletionContext,
78
CompletionList,
89
Diagnostic,
@@ -49,6 +50,45 @@ export class SveltePlugin
4950

5051
constructor(private configManager: LSConfigManager) {}
5152

53+
async getCodeLens(document: Document): Promise<CodeLens[] | null> {
54+
const doc = await this.getSvelteDoc(document);
55+
if (!doc.isSvelte5) return null;
56+
57+
try {
58+
const result = await doc.getCompiled();
59+
// @ts-ignore
60+
const runes = result.metadata.runes as boolean;
61+
62+
return [
63+
{
64+
range: {
65+
start: { line: 0, character: 0 },
66+
end: { line: 0, character: 0 }
67+
},
68+
command: {
69+
title: runes ? 'Runes mode' : 'Legacy mode',
70+
command: 'svelte.openLink',
71+
arguments: ['https://svelte.dev/docs/svelte/legacy-overview']
72+
}
73+
}
74+
];
75+
} catch (e) {
76+
// show an empty code lens in case of a compilation error to prevent code from jumping around
77+
return [
78+
{
79+
range: {
80+
start: { line: 0, character: 0 },
81+
end: { line: 0, character: 0 }
82+
},
83+
command: {
84+
title: '',
85+
command: ''
86+
}
87+
}
88+
];
89+
}
90+
}
91+
5292
async getDiagnostics(
5393
document: Document,
5494
cancellationToken?: CancellationToken

packages/svelte-vscode/src/extension.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ export function activateSvelteLanguageServer(context: ExtensionContext) {
261261

262262
addMigrateToSvelte5Command(getLS, context);
263263

264+
addOpenLinkCommand(context);
265+
264266
languages.setLanguageConfiguration('svelte', {
265267
indentationRules: {
266268
// Matches a valid opening tag that is:
@@ -513,6 +515,14 @@ function addMigrateToSvelte5Command(getLS: () => LanguageClient, context: Extens
513515
);
514516
}
515517

518+
function addOpenLinkCommand(context: ExtensionContext) {
519+
context.subscriptions.push(
520+
commands.registerCommand('svelte.openLink', (url: string) => {
521+
commands.executeCommand('vscode.open', Uri.parse(url));
522+
})
523+
);
524+
}
525+
516526
function createLanguageServer(serverOptions: ServerOptions, clientOptions: LanguageClientOptions) {
517527
return new LanguageClient('svelte', 'Svelte', serverOptions, clientOptions);
518528
}

0 commit comments

Comments
 (0)