Skip to content

Commit ed077a3

Browse files
committed
Factor related identifiers request into a separate file
This allows use to re-use related identifiers when implementing rename
1 parent 17eca18 commit ed077a3

File tree

3 files changed

+82
-42
lines changed

3 files changed

+82
-42
lines changed

Sources/LanguageServerProtocol/Error.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ extension ResponseError {
118118
public static func unknown(_ message: String) -> ResponseError {
119119
return ResponseError(code: .unknownErrorCode, message: message)
120120
}
121+
122+
public static func internalError(_ message: String) -> ResponseError {
123+
return ResponseError(code: .internalError, message: message)
124+
}
121125
}
122126

123127
/// An error during message decoding.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import LSPLogging
14+
import LanguageServerProtocol
15+
import SourceKitD
16+
17+
struct RelatedIdentifier {
18+
let range: Range<Position>
19+
}
20+
21+
struct RelatedIdentifiersResponse {
22+
let relatedIdentifiers: [RelatedIdentifier]
23+
}
24+
25+
extension SwiftLanguageServer {
26+
func relatedIdentifiers(at position: Position, in snapshot: DocumentSnapshot, includeNonEditableBaseNames: Bool)
27+
async throws -> RelatedIdentifiersResponse
28+
{
29+
guard let offset = snapshot.utf8Offset(of: position) else {
30+
throw ResponseError.unknown("invalid position \(position)")
31+
}
32+
33+
let skreq = SKDRequestDictionary(sourcekitd: self.sourcekitd)
34+
skreq[keys.request] = self.requests.relatedidents
35+
skreq[keys.cancelOnSubsequentRequest] = 0
36+
skreq[keys.offset] = offset
37+
skreq[keys.sourcefile] = snapshot.uri.pseudoPath
38+
39+
// FIXME: SourceKit should probably cache this for us.
40+
if let compileCommand = await self.buildSettings(for: snapshot.uri) {
41+
skreq[keys.compilerargs] = compileCommand.compilerArgs
42+
}
43+
44+
let dict = try await self.sourcekitd.send(skreq, fileContents: snapshot.text)
45+
46+
guard
47+
let results: SKDResponseArray = dict[self.keys.results]
48+
else {
49+
throw ResponseError.internalError("sourcekitd response did not contain results or name")
50+
}
51+
52+
try Task.checkCancellation()
53+
54+
var relatedIdentifiers: [RelatedIdentifier] = []
55+
56+
results.forEach { _, value in
57+
if let offset: Int = value[keys.offset],
58+
let start: Position = snapshot.positionOf(utf8Offset: offset),
59+
let length: Int = value[keys.length],
60+
let end: Position = snapshot.positionOf(utf8Offset: offset + length)
61+
{
62+
relatedIdentifiers.append(RelatedIdentifier(range: start..<end))
63+
}
64+
return true
65+
}
66+
return RelatedIdentifiersResponse(relatedIdentifiers: relatedIdentifiers)
67+
}
68+
}

Sources/SourceKitLSP/Swift/SwiftLanguageServer.swift

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -615,49 +615,17 @@ extension SwiftLanguageServer {
615615
public func documentSymbolHighlight(_ req: DocumentHighlightRequest) async throws -> [DocumentHighlight]? {
616616
let snapshot = try self.documentManager.latestSnapshot(req.textDocument.uri)
617617

618-
guard let offset = snapshot.utf8Offset(of: req.position) else {
619-
logger.error("invalid position \(req.position, privacy: .public)")
620-
return nil
621-
}
622-
623-
let skreq = SKDRequestDictionary(sourcekitd: self.sourcekitd)
624-
skreq[keys.request] = self.requests.relatedidents
625-
skreq[keys.cancelOnSubsequentRequest] = 0
626-
skreq[keys.offset] = offset
627-
skreq[keys.sourcefile] = snapshot.uri.pseudoPath
628-
629-
// FIXME: SourceKit should probably cache this for us.
630-
if let compileCommand = await self.buildSettings(for: snapshot.uri) {
631-
skreq[keys.compilerargs] = compileCommand.compilerArgs
632-
}
633-
634-
let dict = try await self.sourcekitd.send(skreq, fileContents: snapshot.text)
635-
636-
guard let results: SKDResponseArray = dict[self.keys.results] else {
637-
return []
638-
}
639-
640-
try Task.checkCancellation()
641-
642-
var highlights: [DocumentHighlight] = []
643-
644-
results.forEach { _, value in
645-
if let offset: Int = value[self.keys.offset],
646-
let start: Position = snapshot.positionOf(utf8Offset: offset),
647-
let length: Int = value[self.keys.length],
648-
let end: Position = snapshot.positionOf(utf8Offset: offset + length)
649-
{
650-
highlights.append(
651-
DocumentHighlight(
652-
range: start..<end,
653-
kind: .read // unknown
654-
)
655-
)
656-
}
657-
return true
618+
let relatedIdentifiers = try await self.relatedIdentifiers(
619+
at: req.position,
620+
in: snapshot,
621+
includeNonEditableBaseNames: false
622+
)
623+
return relatedIdentifiers.relatedIdentifiers.map {
624+
DocumentHighlight(
625+
range: $0.range,
626+
kind: .read // unknown
627+
)
658628
}
659-
660-
return highlights
661629
}
662630

663631
public func foldingRange(_ req: FoldingRangeRequest) async throws -> [FoldingRange]? {

0 commit comments

Comments
 (0)