|
4 | 4 |
|
5 | 5 | package protocol
|
6 | 6 |
|
7 |
| -import "golang.org/x/tools/internal/diff" |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "golang.org/x/tools/internal/diff" |
| 11 | +) |
8 | 12 |
|
9 | 13 | // EditsFromDiffEdits converts diff.Edits to a non-nil slice of LSP TextEdits.
|
10 | 14 | // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textEditArray
|
@@ -60,3 +64,40 @@ func ApplyEdits(m *Mapper, edits []TextEdit) ([]byte, []diff.Edit, error) {
|
60 | 64 | out, err := diff.ApplyBytes(m.Content, diffEdits)
|
61 | 65 | return out, diffEdits, err
|
62 | 66 | }
|
| 67 | + |
| 68 | +// AsTextEdits converts a slice possibly containing AnnotatedTextEdits |
| 69 | +// to a slice of TextEdits. |
| 70 | +func AsTextEdits(edits []Or_TextDocumentEdit_edits_Elem) []TextEdit { |
| 71 | + var result []TextEdit |
| 72 | + for _, e := range edits { |
| 73 | + var te TextEdit |
| 74 | + if x, ok := e.Value.(AnnotatedTextEdit); ok { |
| 75 | + te = x.TextEdit |
| 76 | + } else if x, ok := e.Value.(TextEdit); ok { |
| 77 | + te = x |
| 78 | + } else { |
| 79 | + panic(fmt.Sprintf("unexpected type %T, expected AnnotatedTextEdit or TextEdit", e.Value)) |
| 80 | + } |
| 81 | + result = append(result, te) |
| 82 | + } |
| 83 | + return result |
| 84 | +} |
| 85 | + |
| 86 | +// AsAnnotatedTextEdits converts a slice of TextEdits |
| 87 | +// to a slice of Or_TextDocumentEdit_edits_Elem. |
| 88 | +// (returning a typed nil is required in server: in code_action.go and command.go)) |
| 89 | +func AsAnnotatedTextEdits(edits []TextEdit) []Or_TextDocumentEdit_edits_Elem { |
| 90 | + if edits == nil { |
| 91 | + return []Or_TextDocumentEdit_edits_Elem{} |
| 92 | + } |
| 93 | + var result []Or_TextDocumentEdit_edits_Elem |
| 94 | + for _, e := range edits { |
| 95 | + result = append(result, Or_TextDocumentEdit_edits_Elem{ |
| 96 | + Value: TextEdit{ |
| 97 | + Range: e.Range, |
| 98 | + NewText: e.NewText, |
| 99 | + }, |
| 100 | + }) |
| 101 | + } |
| 102 | + return result |
| 103 | +} |
0 commit comments