@@ -23,23 +23,23 @@ func ApplyLSPTextDocumentContentChangeEvent(textDoc *lsp.TextDocumentItem, chang
2323
2424// ApplyTextChange replaces startingText substring specified by replaceRange with insertText
2525func 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 )
2727 if err != nil {
2828 return "" , err
2929 }
30- end , err := getOffset (startingText , replaceRange .End )
30+ end , err := GetOffset (startingText , replaceRange .End )
3131 if err != nil {
3232 return "" , err
3333 }
3434
3535 return startingText [:start ] + insertText + startingText [end :], nil
3636}
3737
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.
3939// 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 ) {
4141 // Find line
42- lineOffset , err := getLineOffset (text , pos .Line )
42+ lineOffset , err := GetLineOffset (text , pos .Line )
4343 if err != nil {
4444 return - 1 , err
4545 }
@@ -74,13 +74,13 @@ func getOffset(text string, pos lsp.Position) (int, error) {
7474 return - 1 , OutOfRangeError {"Character" , count , character }
7575}
7676
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.
7878// For example:
7979// 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 ) {
8484 if line == 0 {
8585 return 0 , nil
8686 }
@@ -100,6 +100,19 @@ func getLineOffset(text string, line int) (int, error) {
100100 return - 1 , OutOfRangeError {"Line" , count , line }
101101}
102102
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+
103116// OutOfRangeError returned if one attempts to access text out of its range
104117type OutOfRangeError struct {
105118 Type string
0 commit comments