Skip to content
Merged
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()`

Expand Down
164 changes: 89 additions & 75 deletions autoload/coderunner.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,120 +2,57 @@
let s:save_cpo = &cpo
set cpo&vim

command! -range CodeRunnerRun call coderunner#Run(visualmode(), <range>, <line1>, <line2>)
command! -range CodeRunnerRunByFileExt call coderunner#RunByFileExt(visualmode(), <range>, <line1>, <line2>)
command! -range CodeRunnerRunByFileType call coderunner#RunByFileType(visualmode(), <range>, <line1>, <line2>)
command! -range CodeRunnerRunByGlob call coderunner#RunByGlob(visualmode(), <range>, <line1>, <line2>)


let s:script_folder_path = escape(expand('<sfile>: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
Expand Down Expand Up @@ -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(), <range>, <line1>, <line2>)
command! -range CodeRunnerRunByFileExt call coderunner#RunByFileExt(visualmode(), <range>, <line1>, <line2>)
command! -range CodeRunnerRunByFileType call coderunner#RunByFileType(visualmode(), <range>, <line1>, <line2>)
command! -range CodeRunnerRunByGlob call coderunner#RunByGlob(visualmode(), <range>, <line1>, <line2>)
command! CodeRunnerRestart call coderunner#Restart()
endfunction


" This is basic vim plugin boilerplate
let &cpo = s:save_cpo
unlet s:save_cpo
4 changes: 2 additions & 2 deletions plugin/coderunner.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion python_coderunner/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Loading