Skip to content

Commit

Permalink
cli: support autocomplete
Browse files Browse the repository at this point in the history
Release note (cli change): CLI now auto completes on tab
by using `SHOW COMPLETIONS AT OFFSET`.
  • Loading branch information
RichardJCai committed Jan 12, 2022
1 parent 2a70cdc commit fde4512
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pkg/cli/clisqlshell/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,8 +895,20 @@ func (c *cliState) GetCompletions(_ string) []string {
sql, _ := c.ins.GetLineInfo()

if !strings.HasSuffix(sql, "??") {
fmt.Fprintf(c.iCtx.stdout,
"\ntab completion not supported; append '??' and press tab for contextual help\n\n")
query := fmt.Sprintf(`SHOW COMPLETIONS AT OFFSET %d FOR %s`, len(sql), lexbase.EscapeSQLString(sql))
_, rows, err := c.sqlExecCtx.RunQuery(c.conn,
clisqlclient.MakeQuery(query), true)
if err != nil {
fmt.Fprintln(c.iCtx.stdout)
clierror.OutputError(c.iCtx.stdout, err, true /*showSeverity*/, false /*verbose*/)
}

var completions []string
for _, row := range rows {
completions = append(completions, row[0])
}

return completions
} else {
helpText, err := c.serverSideParse(sql)
if helpText != "" {
Expand Down

0 comments on commit fde4512

Please sign in to comment.