Skip to content

Commit

Permalink
Suggestion for a better python support (#274)
Browse files Browse the repository at this point in the history
Co-authored-by: Andreas Hofer <[email protected]>
  • Loading branch information
incoggnito and one-shot-monkey authored Apr 19, 2020
1 parent e011fa1 commit 8a3a0fb
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 8 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
### 19/04/2020
- `g:neoterm_repl_commands` supports lists
- neoterm#repl#exec supports filetype specific execution
- Fix problems sending code blocks with blank lines to ipython or python repl
([\#273](https://github.com/kassio/neoterm/issues/273))
- Paste Magic to send a code block to ipthon cells
([\#270](https://github.com/kassio/neoterm/issues/270))
- Added a python specfic repl file

### 17/04/2020
- Update how _last active_ is updated. Also document when it's updated.
### 14/04/2020
Expand All @@ -10,6 +19,7 @@
- Add `:Tls` to list neoterm instances
- Change the ID calculation. To avoid big ID numbers, the ID is reseted to 1
when there is no instance of a neoterm windows open.

### 12/04/2020
- [neovim only] Add neoterm id on the buffer name. ([\#272](https://github.com/kassio/neoterm/issues/272))
- Fix `:Tclose` and `:TcloseAll`. When the default behavior changed to not
Expand Down
17 changes: 13 additions & 4 deletions autoload/neoterm/repl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ function! neoterm#repl#term(id)
if has_key(g:neoterm.instances, a:id)
let g:neoterm.repl.instance_id = a:id
let g:neoterm.repl.loaded = 1

if !empty(get(g:, 'neoterm_repl_command', ''))
\ && g:neoterm_auto_repl_cmd
\ && !g:neoterm_direct_open_repl
call neoterm#exec({
\ 'cmd': [g:neoterm_repl_command, g:neoterm_eof],
\ 'cmd': g:neoterm_repl_command,
\ 'target': g:neoterm.repl.instance().id
\ })
end
Expand All @@ -39,7 +38,11 @@ function! neoterm#repl#term(id)
endfunction

function! neoterm#repl#set(value)
let g:neoterm_repl_command = a:value
if type(a:value) == v:t_list
let g:neoterm_repl_command = add(a:value, g:neoterm_eof)
else
let g:neoterm_repl_command = [a:value, g:neoterm_eof]
endif
endfunction

function! neoterm#repl#selection()
Expand Down Expand Up @@ -71,5 +74,11 @@ function! neoterm#repl#opfunc(type)
endfunction

function! g:neoterm.repl.exec(command)
call g:neoterm.repl.instance().exec(add(a:command, g:neoterm_eof))
let l:ft_exec = printf('neoterm#repl#%s#exec', &filetype)
try
let ExecByFiletype = function(l:ft_exec)
call ExecByFiletype(a:command)
catch /^Vim\%((\a\+)\)\=:E117/
call g:neoterm.repl.instance().exec(add(a:command, g:neoterm_eof))
endtry
endfunction
63 changes: 63 additions & 0 deletions autoload/neoterm/repl/python.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
if !exists('g:neoterm_repl_enable_ipython_paste_magic')
let g:neoterm_repl_enable_ipython_paste_magic = 0
end

function! neoterm#repl#python#is_valid(value)
let l:cmd = type(a:value) == v:t_list ? join(a:value) : a:value

if l:cmd =~# '\<ipython\>'
return executable('ipython')
elseif l:cmd =~# '\<python\>'
return executable('python')
else
return v:false
end
endfunction

function! neoterm#repl#python#exec(command)
if join(g:neoterm_repl_command) =~ "ipython"
if g:neoterm_repl_enable_ipython_paste_magic == 1
let l:cmd = s:ipython_magic_command_for(a:command)
else
let l:cmd = s:ipython_command_for(a:command)
endif
else
let l:cmd = s:python_command_for(a:command)
end

call g:neoterm.repl.instance().exec(add(l:cmd, g:neoterm_eof))
endfunction

function! s:ipython_magic_command_for(command)
call setreg('+', a:command, 'l')
return ["%paste"]
endfunction

function! s:ipython_command_for(command)
let pycommand = filter(a:command, 'v:val !~ "^\\s*$"')
return get(l:, 'ipycommand', pycommand)
endfunction

function! s:python_command_for(command)
let pycommand = filter(a:command, 'v:val !~ "^\\s*$"')
let index = 0
let ipycommand = []
let prev_line = ''

while index < (len(pycommand))
let prev_indent = len(matchstr(prev_line, '^\s*'))
let curr_line = pycommand[index]
let curr_indent = len(matchstr(curr_line, '^\s*'))

if ((prev_indent > curr_indent) && curr_indent == 0)
call add(ipycommand, '')
endif
call add(ipycommand, curr_line)

let prev_line = curr_line
let l:index += 1
endwhile

return get(l:, 'ipycommand', pycommand)

endfunction
17 changes: 15 additions & 2 deletions doc/neoterm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,23 @@ Default value: irb
*g:neoterm_repl_python*

Sets what python REPL will be used, and any arguments to be passed to it.
Defaults to an empty string, in which case NeoTerm will fall back to IPython
followed by Python.
This option accepts either a String or a list of commands. Its default
value is an empty string, which will try to use `ipython` or `python`,
in this order, to execute the REPL.
Default value: (empty)

Example for a valid command list:
`let g:neoterm_repl = ['conda activate venv', 'clear', 'ipython']`

In this example ipython is loaded in a virtual environment.
The screen output is cleared after activating the venv.

*g:neoterm_repl_enable_ipython_paste_magic*

Allows you to paste & execute a pre-formatted code block from clipboard.
This command is currently only supported in ipython.
Default value: 0

*g:neoterm_repl_r*

Sets what R REPL will be used.
Expand Down
3 changes: 1 addition & 2 deletions ftdetect/set_repl_cmd.vim
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ if has('nvim') || has('terminal')
\ end
" Python
au FileType python
\ let s:argList = split(g:neoterm_repl_python) |
\ if len(s:argList) > 0 && executable(s:argList[0]) |
\ if neoterm#repl#python#is_valid(g:neoterm_repl_python) == 1 |
\ call neoterm#repl#set(g:neoterm_repl_python) |
\ elseif executable('ipython') |
\ call neoterm#repl#set('ipython --no-autoindent') |
Expand Down

0 comments on commit 8a3a0fb

Please sign in to comment.