Skip to content

Commit 5a677fd

Browse files
authored
(fix) normalize uris coming from client (#443)
1 parent 5478142 commit 5a677fd

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

packages/language-server/src/lib/documents/DocumentManager.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
VersionedTextDocumentIdentifier,
66
} from 'vscode-languageserver';
77
import { Document } from './Document';
8+
import { normalizeUri } from '../../utils';
89

910
export type DocumentEvent = 'documentOpen' | 'documentChange' | 'documentClose';
1011

@@ -14,15 +15,17 @@ export type DocumentEvent = 'documentOpen' | 'documentChange' | 'documentClose';
1415
export class DocumentManager {
1516
private emitter = new EventEmitter();
1617
private openedInClient = new Set<string>();
17-
public documents: Map<string, Document> = new Map();
18-
public locked = new Set<string>();
19-
public deleteCandidates = new Set<string>();
18+
private documents: Map<string, Document> = new Map();
19+
private locked = new Set<string>();
20+
private deleteCandidates = new Set<string>();
2021

2122
constructor(
2223
private createDocument: (textDocument: Pick<TextDocumentItem, 'text' | 'uri'>) => Document,
2324
) {}
2425

2526
openDocument(textDocument: Pick<TextDocumentItem, 'text' | 'uri'>): Document {
27+
textDocument = { ...textDocument, uri: normalizeUri(textDocument.uri) };
28+
2629
let document: Document;
2730
if (this.documents.has(textDocument.uri)) {
2831
document = this.documents.get(textDocument.uri)!;
@@ -39,11 +42,11 @@ export class DocumentManager {
3942
}
4043

4144
lockDocument(uri: string): void {
42-
this.locked.add(uri);
45+
this.locked.add(normalizeUri(uri));
4346
}
4447

4548
markAsOpenedInClient(uri: string): void {
46-
this.openedInClient.add(uri);
49+
this.openedInClient.add(normalizeUri(uri));
4750
}
4851

4952
getAllOpenedByClient() {
@@ -53,6 +56,8 @@ export class DocumentManager {
5356
}
5457

5558
releaseDocument(uri: string): void {
59+
uri = normalizeUri(uri);
60+
5661
this.locked.delete(uri);
5762
this.openedInClient.delete(uri);
5863
if (this.deleteCandidates.has(uri)) {
@@ -62,6 +67,8 @@ export class DocumentManager {
6267
}
6368

6469
closeDocument(uri: string) {
70+
uri = normalizeUri(uri);
71+
6572
const document = this.documents.get(uri);
6673
if (!document) {
6774
throw new Error('Cannot call methods on an unopened document');
@@ -83,7 +90,7 @@ export class DocumentManager {
8390
textDocument: VersionedTextDocumentIdentifier,
8491
changes: TextDocumentContentChangeEvent[],
8592
) {
86-
const document = this.documents.get(textDocument.uri);
93+
const document = this.documents.get(normalizeUri(textDocument.uri));
8794
if (!document) {
8895
throw new Error('Cannot call methods on an unopened document');
8996
}
@@ -108,6 +115,10 @@ export class DocumentManager {
108115
this.emitter.on(name, listener);
109116
}
110117

118+
get(uri: string) {
119+
return this.documents.get(normalizeUri(uri));
120+
}
121+
111122
private notify(name: DocumentEvent, document: Document) {
112123
this.emitter.emit(name, document);
113124
}

packages/language-server/src/plugins/PluginHost.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ export class PluginHost implements LSProvider, OnWatchFileChanges {
314314
}
315315

316316
private getDocument(uri: string) {
317-
return this.documentsManager.documents.get(uri);
317+
return this.documentsManager.get(uri);
318318
}
319319

320320
private execute<T>(

packages/language-server/src/server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ export function startServer(options?: LSOptions) {
6868
connection = createConnection(process.stdin, process.stdout);
6969
} else {
7070
connection = createConnection(
71-
new IPCMessageReader(process), new IPCMessageWriter(process));
71+
new IPCMessageReader(process),
72+
new IPCMessageWriter(process),
73+
);
7274
}
7375
}
7476

@@ -276,7 +278,7 @@ export function startServer(options?: LSOptions) {
276278
);
277279

278280
connection.onRequest('$/getCompiledCode', async (uri: DocumentUri) => {
279-
const doc = docManager.documents.get(uri);
281+
const doc = docManager.get(uri);
280282
if (!doc) return null;
281283

282284
if (doc) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class SvelteCheck {
6565
const diagnostics = await this.pluginHost.getDiagnostics({ uri });
6666
return {
6767
filePath: urlToPath(uri) || '',
68-
text: this.docManager.documents.get(uri)?.getText() || '',
68+
text: this.docManager.get(uri)?.getText() || '',
6969
diagnostics,
7070
};
7171
}),

packages/language-server/src/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ export function pathToUrl(path: string) {
1717
return URI.file(path).toString();
1818
}
1919

20+
/**
21+
* URIs coming from the client could be encoded in a different
22+
* way than expected / than the internal services create them.
23+
* This normalizes them to be the same as the internally generated ones.
24+
*/
25+
export function normalizeUri(uri: string): string {
26+
return URI.parse(uri).toString();
27+
}
28+
2029
export function flatten<T>(arr: T[][]): T[] {
2130
return arr.reduce((all, item) => [...all, ...item], []);
2231
}

0 commit comments

Comments
 (0)