FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-133390: Extend completion for .commands in `sqlite3` by StanFromIreland · Pull Request #135432 · python/cpython · GitHub

/ cpython Public

gh-133390: Extend completion for .commands in sqlite3 - #135432

Merged
encukou merged 2 commits into
python:mainfrom
StanFromIreland:sqlite3-.commands
Aug 25, 2025
Merged

gh-133390: Extend completion for .commands in sqlite3#135432
encukou merged 2 commits into
python:mainfrom
StanFromIreland:sqlite3-.commands

Conversation

StanFromIreland commented Jun 12, 2025
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

tanloong left a comment
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Only commands with a dot at the line beginning is considered dot commands. text is the non-whitespace chars under wherever the cursor is at. This means using text.startswith() enables dot command completion for not only actual dot commands but also any inline word that starts with dot:

sqlite> SELECT .<tab>
.help 
.quit 
.version 
sqlite> SELECT .

Using readline.get_line_buffer() instead is more reasonable. This returns the whole current line and can be used to avoid completion for dot-started inline word:

origline = readline.get_line_buffer()
if origline.startswith("."):
    ...

Copy link
Copy Markdown
Member Author

sqlite3 identifiers cannot start with . (OperationalError (SQLITE_ERROR): near ".": syntax error). Calling readline would complicate our imports a bit more, I'm not sure it is worth it. Not much changes, it is invalid just like it was before.

tanloong commented Jun 12, 2025
edited
Loading

Copy link
Copy Markdown
Contributor

I actually also made a local change to add dot commands completion. This is the idea on my local branch, just sharing it for discussion:

  1. Complete without dot if there is any number of whitespaces between the dot and the cursor.
sqlite> . |
help
version
quit
  1. Complete with the dot if cursor is immediately at the right handside of the dot.
sqlite> .|
.help
.version
.quit
  1. Complete nothing if there is any non-whitespace chars between the dot and the cursor (as we currently don't have dot commands that receive arguments).
sqlite> . help |
# no candidates
COMMANDS = ("quit", "help", "version")


def _complete(text, state):
    global _completion_matches

    if state == 0:
        import readline

        origline = readline.get_line_buffer()
        if origline.startswith("."):
            if origline == text:
                text_lstrip = text.lstrip(".")
                _completion_matches = [f".{cmd}" for cmd in COMMANDS
                                       if cmd.startswith(text_lstrip)]
            elif origline.removeprefix(".").lstrip(" ") == text:
                _completion_matches = [cmd for cmd in COMMANDS
                                       if cmd.startswith(text)]
            else:
                _completion_matches.clear()
        else:
            text_upper = text.upper()
            _completion_matches = [c for c in SQLITE_KEYWORDS if c.startswith(text_upper)]
    try:
        return _completion_matches[state] + " "
    except IndexError:
        return None

Comment thread Lib/sqlite3/_completer.py Outdated

encukou commented Aug 25, 2025

Copy link
Copy Markdown
Member

Thank you!
@tanloong, additional improvements are welcome :)

encukou merged commit a553065 into python:main Aug 25, 2025
44 checks passed
StanFromIreland deleted the sqlite3-.commands branch August 25, 2025 13:00
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL