Skip to content

Commit 1cb9e07

Browse files
authored
Merge pull request #428 from DavidGoldman/docsymfix
Fix document symbols regression
2 parents 4e3a051 + 9fbaebb commit 1cb9e07

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

Sources/LanguageServerProtocol/Requests/DocumentSymbolRequest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public struct DocumentSymbol: Hashable, Codable {
9191
public var selectionRange: Range<Position>
9292

9393
/// Children of this symbol, e.g. properties of a class.
94-
public var children: [DocumentSymbol]
94+
public var children: [DocumentSymbol]?
9595

9696
public init(
9797
name: String,
@@ -100,7 +100,7 @@ public struct DocumentSymbol: Hashable, Codable {
100100
deprecated: Bool? = nil,
101101
range: Range<Position>,
102102
selectionRange: Range<Position>,
103-
children: [DocumentSymbol] = [])
103+
children: [DocumentSymbol]? = nil)
104104
{
105105
self.name = name
106106
self.detail = detail

Tests/LanguageServerProtocolTests/CodingTests.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,6 @@ final class CodingTests: XCTestCase {
333333
checkCoding(DocumentSymbolResponse.documentSymbols([DocumentSymbol(name: "mySymbol", kind: .function, range: range, selectionRange: range)]), json: """
334334
[
335335
{
336-
"children" : [
337-
338-
],
339336
"kind" : 12,
340337
"name" : "mySymbol",
341338
"range" : \(rangejson.indented(4, skipFirstLine: true)),

Tests/SourceKitLSPTests/LocalClangTests.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,18 @@ final class LocalClangTests: XCTestCase {
3838

3939
connection = TestSourceKitServer()
4040
sk = connection.client
41+
let documentSymbol = TextDocumentClientCapabilities.DocumentSymbol(
42+
dynamicRegistration: nil,
43+
symbolKind: nil,
44+
hierarchicalDocumentSymbolSupport: true
45+
)
46+
let textDocument = TextDocumentClientCapabilities(documentSymbol: documentSymbol)
4147
_ = try! sk.sendSync(InitializeRequest(
4248
processId: nil,
4349
rootPath: nil,
4450
rootURI: nil,
4551
initializationOptions: nil,
46-
capabilities: ClientCapabilities(workspace: nil, textDocument: nil),
52+
capabilities: ClientCapabilities(workspace: nil, textDocument: textDocument),
4753
trace: .off,
4854
workspaceFolders: nil))
4955
}
@@ -139,6 +145,35 @@ final class LocalClangTests: XCTestCase {
139145
XCTAssertNil(resp)
140146
}
141147

148+
func testDocumentSymbols() throws {
149+
guard haveClangd else { return }
150+
let url = URL(fileURLWithPath: "/a.cpp")
151+
152+
sk.send(DidOpenTextDocumentNotification(textDocument: TextDocumentItem(
153+
uri: DocumentURI(url),
154+
language: .cpp,
155+
version: 1,
156+
text: """
157+
struct S {
158+
void foo() {
159+
int local = 1;
160+
}
161+
};
162+
""")))
163+
164+
guard let resp = try! sk.sendSync(DocumentSymbolRequest(textDocument: TextDocumentIdentifier(url))) else {
165+
XCTFail("Invalid document symbol response")
166+
return
167+
}
168+
guard case let .documentSymbols(syms) = resp else {
169+
XCTFail("Expected a [DocumentSymbol] but got \(resp)")
170+
return
171+
}
172+
XCTAssertEqual(syms.count, 1)
173+
XCTAssertEqual(syms.first?.name, "S")
174+
XCTAssertEqual(syms.first?.children?.first?.name, "foo")
175+
}
176+
142177
func testClangStdHeaderCanary() throws {
143178
guard let ws = try staticSourceKitTibsWorkspace(name: "ClangStdHeaderCanary") else { return }
144179
if ToolchainRegistry.shared.default?.clangd == nil { return }

0 commit comments

Comments
 (0)