-
Notifications
You must be signed in to change notification settings - Fork 3
feat(interactive): add fzf-style fuzzy finder for USE statement #506
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,165 @@ | ||
| // Copyright 2026 apstndb | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package mycli | ||
|
|
||
| import ( | ||
| "context" | ||
| "log/slog" | ||
| "regexp" | ||
|
|
||
| "cloud.google.com/go/spanner/admin/database/apiv1/databasepb" | ||
| "github.com/hymkor/go-multiline-ny" | ||
| "github.com/ktr0731/go-fuzzyfinder" | ||
| "github.com/nyaosorg/go-readline-ny" | ||
| ) | ||
|
|
||
| // fuzzyFinderCommand implements readline.Command for the fuzzy finder feature. | ||
| // It detects the current input context and launches a fuzzy finder with | ||
| // appropriate candidates. The selected value replaces the current argument | ||
| // (completion-style behavior). | ||
| type fuzzyFinderCommand struct { | ||
| editor *multiline.Editor | ||
| cli *Cli | ||
| } | ||
|
|
||
| func (f *fuzzyFinderCommand) String() string { | ||
| return "FUZZY_FINDER" | ||
| } | ||
|
|
||
| // SetEditor is called by go-multiline-ny's BindKey to inject the editor reference. | ||
| func (f *fuzzyFinderCommand) SetEditor(e *multiline.Editor) { | ||
| f.editor = e | ||
| } | ||
|
|
||
| func (f *fuzzyFinderCommand) Call(ctx context.Context, B *readline.Buffer) readline.Result { | ||
| // Use current line buffer, not the full multiline text. | ||
| // B is the buffer for the current line only, so argStartPos must be relative to it. | ||
| input := B.String() | ||
| result := detectFuzzyContext(input) | ||
| if result.contextType == "" { | ||
| return readline.CONTINUE | ||
| } | ||
|
|
||
| candidates, err := f.fetchCandidates(ctx, result.contextType) | ||
| if err != nil { | ||
| slog.Debug("fuzzy finder: failed to fetch candidates", "context", result.contextType, "err", err) | ||
| return readline.CONTINUE | ||
| } | ||
| if len(candidates) == 0 { | ||
| return readline.CONTINUE | ||
| } | ||
|
|
||
| // Terminal handoff: move cursor below editor, run fzf, then restore | ||
| rewind := f.editor.GotoEndLine() | ||
|
|
||
| opts := []fuzzyfinder.Option{} | ||
| if result.argPrefix != "" { | ||
| opts = append(opts, fuzzyfinder.WithQuery(result.argPrefix)) | ||
| } | ||
|
|
||
| idx, err := fuzzyfinder.Find(candidates, func(i int) string { | ||
| return candidates[i] | ||
| }, opts...) | ||
|
|
||
| rewind() | ||
| B.RepaintLastLine() | ||
|
|
||
| if err != nil { | ||
| // User cancelled (Escape/Ctrl+C) or other error | ||
| return readline.CONTINUE | ||
| } | ||
|
|
||
| selected := candidates[idx] | ||
|
|
||
| // Replace the argument portion: delete from argStartPos to end of buffer, | ||
| // then insert the selected value. | ||
| bufLen := len(B.Buffer) | ||
| if result.argStartPos < bufLen { | ||
| B.Delete(result.argStartPos, bufLen-result.argStartPos) | ||
| } | ||
| B.Cursor = result.argStartPos | ||
| B.InsertAndRepaint(selected) | ||
|
|
||
| return readline.CONTINUE | ||
| } | ||
|
|
||
| // fuzzyContextType represents what kind of candidates to provide. | ||
| type fuzzyContextType = string | ||
|
|
||
| const ( | ||
| fuzzyContextDatabase fuzzyContextType = "database" | ||
| ) | ||
|
|
||
| // fuzzyContextResult holds the detected context, the argument prefix typed so far, | ||
| // and the buffer position where the argument starts. | ||
| type fuzzyContextResult struct { | ||
| contextType fuzzyContextType | ||
| argPrefix string // partial argument already typed (used as initial fzf query) | ||
| argStartPos int // position in the current line buffer where the argument starts (in runes) | ||
| } | ||
|
|
||
| // useContextRe matches "USE" followed by optional whitespace and captures any partial argument. | ||
| var useContextRe = regexp.MustCompile(`(?i)^\s*USE(\s+(\S*))?$`) | ||
|
|
||
| // detectFuzzyContext analyzes the current editor buffer to determine | ||
| // what kind of fuzzy completion is appropriate. | ||
| func detectFuzzyContext(input string) fuzzyContextResult { | ||
| if m := useContextRe.FindStringSubmatch(input); m != nil { | ||
| argPrefix := m[2] // may be empty | ||
| // argStartPos: position after "USE " in runes. | ||
| // Find where the argument starts by locating USE + whitespace. | ||
| argStart := len([]rune(input)) - len([]rune(argPrefix)) | ||
| return fuzzyContextResult{ | ||
| contextType: fuzzyContextDatabase, | ||
| argPrefix: argPrefix, | ||
| argStartPos: argStart, | ||
| } | ||
| } | ||
| return fuzzyContextResult{} | ||
| } | ||
|
|
||
| // fetchCandidates returns completion candidates for the given context type. | ||
| func (f *fuzzyFinderCommand) fetchCandidates(ctx context.Context, ctxType fuzzyContextType) ([]string, error) { | ||
| switch ctxType { | ||
| case fuzzyContextDatabase: | ||
| return f.fetchDatabaseCandidates(ctx) | ||
| default: | ||
| return nil, nil | ||
| } | ||
| } | ||
|
|
||
| // fetchDatabaseCandidates lists databases from the current instance. | ||
| func (f *fuzzyFinderCommand) fetchDatabaseCandidates(ctx context.Context) ([]string, error) { | ||
| session := f.cli.SessionHandler.GetSession() | ||
| if session == nil || session.adminClient == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| dbIter := session.adminClient.ListDatabases(ctx, &databasepb.ListDatabasesRequest{ | ||
| Parent: session.InstancePath(), | ||
| }) | ||
|
|
||
| var databases []string | ||
| for db, err := range dbIter.All() { | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| matched := extractDatabaseRe.FindStringSubmatch(db.GetName()) | ||
| if len(matched) > 1 { | ||
| databases = append(databases, matched[1]) | ||
| } | ||
| } | ||
| return databases, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.