MCPcopy Index your code
hub / github.com/bytebase/bytebase / applyContentChanges

Function applyContentChanges

backend/api/lsp/fs.go:46–80  ·  view source on GitHub ↗
(uri lsp.DocumentURI, content []byte, changes []lsp.TextDocumentContentChangeEvent)

Source from the content-addressed store, hash-verified

44}
45
46func applyContentChanges(uri lsp.DocumentURI, content []byte, changes []lsp.TextDocumentContentChangeEvent) ([]byte, error) {
47 for _, change := range changes {
48 if change.Range == nil && change.RangeLength == 0 {
49 content = []byte(change.Text) // new full content
50 continue
51 }
52 start, err := offsetForPosition(content, change.Range.Start)
53 if err != nil {
54 return nil, errors.Wrapf(err, "received textDocument/didChange for invalid position %v on %q", change.Range.Start, uri)
55 }
56
57 end, err := offsetForPosition(content, change.Range.End)
58 if err != nil {
59 return nil, errors.Wrapf(err, "received textDocument/didChange for invalid position %v on %q", change.Range.End, uri)
60 }
61 if start < 0 || end > len(content) || start > end {
62 return nil, errors.Errorf("received textDocument/didChange for out of range position %v on %q", change.Range, uri)
63 }
64
65 // Try avoid doing too many allocations, so use bytes.Buffer
66 b := &bytes.Buffer{}
67 b.Grow(start + len(change.Text) + len(content) - end)
68 if _, err := b.Write(content[:start]); err != nil {
69 return nil, err
70 }
71 if _, err := b.WriteString(change.Text); err != nil {
72 return nil, err
73 }
74 if _, err := b.Write(content[end:]); err != nil {
75 return nil, err
76 }
77 content = b.Bytes()
78 }
79 return content, nil
80}
81
82// DidClose notifies the file system that a file was closed.
83func (fs *MemFS) DidClose(params *lsp.DidCloseTextDocumentParams) {

Callers 1

DidChangeMethod · 0.85

Calls 2

offsetForPositionFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected