(uri lsp.DocumentURI, content []byte, changes []lsp.TextDocumentContentChangeEvent)
| 44 | } |
| 45 | |
| 46 | func 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. |
| 83 | func (fs *MemFS) DidClose(params *lsp.DidCloseTextDocumentParams) { |
no test coverage detected