|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/Balaji01-4D/bubbline" |
| 8 | + "github.com/Balaji01-4D/bubbline/computil" |
| 9 | + "github.com/balajz/pgxls/pkg/engine" |
| 10 | + "github.com/balajz/pgxls/pkg/types" |
| 11 | +) |
| 12 | + |
| 13 | +func (p *pgxCLI) getCompletions() bubbline.AutoCompleteFn { |
| 14 | + go func() { |
| 15 | + err := p.client.Cache(p.compWorker) |
| 16 | + if err != nil { |
| 17 | + p.logger.Error("error caching database schema for completions", "error", err) |
| 18 | + p.logger.Debug("continuing with keyword completions") |
| 19 | + } |
| 20 | + p.logger.Debug("completion cache is ready") |
| 21 | + }() |
| 22 | + |
| 23 | + compEngine := engine.NewCompleter(p.compWorker.Cache()) |
| 24 | + |
| 25 | + return func(v [][]rune, line, col int) (msg string, comps bubbline.Completions) { |
| 26 | + compEngine.DBCache = p.compWorker.Cache() |
| 27 | + sql, _ := computil.Flatten(v, line, col) |
| 28 | + |
| 29 | + items, err := compEngine.Complete(sql, line, col, true) |
| 30 | + if err != nil || len(items) == 0 { |
| 31 | + return "", nil |
| 32 | + } |
| 33 | + |
| 34 | + word, wstart, wend := computil.FindWord(v, line, col) |
| 35 | + |
| 36 | + if lastDot := strings.LastIndex(word, "."); lastDot != -1 { |
| 37 | + wstart += lastDot + 1 |
| 38 | + } |
| 39 | + |
| 40 | + compByCategory := make(map[string][]compCandidate) |
| 41 | + for _, row := range items { |
| 42 | + |
| 43 | + category := "others" |
| 44 | + |
| 45 | + switch row.Kind { |
| 46 | + case types.KeywordCompletion: |
| 47 | + category = "keywords" |
| 48 | + case types.FunctionCompletion: |
| 49 | + category = "functions" |
| 50 | + case types.ClassCompletion: |
| 51 | + category = "tables" |
| 52 | + case types.FieldCompletion: |
| 53 | + category = "columns" |
| 54 | + case types.ModuleCompletion: |
| 55 | + category = "schemas" |
| 56 | + case types.SnippetCompletion: |
| 57 | + category = "snippets" |
| 58 | + } |
| 59 | + |
| 60 | + docs := "" |
| 61 | + if row.Documentation != nil { |
| 62 | + docs = row.Documentation.Value |
| 63 | + } |
| 64 | + |
| 65 | + compByCategory[category] = append(compByCategory[category], compCandidate{ |
| 66 | + completion: row.Label, |
| 67 | + desc: row.Detail, |
| 68 | + moveRight: wend - col, |
| 69 | + deleteLeft: wend - wstart, |
| 70 | + docs: docs, |
| 71 | + }) |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + if len(compByCategory) == 0 { |
| 76 | + return msg, comps |
| 77 | + } |
| 78 | + |
| 79 | + limitKeyword(compByCategory, 8) |
| 80 | + |
| 81 | + categories := make([]string, 0, len(compByCategory)) |
| 82 | + for k := range compByCategory { |
| 83 | + categories = append(categories, k) |
| 84 | + } |
| 85 | + sort.Strings(categories) |
| 86 | + comps = &completions{ |
| 87 | + categories: categories, |
| 88 | + compEntries: compByCategory, |
| 89 | + } |
| 90 | + return msg, comps |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// https://github.com/cockroachdb/cockroach/blob/refs/heads/master/pkg/cli/clisqlshell/complete.go |
| 95 | + |
| 96 | +// completions is the interface between the shell and the bubbline |
| 97 | +// completion infra. |
| 98 | +type completions struct { |
| 99 | + categories []string |
| 100 | + compEntries map[string][]compCandidate |
| 101 | +} |
| 102 | + |
| 103 | +var _ bubbline.Completions = (*completions)(nil) |
| 104 | + |
| 105 | +// NumCategories is part of the bubbline.Completions interface. |
| 106 | +func (c *completions) NumCategories() int { return len(c.categories) } |
| 107 | + |
| 108 | +// CategoryTitle is part of the bubbline.Completions interface. |
| 109 | +func (c *completions) CategoryTitle(cIdx int) string { return c.categories[cIdx] } |
| 110 | + |
| 111 | +// NumEntries is part of the bubbline.Completions interface. |
| 112 | +func (c *completions) NumEntries(cIdx int) int { return len(c.compEntries[c.categories[cIdx]]) } |
| 113 | + |
| 114 | +// Entry is part of the bubbline.Completions interface. |
| 115 | +func (c *completions) Entry(cIdx, eIdx int) bubbline.Entry { |
| 116 | + return &c.compEntries[c.categories[cIdx]][eIdx] |
| 117 | +} |
| 118 | + |
| 119 | +// Candidate is part of the bubbline.Completions interface. |
| 120 | +func (c *completions) Candidate(e bubbline.Entry) bubbline.Candidate { return e.(*compCandidate) } |
| 121 | + |
| 122 | +// compCandidate represents one completion candidate. |
| 123 | +type compCandidate struct { |
| 124 | + completion string |
| 125 | + desc string |
| 126 | + moveRight int |
| 127 | + deleteLeft int |
| 128 | + docs string |
| 129 | +} |
| 130 | + |
| 131 | +var _ bubbline.Entry = (*compCandidate)(nil) |
| 132 | + |
| 133 | +// Title is part of the bubbline.Entry interface. |
| 134 | +func (c *compCandidate) Title() string { return c.completion } |
| 135 | + |
| 136 | +// Description is part of the bubbline.Entry interface. |
| 137 | +func (c *compCandidate) Description() string { return c.desc } |
| 138 | + |
| 139 | +// Replacement is part of the bubbline.Candidate interface. |
| 140 | +func (c *compCandidate) Replacement() string { return c.completion } |
| 141 | + |
| 142 | +// MoveRight is part of the bubbline.Candidate interface. |
| 143 | +func (c *compCandidate) MoveRight() int { return c.moveRight } |
| 144 | + |
| 145 | +// DeleteLeft is part of the bubbline.Candidate interface. |
| 146 | +func (c *compCandidate) DeleteLeft() int { return c.deleteLeft } |
| 147 | + |
| 148 | +func (c *compCandidate) SidePanel() string { return c.docs } |
| 149 | + |
| 150 | +func limitKeyword(comp map[string][]compCandidate, limit int) { |
| 151 | + if comps, ok := comp["keywords"]; ok { |
| 152 | + if len(comps) > limit { |
| 153 | + comp["keywords"] = comps[:limit] |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments