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

Filter file hook for path completer #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions pyvim/commands/completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
)


def create_command_completer(editor):
def create_command_completer(editor, file_filter=lambda _: True):
commands = [c + ' ' for c in get_commands()]

return GrammarCompleter(COMMAND_GRAMMAR, {
'command': WordCompleter(commands),
'location': PathCompleter(expanduser=True),
'location': PathCompleter(expanduser=True, file_filter=file_filter),
'set_option': WordCompleter(sorted(SET_COMMANDS)),
'buffer_name': BufferNameCompleter(editor),
'colorscheme': ColorSchemeCompleter(editor),
Expand Down
3 changes: 2 additions & 1 deletion pyvim/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from .commands.preview import CommandPreviewer
from .editor_buffer import EditorBuffer
from .enums import COMMAND_BUFFER
from .file_filter import file_filter
from .help import HELP_TEXT
from .key_bindings import create_key_bindings
from .layout import EditorLayout, get_terminal_title
Expand Down Expand Up @@ -156,7 +157,7 @@ def handle_action(cli, buffer):
commands_history = FileHistory(os.path.join(self.config_directory, 'commands_history'))
command_buffer = Buffer(accept_action=AcceptAction(handler=handle_action),
enable_history_search=Always(),
completer=create_command_completer(self),
completer=create_command_completer(self, file_filter=file_filter),
history=commands_history)

search_buffer_history = FileHistory(os.path.join(self.config_directory, 'search_history'))
Expand Down
44 changes: 44 additions & 0 deletions pyvim/file_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
FILE_FILTER_FUNCTIONS = {}


def has_file_filter_handler(command):
return command in FILE_FILTER_FUNCTIONS


def call_file_filter_handler(filter, filename):
"""
Execute command.
"""
FILE_FILTER_FUNCTIONS[filter](filename)


def get_file_filters():
return FILE_FILTER_FUNCTIONS.keys()


def add_file_filter(name):
"""
Decorator that registers a function that takes a filename as a
parameter and returns True if valid.

To use, in your ~/.pyvimrc::

from pyvim.file_filter import file_filter

@file_filter('filtername')
def _(filename):
return not filename.endswith('.pyc')
"""
def decorator(func):
FILE_FILTER_FUNCTIONS[name] = func
return func
return decorator


def file_filter(filename):
if not get_file_filters():
return True
return all(
func(filename)
for filter, func in FILE_FILTER_FUNCTIONS.items()
)