@@ -140,6 +140,58 @@ define(function (require, exports, module) {
140140 return tabStops . length > 0 ;
141141 }
142142
143+ /**
144+ * this function is responsible to calculate the indentation level for the current line
145+ *
146+ * @param {Editor } editor - the editor instance
147+ * @param {Object } position - position object with line number
148+ * @returns {String } - the indentation string
149+ */
150+ function getLineIndentation ( editor , position ) {
151+ const line = editor . document . getLine ( position . line ) ;
152+ const match = line . match ( / ^ \s * / ) ;
153+ return match ? match [ 0 ] : '' ;
154+ }
155+
156+ /**
157+ * this function is to add proper indentation to multiline snippet text
158+ *
159+ * @param {String } templateText - the template text with multiple lines
160+ * @param {String } baseIndent - the base indentation string from the current cursor position
161+ * @returns {String } - properly indented text
162+ */
163+ function addIndentationToSnippet ( templateText , baseIndent ) {
164+ const lines = templateText . split ( / ( \r \n | \n ) / g) ;
165+
166+ let result = '' ;
167+ let isFirstLine = true ;
168+
169+ for ( let i = 0 ; i < lines . length ; i ++ ) {
170+ const line = lines [ i ] ;
171+
172+ if ( line === '\n' || line === '\r\n' ) {
173+ result += line ;
174+ continue ;
175+ }
176+
177+ if ( line . trim ( ) === '' ) {
178+ result += line ;
179+ continue ;
180+ }
181+
182+ // we don't want to indent the first line as it inherits the current indent
183+ if ( isFirstLine ) {
184+ result += line ;
185+ isFirstLine = false ;
186+ } else {
187+ // add base indent plus the existing indent in the template text
188+ result += baseIndent + line ;
189+ }
190+ }
191+
192+ return result ;
193+ }
194+
143195 /**
144196 * Insert snippet with tab stops and start navigation session
145197 * this is the main function that handles snippet insertion with cursor positioning
@@ -152,10 +204,16 @@ define(function (require, exports, module) {
152204 function insertSnippetWithTabStops ( editor , templateText , startPos , endPos ) {
153205 const parsed = parseTemplateText ( templateText ) ;
154206
155- editor . document . replaceRange ( parsed . text , startPos , endPos ) ;
207+ // Get the current line's indentation to apply to all subsequent lines
208+ const baseIndent = getLineIndentation ( editor , startPos ) ;
209+
210+ // Apply proper indentation to the snippet text for multi-line snippets
211+ const indentedText = addIndentationToSnippet ( parsed . text , baseIndent ) ;
212+
213+ editor . document . replaceRange ( indentedText , startPos , endPos ) ;
156214
157215 // calculate snippet bounds
158- const lines = parsed . text . split ( "\n" ) ;
216+ const lines = indentedText . split ( "\n" ) ;
159217 const startLine = startPos . line ;
160218 const endLine = startPos . line + lines . length - 1 ;
161219
0 commit comments