Skip to content

Commit

Permalink
go-prompt: fix linter
Browse files Browse the repository at this point in the history
  • Loading branch information
askalt committed Jul 17, 2023
1 parent 5d72c86 commit e80af60
Show file tree
Hide file tree
Showing 19 changed files with 187 additions and 90 deletions.
18 changes: 12 additions & 6 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func (b *Buffer) Document() (d *Document) {
}

// DisplayCursorPosition returns the cursor position on rendered text on terminal emulators.
// So if Document is "日本(cursor)語", DisplayedCursorPosition returns 4 because '日' and '本' are double width characters.
// So if Document is "日本(cursor)語", DisplayedCursorPosition returns 4 because '日' and '本' are
// double width characters.
func (b *Buffer) DisplayCursorPosition() int {
return b.Document().DisplayCursorPosition()
}
Expand Down Expand Up @@ -71,7 +72,8 @@ func (b *Buffer) InsertText(v string, overwrite bool, moveCursor bool) {
// (When doing this, make sure that the cursor_position is valid for this text.
// text/cursor_position should be consistent at any time, otherwise set a Document instead.)
func (b *Buffer) setText(v string) {
debug.Assert(b.cursorPosition <= len([]rune(v)), "length of input should be shorter than cursor position")
debug.Assert(b.cursorPosition <= len([]rune(v)),
"length of input should be shorter than cursor position")
// replace CR with LF
v = strings.ReplaceAll(v, "\r", "\n")
b.workingLines[b.workingIndex] = v
Expand All @@ -88,7 +90,8 @@ func (b *Buffer) setCursorPosition(p int) {

func (b *Buffer) setDocument(d *Document) {
b.cacheDocument = d
b.setCursorPosition(d.cursorPosition) // Call before setText because setText check the relation between cursorPosition and line length.
b.setCursorPosition(d.cursorPosition) // Call before setText because setText check the relation
// between cursorPosition and line length.
b.setText(d.Text)
}

Expand Down Expand Up @@ -132,7 +135,8 @@ func (b *Buffer) CursorDown(count int) {
b.preferredColumn = orig
}

// DeleteBeforeCursor delete specified number of characters before cursor and return the deleted text.
// DeleteBeforeCursor delete specified number of characters before cursor and return the deleted
// text.
func (b *Buffer) DeleteBeforeCursor(count int) (deleted string) {
debug.Assert(count >= 0, "count should be positive")
r := []rune(b.Text())
Expand Down Expand Up @@ -170,13 +174,15 @@ func (b *Buffer) Delete(count int) (deleted string) {
return
}

// JoinNextLine joins the next line to the current one by deleting the line ending after the current line.
// JoinNextLine joins the next line to the current one by deleting the line ending after the
// current line.
func (b *Buffer) JoinNextLine(separator string) {
if !b.Document().OnLastLine() {
b.cursorPosition += b.Document().GetEndOfLinePosition()
b.Delete(1)
// Remove spaces
b.setText(b.Document().TextBeforeCursor() + separator + strings.TrimLeft(b.Document().TextAfterCursor(), " "))
b.setText(b.Document().TextBeforeCursor() + separator + strings.TrimLeft(b.Document().
TextAfterCursor(), " "))
}
}

Expand Down
3 changes: 2 additions & 1 deletion completer/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var (
)

// FilePathCompleter is a completer for your local file system.
// Please caution that you need to set OptionCompletionWordSeparator(completer.FilePathCompletionSeparator)
// Please caution that you need to set OptionCompletionWordSeparator(completer.
// FilePathCompletionSeparator)
// when you use this completer.
type FilePathCompleter struct {
Filter func(fi os.FileInfo) bool
Expand Down
97 changes: 76 additions & 21 deletions completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,38 @@ func TestFormatShortSuggestion(t *testing.T) {
},
{
in: []Suggest{
{Text: "--all-namespaces", Description: "-------------------------------------------------------------------------------------------------------------------------------------------"},
{Text: "--allow-missing-template-keys", Description: "-----------------------------------------------------------------------------------------------------------------------------------------------"},
{Text: "--export", Description: "----------------------------------------------------------------------------------------------------------"},
{Text: "-f", Description: "-----------------------------------------------------------------------------------"},
{Text: "--filename", Description: "-----------------------------------------------------------------------------------"},
{Text: "--include-extended-apis", Description: "------------------------------------------------------------------------------------"},
{
Text: "--all-namespaces",
Description: "---------------------------------------------" +
"-------------------------------" +
"---------------------------------------------------------------",
},
{
Text: "--allow-missing-template-keys",
Description: "---------------------------------------------" +
"-------------------------------" +
"---------------------------------------------------------------",
},
{
Text: "--export",
Description: "---------------------------------------------" +
"-------------------------------------------------------------",
},
{
Text: "-f",
Description: "---------------------------------------------" +
"--------------------------------------",
},
{
Text: "--filename",
Description: "---------------------------------------------" +
"--------------------------------------",
},
{
Text: "--include-extended-apis",
Description: "---------------------------------------------" +
"--------------------------------------",
},
},
expected: []Suggest{
{Text: " --all-namespaces ", Description: " --------------... "},
Expand All @@ -86,23 +112,52 @@ func TestFormatShortSuggestion(t *testing.T) {
},
{
in: []Suggest{
{Text: "--all-namespaces", Description: "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace."},
{Text: "--allow-missing-template-keys", Description: "If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats."},
{Text: "--export", Description: "If true, use 'export' for the resources. Exported resources are stripped of cluster-specific information."},
{Text: "-f", Description: "Filename, directory, or URL to files identifying the resource to get from a server."},
{Text: "--filename", Description: "Filename, directory, or URL to files identifying the resource to get from a server."},
{Text: "--include-extended-apis", Description: "If true, include definitions of new APIs via calls to the API server. [default true]"},
{Text: "--all-namespaces", Description: "If present, list the requested object(s)" +
" across all namespaces. Namespace in current context is ignored even if" +
"specified with --namespace."},
{Text: "--allow-missing-template-keys", Description: "If true, ignore any errors" +
" in templates when a field or map key is missing in the template. Only " +
"applies to golang and jsonpath output formats."},
{Text: "--export", Description: "If true, use 'export' for the resources." +
" Exported resources are stripped of cluster-specific information."},
{Text: "-f", Description: "Filename, directory, or URL to files" +
" identifying the resource to get from a server."},
{Text: "--filename", Description: "Filename, directory, or URL to files" +
" identifying the resource to get from a server."},
{Text: "--include-extended-apis", Description: "If true, include" +
" definitions of new APIs via calls to the API server. [default true]"},
},
expected: []Suggest{
{Text: " --all-namespaces ", Description: " If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace. "},
{Text: " --allow-missing-template-keys ", Description: " If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to golang and jsonpath output formats. "},
{Text: " --export ", Description: " If true, use 'export' for the resources. Exported resources are stripped of cluster-specific information. "},
{Text: " -f ", Description: " Filename, directory, or URL to files identifying the resource to get from a server. "},
{Text: " --filename ", Description: " Filename, directory, or URL to files identifying the resource to get from a server. "},
{Text: " --include-extended-apis ", Description: " If true, include definitions of new APIs via calls to the API server. [default true] "},
},
max: 500,
exWidth: len(" --include-extended-apis " + " If true, include definitions of new APIs via calls to the API server. [default true] "),
{Text: " --all-namespaces ", Description: " If present," +
"list the requested object(s) across all namespaces. " +
"Namespace in current context is ignored even if specified with" +
"--namespace. "},
{Text: " --allow-missing-template-keys ", Description: " If true," +
" ignore any errors in templates when a field or" +
" map key is missing in the template. Only applies to golang" +
" and jsonpath output formats. "},
{Text: " --export ", Description: " If true," +
" use 'export' for the resources. Exported resources are" +
" stripped of cluster-specific information. " +
" "},
{Text: " -f ", Description: " Filename," +
" directory, or URL to files identifying the resource to get" +
" from a server. " +
" "},
{Text: " --filename ", Description: " Filename," +
"directory, or URL to files identifying the resource to get" +
" from a server. " +
" "},
{Text: " --include-extended-apis ", Description: " If true," +
" include definitions of new APIs via calls to the API server." +
"[default true] " +
" "},
},
max: 500,
exWidth: len(" --include-extended-apis " +
"If true, include definitions of new APIs via calls to the API server." +
"[default true] " +
" "),
},
}

Expand Down
12 changes: 8 additions & 4 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func (d *Document) LastKeyStroke() Key {
}

// DisplayCursorPosition returns the cursor position on rendered text on terminal emulators.
// So if Document is "日本(cursor)語", DisplayedCursorPosition returns 4 because '日' and '本' are double width characters.
// So if Document is "日本(cursor)語", DisplayedCursorPosition returns 4 because '日' and '本' are
// double width characters.
func (d *Document) DisplayCursorPosition() int {
var position int
runes := []rune(d.Text)[:d.cursorPosition]
Expand Down Expand Up @@ -174,7 +175,8 @@ func (d *Document) FindStartOfPreviousWordUntilSeparator(sep string) int {
return 0
}

// FindStartOfPreviousWordUntilSeparatorIgnoreNextToCursor is almost the same as FindStartOfPreviousWordWithSpace.
// FindStartOfPreviousWordUntilSeparatorIgnoreNextToCursor is almost the same as
// FindStartOfPreviousWordWithSpace.
// But this can specify Separator. Return 0 if nothing was found.
func (d *Document) FindStartOfPreviousWordUntilSeparatorIgnoreNextToCursor(sep string) int {
if sep == "" {
Expand Down Expand Up @@ -237,7 +239,8 @@ func (d *Document) FindEndOfCurrentWordUntilSeparator(sep string) int {
return len(x)
}

// FindEndOfCurrentWordUntilSeparatorIgnoreNextToCursor is almost the same as FindEndOfCurrentWordWithSpace.
// FindEndOfCurrentWordUntilSeparatorIgnoreNextToCursor is almost the same as
// FindEndOfCurrentWordWithSpace.
// But this can specify Separator. Return 0 if nothing was found.
func (d *Document) FindEndOfCurrentWordUntilSeparatorIgnoreNextToCursor(sep string) int {
if sep == "" {
Expand Down Expand Up @@ -399,7 +402,8 @@ func (d *Document) GetCursorUpPosition(count int, preferredColumn int) int {
return d.TranslateRowColToCursor(row, col) - d.cursorPosition
}

// GetCursorDownPosition return the relative cursor position (character index) where we would be if the
// GetCursorDownPosition return the relative cursor position (character index) where we would be if
// the
// user pressed the arrow-down button.
func (d *Document) GetCursorDownPosition(count int, preferredColumn int) int {
var col int
Expand Down
3 changes: 2 additions & 1 deletion document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,8 @@ func TestDocument_CursorPositionRowAndCol(t *testing.T) {
expectedCol int
}{
{
document: &Document{Text: "line 1\nline 2\nline 3\n", cursorPosition: len("line 1\n" + "lin")},
document: &Document{Text: "line 1\nline 2\nline 3\n",
cursorPosition: len("line 1\n" + "lin")},
expectedRow: 1,
expectedCol: 3,
},
Expand Down
6 changes: 4 additions & 2 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func FilterContains(completions []Suggest, sub string, ignoreCase bool) []Sugges
// FilterFuzzy checks whether the completion.Text fuzzy matches sub.
// Fuzzy searching for "dog" is equivalent to "*d*o*g*". This search term
// would match, for example, "Good food is gone"
// ^ ^ ^
//
// ^ ^ ^
func FilterFuzzy(completions []Suggest, sub string, ignoreCase bool) []Suggest {
return filterSuggestions(completions, sub, ignoreCase, fuzzyMatch)
}
Expand All @@ -49,7 +50,8 @@ func fuzzyMatch(s, sub string) bool {
return true
}

func filterSuggestions(suggestions []Suggest, sub string, ignoreCase bool, function func(string, string) bool) []Suggest {
func filterSuggestions(suggestions []Suggest, sub string, ignoreCase bool, function func(string,
string) bool) []Suggest {
if sub == "" {
return suggestions
}
Expand Down
6 changes: 4 additions & 2 deletions filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ func TestFilter(t *testing.T) {
}

for _, s := range scenarioTable {
if actual := s.filter(s.list, s.substr, s.ignoreCase); !reflect.DeepEqual(actual, s.expected) {
if actual := s.filter(s.list, s.substr, s.ignoreCase); !reflect.DeepEqual(actual, s.
expected) {
t.Errorf("%s: Should be %#v, but got %#v", s.scenario, s.expected, actual)
}
}
Expand All @@ -158,7 +159,8 @@ func TestFuzzyMatch(t *testing.T) {

for _, test := range tests {
if fuzzyMatch(test.s, test.sub) != test.match {
t.Errorf("fuzzymatch, %s in %s: expected %v, got %v", test.sub, test.s, test.match, !test.match)
t.Errorf("fuzzymatch, %s in %s: expected %v, got %v", test.sub, test.s, test.match,
!test.match)
}
}
}
3 changes: 2 additions & 1 deletion input.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ var ASCIISequences = []*ASCIICode{
// Xterm
{Key: F13, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x50}},
{Key: F14, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x51}},
// &ASCIICode{Key: F15, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x52}}, // Conflicts with CPR response
// &ASCIICode{Key: F15, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x52}}, // Conflicts
// with CPR response
{Key: F16, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x52}},
{Key: F17, ASCIICode: []byte{0x1b, 0x5b, 0x15, 0x3b, 0x32, 0x7e}},
{Key: F18, ASCIICode: []byte{0x1b, 0x5b, 0x17, 0x3b, 0x32, 0x7e}},
Expand Down
4 changes: 3 additions & 1 deletion input_posix.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package prompt
Expand All @@ -19,7 +20,8 @@ type PosixParser struct {

// Setup should be called before starting input
func (t *PosixParser) Setup() error {
// Set NonBlocking mode because if syscall.Read block this goroutine, it cannot receive data from stopCh.
// Set NonBlocking mode because if syscall.Read block this goroutine, it cannot receive data
// from stopCh.
if err := syscall.SetNonblock(t.fd, true); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions internal/term/term.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package term
Expand Down
6 changes: 4 additions & 2 deletions key_bind_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func DeleteChar(buf *Buffer) {

// DeleteWord Delete word before the cursor
func DeleteWord(buf *Buffer) {
buf.DeleteBeforeCursor(len([]rune(buf.Document().TextBeforeCursor())) - buf.Document().FindStartOfPreviousWordWithSpace())
buf.DeleteBeforeCursor(len([]rune(buf.Document().TextBeforeCursor())) - buf.Document().
FindStartOfPreviousWordWithSpace())
}

// DeleteBeforeChar Go to Backspace
Expand Down Expand Up @@ -62,5 +63,6 @@ func GoRightWord(buf *Buffer) {

// GoLeftWord Backward one word
func GoLeftWord(buf *Buffer) {
buf.CursorLeft(len([]rune(buf.Document().TextBeforeCursor())) - buf.Document().FindStartOfPreviousWordWithSpace())
buf.CursorLeft(len([]rune(buf.Document().TextBeforeCursor())) - buf.Document().
FindStartOfPreviousWordWithSpace())
}
Loading

0 comments on commit e80af60

Please sign in to comment.