Skip to content

Commit d11c101

Browse files
authored
Merge pull request #1634 from lokesh-tr/support-semantic-functionality
Add support for semantic functionality in macro expansion reference documents
2 parents 3cc1cbc + 0784041 commit d11c101

9 files changed

+67
-36
lines changed

Sources/SourceKitLSP/Swift/CursorInfo.swift

+3-2
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

+2-1
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

+15-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ actor MacroExpansionManager {
6868
) async throws -> String {
6969
let expansions = try await macroExpansions(
7070
in: macroExpansionURLData.parent,
71-
at: macroExpansionURLData.selectionRange
71+
at: macroExpansionURLData.parentSelectionRange
7272
)
7373
guard let expansion = expansions.filter({ $0.bufferName == macroExpansionURLData.bufferName }).only else {
7474
throw ResponseError.unknown("Failed to find macro expansion for \(macroExpansionURLData.bufferName).")
@@ -195,7 +195,7 @@ extension SwiftLanguageService {
195195
MacroExpansionReferenceDocumentURLData(
196196
macroExpansionEditRange: macroEdit.range,
197197
parent: expandMacroCommand.textDocument.uri,
198-
selectionRange: expandMacroCommand.positionRange,
198+
parentSelectionRange: expandMacroCommand.positionRange,
199199
bufferName: bufferName
200200
)
201201
)
@@ -224,10 +224,21 @@ extension SwiftLanguageService {
224224
let expansionURIs = try macroExpansionReferenceDocumentURLs.map {
225225
return DocumentURI(try $0.url)
226226
}
227+
228+
let uri = expandMacroCommand.textDocument.uri.primaryFile ?? expandMacroCommand.textDocument.uri
229+
230+
let position =
231+
switch try? ReferenceDocumentURL(from: expandMacroCommand.textDocument.uri) {
232+
case .macroExpansion(let data):
233+
data.primaryFileSelectionRange.lowerBound
234+
case nil:
235+
expandMacroCommand.positionRange.lowerBound
236+
}
237+
227238
Task {
228239
let req = PeekDocumentsRequest(
229-
uri: expandMacroCommand.textDocument.uri,
230-
position: expandMacroCommand.positionRange.lowerBound,
240+
uri: uri,
241+
position: position,
231242
locations: expansionURIs
232243
)
233244

Sources/SourceKitLSP/Swift/MacroExpansionReferenceDocumentURLData.swift

+31-17
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ 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+
/// - `fromLine`, `fromColumn`, `toLine`, `toColumn` represents the cursor's selection range in
24+
/// its `parent` (i.e. `parentSelectionRange`)
25+
/// - `bufferName` denotes the buffer name of the specific macro expansion edit
2326
/// - `parent` denoting the URI of the document from which the macro was expanded. For a first-level macro expansion,
2427
/// this is a file URI. For nested macro expansions, this is a `sourcekit-lsp://swift-macro-expansion` URL.
25-
/// - `fromLine`, `fromColumn`, `toLine`, `toColumn` represents the cursor's `selectionRange`
26-
/// - `bufferName` denotes the buffer name of the specific macro expansion edit
2728
package struct MacroExpansionReferenceDocumentURLData {
2829
package static let documentType = "swift-macro-expansion"
2930

@@ -33,7 +34,7 @@ package struct MacroExpansionReferenceDocumentURLData {
3334
package var parent: DocumentURI
3435

3536
/// The range that was selected in `parent` when the macro was expanded.
36-
package var selectionRange: Range<Position>
37+
package var parentSelectionRange: Range<Position>
3738

3839
/// ## Example
3940
///
@@ -47,7 +48,7 @@ package struct MacroExpansionReferenceDocumentURLData {
4748
///
4849
/// Generated content of reference document url:
4950
/// 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`
51+
/// `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`
5152
/// ```swift
5253
/// (a + b, "a + b")
5354
/// ```
@@ -64,12 +65,12 @@ package struct MacroExpansionReferenceDocumentURLData {
6465
package init(
6566
macroExpansionEditRange: Range<Position>,
6667
parent: DocumentURI,
67-
selectionRange: Range<Position>,
68+
parentSelectionRange: Range<Position>,
6869
bufferName: String
6970
) {
7071
self.macroExpansionEditRange = macroExpansionEditRange
7172
self.parent = parent
72-
self.selectionRange = selectionRange
73+
self.parentSelectionRange = parentSelectionRange
7374
self.bufferName = bufferName
7475
}
7576

@@ -78,13 +79,17 @@ package struct MacroExpansionReferenceDocumentURLData {
7879
}
7980

8081
package var queryItems: [URLQueryItem] {
81-
return [
82-
URLQueryItem(name: Parameters.parent, value: parent.stringValue),
83-
URLQueryItem(name: Parameters.fromLine, value: String(selectionRange.lowerBound.line)),
84-
URLQueryItem(name: Parameters.fromColumn, value: String(selectionRange.lowerBound.utf16index)),
85-
URLQueryItem(name: Parameters.toLine, value: String(selectionRange.upperBound.line)),
86-
URLQueryItem(name: Parameters.toColumn, value: String(selectionRange.upperBound.utf16index)),
82+
[
83+
URLQueryItem(name: Parameters.fromLine, value: String(parentSelectionRange.lowerBound.line)),
84+
URLQueryItem(name: Parameters.fromColumn, value: String(parentSelectionRange.lowerBound.utf16index)),
85+
URLQueryItem(name: Parameters.toLine, value: String(parentSelectionRange.upperBound.line)),
86+
URLQueryItem(name: Parameters.toColumn, value: String(parentSelectionRange.upperBound.utf16index)),
8787
URLQueryItem(name: Parameters.bufferName, value: bufferName),
88+
89+
// *Note*: Having `parent` as the last parameter will ensure that the url's parameters aren't mistaken to be its
90+
// `parent`'s parameters in certain environments where percent encoding gets removed or added
91+
// unnecessarily (for example: VS Code)
92+
URLQueryItem(name: Parameters.parent, value: parent.stringValue),
8893
]
8994
}
9095

@@ -100,7 +105,7 @@ package struct MacroExpansionReferenceDocumentURLData {
100105
}
101106

102107
self.parent = try DocumentURI(string: parent)
103-
self.selectionRange =
108+
self.parentSelectionRange =
104109
Position(line: fromLine, utf16index: fromColumn)..<Position(line: toLine, utf16index: toColumn)
105110
self.bufferName = bufferName
106111
self.macroExpansionEditRange = try Self.parse(displayName: displayName)
@@ -121,7 +126,7 @@ package struct MacroExpansionReferenceDocumentURLData {
121126
///
122127
/// Generated content of reference document url:
123128
/// 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`
129+
/// `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`
125130
/// ```swift
126131
/// (a + b, "a + b")
127132
/// ```
@@ -134,9 +139,18 @@ package struct MacroExpansionReferenceDocumentURLData {
134139
package var primaryFile: DocumentURI {
135140
switch try? ReferenceDocumentURL(from: parent) {
136141
case .macroExpansion(let data):
137-
return data.primaryFile
142+
data.primaryFile
143+
case nil:
144+
parent
145+
}
146+
}
147+
148+
package var primaryFileSelectionRange: Range<Position> {
149+
switch try? ReferenceDocumentURL(from: parent) {
150+
case .macroExpansion(let data):
151+
data.primaryFileSelectionRange
138152
case nil:
139-
return parent
153+
self.parentSelectionRange
140154
}
141155
}
142156

Sources/SourceKitLSP/Swift/OpenInterface.swift

+3-2
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

+2-1
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

+3-2
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

+5-5
Original file line numberDiff line numberDiff line change
@@ -529,13 +529,13 @@ extension SwiftLanguageService {
529529
return
530530
}
531531
do {
532-
let snapshot = try await documentManager.latestSnapshot(document)
532+
let snapshot = try await self.latestSnapshot(for: document)
533533
let buildSettings = await self.buildSettings(for: document)
534534
let diagnosticReport = try await self.diagnosticReportManager.diagnosticReport(
535535
for: snapshot,
536536
buildSettings: buildSettings
537537
)
538-
let latestSnapshotID = try? await documentManager.latestSnapshot(snapshot.uri).id
538+
let latestSnapshotID = try? await self.latestSnapshot(for: snapshot.uri).id
539539
if latestSnapshotID != snapshot.id {
540540
// Check that the document wasn't modified while we were getting diagnostics. This could happen because we are
541541
// calling `publishDiagnosticsIfNeeded` outside of `messageHandlingQueue` and thus a concurrent edit is
@@ -766,7 +766,7 @@ extension SwiftLanguageService {
766766
}
767767

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

771771
let relatedIdentifiers = try await self.relatedIdentifiers(
772772
at: req.position,
@@ -867,7 +867,7 @@ extension SwiftLanguageService {
867867
}
868868

869869
func retrieveQuickFixCodeActions(_ params: CodeActionRequest) async throws -> [CodeAction] {
870-
let snapshot = try documentManager.latestSnapshot(params.textDocument.uri)
870+
let snapshot = try await self.latestSnapshot(for: params.textDocument.uri)
871871
let buildSettings = await self.buildSettings(for: params.textDocument.uri)
872872
let diagnosticReport = try await self.diagnosticReportManager.diagnosticReport(
873873
for: snapshot,
@@ -960,7 +960,7 @@ extension SwiftLanguageService {
960960
await semanticIndexManager?.prepareFileForEditorFunctionality(
961961
req.textDocument.uri.primaryFile ?? req.textDocument.uri
962962
)
963-
let snapshot = try documentManager.latestSnapshot(req.textDocument.uri)
963+
let snapshot = try await self.latestSnapshot(for: req.textDocument.uri)
964964
let buildSettings = await self.buildSettings(for: req.textDocument.uri)
965965
let diagnosticReport = try await self.diagnosticReportManager.diagnosticReport(
966966
for: snapshot,

Sources/SourceKitLSP/Swift/VariableTypeInfo.swift

+3-2
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)