|
| 1 | +import { afterEach, beforeEach, describe, expect, test } from "vitest"; |
| 2 | +import { DefinitionRequest } from "vscode-languageserver"; |
| 3 | +import { TestClient } from "../test-client.js"; |
| 4 | +import documentSettings from "./document-settings.js"; |
| 5 | +import schemaRegistry from "./schema-registry.js"; |
| 6 | +import workspace from "./workspace.js"; |
| 7 | +import definitionFeature from "./definition.js"; |
| 8 | + |
| 9 | +import type { DocumentSettings } from "./document-settings.js"; |
| 10 | + |
| 11 | + |
| 12 | +describe("Feature - Goto Definition", () => { |
| 13 | + let client: TestClient<DocumentSettings>; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + client = new TestClient([ |
| 17 | + workspace, |
| 18 | + documentSettings, |
| 19 | + schemaRegistry, |
| 20 | + definitionFeature |
| 21 | + ]); |
| 22 | + await client.start(); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(async () => { |
| 26 | + await client.stop(); |
| 27 | + }); |
| 28 | + |
| 29 | + test("no defintions", async () => { |
| 30 | + const documentUri = await client.openDocument("./subject.schema.json", `{}`); |
| 31 | + |
| 32 | + const response = await client.sendRequest(DefinitionRequest.type, { |
| 33 | + textDocument: { uri: documentUri }, |
| 34 | + position: { |
| 35 | + line: 0, |
| 36 | + character: 1 |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + expect(response).to.eql([]); |
| 41 | + }); |
| 42 | + |
| 43 | + test("don't return definitions that do not match location", async () => { |
| 44 | + const documentUri = await client.openDocument("./subject.schema.json", `{ |
| 45 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 46 | + "$ref": "#/$defs/locations", |
| 47 | + "$defs": { |
| 48 | + "names": { |
| 49 | + |
| 50 | + }, |
| 51 | + "locations": { |
| 52 | + |
| 53 | + } |
| 54 | + }, |
| 55 | +}`); |
| 56 | + |
| 57 | + const response = await client.sendRequest(DefinitionRequest.type, { |
| 58 | + textDocument: { uri: documentUri }, |
| 59 | + position: { |
| 60 | + line: 2, |
| 61 | + character: 11 |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + expect(response).to.eql([{ |
| 66 | + uri: documentUri, |
| 67 | + range: { |
| 68 | + start: { line: 7, character: 17 }, |
| 69 | + end: { line: 9, character: 5 } |
| 70 | + } |
| 71 | + }]); |
| 72 | + }); |
| 73 | + |
| 74 | + test("match one reference", async () => { |
| 75 | + const documentUri = await client.openDocument("./subject.schema.json", `{ |
| 76 | + "$schema":"https://json-schema.org/draft/2020-12/schema", |
| 77 | + "$ref": "#/$defs/names", |
| 78 | + "$defs":{ |
| 79 | + "names": { |
| 80 | + |
| 81 | + } |
| 82 | + }, |
| 83 | +}`); |
| 84 | + |
| 85 | + const response = await client.sendRequest(DefinitionRequest.type, { |
| 86 | + textDocument: { uri: documentUri }, |
| 87 | + position: { |
| 88 | + line: 2, |
| 89 | + character: 20 |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + expect(response).to.eql([ |
| 94 | + { |
| 95 | + "uri": documentUri, |
| 96 | + "range": { |
| 97 | + "start": { "line": 4, "character": 13 }, |
| 98 | + "end": { "line": 6, "character": 5 } |
| 99 | + } |
| 100 | + } |
| 101 | + ]); |
| 102 | + }); |
| 103 | + test("match one definition", async () => { |
| 104 | + const documentUri = await client.openDocument("./subject.schema.json", `{ |
| 105 | + "$schema":"https://json-schema.org/draft/2020-12/schema", |
| 106 | + "$ref": "#/$defs/names", |
| 107 | + "$defs":{ |
| 108 | + "names": { |
| 109 | + |
| 110 | + } |
| 111 | + }, |
| 112 | +}`); |
| 113 | + |
| 114 | + const response = await client.sendRequest(DefinitionRequest.type, { |
| 115 | + textDocument: { uri: documentUri }, |
| 116 | + position: { |
| 117 | + line: 2, |
| 118 | + character: 18 |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + expect(response).to.eql([ |
| 123 | + { |
| 124 | + "uri": documentUri, |
| 125 | + "range": { |
| 126 | + "start": { "line": 4, "character": 13 }, |
| 127 | + "end": { "line": 6, "character": 5 } |
| 128 | + } |
| 129 | + } |
| 130 | + ]); |
| 131 | + }); |
| 132 | + |
| 133 | + test("cross file definition", async () => { |
| 134 | + const documentUriA = await client.openDocument("./subjectA.schema.json", `{ |
| 135 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 136 | + "definitions": { |
| 137 | + "person": { |
| 138 | +
|
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | +`); |
| 143 | + const documentUriB = await client.openDocument("./subjectB.schema.json", `{ |
| 144 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 145 | + "$ref": "./subjectA.schema.json#/definitions/person" |
| 146 | +} |
| 147 | +`); |
| 148 | + |
| 149 | + const response = await client.sendRequest(DefinitionRequest.type, { |
| 150 | + textDocument: { uri: documentUriB }, |
| 151 | + position: { |
| 152 | + line: 2, |
| 153 | + character: 20 |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + expect(response).to.eql([ |
| 158 | + { |
| 159 | + "uri": documentUriA, |
| 160 | + "range": { |
| 161 | + "start": { "line": 3, "character": 14 }, |
| 162 | + "end": { "line": 5, "character": 5 } |
| 163 | + } |
| 164 | + } |
| 165 | + ]); |
| 166 | + }); |
| 167 | + |
| 168 | + test("match self identified externally", async () => { |
| 169 | + const documentUri = await client.openDocument("./subject.schema.json", `{ |
| 170 | + "$schema":"http://json-schema.org/draft-07/schema#", |
| 171 | + "$ref": "https://example.com/schemas/two#/definitions/names", |
| 172 | +}`); |
| 173 | + |
| 174 | + const documentUriB = await client.openDocument("./subjectB.schema.json", `{ |
| 175 | + "$schema":"http://json-schema.org/draft-07/schema#", |
| 176 | + "$id": "https://example.com/schemas/two", |
| 177 | + "definitions":{ |
| 178 | + "names": { |
| 179 | + |
| 180 | + } |
| 181 | + } |
| 182 | +}`); |
| 183 | + |
| 184 | + const response = await client.sendRequest(DefinitionRequest.type, { |
| 185 | + textDocument: { uri: documentUri }, |
| 186 | + position: { |
| 187 | + line: 2, |
| 188 | + character: 37 |
| 189 | + } |
| 190 | + }); |
| 191 | + |
| 192 | + expect(response).to.eql([ |
| 193 | + { |
| 194 | + "uri": documentUriB, |
| 195 | + "range": { |
| 196 | + "start": { "line": 4, "character": 13 }, |
| 197 | + "end": { "line": 6, "character": 5 } |
| 198 | + } |
| 199 | + } |
| 200 | + ]); |
| 201 | + }); |
| 202 | + |
| 203 | + test("match self identified internally", async () => { |
| 204 | + const documentUri = await client.openDocument("./subject.schema.json", `{ |
| 205 | + "$schema":"http://json-schema.org/draft-07/schema#", |
| 206 | + "$id": "https://example.com/person.json", |
| 207 | + "type": "object", |
| 208 | + "properties": { |
| 209 | + "names": { "$ref": "https://example.com/person.json" } |
| 210 | + } |
| 211 | +}`); |
| 212 | + |
| 213 | + const response = await client.sendRequest(DefinitionRequest.type, { |
| 214 | + textDocument: { uri: documentUri }, |
| 215 | + position: { |
| 216 | + line: 5, |
| 217 | + character: 30 |
| 218 | + } |
| 219 | + }); |
| 220 | + |
| 221 | + expect(response).to.eql([ |
| 222 | + { |
| 223 | + "uri": documentUri, |
| 224 | + "range": { |
| 225 | + "start": { "line": 0, "character": 0 }, |
| 226 | + "end": { "line": 7, "character": 1 } |
| 227 | + } |
| 228 | + } |
| 229 | + ]); |
| 230 | + }); |
| 231 | +}); |
0 commit comments