|
| 1 | +package edit |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strings" |
| 6 | + "unicode" |
| 7 | + "unicode/utf8" |
| 8 | + |
| 9 | + "src.elv.sh/pkg/cli" |
| 10 | + "src.elv.sh/pkg/cli/tk" |
| 11 | + "src.elv.sh/pkg/eval" |
| 12 | + "src.elv.sh/pkg/eval/vars" |
| 13 | +) |
| 14 | + |
| 15 | +func defaultHistoryProvider(hs *histStore, code string) string { |
| 16 | + if hs == nil { |
| 17 | + return "" |
| 18 | + } |
| 19 | + if code == "" { |
| 20 | + return "" |
| 21 | + } |
| 22 | + |
| 23 | + // Get commands from history that start with the current input |
| 24 | + // TODO: Optimize this to avoid fetching all commands |
| 25 | + cmds, err := hs.AllCmds() |
| 26 | + if err != nil { |
| 27 | + return "" |
| 28 | + } |
| 29 | + |
| 30 | + // Search backwards through history for the most recent match |
| 31 | + for i := len(cmds) - 1; i >= 0; i-- { |
| 32 | + text := cmds[i].Text |
| 33 | + // Match commands that start with current input |
| 34 | + // Also handle multiline commands - only match against the first line |
| 35 | + if strings.HasPrefix(text, code) && len(text) > len(code) { |
| 36 | + text = TrimSpaceRight(text) |
| 37 | + // Return the remaining part after the current input |
| 38 | + return text[len(code):] |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return "" |
| 43 | +} |
| 44 | + |
| 45 | +var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1} |
| 46 | + |
| 47 | +func TrimSpaceRight(s string) string { |
| 48 | + start := 0 |
| 49 | + stop := len(s) |
| 50 | + for ; stop > start; stop-- { |
| 51 | + c := s[stop-1] |
| 52 | + if c >= utf8.RuneSelf { |
| 53 | + // start has been already trimmed above, should trim end only |
| 54 | + return strings.TrimRightFunc(s[start:stop], unicode.IsSpace) |
| 55 | + } |
| 56 | + if asciiSpace[c] == 0 { |
| 57 | + break |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return s[start:stop] |
| 62 | +} |
| 63 | + |
| 64 | +func initAutoSuggestionSpec(appSpec *cli.AppSpec, hs *histStore, enabled vars.PtrVar, provider vars.PtrVar, ev *eval.Evaler) { |
| 65 | + appSpec.AutoSuggestionProvider = func(code string) string { |
| 66 | + if !enabled.GetRaw().(bool) { |
| 67 | + return "" |
| 68 | + } |
| 69 | + |
| 70 | + // Check if user has set a custom provider function |
| 71 | + providerFn := provider.GetRaw() |
| 72 | + if fn, ok := providerFn.(eval.Callable); ok { |
| 73 | + // Call user-defined provider function |
| 74 | + var result string |
| 75 | + valuesCb := func(ch <-chan any) { |
| 76 | + for v := range ch { |
| 77 | + if s, ok := v.(string); ok && result == "" { |
| 78 | + result = s |
| 79 | + return |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + bytesCb := func(_ *os.File) {} |
| 84 | + |
| 85 | + port, done, err := eval.PipePort(valuesCb, bytesCb) |
| 86 | + if err != nil { |
| 87 | + return "" |
| 88 | + } |
| 89 | + err = ev.Call(fn, |
| 90 | + eval.CallCfg{Args: []any{code}, From: "[auto-suggestion provider]"}, |
| 91 | + eval.EvalCfg{Ports: []*eval.Port{nil, port, nil}}) |
| 92 | + done() |
| 93 | + if err == nil && result != "" { |
| 94 | + return result |
| 95 | + } |
| 96 | + return "" |
| 97 | + } |
| 98 | + |
| 99 | + // Fall back to default history-based provider |
| 100 | + return defaultHistoryProvider(hs, code) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func initAutoSuggestionAPI(app cli.App, enabled vars.PtrVar, provider vars.PtrVar, nb eval.NsBuilder) { |
| 105 | + acceptFn := func() { |
| 106 | + codeArea, ok := focusedCodeArea(app) |
| 107 | + if !ok { |
| 108 | + return |
| 109 | + } |
| 110 | + codeArea.MutateState(func(s *tk.CodeAreaState) { |
| 111 | + // Only accept if there's an autosuggestion pending |
| 112 | + if s.Pending.Content != "" && s.Pending.AutoSuggestion { |
| 113 | + s.ApplyPending() |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | + |
| 118 | + nb.AddNs("auto-suggestion", |
| 119 | + eval.BuildNsNamed("edit:auto-suggestion"). |
| 120 | + AddVar("enabled", enabled). |
| 121 | + AddVar("provider", provider). |
| 122 | + AddGoFn("accept", acceptFn)) |
| 123 | +} |
0 commit comments