Skip to content

Commit 76baef3

Browse files
committed
definition to get added to the bottom of
1 parent 0ec64fa commit 76baef3

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

language-server/src/features/codeAction/extractSubschema.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class ExtractSubSchemaToDefs {
4949
return [];
5050
}
5151

52-
const dialectUri = /** @type {string} */ (node.root.dialectUri);
52+
const dialectUri = /** @type {string} */(node.root.dialectUri);
5353
const definitionsKeyword = getKeywordName(dialectUri, "https://json-schema.org/keyword/definitions");
5454

5555
const definitionsNode = SchemaNode.step(definitionsKeyword, node.root);
@@ -69,7 +69,10 @@ export class ExtractSubSchemaToDefs {
6969
const newDefName = `def${highestDefNumber + 1}`;
7070
const extractedDef = schemaDocument.textDocument.getText(range);
7171
const settings = await this.configuration.get();
72-
72+
const lastDefinition = definitionsNode?.children.at(-1);
73+
const lastDefinitionPosition = (lastDefinition?.offset && lastDefinition?.textLength)
74+
? lastDefinition.offset + lastDefinition.textLength
75+
: /** @type {number} */ (definitionsNode?.offset) + 1;
7376
/** @type {CodeAction} */
7477
const codeAction = {
7578
title: `Extract '${newDefName}' to ${definitionsKeyword}`,
@@ -84,10 +87,10 @@ export class ExtractSubSchemaToDefs {
8487
definitionsNode
8588
? withFormatting(schemaDocument.textDocument, {
8689
range: {
87-
start: schemaDocument.textDocument.positionAt(definitionsNode.offset + 1),
88-
end: schemaDocument.textDocument.positionAt(definitionsNode.offset + 1)
90+
start: schemaDocument.textDocument.positionAt(lastDefinitionPosition),
91+
end: schemaDocument.textDocument.positionAt(lastDefinitionPosition)
8992
},
90-
newText: `\n"${newDefName}": ${extractedDef},`
93+
newText: lastDefinition ? `,\n"${newDefName}": ${extractedDef}` : `\n"${newDefName}": ${extractedDef}`
9194
}, settings)
9295
: withFormatting(schemaDocument.textDocument, {
9396
range: {

0 commit comments

Comments
 (0)