Skip to content

Commit ca0d073

Browse files
Format
1 parent 30ea575 commit ca0d073

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

server/src/jsonConfig.ts

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ export function getConfigCompletions(
277277

278278
const content = document.getText();
279279
const offset = document.offsetAt(position);
280-
280+
281281
// Parse the document with jsonc-parser (handles incomplete JSON)
282282
const errors: jsoncParser.ParseError[] = [];
283283
const root = jsoncParser.parseTree(content, errors);
@@ -287,7 +287,7 @@ export function getConfigCompletions(
287287

288288
// Get the location at the cursor
289289
const location = jsoncParser.getLocation(content, offset);
290-
290+
291291
// Find the nearest object node that contains the cursor
292292
const currentObjectNode = findContainingObjectNode(root, offset);
293293
if (!currentObjectNode) {
@@ -331,20 +331,29 @@ export function getConfigCompletions(
331331
return item;
332332
});
333333

334-
return completions.length > 0 ? completions : getTopLevelCompletions(schemaInfo);
334+
return completions.length > 0
335+
? completions
336+
: getTopLevelCompletions(schemaInfo);
335337
}
336338

337339
// Helper functions for jsonc-parser based completion
338340

339-
function findContainingObjectNode(node: jsoncParser.Node | undefined, offset: number): jsoncParser.Node | undefined {
341+
function findContainingObjectNode(
342+
node: jsoncParser.Node | undefined,
343+
offset: number,
344+
): jsoncParser.Node | undefined {
340345
if (!node) {
341346
return undefined;
342347
}
343348

344349
let bestMatch: jsoncParser.Node | undefined = undefined;
345350

346351
// If this node is an object and contains the offset, it's a potential match
347-
if (node.type === 'object' && node.offset <= offset && node.offset + node.length >= offset) {
352+
if (
353+
node.type === "object" &&
354+
node.offset <= offset &&
355+
node.offset + node.length >= offset
356+
) {
348357
bestMatch = node;
349358
}
350359

@@ -354,7 +363,10 @@ function findContainingObjectNode(node: jsoncParser.Node | undefined, offset: nu
354363
const result = findContainingObjectNode(child, offset);
355364
if (result) {
356365
// Prefer deeper/more specific matches
357-
if (!bestMatch || (result.offset > bestMatch.offset && result.length < bestMatch.length)) {
366+
if (
367+
!bestMatch ||
368+
(result.offset > bestMatch.offset && result.length < bestMatch.length)
369+
) {
358370
bestMatch = result;
359371
}
360372
}
@@ -364,22 +376,32 @@ function findContainingObjectNode(node: jsoncParser.Node | undefined, offset: nu
364376
return bestMatch;
365377
}
366378

367-
function getPathToNode(root: jsoncParser.Node, targetNode: jsoncParser.Node): string[] | undefined {
368-
function buildPath(node: jsoncParser.Node, currentPath: string[]): string[] | undefined {
379+
function getPathToNode(
380+
root: jsoncParser.Node,
381+
targetNode: jsoncParser.Node,
382+
): string[] | undefined {
383+
function buildPath(
384+
node: jsoncParser.Node,
385+
currentPath: string[],
386+
): string[] | undefined {
369387
if (node === targetNode) {
370388
return currentPath;
371389
}
372390

373391
if (node.children) {
374392
for (const child of node.children) {
375393
let newPath = [...currentPath];
376-
394+
377395
// If this child is a property node, add its key to the path
378-
if (child.type === 'property' && child.children && child.children.length >= 2) {
396+
if (
397+
child.type === "property" &&
398+
child.children &&
399+
child.children.length >= 2
400+
) {
379401
const keyNode = child.children[0];
380-
if (keyNode.type === 'string') {
402+
if (keyNode.type === "string") {
381403
const key = jsoncParser.getNodeValue(keyNode);
382-
if (typeof key === 'string') {
404+
if (typeof key === "string") {
383405
newPath = [...newPath, key];
384406
}
385407
}
@@ -400,14 +422,18 @@ function getPathToNode(root: jsoncParser.Node, targetNode: jsoncParser.Node): st
400422

401423
function getExistingKeys(objectNode: jsoncParser.Node): string[] {
402424
const keys: string[] = [];
403-
404-
if (objectNode.type === 'object' && objectNode.children) {
425+
426+
if (objectNode.type === "object" && objectNode.children) {
405427
for (const child of objectNode.children) {
406-
if (child.type === 'property' && child.children && child.children.length >= 1) {
428+
if (
429+
child.type === "property" &&
430+
child.children &&
431+
child.children.length >= 1
432+
) {
407433
const keyNode = child.children[0];
408-
if (keyNode.type === 'string') {
434+
if (keyNode.type === "string") {
409435
const key = jsoncParser.getNodeValue(keyNode);
410-
if (typeof key === 'string') {
436+
if (typeof key === "string") {
411437
keys.push(key);
412438
}
413439
}
@@ -420,11 +446,11 @@ function getExistingKeys(objectNode: jsoncParser.Node): string[] {
420446

421447
function resolveSchemaForPath(schema: any, path: string[]): any {
422448
let current = schema;
423-
449+
424450
for (const segment of path) {
425451
if (current.properties && current.properties[segment]) {
426452
const prop = current.properties[segment];
427-
453+
428454
// Handle $ref
429455
if (prop.$ref) {
430456
const refPath = prop.$ref.replace("#/", "").split("/");
@@ -445,7 +471,7 @@ function resolveSchemaForPath(schema: any, path: string[]): any {
445471
return null;
446472
}
447473
}
448-
474+
449475
return current;
450476
}
451477

server/src/server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,10 @@ async function handleJsonConfigCompletion(
639639
const params = msg.params as p.CompletionParams;
640640
const content = getOpenedFileContent(params.textDocument.uri);
641641
const document = createJsonTextDocument(params.textDocument.uri, content, 1);
642-
const completions = jsonConfig.getConfigCompletions(document, params.position);
642+
const completions = jsonConfig.getConfigCompletions(
643+
document,
644+
params.position,
645+
);
643646
let response: p.ResponseMessage = {
644647
jsonrpc: c.jsonrpcVersion,
645648
id: msg.id,

0 commit comments

Comments
 (0)