-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add multicursor autocompletions #4028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,9 @@ func (b *Buffer) GetSuggestions() { | |
|
|
||
| // Autocomplete starts the autocomplete process | ||
| func (b *Buffer) Autocomplete(c Completer) bool { | ||
| if !b.GetActiveCursor().CanAutocomplete() { | ||
| return false | ||
| } | ||
| b.Completions, b.Suggestions = c(b) | ||
| if len(b.Completions) != len(b.Suggestions) || len(b.Completions) == 0 { | ||
| return false | ||
|
|
@@ -49,23 +52,57 @@ func (b *Buffer) CycleAutocomplete(forward bool) { | |
| b.CurSuggestion = len(b.Suggestions) - 1 | ||
| } | ||
|
|
||
| c := b.GetActiveCursor() | ||
| // cycle autocomplete for all except active cursors | ||
| for i, c := range b.cursors { | ||
| if i == b.curCursor || !c.CanAutocomplete() { | ||
| continue | ||
| } | ||
|
|
||
| activeWord, _ := b.GetWordCursor(b.GetActiveCursor()) | ||
| word, _ := b.GetWordCursor(c); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unneeded semicolon (see
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Damn, this must be automatically found and not every time by you within the review! 😓 |
||
| if !bytes.Equal(word, activeWord) { | ||
| continue | ||
| } | ||
|
|
||
| b.AutocompleteSingle(c, prevSuggestion) | ||
| } | ||
|
|
||
| // cycle autocomplete for active cursor | ||
| b.AutocompleteSingle(b.GetActiveCursor(), prevSuggestion) | ||
|
|
||
| if len(b.Suggestions) > 1 { | ||
| b.HasSuggestions = true | ||
| } | ||
| } | ||
|
|
||
| func (b *Buffer) AutocompleteSingle(c *Cursor, prevSuggestion int) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this function to be public? I'm not sure we want to expose this low-level function to plugins... So maybe name it e.g. just |
||
| start := c.Loc | ||
| end := c.Loc | ||
|
|
||
| if prevSuggestion < len(b.Suggestions) && prevSuggestion >= 0 { | ||
| start = end.Move(-util.CharacterCountInString(b.Completions[prevSuggestion]), b) | ||
| } | ||
|
|
||
| b.Replace(start, end, b.Completions[b.CurSuggestion]) | ||
| if len(b.Suggestions) > 1 { | ||
| b.HasSuggestions = true | ||
| } | ||
|
|
||
| func (c *Cursor) CanAutocomplete() bool { | ||
| if c.X == 0 { | ||
| return false | ||
| } | ||
|
|
||
| r := c.RuneUnder(c.X) | ||
| prev := c.RuneUnder(c.X - 1) | ||
| if !util.IsAutocomplete(prev) || util.IsWordChar(r) { | ||
| // don't autocomplete if cursor is within a word | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // GetWord gets the most recent word separated by any separator | ||
| // GetWordCursor gets the most recent word separated by any separator | ||
| // (whitespace, punctuation, any non alphanumeric character) | ||
| func (b *Buffer) GetWord() ([]byte, int) { | ||
| c := b.GetActiveCursor() | ||
| func (b *Buffer) GetWordCursor(c *Cursor) ([]byte, int) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not make it a method of |
||
| l := b.LineBytes(c.Y) | ||
| l = util.SliceStart(l, c.X) | ||
|
|
||
|
|
@@ -82,6 +119,10 @@ func (b *Buffer) GetWord() ([]byte, int) { | |
| return input, c.X - util.CharacterCount(input) | ||
| } | ||
|
|
||
| func (b *Buffer) GetWord() ([]byte, int) { | ||
| return b.GetWordCursor(b.GetActiveCursor()) | ||
| } | ||
|
|
||
| // GetArg gets the most recent word (separated by ' ' only) | ||
| func (b *Buffer) GetArg() (string, int) { | ||
| c := b.GetActiveCursor() | ||
|
|
@@ -153,7 +194,7 @@ func FileComplete(b *Buffer) ([]string, []string) { | |
| // BufferComplete autocompletes based on previous words in the buffer | ||
| func BufferComplete(b *Buffer) ([]string, []string) { | ||
| c := b.GetActiveCursor() | ||
| input, argstart := b.GetWord() | ||
| input, argstart := b.GetWordCursor(c) | ||
|
|
||
| if argstart == -1 { | ||
| return []string{}, []string{} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can get
activeWordonce before the loop, instead of recalculating it on each iteration?BTW then we don't need to postpone autocompleting the active cursor until after the loop, we can do it before the loop... but only with an extra change like this:
Hmm, maybe an extra change like this would be good to apply anyway?