-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Syuparn/enable-cursor
enable to use cursor
- Loading branch information
Showing
6 changed files
with
232 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/peterh/liner" | ||
) | ||
|
||
func newWordCompleter() liner.WordCompleter { | ||
return func(line string, pos int) (head string, completions []string, tail string) { | ||
head, partial, tail := splitLine(line, pos) | ||
|
||
if partial == "" { | ||
// match to nothing | ||
return | ||
} | ||
|
||
// function auto-completes | ||
for name := range funcMap() { | ||
if strings.HasPrefix(name, partial) { | ||
completions = append(completions, name) | ||
} | ||
} | ||
return | ||
} | ||
} | ||
|
||
func splitLine(line string, pos int) (head, partial, tail string) { | ||
startPos := partialStartPos(line, pos, "{}()<>=| ") | ||
endPos := partialEndPos(line, pos, "{}()<>=| ") | ||
|
||
head = subStr(line, 0, startPos) | ||
tail = subStr(line, endPos, len(line)) | ||
partial = subStr(line, startPos, endPos) | ||
return | ||
} | ||
|
||
func partialStartPos(line string, pos int, delims string) int { | ||
delimPos := strings.LastIndexAny(subStr(line, 0, pos), delims) | ||
if delimPos < 0 { | ||
return 0 | ||
} | ||
return delimPos + 1 | ||
} | ||
|
||
func partialEndPos(line string, pos int, delims string) int { | ||
relativeDelimPos := strings.IndexAny(subStr(line, pos, len(line)), delims) | ||
if relativeDelimPos < 0 { | ||
return len(line) | ||
} | ||
return pos + relativeDelimPos | ||
} | ||
|
||
func subStr(str string, start, end int) string { | ||
if start >= len(str) { | ||
return "" | ||
} | ||
if end <= 0 { | ||
return "" | ||
} | ||
|
||
if end > len(str) { | ||
end = len(str) | ||
} | ||
if start < 0 { | ||
start = 0 | ||
} | ||
|
||
return str[start:end] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewCompleter(t *testing.T) { | ||
c := newWordCompleter() | ||
|
||
type completion struct { | ||
head string | ||
completions []string | ||
tail string | ||
} | ||
|
||
tests := []struct { | ||
title string | ||
line string | ||
pos int | ||
expected completion | ||
}{ | ||
{ | ||
"empty", | ||
"", | ||
0, | ||
completion{ | ||
head: "", | ||
completions: []string{}, | ||
tail: "", | ||
}, | ||
}, | ||
{ | ||
"only partials", | ||
"low", | ||
3, | ||
completion{ | ||
head: "", | ||
completions: []string{"lower"}, | ||
tail: "", | ||
}, | ||
}, | ||
{ | ||
"cursor is not at the end", | ||
"low", | ||
1, | ||
completion{ | ||
head: "", | ||
completions: []string{"lower"}, | ||
tail: "", | ||
}, | ||
}, | ||
{ | ||
"two tokens", | ||
"if low", | ||
6, | ||
completion{ | ||
head: "if ", | ||
completions: []string{"lower"}, | ||
tail: "", | ||
}, | ||
}, | ||
{ | ||
"two tokens and cursor in the middle", | ||
"if low", | ||
3, | ||
completion{ | ||
head: "if ", | ||
completions: []string{"lower"}, | ||
tail: "", | ||
}, | ||
}, | ||
{ | ||
"cursor points non-function", | ||
"if low", | ||
0, | ||
completion{ | ||
head: "", | ||
completions: []string{}, | ||
tail: " low", | ||
}, | ||
}, | ||
{ | ||
"many tokens (cursor at the end)", | ||
"1 | add int6", | ||
12, | ||
completion{ | ||
head: "1 | add ", | ||
completions: []string{"int64"}, | ||
tail: "", | ||
}, | ||
}, | ||
{ | ||
"many tokens (cursor in the middle)", | ||
"1 | ad int64", | ||
4, | ||
completion{ | ||
head: "1 | ", | ||
completions: []string{"add", "add1f", "add1", "addf", "adler32sum"}, | ||
tail: " int64", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.title, func(t *testing.T) { | ||
head, completions, tail := c(tt.line, tt.pos) | ||
|
||
assert.Equal(t, tt.expected.head, head, "wrong head") | ||
assert.ElementsMatch(t, tt.expected.completions, completions, "wrong completions") | ||
assert.Equal(t, tt.expected.tail, tail, "wrong tail") | ||
}) | ||
} | ||
} | ||
|
||
func allFuncNames() []string { | ||
names := []string{} | ||
for name := range funcMap() { | ||
names = append(names, name) | ||
} | ||
return names | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters