From 9db190c977d1f76e2f574882e4edde062eecbb0d Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Sat, 9 Mar 2024 19:08:34 +0100 Subject: roles completion --- py/roles.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 py/roles.py (limited to 'py/roles.py') diff --git a/py/roles.py b/py/roles.py new file mode 100644 index 0000000..8f7f161 --- /dev/null +++ b/py/roles.py @@ -0,0 +1,16 @@ +import vim +import os +import configparser + +roles_config_path = os.path.expanduser('~/.vim/roles.ini') # TODO configure +roles = configparser.ConfigParser() +roles.read(roles_config_path) + +role_names = [name for name in roles.sections() if not '.' in name] + +role_list = [f'"{name}"' for name in role_names] +role_list = ", ".join(role_list) + +role_list = f"[{role_list}]" + +vim.command(f'let l:role_list = {role_list}') -- cgit v1.2.3 From f4130feb986760a8d956983cb5cfe8d7106e7d4a Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Sat, 9 Mar 2024 19:37:55 +0100 Subject: roles example file --- py/roles.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'py/roles.py') diff --git a/py/roles.py b/py/roles.py index 8f7f161..7c7cf13 100644 --- a/py/roles.py +++ b/py/roles.py @@ -2,7 +2,7 @@ import vim import os import configparser -roles_config_path = os.path.expanduser('~/.vim/roles.ini') # TODO configure +roles_config_path = os.path.expanduser(vim.eval("g:vim_ai_roles_config_file")) roles = configparser.ConfigParser() roles.read(roles_config_path) -- cgit v1.2.3 From af52f039513b11e9820ce0bf1e46596a454ebd44 Mon Sep 17 00:00:00 2001 From: Konfekt Date: Sun, 10 Mar 2024 21:28:54 +0100 Subject: Ensure role config file exists before loading to prevent errors The problem was that the application tried to load a roles configuration file without checking whether it actually exists, potentially leading to unhandled exceptions if the file is missing. Ensure that the roles configuration file exists before attempting to read from it; raise an exception with a clear message if the file is not found. diff --git a/py/roles.py b/py/roles.py: -if not os.path.exists(roles_config_path): - raise Exception(f"Role config file does not exist: {roles_config_path}") - --- py/roles.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'py/roles.py') diff --git a/py/roles.py b/py/roles.py index 7c7cf13..a8689f2 100644 --- a/py/roles.py +++ b/py/roles.py @@ -3,6 +3,9 @@ import os import configparser roles_config_path = os.path.expanduser(vim.eval("g:vim_ai_roles_config_file")) +if not os.path.exists(roles_config_path): + raise Exception(f"Role config file does not exist: {roles_config_path}") + roles = configparser.ConfigParser() roles.read(roles_config_path) -- cgit v1.2.3 From 881fd24a6d2c0de387256b4d6e05f5d0a53cc8e2 Mon Sep 17 00:00:00 2001 From: Konfekt Date: Sun, 10 Mar 2024 21:29:32 +0100 Subject: optionally supplement roles dict by vim function source The application was restricted to loading role configurations only from a predefined config file, which limited extensibility. Enable dynamic role configuration by invoking a custom Vim function if it is defined. This allows users to extend the role configurations beyond the static file. diff --git a/doc/vim-ai.txt b/doc/vim-ai.txt: -The roles in g:vim_ai_roles_config_file are converted to a Vim dictionary. -Optionally, additional roles can be added by defining a function VimAIRoleParser() -whose output is a dictionary of the same format as g:vim_ai_roles_config_file. - diff --git a/py/roles.py b/py/roles.py: -if vim.eval('exists("*VimAIRoleParser")'): - roles.update(vim.eval('VimAIRoleParser()')) - diff --git a/py/utils.py b/py/utils.py: - if vim.eval('exists("*VimAIRoleParser")'): - roles.update(vim.eval('VimAIRoleParser()')) - --- py/roles.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'py/roles.py') diff --git a/py/roles.py b/py/roles.py index a8689f2..6e0a52e 100644 --- a/py/roles.py +++ b/py/roles.py @@ -9,6 +9,13 @@ if not os.path.exists(roles_config_path): roles = configparser.ConfigParser() roles.read(roles_config_path) +if vim.eval("exists('g:vim_ai_roles_config_function')") == '1': + roles_config_function = vim.eval("g:vim_ai_roles_config_function") + if not vim.eval("exists('*" + roles_config_function + "')"): + raise Exception(f"Role config function does not exist: {roles_config_function}") + else: + roles.update(vim.eval(roles_config_function + "()")) + role_names = [name for name in roles.sections() if not '.' in name] role_list = [f'"{name}"' for name in role_names] -- cgit v1.2.3 From 6a053767af08d6edfb46b4be72f05a9b3bc7be04 Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Sun, 24 Mar 2024 11:29:22 +0100 Subject: reusing parsing code --- py/roles.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'py/roles.py') diff --git a/py/roles.py b/py/roles.py index 6e0a52e..b7d19e1 100644 --- a/py/roles.py +++ b/py/roles.py @@ -1,6 +1,8 @@ import vim -import os -import configparser + +# import utils +plugin_root = vim.eval("s:plugin_root") +vim.command(f"py3file {plugin_root}/py/utils.py") roles_config_path = os.path.expanduser(vim.eval("g:vim_ai_roles_config_file")) if not os.path.exists(roles_config_path): @@ -9,12 +11,7 @@ if not os.path.exists(roles_config_path): roles = configparser.ConfigParser() roles.read(roles_config_path) -if vim.eval("exists('g:vim_ai_roles_config_function')") == '1': - roles_config_function = vim.eval("g:vim_ai_roles_config_function") - if not vim.eval("exists('*" + roles_config_function + "')"): - raise Exception(f"Role config function does not exist: {roles_config_function}") - else: - roles.update(vim.eval(roles_config_function + "()")) +enhance_roles_with_custom_function(roles) role_names = [name for name in roles.sections() if not '.' in name] -- cgit v1.2.3