diff --git a/pyvim/commands/completer.py b/pyvim/commands/completer.py index 6e0c046..e4c039b 100644 --- a/pyvim/commands/completer.py +++ b/pyvim/commands/completer.py @@ -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), diff --git a/pyvim/editor.py b/pyvim/editor.py index f26d2ae..7996ec4 100644 --- a/pyvim/editor.py +++ b/pyvim/editor.py @@ -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 @@ -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')) diff --git a/pyvim/file_filter.py b/pyvim/file_filter.py new file mode 100644 index 0000000..24e690e --- /dev/null +++ b/pyvim/file_filter.py @@ -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() + )