diff --git a/README.md b/README.md index f7a7069..11a1a13 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ https://github.com/user-attachments/assets/63109233-1e5d-4d54-b890-30eb07dab826 - [`CodeRunnerRunByFileType`](#coderunnerrunbyfiletype) - [`CodeRunnerRunByGlob`](#coderunnerrunbyglob) - [`CodeRunnerRun`](#coderunnerrun) - - [`coderunner#Load()`](#coderunnerload) + - [`CodeRunnerRestart`](#coderunnerrestart) - [`coderunner#RemoveCoderunnerTempfiles()`](#coderunnerremovecoderunnertempfiles) - [Configuration](#configuration) - [`g:coderunner_by_file_ext`](#gcoderunner_by_file_ext) @@ -35,9 +35,9 @@ https://github.com/user-attachments/assets/63109233-1e5d-4d54-b890-30eb07dab826 ```vim :echo has('python3') " should return 1 ``` -- To use `CodeRunnerRunByGlob` python version must be above 3.10. Check with: +- Python 3.11+ for all features except `CodeRunnerRunByGlob`. For `CodeRunnerRunByGlob` Python 3.13+ is required. Check with: ```vim - :py3 import sys;print(sys.version) " should return >= 3.10 + :py3 import sys;print(sys.version) ``` ## Installation @@ -66,9 +66,9 @@ Tries to execute the code if the full path of the current file corresponds to so Tries to execute the code using the fallback strategy defined in [`g:coderunner_runners_order`](#gcoderunner_runners_order). -### `coderunner#Load()` +### `CodeRunnerRestart` -The function that loads the coderunner module also updates the variables: g:coderunner_by_file_ext, g:coderunner_by_file_type, g:coderunner_by_glob. +Reloads the coderunner module. Useful during development or after changing configuration variables at runtime. ### `coderunner#RemoveCoderunnerTempfiles()` diff --git a/autoload/coderunner.vim b/autoload/coderunner.vim index c5ba1f4..9fa718f 100644 --- a/autoload/coderunner.vim +++ b/autoload/coderunner.vim @@ -2,120 +2,57 @@ let s:save_cpo = &cpo set cpo&vim -command! -range CodeRunnerRun call coderunner#Run(visualmode(), , , ) -command! -range CodeRunnerRunByFileExt call coderunner#RunByFileExt(visualmode(), , , ) -command! -range CodeRunnerRunByFileType call coderunner#RunByFileType(visualmode(), , , ) -command! -range CodeRunnerRunByGlob call coderunner#RunByGlob(visualmode(), , , ) - let s:script_folder_path = escape(expand(':p:h'), '\') -function coderunner#Load() abort -python3 << EOF -import os -import sys -import traceback -import typing - -import vim - - -def safe_coderunner_access(func): - def wrapper(*args, **kwargs): - if "coderunner" in globals() and coderunner is not None: - return func(*args, **kwargs) - vim.command("redraw | echohl WarningMsg") - vim.command("echom 'CodeRunner unavailable, please look at messages.'") - vim.command("echohl None") - return wrapper - - -@safe_coderunner_access -def coderunner_run(): - coderunner.run() - - -@safe_coderunner_access -def coderunner_run_by_file_ext(): - coderunner.run_by_file_ext() - - -@safe_coderunner_access -def coderunner_run_by_file_type(): - coderunner.run_by_file_type() - - -@safe_coderunner_access -def coderunner_run_by_glob(): - coderunner.run_by_glob() - - -@safe_coderunner_access -def coderunner_remove_coderunner_tempfiles(): - coderunner.remove_coderunner_tempfiles() - - -@safe_coderunner_access -def coderunner_on_exit(): - coderunner.on_exit() - - -sys.path.insert(0, os.path.dirname(vim.eval("s:script_folder_path"))) -try: - from python_coderunner import TCodeRunner, TVimCodeRunnerFactory +function! coderunner#Start() abort + call s:load() + call s:register_commands() +endfunction - coderunner: TCodeRunner = TVimCodeRunnerFactory().create() -except Exception as error: - vim.command("redraw | echohl ErrorMsg") - for line in traceback.format_exc().splitlines(): - vim.command("echom '{0}'".format(line.replace("'", "''"))) - vim.command("echom 'CodeRunner unavailable: {0}'".format(str(error).replace("'", "''"))) - vim.command("echohl None") - vim.command("return 0") -else: - vim.command("return 1") -EOF +function! coderunner#Restart() abort + call s:load() endfunction -function coderunner#Run(visualmode, range, first_line, last_line) range abort +function! coderunner#Run(visualmode, range, first_line, last_line) range abort python3 << EOF coderunner_run() EOF endfunction -function coderunner#RunByFileExt(visualmode, range, first_line, last_line) range abort +function! coderunner#RunByFileExt(visualmode, range, first_line, last_line) range abort python3 << EOF coderunner_run_by_file_ext() EOF endfunction -function coderunner#RunByFileType(visualmode, range, first_line, last_line) range abort +function! coderunner#RunByFileType(visualmode, range, first_line, last_line) range abort python3 << EOF coderunner_run_by_file_type() EOF endfunction -function coderunner#RunByGlob(visualmode, range, first_line, last_line) range abort +function! coderunner#RunByGlob(visualmode, range, first_line, last_line) range abort python3 << EOF coderunner_run_by_glob() EOF endfunction -function coderunner#RemoveCoderunnerTempfiles() abort +function! coderunner#RemoveCoderunnerTempfiles() abort python3 << EOF coderunner_remove_coderunner_tempfiles() EOF endfunction -function coderunner#OnExit() abort +function! coderunner#OnExit() abort python3 << EOF coderunner_on_exit() EOF @@ -155,6 +92,83 @@ function! coderunner#GetSelectedText(visualmode, range, first_line, last_line) a endfunction +function! s:load() abort +python3 << EOF +import os +import sys +import traceback + +import vim + + +def safe_coderunner_access(func): + def wrapper(*args, **kwargs): + if "coderunner" in globals() and coderunner is not None: + return func(*args, **kwargs) + vim.command("redraw | echohl WarningMsg") + vim.command("echom 'CodeRunner unavailable, please look at messages.'") + vim.command("echohl None") + return wrapper + + +@safe_coderunner_access +def coderunner_run(): + coderunner.run() + + +@safe_coderunner_access +def coderunner_run_by_file_ext(): + coderunner.run_by_file_ext() + + +@safe_coderunner_access +def coderunner_run_by_file_type(): + coderunner.run_by_file_type() + + +@safe_coderunner_access +def coderunner_run_by_glob(): + coderunner.run_by_glob() + + +@safe_coderunner_access +def coderunner_remove_coderunner_tempfiles(): + coderunner.remove_coderunner_tempfiles() + + +@safe_coderunner_access +def coderunner_on_exit(): + coderunner.on_exit() + + +sys.path.insert(0, os.path.dirname(vim.eval("s:script_folder_path"))) +try: + from python_coderunner import TCodeRunner, TVimCodeRunnerFactory + + + coderunner: TCodeRunner = TVimCodeRunnerFactory().create() +except Exception as error: + vim.command("redraw | echohl ErrorMsg") + for line in traceback.format_exc().splitlines(): + vim.command("echom '{0}'".format(line.replace("'", "''"))) + vim.command("echom 'CodeRunner unavailable: {0}'".format(str(error).replace("'", "''"))) + vim.command("echohl None") + vim.command("return 0") +else: + vim.command("return 1") +EOF +endfunction + + +function! s:register_commands() abort + command! -range CodeRunnerRun call coderunner#Run(visualmode(), , , ) + command! -range CodeRunnerRunByFileExt call coderunner#RunByFileExt(visualmode(), , , ) + command! -range CodeRunnerRunByFileType call coderunner#RunByFileType(visualmode(), , , ) + command! -range CodeRunnerRunByGlob call coderunner#RunByGlob(visualmode(), , , ) + command! CodeRunnerRestart call coderunner#Restart() +endfunction + + " This is basic vim plugin boilerplate let &cpo = s:save_cpo unlet s:save_cpo diff --git a/plugin/coderunner.vim b/plugin/coderunner.vim index 06edda2..f8774d5 100644 --- a/plugin/coderunner.vim +++ b/plugin/coderunner.vim @@ -34,10 +34,10 @@ let g:coderunner_tempfile_prefix = get(g:, 'coderunner_tempfile_prefix', 'coderu if has('vim_starting') augroup coderunnerVimEnter autocmd! - autocmd VimEnter * call coderunner#Load() + autocmd VimEnter * call coderunner#Start() augroup END else - call coderunner#Load() + call coderunner#Start() endif augroup coderunnerVimLeave diff --git a/python_coderunner/pyproject.toml b/python_coderunner/pyproject.toml index ddbcdc3..7e50d18 100644 --- a/python_coderunner/pyproject.toml +++ b/python_coderunner/pyproject.toml @@ -3,7 +3,7 @@ name = "vim-code-runner" authors = [ {name = "Zahar Chernenko", email = "zaharchernenko35@gmail.com"}, ] -version = "1.0.3" +version = "1.0.4" requires-python = ">=3.10" [dependency-groups] diff --git a/python_coderunner/src/config/validator/dispatchers_order_validator.py b/python_coderunner/src/config/validator/dispatchers_order_validator.py index aca3a41..2d74dcd 100644 --- a/python_coderunner/src/config/validator/dispatchers_order_validator.py +++ b/python_coderunner/src/config/validator/dispatchers_order_validator.py @@ -15,4 +15,4 @@ def __call__(self, value: Any) -> list[EDispatchersTypes]: if invalid_items := [v for v in value if v not in self._ALLOWED_DISPATCHER_TYPES]: raise ValidationError(f"Invalid dispatcher types values: {', '.join(map(str, invalid_items))}.") - return value + return [EDispatchersTypes(v) for v in value]