Skip to content

Commit ead445c

Browse files
committed
Support Semantic Functionality in Macro Expansion Reference Documents & Nested Macro Expansions
1 parent 5833322 commit ead445c

11 files changed

+68
-24
lines changed

Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ let package = Package(
124124
.product(name: "SwiftParser", package: "swift-syntax"),
125125
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
126126
],
127-
exclude: ["CMakeLists.txt"]
127+
exclude: ["CMakeLists.txt"],
128+
swiftSettings: [
129+
.enableUpcomingFeature("BareSlashRegexLiterals")
130+
]
128131
),
129132

130133
.testTarget(

Sources/Diagnose/ActiveRequestsCommand.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import ArgumentParser
1414
import Foundation
1515
import RegexBuilder
16+
1617
import class TSCBasic.Process
1718

1819
package struct ActiveRequestsCommand: AsyncParsableCommand {

Sources/SourceKitLSP/Swift/CursorInfo.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ extension SwiftLanguageService {
147147
additionalParameters appendAdditionalParameters: ((SKDRequestDictionary) -> Void)? = nil
148148
) async throws -> (cursorInfo: [CursorInfo], refactorActions: [SemanticRefactorCommand]) {
149149
let documentManager = try self.documentManager
150-
let snapshot = try documentManager.latestSnapshot(uri)
150+
let snapshot = try await self.latestSnapshot(for: uri)
151151

152152
let offsetRange = snapshot.utf8OffsetRange(of: range)
153153

@@ -158,7 +158,8 @@ extension SwiftLanguageService {
158158
keys.cancelOnSubsequentRequest: 0,
159159
keys.offset: offsetRange.lowerBound,
160160
keys.length: offsetRange.upperBound != offsetRange.lowerBound ? offsetRange.count : nil,
161-
keys.sourceFile: snapshot.uri.pseudoPath,
161+
keys.sourceFile: snapshot.uri.sourcekitdSourceFile,
162+
keys.primaryFile: snapshot.uri.primaryFile?.pseudoPath,
162163
keys.compilerArgs: await self.buildSettings(for: uri)?.compilerArgs as [SKDRequestValue]?,
163164
])
164165

Sources/SourceKitLSP/Swift/DiagnosticReportManager.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ actor DiagnosticReportManager {
104104

105105
let skreq = sourcekitd.dictionary([
106106
keys.request: requests.diagnostics,
107-
keys.sourceFile: snapshot.uri.pseudoPath,
107+
keys.sourceFile: snapshot.uri.sourcekitdSourceFile,
108+
keys.primaryFile: snapshot.uri.primaryFile?.pseudoPath,
108109
keys.compilerArgs: compilerArgs as [SKDRequestValue],
109110
])
110111

Sources/SourceKitLSP/Swift/MacroExpansion.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,30 @@ extension SwiftLanguageService {
252252
let expansionURIs = try macroExpansionReferenceDocumentURLs.map {
253253
return DocumentURI(try $0.url)
254254
}
255+
256+
let primaryFileCursorPosition =
257+
switch try? ReferenceDocumentURL(from: expandMacroCommand.textDocument.uri) {
258+
case .macroExpansion(let data):
259+
data.primaryFileCursorPosition
260+
case nil:
261+
expandMacroCommand.positionRange.lowerBound
262+
}
263+
264+
// If there was only one expansion, show the peeked editor in the position's lower bound at which the macro
265+
// expands. If there are multiple expansions available, show the peeked editor in the cursor's selected
266+
// position's lower bound in the `primaryFile`.
267+
let peekedEditorPosition =
268+
switch macroExpansionReferenceDocumentURLs.only {
269+
case .macroExpansion(let data):
270+
data.macroExpansionEditRange.lowerBound
271+
default:
272+
primaryFileCursorPosition
273+
}
274+
255275
Task {
256276
let req = PeekDocumentsRequest(
257-
uri: expandMacroCommand.textDocument.uri,
258-
position: expandMacroCommand.positionRange.lowerBound,
277+
uri: expandMacroCommand.textDocument.uri.primaryFile ?? expandMacroCommand.textDocument.uri,
278+
position: peekedEditorPosition,
259279
locations: expansionURIs
260280
)
261281

Sources/SourceKitLSP/Swift/MacroExpansionReferenceDocumentURLData.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import LanguageServerProtocol
1515
import RegexBuilder
1616

1717
/// Represents url of macro expansion reference document as follows:
18-
/// `sourcekit-lsp://swift-macro-expansion/LaCb-LcCd.swift?parent=&fromLine=&fromColumn=&toLine=&toColumn=&bufferName=`
18+
/// `sourcekit-lsp://swift-macro-expansion/LaCb-LcCd.swift?fromLine=&fromColumn=&toLine=&toColumn=&bufferName=&parent=`
1919
///
2020
/// Here,
2121
/// - `LaCb-LcCd.swift`, the `displayName`, represents where the macro will expand to or
2222
/// replace in the source file (i.e. `macroExpansionEditRange`)
23-
/// - `parent` denoting the URI of the document from which the macro was expanded. For a first-level macro expansion,
24-
/// this is a file URI. For nested macro expansions, this is a `sourcekit-lsp://swift-macro-expansion` URL.
2523
/// - `fromLine`, `fromColumn`, `toLine`, `toColumn` represents the cursor's `selectionRange`
2624
/// - `bufferName` denotes the buffer name of the specific macro expansion edit
25+
/// - `parent` denoting the URI of the document from which the macro was expanded. For a first-level macro expansion,
26+
/// this is a file URI. For nested macro expansions, this is a `sourcekit-lsp://swift-macro-expansion` URL.
2727
package struct MacroExpansionReferenceDocumentURLData {
2828
package static let documentType = "swift-macro-expansion"
2929

@@ -47,7 +47,7 @@ package struct MacroExpansionReferenceDocumentURLData {
4747
///
4848
/// Generated content of reference document url:
4949
/// URL:
50-
/// `sourcekit-lsp://swift-macro-expansion/L3C7-L3C23.swift?primaryFilePath=/path/to/swift_file.swift&fromLine=3&fromColumn=8&toLine=3&toColumn=8&bufferName=@__swift_macro_..._Stringify_.swift`
50+
/// `sourcekit-lsp://swift-macro-expansion/L3C7-L3C23.swift?fromLine=3&fromColumn=8&toLine=3&toColumn=8&bufferName=@__swift_macro_..._Stringify_.swift&parent=/path/to/swift_file.swift`
5151
/// ```swift
5252
/// (a + b, "a + b")
5353
/// ```
@@ -78,13 +78,17 @@ package struct MacroExpansionReferenceDocumentURLData {
7878
}
7979

8080
package var queryItems: [URLQueryItem] {
81-
return [
82-
URLQueryItem(name: Parameters.parent, value: parent.stringValue),
81+
[
8382
URLQueryItem(name: Parameters.fromLine, value: String(selectionRange.lowerBound.line)),
8483
URLQueryItem(name: Parameters.fromColumn, value: String(selectionRange.lowerBound.utf16index)),
8584
URLQueryItem(name: Parameters.toLine, value: String(selectionRange.upperBound.line)),
8685
URLQueryItem(name: Parameters.toColumn, value: String(selectionRange.upperBound.utf16index)),
8786
URLQueryItem(name: Parameters.bufferName, value: bufferName),
87+
88+
// *Note*: Having `parent` as the last parameter will ensure that the url's parameters aren't mistaken to be its
89+
// `parent`'s parameters in certain environments where percent encoding gets removed or added
90+
// unnecessarily (for example: VS Code)
91+
URLQueryItem(name: Parameters.parent, value: parent.stringValue),
8892
]
8993
}
9094

@@ -121,7 +125,7 @@ package struct MacroExpansionReferenceDocumentURLData {
121125
///
122126
/// Generated content of reference document url:
123127
/// URL:
124-
/// `sourcekit-lsp://swift-macro-expansion/L3C7-L3C23.swift?primaryFilePath=/path/to/swift_file.swift&fromLine=3&fromColumn=8&toLine=3&toColumn=8&bufferName=@__swift_macro_..._Stringify_.swift`
128+
/// `sourcekit-lsp://swift-macro-expansion/L3C7-L3C23.swift?fromLine=3&fromColumn=8&toLine=3&toColumn=8&bufferName=@__swift_macro_..._Stringify_.swift&parent=/path/to/swift_file.swift`
125129
/// ```swift
126130
/// (a + b, "a + b")
127131
/// ```
@@ -140,6 +144,16 @@ package struct MacroExpansionReferenceDocumentURLData {
140144
}
141145
}
142146

147+
/// The cursor position's lower bound in the `primaryFile`
148+
package var primaryFileCursorPosition: Position {
149+
switch try? ReferenceDocumentURL(from: parent) {
150+
case .macroExpansion(let data):
151+
return data.primaryFileCursorPosition
152+
case nil:
153+
return self.selectionRange.lowerBound
154+
}
155+
}
156+
143157
private struct Parameters {
144158
static let parent = "parent"
145159
static let fromLine = "fromLine"

Sources/SourceKitLSP/Swift/OpenInterface.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extension SwiftLanguageService {
3737
let interfaceFilePath = self.generatedInterfacesPath.appendingPathComponent("\(name).swiftinterface")
3838
let interfaceDocURI = DocumentURI(interfaceFilePath)
3939
// has interface already been generated
40-
if let snapshot = try? self.documentManager.latestSnapshot(interfaceDocURI) {
40+
if let snapshot = try? await self.latestSnapshot(for: interfaceDocURI) {
4141
return await self.generatedInterfaceDetails(
4242
uri: interfaceDocURI,
4343
snapshot: snapshot,
@@ -111,7 +111,8 @@ extension SwiftLanguageService {
111111
let keys = self.keys
112112
let skreq = sourcekitd.dictionary([
113113
keys.request: requests.editorFindUSR,
114-
keys.sourceFile: uri.pseudoPath,
114+
keys.sourceFile: uri.sourcekitdSourceFile,
115+
keys.primaryFile: uri.primaryFile?.pseudoPath,
115116
keys.usr: symbol,
116117
])
117118

Sources/SourceKitLSP/Swift/RelatedIdentifiers.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ extension SwiftLanguageService {
6969
keys.request: requests.relatedIdents,
7070
keys.cancelOnSubsequentRequest: 0,
7171
keys.offset: snapshot.utf8Offset(of: position),
72-
keys.sourceFile: snapshot.uri.pseudoPath,
72+
keys.sourceFile: snapshot.uri.sourcekitdSourceFile,
73+
keys.primaryFile: snapshot.uri.primaryFile?.pseudoPath,
7374
keys.includeNonEditableBaseNames: includeNonEditableBaseNames ? 1 : 0,
7475
keys.compilerArgs: await self.buildSettings(for: snapshot.uri)?.compilerArgs as [SKDRequestValue]?,
7576
])

Sources/SourceKitLSP/Swift/SemanticTokens.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ extension SwiftLanguageService {
2626

2727
let skreq = sourcekitd.dictionary([
2828
keys.request: requests.semanticTokens,
29-
keys.sourceFile: snapshot.uri.pseudoPath,
29+
keys.sourceFile: snapshot.uri.sourcekitdSourceFile,
30+
keys.primaryFile: snapshot.uri.primaryFile?.pseudoPath,
3031
keys.compilerArgs: buildSettings.compilerArgs as [SKDRequestValue],
3132
])
3233

@@ -84,7 +85,7 @@ extension SwiftLanguageService {
8485
package func documentSemanticTokens(
8586
_ req: DocumentSemanticTokensRequest
8687
) async throws -> DocumentSemanticTokensResponse? {
87-
let snapshot = try self.documentManager.latestSnapshot(req.textDocument.uri)
88+
let snapshot = try await self.latestSnapshot(for: req.textDocument.uri)
8889

8990
let tokens = try await mergedAndSortedTokens(for: snapshot)
9091
let encodedTokens = tokens.lspEncoded

Sources/SourceKitLSP/Swift/SwiftLanguageService.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ extension SwiftLanguageService {
538538
return
539539
}
540540
do {
541-
let snapshot = try await documentManager.latestSnapshot(document)
541+
let snapshot = try await self.latestSnapshot(for: document)
542542
let buildSettings = await self.buildSettings(for: document)
543543
let diagnosticReport = try await self.diagnosticReportManager.diagnosticReport(
544544
for: snapshot,
@@ -775,7 +775,7 @@ extension SwiftLanguageService {
775775
}
776776

777777
package func documentSymbolHighlight(_ req: DocumentHighlightRequest) async throws -> [DocumentHighlight]? {
778-
let snapshot = try self.documentManager.latestSnapshot(req.textDocument.uri)
778+
let snapshot = try await self.latestSnapshot(for: req.textDocument.uri)
779779

780780
let relatedIdentifiers = try await self.relatedIdentifiers(
781781
at: req.position,
@@ -879,7 +879,7 @@ extension SwiftLanguageService {
879879
}
880880

881881
func retrieveQuickFixCodeActions(_ params: CodeActionRequest) async throws -> [CodeAction] {
882-
let snapshot = try documentManager.latestSnapshot(params.textDocument.uri)
882+
let snapshot = try await self.latestSnapshot(for: params.textDocument.uri)
883883
let buildSettings = await self.buildSettings(for: params.textDocument.uri)
884884
let diagnosticReport = try await self.diagnosticReportManager.diagnosticReport(
885885
for: snapshot,
@@ -972,7 +972,7 @@ extension SwiftLanguageService {
972972
await semanticIndexManager?.prepareFileForEditorFunctionality(
973973
req.textDocument.uri.primaryFile ?? req.textDocument.uri
974974
)
975-
let snapshot = try documentManager.latestSnapshot(req.textDocument.uri)
975+
let snapshot = try await self.latestSnapshot(for: req.textDocument.uri)
976976
let buildSettings = await self.buildSettings(for: req.textDocument.uri)
977977
let diagnosticReport = try await self.diagnosticReportManager.diagnosticReport(
978978
for: snapshot,

Sources/SourceKitLSP/Swift/VariableTypeInfo.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,12 @@ extension SwiftLanguageService {
8383
_ uri: DocumentURI,
8484
_ range: Range<Position>? = nil
8585
) async throws -> [VariableTypeInfo] {
86-
let snapshot = try documentManager.latestSnapshot(uri)
86+
let snapshot = try await self.latestSnapshot(for: uri)
8787

8888
let skreq = sourcekitd.dictionary([
8989
keys.request: requests.collectVariableType,
90-
keys.sourceFile: snapshot.uri.pseudoPath,
90+
keys.sourceFile: snapshot.uri.sourcekitdSourceFile,
91+
keys.primaryFile: snapshot.uri.primaryFile?.pseudoPath,
9192
keys.compilerArgs: await self.buildSettings(for: uri)?.compilerArgs as [SKDRequestValue]?,
9293
])
9394

0 commit comments

Comments
 (0)