Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions language-server/src/build-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Formatting } from "./features/Formatting.ts";
import { Hover } from "./features/Hover.ts";
import { Completion } from "./features/Completion.ts";
import { FoldingRanges } from "./features/FoldingRanges.ts";
import { DocumentSymbols } from "./features/DocumentSymbols.ts";

import "@hyperjump/json-schema/draft-2020-12";
import "@hyperjump/json-schema/draft-2019-09";
Expand Down Expand Up @@ -39,6 +40,7 @@ export const buildServer = (connection: Connection): Server => {
new Hover(server, documents);
new Completion(server, documents);
new FoldingRanges(server, documents);
new DocumentSymbols(server, documents);

return server;
};
273 changes: 273 additions & 0 deletions language-server/src/features/DocumentSymbols.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
import { describe, test, expect, beforeEach, afterEach } from "vitest";
import { TestClient } from "../test/TestClient.ts";
import { DocumentSymbolRequest, SymbolKind } from "vscode-languageserver";

describe("DocumentSymbols", () => {
let client: TestClient;

beforeEach(async () => {
client = new TestClient();
await client.start();
});

afterEach(async () => {
await client.stop();
});

test("should return document symbols for flat JSON object", async () => {
await client.writeDocument("test.json", `{
"name": "Alice",
"age": 30,
"active": true,
"address": null
}`);
const uri = await client.openDocument("test.json");

const result = await client.sendRequest(DocumentSymbolRequest.type, {
textDocument: { uri }
});

expect(result).toEqual([
{
name: "name",
kind: SymbolKind.Property,
range: {
start: { line: 1, character: 6 },
end: { line: 1, character: 12 }
},
selectionRange: {
start: { line: 1, character: 6 },
end: { line: 1, character: 12 }
}
},
{
name: "Alice",
kind: SymbolKind.String,
range: {
start: { line: 1, character: 14 },
end: { line: 1, character: 21 }
},
selectionRange: {
start: { line: 1, character: 14 },
end: { line: 1, character: 21 }
}
},
{
name: "age",
kind: SymbolKind.Property,
range: {
start: { line: 2, character: 6 },
end: { line: 2, character: 11 }
},
selectionRange: {
start: { line: 2, character: 6 },
end: { line: 2, character: 11 }
}
},
{
name: "30",
kind: SymbolKind.Number,
range: {
start: { line: 2, character: 13 },
end: { line: 2, character: 15 }
},
selectionRange: {
start: { line: 2, character: 13 },
end: { line: 2, character: 15 }
}
},
{
name: "active",
kind: SymbolKind.Property,
range: {
start: { line: 3, character: 6 },
end: { line: 3, character: 14 }
},
selectionRange: {
start: { line: 3, character: 6 },
end: { line: 3, character: 14 }
}
},
{
name: "true",
kind: SymbolKind.Boolean,
range: {
start: { line: 3, character: 16 },
end: { line: 3, character: 20 }
},
selectionRange: {
start: { line: 3, character: 16 },
end: { line: 3, character: 20 }
}
},
{
name: "address",
kind: SymbolKind.Property,
range: {
start: { line: 4, character: 6 },
end: { line: 4, character: 15 }
},
selectionRange: {
start: { line: 4, character: 6 },
end: { line: 4, character: 15 }
}
},
{
name: "null",
kind: SymbolKind.Null,
range: {
start: { line: 4, character: 17 },
end: { line: 4, character: 21 }
},
selectionRange: {
start: { line: 4, character: 17 },
end: { line: 4, character: 21 }
}
}
]);
});

test("should return document symbols for nested JSON objects", async () => {
await client.writeDocument("test.json", `{
"server": {
"port": 8080
}
}`);
const uri = await client.openDocument("test.json");

const result = await client.sendRequest(DocumentSymbolRequest.type, {
textDocument: { uri }
});

expect(result).toEqual([
{
name: "server",
kind: SymbolKind.Property,
range: {
start: { line: 1, character: 6 },
end: { line: 1, character: 14 }
},
selectionRange: {
start: { line: 1, character: 6 },
end: { line: 1, character: 14 }
}
},
{
name: "server",
kind: SymbolKind.Object,
range: {
start: { line: 1, character: 16 },
end: { line: 3, character: 7 }
},
selectionRange: {
start: { line: 1, character: 16 },
end: { line: 3, character: 7 }
},
children: [
{
name: "port",
kind: SymbolKind.Property,
range: {
start: { line: 2, character: 8 },
end: { line: 2, character: 14 }
},
selectionRange: {
start: { line: 2, character: 8 },
end: { line: 2, character: 14 }
}
},
{
name: "8080",
kind: SymbolKind.Number,
range: {
start: { line: 2, character: 16 },
end: { line: 2, character: 20 }
},
selectionRange: {
start: { line: 2, character: 16 },
end: { line: 2, character: 20 }
}
}
]
}
]);
});

test("should return document symbols for JSON arrays", async () => {
await client.writeDocument("test.json", `{
"plugins": [
"auth",
"logger"
]
}`);
const uri = await client.openDocument("test.json");

const result = await client.sendRequest(DocumentSymbolRequest.type, {
textDocument: { uri }
});

expect(result).toEqual([
{
name: "plugins",
kind: SymbolKind.Property,
range: {
start: { line: 1, character: 6 },
end: { line: 1, character: 15 }
},
selectionRange: {
start: { line: 1, character: 6 },
end: { line: 1, character: 15 }
}
},
{
name: "plugins",
kind: SymbolKind.Array,
range: {
start: { line: 1, character: 17 },
end: { line: 4, character: 7 }
},
selectionRange: {
start: { line: 1, character: 17 },
end: { line: 4, character: 7 }
},
children: [
{
name: "0",
kind: SymbolKind.String,
range: {
start: { line: 2, character: 8 },
end: { line: 2, character: 14 }
},
selectionRange: {
start: { line: 2, character: 8 },
end: { line: 2, character: 14 }
}
},
{
name: "1",
kind: SymbolKind.String,
range: {
start: { line: 3, character: 8 },
end: { line: 3, character: 16 }
},
selectionRange: {
start: { line: 3, character: 8 },
end: { line: 3, character: 16 }
}
}
]
}
]);
});

test("should return empty array for empty JSON object or empty array root", async () => {
await client.writeDocument("test.json", "{}\n");
const uri = await client.openDocument("test.json");

const result = await client.sendRequest(DocumentSymbolRequest.type, {
textDocument: { uri }
});

expect(result).toEqual([]);
});
});
Loading
Loading