|
| 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