vendor tsgo
This commit is contained in:
30
tools/tsgo/internal/core/textchange.go
Normal file
30
tools/tsgo/internal/core/textchange.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package core
|
||||
|
||||
import "strings"
|
||||
|
||||
type TextChange struct {
|
||||
TextRange
|
||||
NewText string
|
||||
}
|
||||
|
||||
func (t TextChange) ApplyTo(text string) string {
|
||||
return text[:t.Pos()] + t.NewText + text[t.End():]
|
||||
}
|
||||
|
||||
func ApplyBulkEdits(text string, edits []TextChange) string {
|
||||
b := strings.Builder{}
|
||||
b.Grow(len(text))
|
||||
lastEnd := 0
|
||||
for _, e := range edits {
|
||||
start := e.TextRange.Pos()
|
||||
if start != lastEnd {
|
||||
b.WriteString(text[lastEnd:e.TextRange.Pos()])
|
||||
}
|
||||
b.WriteString(e.NewText)
|
||||
|
||||
lastEnd = e.TextRange.End()
|
||||
}
|
||||
b.WriteString(text[lastEnd:])
|
||||
|
||||
return b.String()
|
||||
}
|
||||
Reference in New Issue
Block a user