Skip to content

Commit

Permalink
fix: decode fileId
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Feb 13, 2024
1 parent aa1198f commit 9d26fcd
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 24 deletions.
5 changes: 4 additions & 1 deletion packages/language-server/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@volar/language-core';
import type ts from 'typescript';
import type { HTMLDocument } from 'vscode-html-languageservice';
import { URI } from 'vscode-uri';
import { type AstroInstall, getLanguageServerTypesDir } from '../utils.js';
import { astro2tsx } from './astro2tsx';
import { AstroMetadata, getAstroMetadata } from './parseAstro';
Expand All @@ -23,7 +24,9 @@ export function getLanguageModule(
return {
createVirtualCode(fileId, languageId, snapshot) {
if (languageId === 'astro') {
const fileName = fileId.includes('://') ? fileId.split('://')[1] : fileId;
const fileName = fileId.includes('://')
? URI.parse(fileId).fsPath.replace(/\\/g, '/')
: fileId;
return new AstroVirtualCode(fileName, snapshot);
}
},
Expand Down
5 changes: 4 additions & 1 deletion packages/language-server/src/core/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import {
forEachEmbeddedCode,
} from '@volar/language-core';
import type ts from 'typescript';
import { URI } from 'vscode-uri';
import { framework2tsx } from './utils.js';

export function getSvelteLanguageModule(): LanguagePlugin<SvelteVirtualCode> {
return {
createVirtualCode(fileId, languageId, snapshot) {
if (languageId === 'svelte') {
const fileName = fileId.includes('://') ? fileId.split('://')[1] : fileId;
const fileName = fileId.includes('://')
? URI.parse(fileId).fsPath.replace(/\\/g, '/')
: fileId;
return new SvelteVirtualCode(fileName, snapshot);
}
},
Expand Down
5 changes: 4 additions & 1 deletion packages/language-server/src/core/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import {
forEachEmbeddedCode,
} from '@volar/language-core';
import type ts from 'typescript';
import { URI } from 'vscode-uri';
import { framework2tsx } from './utils.js';

export function getVueLanguageModule(): LanguagePlugin<VueVirtualCode> {
return {
createVirtualCode(fileId, languageId, snapshot) {
if (languageId === 'vue') {
const fileName = fileId.includes('://') ? fileId.split('://')[1] : fileId;
const fileName = fileId.includes('://')
? URI.parse(fileId).fsPath.replace(/\\/g, '/')
: fileId;
return new VueVirtualCode(fileName, snapshot);
}
},
Expand Down
11 changes: 3 additions & 8 deletions packages/ts-plugin/src/astro2tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,16 @@ function safeConvertToTSX(content: string, options: ConvertToTSXOptions) {
}
}

export function astro2tsx(input: string, fileName: string, ts: typeof import('typescript')) {
export function astro2tsx(input: string, fileName: string) {
const tsx = safeConvertToTSX(input, { filename: fileName });

return {
virtualFile: getVirtualFileTSX(input, tsx, fileName, ts),
virtualFile: getVirtualFileTSX(input, tsx, fileName),
diagnostics: tsx.diagnostics,
};
}

function getVirtualFileTSX(
input: string,
tsx: TSXResult,
fileName: string,
ts: typeof import('typescript')
): VirtualCode {
function getVirtualFileTSX(input: string, tsx: TSXResult, fileName: string): VirtualCode {
tsx.code = patchTSX(tsx.code, fileName);
const v3Mappings = decode(tsx.map.mappings);
const sourcedDoc = TextDocument.create(fileName, 'astro', 0, input);
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createLanguageServicePlugin } from '@volar/typescript/lib/quickstart/createLanguageServicePlugin.js';
import { getLanguageModule } from './language.js';

export = createLanguageServicePlugin((ts) => [getLanguageModule(ts)]);
export = createLanguageServicePlugin(() => [getLanguageModule()]);
18 changes: 6 additions & 12 deletions packages/ts-plugin/src/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import {
import type ts from 'typescript';
import { astro2tsx } from './astro2tsx.js';

export function getLanguageModule(
ts: typeof import('typescript')
): LanguagePlugin<AstroVirtualCode> {
export function getLanguageModule(): LanguagePlugin<AstroVirtualCode> {
return {
createVirtualCode(fileId, languageId, snapshot) {
if (languageId === 'astro') {
const fileName = fileId.includes('://') ? fileId.split('://')[1] : fileId;
return new AstroVirtualCode(fileName, snapshot, ts);
// fileId will never be a uri in ts plugin
const fileName = fileId;
return new AstroVirtualCode(fileName, snapshot);
}
},
updateVirtualCode(_fileId, astroFile, snapshot) {
Expand Down Expand Up @@ -47,8 +46,7 @@ export class AstroVirtualCode implements VirtualCode {

constructor(
public fileName: string,
public snapshot: ts.IScriptSnapshot,
private readonly ts: typeof import('typescript')
public snapshot: ts.IScriptSnapshot
) {
this.onSnapshotUpdated();
}
Expand Down Expand Up @@ -77,11 +75,7 @@ export class AstroVirtualCode implements VirtualCode {

this.embeddedCodes = [];

const tsx = astro2tsx(
this.snapshot.getText(0, this.snapshot.getLength()),
this.fileName,
this.ts
);
const tsx = astro2tsx(this.snapshot.getText(0, this.snapshot.getLength()), this.fileName);

this.embeddedCodes.push(tsx.virtualFile);
}
Expand Down

0 comments on commit 9d26fcd

Please sign in to comment.