@@ -3,6 +3,7 @@ package format_test
3
3
import (
4
4
"os"
5
5
"path/filepath"
6
+ "strings"
6
7
"testing"
7
8
8
9
"github.com/microsoft/typescript-go/internal/ast"
@@ -15,6 +16,24 @@ import (
15
16
"gotest.tools/v3/assert"
16
17
)
17
18
19
+ func applyBulkEdits (text string , edits []core.TextChange ) string {
20
+ b := strings.Builder {}
21
+ b .Grow (len (text ))
22
+ lastEnd := 0
23
+ for _ , e := range edits {
24
+ start := e .TextRange .Pos ()
25
+ if start != lastEnd {
26
+ b .WriteString (text [lastEnd :e .TextRange .Pos ()])
27
+ }
28
+ b .WriteString (e .NewText )
29
+
30
+ lastEnd = e .TextRange .End ()
31
+ }
32
+ b .WriteString (text [lastEnd :])
33
+
34
+ return b .String ()
35
+ }
36
+
18
37
func TestFormat (t * testing.T ) {
19
38
t .Parallel ()
20
39
@@ -46,11 +65,7 @@ func TestFormat(t *testing.T) {
46
65
)
47
66
ast .SetParentInChildren (sourceFile .AsNode ())
48
67
edits := format .FormatDocument (ctx , sourceFile )
49
- newText := text
50
- for i := len (edits ) - 1 ; i >= 0 ; i -- { // iterate edits back to front so no spans need to be adjusted
51
- e := edits [i ]
52
- newText = e .ApplyTo (newText )
53
- }
68
+ newText := applyBulkEdits (text , edits )
54
69
assert .Assert (t , len (newText ) > 0 )
55
70
assert .Assert (t , text != newText )
56
71
})
@@ -86,11 +101,7 @@ func BenchmarkFormat(b *testing.B) {
86
101
b .Run ("format checker.ts" , func (b * testing.B ) {
87
102
for b .Loop () {
88
103
edits := format .FormatDocument (ctx , sourceFile )
89
- newText := text
90
- for i := len (edits ) - 1 ; i >= 0 ; i -- { // iterate edits back to front so no spans need to be adjusted
91
- e := edits [i ]
92
- newText = e .ApplyTo (newText )
93
- }
104
+ newText := applyBulkEdits (text , edits )
94
105
assert .Assert (b , len (newText ) > 0 )
95
106
}
96
107
})
0 commit comments