| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package protocol |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"slices" |
| 10 |
|
| 11 |
"golang.org/x/tools/internal/diff" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
func EditsFromDiffEdits(m *Mapper, edits []diff.Edit) ([]TextEdit, error) { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
edits = slices.Clone(edits) |
| 21 |
diff.SortEdits(edits) |
| 22 |
|
| 23 |
result := make([]TextEdit, len(edits)) |
| 24 |
for i, edit := range edits { |
| 25 |
rng, err := m.OffsetRange(edit.Start, edit.End) |
| 26 |
if err != nil { |
| 27 |
return nil, err |
| 28 |
} |
| 29 |
result[i] = TextEdit{ |
| 30 |
Range: rng, |
| 31 |
NewText: edit.New, |
| 32 |
} |
| 33 |
} |
| 34 |
return result, nil |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
func EditsToDiffEdits(m *Mapper, edits []TextEdit) ([]diff.Edit, error) { |
| 40 |
if edits == nil { |
| 41 |
return nil, nil |
| 42 |
} |
| 43 |
result := make([]diff.Edit, len(edits)) |
| 44 |
for i, edit := range edits { |
| 45 |
start, end, err := m.RangeOffsets(edit.Range) |
| 46 |
if err != nil { |
| 47 |
return nil, err |
| 48 |
} |
| 49 |
result[i] = diff.Edit{ |
| 50 |
Start: start, |
| 51 |
End: end, |
| 52 |
New: edit.NewText, |
| 53 |
} |
| 54 |
} |
| 55 |
return result, nil |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
func ApplyEdits(m *Mapper, edits []TextEdit) ([]byte, []diff.Edit, error) { |
| 61 |
diffEdits, err := EditsToDiffEdits(m, edits) |
| 62 |
if err != nil { |
| 63 |
return nil, nil, err |
| 64 |
} |
| 65 |
out, err := diff.ApplyBytes(m.Content, diffEdits) |
| 66 |
return out, diffEdits, err |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
func AsTextEdits(edits []Or_TextDocumentEdit_edits_Elem) []TextEdit { |
| 72 |
var result []TextEdit |
| 73 |
for _, e := range edits { |
| 74 |
var te TextEdit |
| 75 |
if x, ok := e.Value.(AnnotatedTextEdit); ok { |
| 76 |
te = x.TextEdit |
| 77 |
} else if x, ok := e.Value.(TextEdit); ok { |
| 78 |
te = x |
| 79 |
} else { |
| 80 |
panic(fmt.Sprintf("unexpected type %T, expected AnnotatedTextEdit or TextEdit", e.Value)) |
| 81 |
} |
| 82 |
result = append(result, te) |
| 83 |
} |
| 84 |
return result |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
func AsAnnotatedTextEdits(edits []TextEdit) []Or_TextDocumentEdit_edits_Elem { |
| 91 |
if edits == nil { |
| 92 |
return []Or_TextDocumentEdit_edits_Elem{} |
| 93 |
} |
| 94 |
var result []Or_TextDocumentEdit_edits_Elem |
| 95 |
for _, e := range edits { |
| 96 |
result = append(result, Or_TextDocumentEdit_edits_Elem{ |
| 97 |
Value: TextEdit{ |
| 98 |
Range: e.Range, |
| 99 |
NewText: e.NewText, |
| 100 |
}, |
| 101 |
}) |
| 102 |
} |
| 103 |
return result |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
type fileHandle interface { |
| 108 |
URI() DocumentURI |
| 109 |
Version() int32 |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
func NewWorkspaceEdit(changes ...DocumentChange) *WorkspaceEdit { |
| 116 |
return &WorkspaceEdit{DocumentChanges: changes} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
func DocumentChangeEdit(fh fileHandle, textedits []TextEdit) DocumentChange { |
| 122 |
return DocumentChange{ |
| 123 |
TextDocumentEdit: &TextDocumentEdit{ |
| 124 |
TextDocument: OptionalVersionedTextDocumentIdentifier{ |
| 125 |
Version: fh.Version(), |
| 126 |
TextDocumentIdentifier: TextDocumentIdentifier{URI: fh.URI()}, |
| 127 |
}, |
| 128 |
Edits: AsAnnotatedTextEdits(textedits), |
| 129 |
}, |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
func DocumentChangeCreate(uri DocumentURI) DocumentChange { |
| 135 |
return DocumentChange{ |
| 136 |
CreateFile: &CreateFile{ |
| 137 |
Kind: "create", |
| 138 |
URI: uri, |
| 139 |
}, |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
func DocumentChangeDelete(uri DocumentURI) DocumentChange { |
| 145 |
return DocumentChange{ |
| 146 |
DeleteFile: &DeleteFile{ |
| 147 |
Kind: "delete", |
| 148 |
URI: uri, |
| 149 |
}, |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
func DocumentChangeRename(src, dst DocumentURI) DocumentChange { |
| 155 |
return DocumentChange{ |
| 156 |
RenameFile: &RenameFile{ |
| 157 |
Kind: "rename", |
| 158 |
OldURI: src, |
| 159 |
NewURI: dst, |
| 160 |
}, |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
func SelectCompletionTextEdit(item CompletionItem, useReplaceMode bool) (TextEdit, error) { |
| 167 |
var edit TextEdit |
| 168 |
switch typ := item.TextEdit.Value.(type) { |
| 169 |
case TextEdit: |
| 170 |
return typ, nil |
| 171 |
case InsertReplaceEdit: |
| 172 |
if useReplaceMode { |
| 173 |
return TextEdit{ |
| 174 |
NewText: typ.NewText, |
| 175 |
Range: typ.Replace, |
| 176 |
}, nil |
| 177 |
} else { |
| 178 |
return TextEdit{ |
| 179 |
NewText: typ.NewText, |
| 180 |
Range: typ.Insert, |
| 181 |
}, nil |
| 182 |
} |
| 183 |
default: |
| 184 |
return edit, fmt.Errorf("unsupported edit type %T", typ) |
| 185 |
} |
| 186 |
} |