@@ -62,20 +62,26 @@ define(function (require, exports, module) {
6262 // this is the actual source code for the element that we need to duplicate
6363 const text = editor . getTextBetween ( range . from , range . to ) ;
6464 // this is the indentation on the line
65- const indent = editor . getTextBetween ( { line : range . from . line , ch : 0 } , range . from ) ;
66-
67- // this is the position where we need to insert
68- // we're giving the char as 0 because since we insert a new line using '\n'
69- // that's why writing any char value will not work, as the line is empty
70- // and codemirror doesn't allow to insert at a column (ch) greater than the length of the line
71- // So, the logic is to just append the indent before the text at this insertPos
72- const insertPos = {
73- line : range . from . line + ( range . to . line - range . from . line + 1 ) ,
74- ch : 0
75- } ;
76-
77- editor . replaceRange ( '\n' , range . to ) ;
78- editor . replaceRange ( indent + text , insertPos ) ;
65+ const indent = editor . getTextBetween ( { line : range . from . line , ch : 0 } , range . from ) ;
66+
67+ // make sure there is only indentation and no text before it
68+ if ( indent . trim ( ) === "" ) {
69+ // this is the position where we need to insert
70+ // we're giving the char as 0 because since we insert a new line using '\n'
71+ // that's why writing any char value will not work, as the line is empty
72+ // and codemirror doesn't allow to insert at a column (ch) greater than the length of the line
73+ // So, the logic is to just append the indent before the text at this insertPos
74+ const insertPos = {
75+ line : range . from . line + ( range . to . line - range . from . line + 1 ) ,
76+ ch : 0
77+ } ;
78+
79+ editor . replaceRange ( "\n" , range . to ) ;
80+ editor . replaceRange ( indent + text , insertPos ) ;
81+ } else {
82+ // if there is some text, we just add the duplicated text right next to it
83+ editor . replaceRange ( text , range . from ) ;
84+ }
7985 }
8086
8187 /**
0 commit comments