Skip to content

Add tab completion for file paths in save/open prompts - #323

Open
InsanePrawn wants to merge 9 commits into
asottile:mainfrom
InsanePrawn:prawn/fileglob
Open

Add tab completion for file paths in save/open prompts#323
InsanePrawn wants to merge 9 commits into
asottile:mainfrom
InsanePrawn:prawn/fileglob

Conversation

@InsanePrawn

Copy link
Copy Markdown

No description provided.

Comment thread babi/prompt.py Outdated
self,
screen: Screen,
prompt: str, lst: list[str],
file_glob: bool = False,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

this is a boolean trap -- it should be named-only if anything

@InsanePrawn InsanePrawn Oct 4, 2023

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Sorry, not sure I understand what exactly you'd like me to do here.

Something like lst: list[str], *, file_glob: bool[ = False]?

Do you want me to drop the default value as well?

[Also realised I forgot a line break between prompt and lst]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

hi, I hope my comment doesn't come out of place -- anthony made a video on boolean traps

Comment thread babi/prompt.py Outdated
Comment on lines +30 to +32
self._dispatch = Prompt.DISPATCH.copy()
if file_glob:
self._dispatch[b'^I'] = Prompt._file_complete

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

unknown keys are already ignored in prompts -- the completion callback should just check if it's enabled or not rather than having to copy this dict every time

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I don't think I understand this point - a key seems to be considered "known" as soon as it's in the dispatch dict, so I can't just add it globally.

If I didn't copy the dict, I'd be modifying the global DISPATCH dict, AFAIUI?

Also, for the callback to check on whether globbing is enabled, i'd have to make file_glob an instance variable, correct?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I'm saying you should handle the option inside the callback

Comment thread babi/prompt.py Outdated
Comment thread babi/prompt.py
self._s = self._s[:self._x]

def _file_complete(self) -> None:
partial = self._s[:self._x]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

this doesn't seem quite right -- it's going to take imagine foo.txt is on disk and you've written fo][od and you tab you're going to get foo.txtod which is nonsensical. I think it should really only listen to tab if you're at the end of the string

@InsanePrawn InsanePrawn Oct 4, 2023

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Sometimes, this is what I want, and I use this with tools that allow it.
Example: so][/foo.txt -> someLongFolder/foo.txt

This is especially useful to me to create a new file in an existing folder.

I understand this is a question of preference, I'd personally prefer this behaviour to stay as is.

[I usually add a space to the right of the cursor before hitting tab for visual separation and then delete it after completion has done its job, but that uses the same mechanic. I doubt we'd want to check for whitespace after the cursor and make it conditional]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

in that case it could listen to tab if it is at the end of the string or before a path separator I guess?

Comment thread babi/screen.py
Comment on lines +724 to +726
filename = self.prompt(
'enter filename', default=self.file.filename, file_glob=True,
)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

this changes the behaviour. these shouldn't have a default

Comment thread babi/prompt.py
screen: Screen,
prompt: str, lst: list[str],
*,
file_glob: bool,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

this is maybe not a good name -- it's really a flag for whether tab completion of files occurs -- and can probably default to False

Comment thread babi/prompt.py
b'KEY_DC': _delete,
b'^K': _cut_to_end,
# misc
b'^I': _tab,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

this belongs with editing

Comment on lines +43 to +44
base = 'globtest'
prefix = base + 'ffff.txt'

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

rather than variables I'd rather see the actual strings in the assertions -- rather than having to hunt around for what is actually being tested the expressions should be clear in tests. https://testing.googleblog.com/2014/07/testing-on-toilet-dont-put-logic-in.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants