Skip to content

[HTML] Support the tsserver resolving files relative to the html file #121517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 117 additions & 41 deletions extensions/html-language-features/server/src/modes/javascriptMode.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ export function getLanguageModes(supportedLanguages: { [languageId: string]: boo
modes['css'] = getCSSMode(cssLanguageService, documentRegions, workspace);
}
if (supportedLanguages['javascript']) {
modes['javascript'] = getJavaScriptMode(documentRegions, 'javascript', workspace);
modes['typescript'] = getJavaScriptMode(documentRegions, 'typescript', workspace);
modes['javascript'] = getJavaScriptMode(documentRegions, 'javascript', workspace, requestService);
modes['typescript'] = getJavaScriptMode(documentRegions, 'typescript', workspace, requestService);
}
return {
async updateDataProviders(dataProviders: IHTMLDataProvider[]): Promise<void> {
Expand Down
56 changes: 38 additions & 18 deletions extensions/html-language-features/server/src/node/nodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,41 @@ import { URI as Uri } from 'vscode-uri';

import * as fs from 'fs';
import { FileType } from 'vscode-css-languageservice';
import { FileStat } from 'vscode-html-languageservice';

export function getNodeFileFS(): FileSystemProvider {
/**
* This extension is for the TSServer in the JavaScript mode which
* require sync access to stat's mtime due to TSServer API limitations
*/
export interface NodeRequestService extends FileSystemProvider {
statSync(location: string): FileStat
}

export function getNodeFileFS(): NodeRequestService {
function ensureFileUri(location: string) {
if (getScheme(location) !== 'file') {
throw new Error('fileSystemProvider can only handle file URLs');
if (getScheme(location) !== 'file' && getScheme(location) !== '') {
throw new Error(`fileSystemProvider can only handle file URLs, got ${getScheme(location)}`);
}
}

const fsStatToFileStat = (stats: fs.Stats) => {
let type = FileType.Unknown;
if (stats.isFile()) {
type = FileType.File;
} else if (stats.isDirectory()) {
type = FileType.Directory;
} else if (stats.isSymbolicLink()) {
type = FileType.SymbolicLink;
}

return {
type,
ctime: stats.ctime.getTime(),
mtime: stats.mtime.getTime(),
size: stats.size
};
};

return {
stat(location: string) {
ensureFileUri(location);
Expand All @@ -29,24 +57,16 @@ export function getNodeFileFS(): FileSystemProvider {
}
}

let type = FileType.Unknown;
if (stats.isFile()) {
type = FileType.File;
} else if (stats.isDirectory()) {
type = FileType.Directory;
} else if (stats.isSymbolicLink()) {
type = FileType.SymbolicLink;
}

c({
type,
ctime: stats.ctime.getTime(),
mtime: stats.mtime.getTime(),
size: stats.size
});
c(fsStatToFileStat(stats));
});
});
},
statSync(location: string) {
ensureFileUri(location);
const uri = Uri.parse(location);
const stats = fs.statSync(uri.fsPath);
return fsStatToFileStat(stats);
},
readDirectory(location: string) {
ensureFileUri(location);
return new Promise((c, e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ suite('HTML Completion', () => {
});
});

suite('Live TSServer inside the HTML script tags', () => {
const fixtureRoot = path.resolve(__dirname, '../../src/test/jsdocImportFixtures');
const fixtureWorkspace = { name: 'fixture', uri: URI.file(fixtureRoot).toString() };
const indexHtmlUri = URI.file(path.resolve(fixtureRoot, 'index.html')).toString();

test('Imports across files when using fixtured data from the file system', async () => {
await testCompletionFor('<html><script>/** @type {import("./jsDocTypes").SomeType } */\nconst a = {}; \n a.| \n</script><html>', {
items: [
{ label: 'other' },
{ label: 'property' },
]
}, indexHtmlUri, [fixtureWorkspace]);
});

test('Does not run the extended tsserver when _not_ using the local file system', async () => {
await testCompletionFor('<html><script>/** @type {import("./jsDocTypes").SomeType } */\nconst a {};\n a.|</script></html>', {
// As it's an 'any' then it has no completions
items: []
}, 'vfs://index.html', [{ name: 'vfs', uri: 'vfs://' }]);
});
});

suite('HTML Path Completion', () => {
const triggerSuggestCommand = {
title: 'Suggest',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

export type SomeType = {
property: string
other: number
};