Skip to content

Commit fd6769e

Browse files
committed
Added textutils.ExtractRange function
1 parent 750f0bf commit fd6769e

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

Diff for: handler/textutils/textutils.go

+23-10
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ func ApplyLSPTextDocumentContentChangeEvent(textDoc *lsp.TextDocumentItem, chang
2323

2424
// ApplyTextChange replaces startingText substring specified by replaceRange with insertText
2525
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)
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
104117
type OutOfRangeError struct {
105118
Type string

Diff for: handler/textutils/textutils_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func TestGetOffset(t *testing.T) {
128128
st := strings.Replace(test.Text, "\n", "\\n", -1)
129129

130130
t.Logf("getOffset(\"%s\", {Line: %d, Character: %d}) == %d", st, test.Line, test.Char, test.Exp)
131-
act, err := getOffset(test.Text, lsp.Position{Line: test.Line, Character: test.Char})
131+
act, err := GetOffset(test.Text, lsp.Position{Line: test.Line, Character: test.Char})
132132
if act != test.Exp {
133133
t.Errorf("getOffset(\"%s\", {Line: %d, Character: %d}) != %d, got %d instead", st, test.Line, test.Char, test.Exp, act)
134134
}
@@ -158,7 +158,7 @@ func TestGetLineOffset(t *testing.T) {
158158
st := strings.Replace(test.Text, "\n", "\\n", -1)
159159

160160
t.Logf("getLineOffset(\"%s\", %d) == %d", st, test.Line, test.Exp)
161-
act, err := getLineOffset(test.Text, test.Line)
161+
act, err := GetLineOffset(test.Text, test.Line)
162162
if act != test.Exp {
163163
t.Errorf("getLineOffset(\"%s\", %d) != %d, got %d instead", st, test.Line, test.Exp, act)
164164
}

0 commit comments

Comments
 (0)