Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Features
* Default to standards-compliant `utf8mb4` character set.
* Stream input from STDIN to consume less memory, adding `--noninteractive` and `--format=` CLI arguments.
* Remove suggested quoting on completions for identifiers with uppercase.
* Add true fuzzy-match completions with rapidfuzz.


Bug Fixes
Expand Down
20 changes: 20 additions & 0 deletions mycli/sqlcompleter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from prompt_toolkit.completion import CompleteEvent, Completer, Completion
from prompt_toolkit.completion.base import Document
from pygments.lexers._mysql_builtins import MYSQL_DATATYPES, MYSQL_FUNCTIONS, MYSQL_KEYWORDS
import rapidfuzz

from mycli.packages.completion_engine import suggest_type
from mycli.packages.filepaths import complete_path, parse_path, suggest_path
Expand Down Expand Up @@ -996,6 +997,25 @@ def find_matches(
completions.append(item)
continue

if len(text) >= 4:
rapidfuzz_matches = rapidfuzz.process.extract(
text,
collection,
scorer=rapidfuzz.fuzz.WRatio,
# todo: maybe make our own processor which only does case-folding
# because underscores are valuable info
processor=rapidfuzz.utils.default_process,
limit=20,
score_cutoff=75,
)
for elt in rapidfuzz_matches:
item, _score, _type = elt
if len(item) < len(text) / 1.5:
continue
if item in completions:
continue
completions.append(item)

else:
match_end_limit = len(text) if start_only else None
for item in collection:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies = [
"pyperclip >= 1.8.1",
"pycryptodomex",
"pyfzf >= 0.3.1",
"rapidfuzz ~= 3.14.3",
]

[build-system]
Expand Down
12 changes: 12 additions & 0 deletions test/test_smart_completion_public_schema_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,18 @@ def test_table_names_inter_partial(completer, complete_event):
result = list(completer.get_completions(Document(text=text, cursor_position=position), complete_event))
assert result == [
Completion(text="time_zone_leap_second", start_position=-9),
Completion(text='time_zone_name', start_position=-9),
Completion(text='time_zone_transition', start_position=-9),
Completion(text='time_zone_transition_type', start_position=-9),
]


def test_table_names_fuzzy(completer, complete_event):
text = "SELECT * FROM tim_leap"
position = len("SELECT * FROM tim_leap")
result = list(completer.get_completions(Document(text=text, cursor_position=position), complete_event))
assert result == [
Completion(text="time_zone_leap_second", start_position=-8),
]


Expand Down