@@ -30,7 +30,7 @@ describe('provideDocumentRangeFormattingEdits', () => {
3030 createTextEdit (
3131 selection . start . character - 1 ,
3232 selection . end . character ,
33- ` <div>
33+ `\n <div>
3434 <div>2</div>
3535 </div>` ,
3636 ) ,
@@ -75,6 +75,106 @@ describe('provideDocumentRangeFormattingEdits', () => {
7575 const result = restrictFormattingEditsToRange ( document , selection , edits , createTextEdit ) ;
7676 expect ( applyEdits ( document , result ) ) . toMatchInlineSnapshot ( `"01X23456789"` ) ;
7777 } ) ;
78+
79+ test ( 'handles deletion where newText is shorter than oldText in selection' , ( ) => {
80+ const document = createDocument ( 'ab ' ) ;
81+ const selection = createRange ( 1 , 3 ) ;
82+ const edits = [ createTextEdit ( 0 , 4 , 'ab' ) ] ;
83+ const result = restrictFormattingEditsToRange ( document , selection , edits , createTextEdit ) ;
84+ expect ( applyEdits ( document , result ) ) . toMatchInlineSnapshot ( `"ab "` ) ;
85+ } ) ;
86+
87+ test ( 'handles newText completely exhausted before reaching overlapEnd' , ( ) => {
88+ const document = createDocument ( 'abcdef' ) ;
89+ const selection = createRange ( 1 , 5 ) ; // select "bcde"
90+ const edits = [ createTextEdit ( 0 , 6 , 'ab' ) ] ; // replace all with just "ab"
91+ const result = restrictFormattingEditsToRange ( document , selection , edits , createTextEdit ) ;
92+ expect ( applyEdits ( document , result ) ) . toMatchInlineSnapshot ( `"af"` ) ;
93+ } ) ;
94+
95+ test ( 'handles insertion where newText is longer than oldText' , ( ) => {
96+ const document = createDocument ( 'abc' ) ;
97+ const selection = createRange ( 1 , 2 ) ; // select "b"
98+ const edits = [ createTextEdit ( 0 , 3 , 'aXYZc' ) ] ; // insert XYZ in the middle
99+ const result = restrictFormattingEditsToRange ( document , selection , edits , createTextEdit ) ;
100+ expect ( applyEdits ( document , result ) ) . toMatchInlineSnapshot ( `"aXYZc"` ) ;
101+ } ) ;
102+
103+ test ( 'handles whitespace-only differences' , ( ) => {
104+ const document = createDocument ( 'a b c' ) ;
105+ const selection = createRange ( 1 , 6 ) ; // select " b "
106+ const edits = [ createTextEdit ( 0 , 7 , 'a b c' ) ] ; // normalize spaces
107+ const result = restrictFormattingEditsToRange ( document , selection , edits , createTextEdit ) ;
108+ expect ( applyEdits ( document , result ) ) . toMatchInlineSnapshot ( `"a b c"` ) ;
109+ } ) ;
110+
111+ test ( 'handles edit range completely before selection' , ( ) => {
112+ const document = createDocument ( '0123456789' ) ;
113+ const selection = createRange ( 5 , 8 ) ;
114+ const edits = [ createTextEdit ( 0 , 3 , 'XYZ' ) ] ;
115+ const result = restrictFormattingEditsToRange ( document , selection , edits , createTextEdit ) ;
116+ expect ( applyEdits ( document , result ) ) . toMatchInlineSnapshot ( `"0123456789"` ) ;
117+ } ) ;
118+
119+ test ( 'handles edit range completely after selection' , ( ) => {
120+ const document = createDocument ( '0123456789' ) ;
121+ const selection = createRange ( 2 , 5 ) ;
122+ const edits = [ createTextEdit ( 7 , 10 , 'XYZ' ) ] ;
123+ const result = restrictFormattingEditsToRange ( document , selection , edits , createTextEdit ) ;
124+ expect ( applyEdits ( document , result ) ) . toMatchInlineSnapshot ( `"0123456789"` ) ;
125+ } ) ;
126+
127+ test ( 'handles empty selection' , ( ) => {
128+ const document = createDocument ( '0123456789' ) ;
129+ const selection = createRange ( 5 , 5 ) ; // empty selection at position 5
130+ const edits = [ createTextEdit ( 3 , 7 , 'ABCD' ) ] ;
131+ const result = restrictFormattingEditsToRange ( document , selection , edits , createTextEdit ) ;
132+ expect ( applyEdits ( document , result ) ) . toMatchInlineSnapshot ( `"0123456789"` ) ;
133+ } ) ;
134+
135+ test ( 'handles empty edit (pure insertion)' , ( ) => {
136+ const document = createDocument ( '0123456789' ) ;
137+ const selection = createRange ( 3 , 7 ) ;
138+ const edits = [ createTextEdit ( 5 , 5 , 'XXX' ) ] ; // insert at position 5
139+ const result = restrictFormattingEditsToRange ( document , selection , edits , createTextEdit ) ;
140+ expect ( applyEdits ( document , result ) ) . toMatchInlineSnapshot ( `"01234XXX56789"` ) ;
141+ } ) ;
142+
143+ test ( 'handles multiple edits within selection' , ( ) => {
144+ const document = createDocument ( '0123456789' ) ;
145+ const selection = createRange ( 2 , 8 ) ;
146+ const edits = [
147+ createTextEdit ( 3 , 4 , 'A' ) ,
148+ createTextEdit ( 6 , 7 , 'B' ) ,
149+ ] ;
150+ const result = restrictFormattingEditsToRange ( document , selection , edits , createTextEdit ) ;
151+ expect ( applyEdits ( document , result ) ) . toMatchInlineSnapshot ( `"012A45B789"` ) ;
152+ } ) ;
153+
154+ test ( 'handles edit with mixed whitespace and non-whitespace changes' , ( ) => {
155+ const document = createDocument ( 'a\n\tb\n\tc' ) ;
156+ const selection = createRange ( 1 , 5 ) ; // select "\n\tb\n"
157+ const edits = [ createTextEdit ( 0 , 7 , 'a b c' ) ] ; // normalize all whitespace
158+ const result = restrictFormattingEditsToRange ( document , selection , edits , createTextEdit ) ;
159+ expect ( applyEdits ( document , result ) ) . toMatchInlineSnapshot ( `"a b c"` ) ;
160+ } ) ;
161+
162+ test ( 'handles non-ASCII characters' , ( ) => {
163+ const document = createDocument ( '你好世界' ) ;
164+ const selection = createRange ( 1 , 3 ) ; // select "好世"
165+ const edits = [ createTextEdit ( 0 , 4 , '你好朋友' ) ] ;
166+ const result = restrictFormattingEditsToRange ( document , selection , edits , createTextEdit ) ;
167+ expect ( applyEdits ( document , result ) ) . toMatchInlineSnapshot ( `"你好朋界"` ) ;
168+ } ) ;
169+
170+ test ( 'handles overlapStart equals overlapEnd' , ( ) => {
171+ // When edit and selection don't actually overlap in content
172+ const document = createDocument ( '0123456789' ) ;
173+ const selection = createRange ( 5 , 5 ) ;
174+ const edits = [ createTextEdit ( 3 , 7 , 'WXYZ' ) ] ;
175+ const result = restrictFormattingEditsToRange ( document , selection , edits , createTextEdit ) ;
176+ expect ( applyEdits ( document , result ) ) . toMatchInlineSnapshot ( `"0123456789"` ) ;
177+ } ) ;
78178} ) ;
79179
80180// self implementation of vscode test utils
0 commit comments