Skip to content

Column completion is too restrictive and doesn't work in common SQL patterns #33

@joehorz

Description

@joehorz

Description:

I've been using cmp-bee and noticed that the column completion has very restrictive trigger conditions. Currently, it only triggers when:

  1. The alias is preceded by whitespace or a left parenthesis [%s%(]
  2. The alias is followed immediately by a dot and nothing else (end of line)

This prevents column completion in many common SQL patterns, for example:

-- 1. After a comma in SELECT list
SELECT id, t.  -- Doesn't trigger (missing comma in pattern)

-- 2. After operators in WHERE clause
WHERE a.id = t.  -- Doesn't trigger (missing operators in pattern)

-- 3. When continuing to type after the dot
SELECT t.column_name  -- Only triggers at t., not when typing t.c (requires $ at end)

Affected Code:

File: /cmp-dbee/lua/cmp-dbee/handler.lua

Method: function Handler:get_completion()

if m.alias and cursor_before_line:match("[%s%(]" .. m.alias .. "%.$") then
    -- column completion logic
end

Expected behavior:

Column completion should trigger in all common SQL contexts where a table alias is valid, including:

  1. After commas, operators, and other SQL delimiters
  2. When the dot is not at the end of line (while typing column names)

Questions:

  1. What was the reasoning behind restricting the alias pattern to only [%s%(]? Was this intentional for a specific use case?
  2. Why require the dot to be at the end of line ($anchor), preventing completion while typing column names?

Proposed solution:

The regex pattern could be made more flexible. Currently it's:

if m.alias and cursor_before_line:match("[%s%(]" .. m.alias .. "%.$") then

Could be changed to something like:

 if m.alias and cursor_before_line:match("([^%w_])" .. m.alias .. "%." .. "(.*)$") then

This would:

  1. Allow any non-word character before the alias [^%w_](including space, comma, operators, parentheses, etc.)
  2. Allow the dot to be anywhere in the line, not just at the end (removing the $anchor)

Additional context:

The current implementation makes the plugin less useful for everyday SQL writing, especially when working with complex queries that involve multiple joins and conditions. Making the completion trigger more liberal would significantly improve the developer experience.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions