@@ -49,7 +49,7 @@ export class ExtractSubSchemaToDefs {
49
49
return [ ] ;
50
50
}
51
51
52
- const dialectUri = /** @type {string } */ ( node . root . dialectUri ) ;
52
+ const dialectUri = /** @type {string } */ ( node . root . dialectUri ) ;
53
53
const definitionsKeyword = getKeywordName ( dialectUri , "https://json-schema.org/keyword/definitions" ) ;
54
54
55
55
const definitionsNode = SchemaNode . step ( definitionsKeyword , node . root ) ;
@@ -69,7 +69,10 @@ export class ExtractSubSchemaToDefs {
69
69
const newDefName = `def${ highestDefNumber + 1 } ` ;
70
70
const extractedDef = schemaDocument . textDocument . getText ( range ) ;
71
71
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 ;
73
76
/** @type {CodeAction } */
74
77
const codeAction = {
75
78
title : `Extract '${ newDefName } ' to ${ definitionsKeyword } ` ,
@@ -84,10 +87,10 @@ export class ExtractSubSchemaToDefs {
84
87
definitionsNode
85
88
? withFormatting ( schemaDocument . textDocument , {
86
89
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 )
89
92
} ,
90
- newText : ` \n"${ newDefName } ": ${ extractedDef } , `
93
+ newText : lastDefinition ? `, \n"${ newDefName } ": ${ extractedDef } ` : `\n" ${ newDefName } ": ${ extractedDef } `
91
94
} , settings )
92
95
: withFormatting ( schemaDocument . textDocument , {
93
96
range : {
0 commit comments