-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close #31 - Feat: Add neural completion buffer
Add a buffer that invokes a neural completion for the entire buffer contents. * Tech: Split neural prompt and run and add line options * Config: Add initialisation settings for neural buffer * Config: Add completion trigger plug mapping for neural buffer * Fix: prompt ui escape keys * Test: Add filetype in test vimrc for neural buffer tests
- Loading branch information
Showing
12 changed files
with
262 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ | |
/env | ||
__pycache__ | ||
tags | ||
# pyenv | ||
.python-version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
" Author: Anexon <[email protected]> | ||
" Description: A Neural Scratch Buffer acts as a playground for interacting with | ||
" Neural sources directly, sending all content of the buffer to the source. | ||
|
||
scriptencoding utf-8 | ||
|
||
call neural#config#Load() | ||
|
||
function! s:GetOptions(options_dict_string) abort | ||
call neural#config#Load() | ||
|
||
" TODO: Set buffer name based on source. | ||
let l:options = { | ||
\ 'name': 'Neural Buffer', | ||
\ 'create_mode': g:neural.buffer.create_mode, | ||
\ 'wrap': g:neural.buffer.wrap, | ||
\} | ||
|
||
" Override default options for the buffer instance. | ||
if !empty(a:options_dict_string) | ||
let l:options_dict = eval(a:options_dict_string) | ||
|
||
if has_key(l:options_dict, 'name') | ||
let l:options.name = l:options_dict.name | ||
endif | ||
|
||
if has_key(l:options_dict, 'create_mode') | ||
let l:options.create_mode = l:options_dict.create_mode | ||
endif | ||
|
||
if has_key(l:options_dict, 'wrap') | ||
let l:options.wrap = l:options_dict.wrap | ||
endif | ||
endif | ||
|
||
return l:options | ||
endfunction | ||
|
||
function! neural#buffer#CreateBuffer(options) abort | ||
let l:buffer_options = s:GetOptions(a:options) | ||
" echo l:buffer_options.name | ||
" echo bufnr(l:buffer_options.name) | ||
|
||
" TODO: Add auto incrementing buffer names instead of switching. | ||
if bufexists(l:buffer_options.name) | ||
execute 'buffer' bufnr(l:buffer_options.name) | ||
else | ||
if l:buffer_options.create_mode is# 'vertical' | ||
vertical new | ||
elseif l:buffer_options.create_mode is# 'horizontal' | ||
new | ||
else | ||
call neural#OutputErrorMessage('Invalid create mode for Neural Buffer. Must be horizontal or vertical.') | ||
endif | ||
|
||
if l:buffer_options.wrap | ||
setlocal wrap linebreak | ||
else | ||
setlocal nowrap nolinebreak | ||
endif | ||
|
||
execute 'file ' . escape(l:buffer_options.name, ' ') | ||
setlocal filetype=neuralbuf | ||
setlocal buftype=nofile | ||
setlocal bufhidden=hide | ||
setlocal noswapfile | ||
endif | ||
|
||
" Switch into insert mode when entering the buffer | ||
startinsert | ||
endfunction | ||
|
||
function! neural#buffer#RunBuffer() abort | ||
let l:buffer_contents = join(getline(1, '$'), "\n") | ||
let l:options = { | ||
\ 'line': line('$'), | ||
\ 'echo': 0, | ||
\} | ||
|
||
if &filetype is# 'neuralbuf' | ||
call neural#Run(l:buffer_contents, l:options) | ||
endif | ||
endfunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
" Author: Anexon <[email protected]> | ||
" Description: Neural Buffer for interacting with neural sources directly. | ||
|
||
call neural#config#Load() | ||
|
||
command! -buffer -nargs=0 NeuralRun :call neural#buffer#RunBuffer() | ||
|
||
nnoremap <buffer> <Plug>(neural_completion) :NeuralRun<Return> | ||
" Keybindings of Neural Buffer | ||
if exists('*keytrans') && exists('g:neural.buffer.completion_key') | ||
execute 'nnoremap ' . keytrans(g:neural.buffer.completion_key) . ' <Plug>(neural_completion)' | ||
execute 'inoremap ' . keytrans(g:neural.buffer.completion_key) . ' <Esc><Plug>(neural_completion)' | ||
else | ||
nnoremap <C-CR> <Plug>(neural_completion) | ||
inoremap <C-CR> <Esc><Plug>(neural_completion) | ||
endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
local next = next | ||
|
||
-- External dependencies | ||
local UI = {} | ||
local AnimatedSign = {} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
Before: | ||
Save g:neural | ||
|
||
runtime autoload/neural.vim | ||
|
||
unlet! g:neural | ||
|
||
let g:calls = [] | ||
|
||
function! neural#Run(prompt, options) abort | ||
call add(g:calls, ['neural#Run', a:prompt, a:options]) | ||
endfunction | ||
|
||
After: | ||
unlet! g:calls | ||
|
||
runtime autoload/neural/job.vim | ||
|
||
Restore | ||
|
||
Execute(It should create a neural buffer with default settings): | ||
NeuralBuffer | ||
|
||
AssertEqual bufexists('Neural Buffer'), 1 | ||
AssertEqual &filetype, 'neuralbuf' | ||
" TODO: Assert if created with new or vertical new | ||
AssertEqual &l:wrap, 1 | ||
AssertEqual &l:linebreak, 1 | ||
|
||
bdelete! Neural Buffer | ||
|
||
Execute(It should create a neural buffer with arguments): | ||
NeuralBuffer {"name": "Test Name", "create_mode": "horizontal", "wrap": v:false} | ||
|
||
AssertEqual bufexists('Test Name'), 1 | ||
AssertEqual &filetype, 'neuralbuf' | ||
" TODO: Assert if created with new or vertical new | ||
AssertEqual &l:wrap, 0 | ||
AssertEqual &l:linebreak, 0 | ||
|
||
bdelete! Test Name | ||
|
||
Given neuralbuf(A Neural buffer): | ||
write a story | ||
Execute(It should correctly run neural): | ||
NeuralRun | ||
|
||
AssertEqual | ||
\ [ | ||
\ ['neural#Run', 'write a story', {'line': 1, 'echo': 0}], | ||
\ ], | ||
\ g:calls | ||
|
||
|
||
" Plug mappings | ||
Execute(The correct neural buffer keybinds should be configured): | ||
redir => g:output | ||
silent map <Plug>(neural_completion) | ||
redir END | ||
|
||
AssertEqual | ||
\ [ | ||
\ 'n <Plug>(neural_completion) *@:NeuralRun<CR>', | ||
\ ], | ||
\ sort(split(g:output, "\n")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.