@@ -23,23 +23,23 @@ func ApplyLSPTextDocumentContentChangeEvent(textDoc *lsp.TextDocumentItem, chang
23
23
24
24
// ApplyTextChange replaces startingText substring specified by replaceRange with insertText
25
25
func ApplyTextChange (startingText string , replaceRange lsp.Range , insertText string ) (res string , err error ) {
26
- start , err := getOffset (startingText , replaceRange .Start )
26
+ start , err := GetOffset (startingText , replaceRange .Start )
27
27
if err != nil {
28
28
return "" , err
29
29
}
30
- end , err := getOffset (startingText , replaceRange .End )
30
+ end , err := GetOffset (startingText , replaceRange .End )
31
31
if err != nil {
32
32
return "" , err
33
33
}
34
34
35
35
return startingText [:start ] + insertText + startingText [end :], nil
36
36
}
37
37
38
- // getOffset computes the offset in the text expressed by the lsp.Position.
38
+ // GetOffset computes the offset in the text expressed by the lsp.Position.
39
39
// Returns OutOfRangeError if the position is out of range.
40
- func getOffset (text string , pos lsp.Position ) (int , error ) {
40
+ func GetOffset (text string , pos lsp.Position ) (int , error ) {
41
41
// Find line
42
- lineOffset , err := getLineOffset (text , pos .Line )
42
+ lineOffset , err := GetLineOffset (text , pos .Line )
43
43
if err != nil {
44
44
return - 1 , err
45
45
}
@@ -74,13 +74,13 @@ func getOffset(text string, pos lsp.Position) (int, error) {
74
74
return - 1 , OutOfRangeError {"Character" , count , character }
75
75
}
76
76
77
- // getLineOffset finds the offset/position of the beginning of a line within the text.
77
+ // GetLineOffset finds the offset/position of the beginning of a line within the text.
78
78
// For example:
79
79
// text := "foo\nfoobar\nbaz"
80
- // getLineOffset (text, 0) == 0
81
- // getLineOffset (text, 1) == 4
82
- // getLineOffset (text, 2) == 11
83
- func getLineOffset (text string , line int ) (int , error ) {
80
+ // GetLineOffset (text, 0) == 0
81
+ // GetLineOffset (text, 1) == 4
82
+ // GetLineOffset (text, 2) == 11
83
+ func GetLineOffset (text string , line int ) (int , error ) {
84
84
if line == 0 {
85
85
return 0 , nil
86
86
}
@@ -100,6 +100,19 @@ func getLineOffset(text string, line int) (int, error) {
100
100
return - 1 , OutOfRangeError {"Line" , count , line }
101
101
}
102
102
103
+ // ExtractRange extract a piece of text from a text document given the range
104
+ func ExtractRange (text string , textRange lsp.Range ) (string , error ) {
105
+ start , err := GetOffset (text , textRange .Start )
106
+ if err != nil {
107
+ return "" , err
108
+ }
109
+ end , err := GetOffset (text , textRange .End )
110
+ if err != nil {
111
+ return "" , err
112
+ }
113
+ return text [start :end ], nil
114
+ }
115
+
103
116
// OutOfRangeError returned if one attempts to access text out of its range
104
117
type OutOfRangeError struct {
105
118
Type string
0 commit comments