Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filename autocompleter: "cnf" matches CreateNewFile.java and create-new-file.py #1908

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 38 additions & 2 deletions aider/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ def __init__(self, items=None):
if items is not None:
self.show_group = len(items) > 1

import re

class AbbreviationMatcher:
def __init__(self, input_str):
self.input_parts = self._split_input(input_str)

@staticmethod
def _split_input(input_str):
if input_str.islower():
input_str = input_str.upper()
elif input_str[0].islower():
input_str = input_str.capitalize()

return re.findall(r'[A-Z][a-z]*|[0-9]+', input_str)

@staticmethod
def _split_target(target):
# Handle path-like strings
target = target.split('/')[-1]

# Split on camelCase, snake_case, kebab-case, and PascalCase
return re.findall(r'[A-Z]?[a-z]+|[A-Z]+(?=[A-Z][a-z]|\d|\W|$)|\d+', target)

def test(self, target):
target_parts = self._split_target(target)

if len(self.input_parts) > len(target_parts):
return False

return all(
target_part.lower().startswith(input_part.lower())
for input_part, target_part in zip(self.input_parts, target_parts)
)


class AutoCompleter(Completer):
def __init__(
Expand Down Expand Up @@ -127,7 +161,8 @@ def get_command_completions(self, document, complete_event, text, words):
if candidates is None:
return

candidates = [word for word in candidates if partial in word.lower()]
abbrev_matcher = AbbreviationMatcher(partial)
candidates = [word for word in candidates if partial in word.lower() or abbrev_matcher.test(word)]
for candidate in sorted(candidates):
yield Completion(candidate, start_position=-len(words[-1]))

Expand All @@ -153,8 +188,9 @@ def get_completions(self, document, complete_event):

last_word = words[-1]
completions = []
abbrev_matcher = AbbreviationMatcher(last_word)
for word_match, word_insert in candidates:
if word_match.lower().startswith(last_word.lower()):
if word_match.lower().startswith(last_word.lower()) or abbrev_matcher.test(word_match):
completions.append((word_insert, -len(last_word), word_match))

rel_fnames = self.fname_to_rel_fnames.get(word_match, [])
Expand Down