Debian Codegraph @

gopls/internal/protocol/edits.go

raw symbols SCIP
1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package protocol
6
7 import (
8 "fmt"
9 "slices"
10
11 "golang.org/x/tools/internal/diff"
12 )
13
14 // EditsFromDiffEdits converts diff.Edits to a non-nil slice of LSP TextEdits.
15 // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEditArray
16 func EditsFromDiffEdits(m *Mapper, edits []diff.Edit) ([]TextEdit, error) {
17 // LSP doesn't require TextEditArray to be sorted:
18 // this is the receiver's concern. But govim, and perhaps
19 // other clients have historically relied on the order.
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 // EditsToDiffEdits converts LSP TextEdits to diff.Edits.
38 // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEditArray
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 // ApplyEdits applies the patch (edits) to m.Content and returns the result.
59 // It also returns the edits converted to diff-package form.
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 // AsTextEdits converts a slice possibly containing AnnotatedTextEdits
70 // to a slice of TextEdits.
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 // AsAnnotatedTextEdits converts a slice of TextEdits
88 // to a slice of Or_TextDocumentEdit_edits_Elem.
89 // (returning a typed nil is required in server: in code_action.go and command.go))
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 // fileHandle abstracts file.Handle to avoid a cycle.
107 type fileHandle interface {
108 URI() DocumentURI
109 Version() int32
110 }
111
112 // NewWorkspaceEdit constructs a WorkspaceEdit from a list of document changes.
113 //
114 // Any ChangeAnnotations must be added after.
115 func NewWorkspaceEdit(changes ...DocumentChange) *WorkspaceEdit {
116 return &WorkspaceEdit{DocumentChanges: changes}
117 }
118
119 // DocumentChangeEdit constructs a DocumentChange containing a
120 // TextDocumentEdit from a file.Handle and a list of TextEdits.
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 // DocumentChangeCreate constructs a DocumentChange that creates a file.
134 func DocumentChangeCreate(uri DocumentURI) DocumentChange {
135 return DocumentChange{
136 CreateFile: &CreateFile{
137 Kind: "create",
138 URI: uri,
139 },
140 }
141 }
142
143 // DocumentChangeDelete constructs a DocumentChange that deletes a file.
144 func DocumentChangeDelete(uri DocumentURI) DocumentChange {
145 return DocumentChange{
146 DeleteFile: &DeleteFile{
147 Kind: "delete",
148 URI: uri,
149 },
150 }
151 }
152
153 // DocumentChangeRename constructs a DocumentChange that renames a file.
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 // SelectCompletionTextEdit returns insert or replace mode TextEdit
165 // included in the completion item.
166 func SelectCompletionTextEdit(item CompletionItem, useReplaceMode bool) (TextEdit, error) {
167 var edit TextEdit
168 switch typ := item.TextEdit.Value.(type) {
169 case TextEdit: // old style completion item.
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 }