Skip to content

Commit 74a6923

Browse files
committed
Fix semantic functionality not working in macro expansion reference documents by properly handling URIs
1 parent d801d41 commit 74a6923

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/sourcekit-lsp/LanguageClientManager.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { LSPLogger, LSPOutputChannel } from "./LSPOutputChannel";
3030
import { SwiftOutputChannel } from "../ui/SwiftOutputChannel";
3131
import { promptForDiagnostics } from "../commands/captureDiagnostics";
3232
import { activateGetReferenceDocument } from "./getReferenceDocument";
33+
import { uriConverters } from "./uriConverters";
3334

3435
interface SourceKitLogMessageParams extends langclient.LogMessageParams {
3536
logName?: string;
@@ -574,6 +575,7 @@ export class LanguageClientManager {
574575
};
575576
})(),
576577
},
578+
uriConverters: uriConverters,
577579
errorHandler,
578580
// Avoid attempting to reinitialize multiple times. If we fail to initialize
579581
// we aren't doing anything different the second time and so will fail again.

src/sourcekit-lsp/uriConverters.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the VS Code Swift open source project
4+
//
5+
// Copyright (c) 2024 the VS Code Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import * as vscode from "vscode";
16+
17+
export const uriConverters = {
18+
code2Protocol: (value: vscode.Uri) => {
19+
if (value.scheme === "sourcekit-lsp") {
20+
// Custom encoding for `sourcekit-lsp://` requests having a `parent` parameter which
21+
// can be a `file://` URL or a `sourcekit-lsp://` URL.
22+
const scheme = value.scheme;
23+
const authority = value.authority;
24+
const path = value.path;
25+
const fragment = value.fragment;
26+
27+
let query = value.query;
28+
29+
const parentParameter = "parent=";
30+
const parentParameterIndex = query.indexOf("parent=");
31+
32+
if (parentParameterIndex === -1) {
33+
// No need to do this encoding if there's no parent parameter in the
34+
// reference document URL
35+
36+
// Same as the default implementation
37+
return value.toString();
38+
}
39+
40+
// Apply encoding from the start of the value of the `parent` parameter's value
41+
// till the end of the url string (`parent` parameter is the last parameter of the
42+
// reference document url)
43+
44+
const startIndex = parentParameterIndex + parentParameter.length;
45+
46+
const before = query.slice(0, startIndex);
47+
const toEncode = query.slice(startIndex);
48+
49+
// Replace only the percent, equals, ampersand signs
50+
// This is inline with how sourcekit-lsp handles the URLs in its test cases.
51+
const encoded = toEncode.replace(/%/g, "%25").replace(/=/g, "%3D").replace(/&/g, "%26");
52+
53+
query = before + encoded;
54+
55+
let uriString = scheme + "://" + authority + path;
56+
57+
if (query !== "" && query !== null) {
58+
uriString += "?" + query;
59+
}
60+
61+
if (fragment !== "" && fragment !== null) {
62+
uriString += "#" + fragment;
63+
}
64+
65+
return uriString;
66+
} else {
67+
// Same as the default implementation
68+
return value.toString();
69+
}
70+
},
71+
protocol2Code: (value: string) => {
72+
// Same as the default implementation
73+
return vscode.Uri.parse(value);
74+
},
75+
};

0 commit comments

Comments
 (0)