Skip to content

Commit 9abbe19

Browse files
authored
(fix) support for multiple folders in workspace (#480)
1 parent c742eab commit 9abbe19

File tree

12 files changed

+31
-30
lines changed

12 files changed

+31
-30
lines changed

packages/language-server/src/plugins/typescript/LSAndTSDocResolver.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { SnapshotManager } from './SnapshotManager';
1313
export class LSAndTSDocResolver {
1414
constructor(
1515
private readonly docManager: DocumentManager,
16-
private readonly workspacePath: string,
16+
private readonly workspaceUris: string[],
1717
) {
1818
docManager.on(
1919
'documentChange',
@@ -42,7 +42,7 @@ export class LSAndTSDocResolver {
4242
};
4343

4444
getLSForPath(path: string) {
45-
return getLanguageServiceForPath(path, this.workspacePath, this.createDocument);
45+
return getLanguageServiceForPath(path, this.workspaceUris, this.createDocument);
4646
}
4747

4848
getLSAndTSDoc(
@@ -53,7 +53,7 @@ export class LSAndTSDocResolver {
5353
} {
5454
const lang = getLanguageServiceForDocument(
5555
document,
56-
this.workspacePath,
56+
this.workspaceUris,
5757
this.createDocument,
5858
);
5959
const filePath = document.getFilePath()!;
@@ -95,6 +95,6 @@ export class LSAndTSDocResolver {
9595
}
9696

9797
private getTSService(filePath: string): LanguageServiceContainer {
98-
return getService(filePath, this.workspacePath, this.createDocument);
98+
return getService(filePath, this.workspaceUris, this.createDocument);
9999
}
100100
}

packages/language-server/src/plugins/typescript/TypeScriptPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ export class TypeScriptPlugin
7474
constructor(
7575
docManager: DocumentManager,
7676
configManager: LSConfigManager,
77-
workspacePath: string,
77+
workspaceUris: string[],
7878
) {
7979
this.configManager = configManager;
80-
this.lsAndTsDocResolver = new LSAndTSDocResolver(docManager, workspacePath);
80+
this.lsAndTsDocResolver = new LSAndTSDocResolver(docManager, workspaceUris);
8181
this.completionProvider = new CompletionsProviderImpl(this.lsAndTsDocResolver);
8282
this.codeActionsProvider = new CodeActionsProviderImpl(
8383
this.lsAndTsDocResolver,

packages/language-server/src/plugins/typescript/service.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,24 @@ export type CreateDocument = (fileName: string, content: string) => Document;
2323

2424
export function getLanguageServiceForPath(
2525
path: string,
26-
workspacePath: string,
26+
workspaceUris: string[],
2727
createDocument: CreateDocument,
2828
): ts.LanguageService {
29-
return getService(path, workspacePath, createDocument).getService();
29+
return getService(path, workspaceUris, createDocument).getService();
3030
}
3131

3232
export function getLanguageServiceForDocument(
3333
document: Document,
34-
workspacePath: string,
34+
workspaceUris: string[],
3535
createDocument: CreateDocument,
3636
): ts.LanguageService {
37-
return getService(document.getFilePath() || '', workspacePath, createDocument).updateDocument(
37+
return getService(document.getFilePath() || '', workspaceUris, createDocument).updateDocument(
3838
document,
3939
);
4040
}
4141

42-
export function getService(path: string, workspacePath: string, createDocument: CreateDocument) {
43-
const tsconfigPath = findTsConfigPath(path, workspacePath);
42+
export function getService(path: string, workspaceUris: string[], createDocument: CreateDocument) {
43+
const tsconfigPath = findTsConfigPath(path, workspaceUris);
4444

4545
let service: LanguageServiceContainer;
4646
if (services.has(tsconfigPath)) {

packages/language-server/src/plugins/typescript/utils.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dirname, relative, isAbsolute } from 'path';
1+
import { dirname } from 'path';
22
import ts from 'typescript';
33
import {
44
CompletionItemKind,
@@ -8,6 +8,7 @@ import {
88
SymbolKind,
99
} from 'vscode-languageserver';
1010
import { mapRangeToOriginal } from '../../lib/documents';
11+
import { pathToUrl } from '../../utils';
1112
import { SnapshotFragment } from './DocumentSnapshot';
1213

1314
export function getScriptKindFromFileName(fileName: string): ts.ScriptKind {
@@ -102,20 +103,19 @@ export function convertToLocationRange(defDoc: SnapshotFragment, textSpan: ts.Te
102103
return range;
103104
}
104105

105-
export function findTsConfigPath(fileName: string, rootPath: string) {
106+
export function findTsConfigPath(fileName: string, rootUris: string[]) {
106107
const searchDir = dirname(fileName);
107108

108109
const path =
109110
ts.findConfigFile(searchDir, ts.sys.fileExists, 'tsconfig.json') ||
110111
ts.findConfigFile(searchDir, ts.sys.fileExists, 'jsconfig.json') ||
111112
'';
112113
// Don't return config files that exceed the current workspace context.
113-
return !!path && isSubPath(rootPath, path) ? path : '';
114+
return !!path && rootUris.some(rootUri => isSubPath(rootUri, path)) ? path : '';
114115
}
115116

116-
export function isSubPath(path: string, possibleSubPath: string): boolean {
117-
const relativePath = relative(path, possibleSubPath);
118-
return !!relativePath && !relativePath.startsWith('..') && !isAbsolute(relativePath);
117+
export function isSubPath(uri: string, possibleSubPath: string): boolean {
118+
return pathToUrl(possibleSubPath).startsWith(uri);
119119
}
120120

121121
export function symbolKindFromString(kind: string): SymbolKind {

packages/language-server/src/server.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ export function startServer(options?: LSOptions) {
8686
let sveltePlugin: SveltePlugin = undefined as any;
8787

8888
connection.onInitialize((evt) => {
89-
const workspacePath = urlToPath(evt.rootUri || '') || '';
90-
Logger.log('Initialize language server at ', workspacePath);
91-
if (!workspacePath) {
89+
const workspaceUris = evt.workspaceFolders?.map(folder => folder.uri.toString())
90+
?? [evt.rootUri ?? ''];
91+
Logger.log('Initialize language server at ', workspaceUris.join(', '));
92+
if (workspaceUris.length === 0) {
9293
Logger.error('No workspace path set');
9394
}
9495

@@ -102,7 +103,7 @@ export function startServer(options?: LSOptions) {
102103
);
103104
pluginHost.register(new HTMLPlugin(docManager, configManager));
104105
pluginHost.register(new CSSPlugin(docManager, configManager));
105-
pluginHost.register(new TypeScriptPlugin(docManager, configManager, workspacePath));
106+
pluginHost.register(new TypeScriptPlugin(docManager, configManager, workspaceUris));
106107

107108
const clientSupportApplyEditCommand = !!evt.capabilities.workspace?.applyEdit;
108109

packages/language-server/src/svelte-check.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { LSConfigManager } from './ls-config';
33
import { CSSPlugin, HTMLPlugin, PluginHost, SveltePlugin, TypeScriptPlugin } from './plugins';
44
import { Diagnostic } from 'vscode-languageserver';
55
import { Logger } from './logger';
6-
import { urlToPath } from './utils';
6+
import { urlToPath, pathToUrl } from './utils';
77

88
export interface SvelteCheckOptions {
99
compilerWarnings?: Record<string, 'ignore' | 'error'>;
@@ -35,7 +35,7 @@ export class SvelteCheck {
3535
this.pluginHost.register(new HTMLPlugin(this.docManager, this.configManager));
3636
this.pluginHost.register(new CSSPlugin(this.docManager, this.configManager));
3737
this.pluginHost.register(
38-
new TypeScriptPlugin(this.docManager, this.configManager, workspacePath),
38+
new TypeScriptPlugin(this.docManager, this.configManager, [pathToUrl(workspacePath)]),
3939
);
4040
}
4141

packages/language-server/test/plugins/typescript/TypescriptPlugin.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('TypescriptPlugin', () => {
2020
const filePath = path.join(testDir, filename);
2121
const document = new Document(pathToUrl(filePath), ts.sys.readFile(filePath) || '');
2222
const pluginManager = new LSConfigManager();
23-
const plugin = new TypeScriptPlugin(docManager, pluginManager, testDir);
23+
const plugin = new TypeScriptPlugin(docManager, pluginManager, [pathToUrl(testDir)]);
2424
docManager.openDocument(<any>'some doc');
2525
return { plugin, document };
2626
}

packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('CodeActionsProvider', () => {
2727
const docManager = new DocumentManager(
2828
(textDocument) => new Document(textDocument.uri, textDocument.text),
2929
);
30-
const lsAndTsDocResolver = new LSAndTSDocResolver(docManager, testDir);
30+
const lsAndTsDocResolver = new LSAndTSDocResolver(docManager, [pathToUrl(testDir)]);
3131
const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver);
3232
const provider = new CodeActionsProviderImpl(lsAndTsDocResolver, completionProvider);
3333
const filePath = getFullPath(filename);

packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('CompletionProviderImpl', () => {
3333
const docManager = new DocumentManager(
3434
(textDocument) => new Document(textDocument.uri, textDocument.text),
3535
);
36-
const lsAndTsDocResolver = new LSAndTSDocResolver(docManager, testDir);
36+
const lsAndTsDocResolver = new LSAndTSDocResolver(docManager, [pathToUrl(testDir)]);
3737
const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver);
3838
const filePath = join(testFilesDir, filename);
3939
const document = docManager.openDocument(<any>{

packages/language-server/test/plugins/typescript/features/DiagnosticsProvider.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('DiagnosticsProvider', () => {
1313
const filePath = path.join(testDir, 'testfiles', filename);
1414
const document = new Document(pathToUrl(filePath), ts.sys.readFile(filePath)!);
1515
const pluginManager = new LSConfigManager();
16-
const plugin = new TypeScriptPlugin(docManager, pluginManager, testDir);
16+
const plugin = new TypeScriptPlugin(docManager, pluginManager, [pathToUrl(testDir)]);
1717
docManager.openDocument(<any>'some doc');
1818
return { plugin, document };
1919
}

0 commit comments

Comments
 (0)