diff --git a/autoload/neural/pre_process/sh.vim b/autoload/neural/pre_process/sh.vim new file mode 100644 index 0000000..dd6c40f --- /dev/null +++ b/autoload/neural/pre_process/sh.vim @@ -0,0 +1,44 @@ +" Author: w0rp +" Description: Pre-processing rules for shell scripts + +function! neural#pre_process#sh#GetScriptType(buffer) abort + let l:current_syntax = getbufvar(a:buffer, 'current_syntax', '') + + if getbufvar(a:buffer, 'is_bash', 0) + return 'bash' + endif + + if l:current_syntax is# 'zsh' + return 'zsh' + endif + + if getbufvar(a:buffer, 'is_kornshell', 0) + " https://www.youtube.com/watch?v=jRGrNDV2mKc + return 'ksh' + endif + + return 'sh' +endfunction + +function! neural#pre_process#sh#GetScriptTypePrefix(script_type) abort + if a:script_type is# 'bash' + return 'Write Bash syntax. ' + endif + + if a:script_type is# 'zsh' + return 'Write zsh syntax. ' + endif + + if a:script_type is# 'ksh' + return 'Write Kornshell syntax. ' + endif + + return 'Write shell script syntax. ' +endfunction + +function! neural#pre_process#sh#Process(buffer, input) abort + let l:script_type = neural#pre_process#sh#GetScriptType(a:buffer) + let l:prefix = neural#pre_process#sh#GetScriptTypePrefix(l:script_type) + + let a:input.prompt = l:prefix . a:input.prompt +endfunction diff --git a/test/vim/pre_process/test_sh.vader b/test/vim/pre_process/test_sh.vader new file mode 100644 index 0000000..6a1f320 --- /dev/null +++ b/test/vim/pre_process/test_sh.vader @@ -0,0 +1,36 @@ +Before: + Save b:current_syntax + + let g:input = {'prompt': 'Do something.'} + call neural#config#Load() + +After: + Restore + + unlet! g:input + unlet! b:is_bash + unlet! b:is_kornshell + +Given sh(An empty shell Script): +Execute(sh filetypes should ask for shell script syntax by default): + call neural#PreProcess(bufnr(''), g:input) + + AssertEqual 'Write shell script syntax. Do something.', g:input.prompt + +Execute(sh filetypes should ask for Bash script syntax for Bash files): + let b:is_bash = 1 + call neural#PreProcess(bufnr(''), g:input) + + AssertEqual 'Write Bash syntax. Do something.', g:input.prompt + +Execute(sh filetypes should ask for zsh script syntax for zsh files): + let b:current_syntax = 'zsh' + call neural#PreProcess(bufnr(''), g:input) + + AssertEqual 'Write zsh syntax. Do something.', g:input.prompt + +Execute(sh filetypes should ask for ksh script syntax for ksh files): + let b:is_kornshell = 1 + call neural#PreProcess(bufnr(''), g:input) + + AssertEqual 'Write Kornshell syntax. Do something.', g:input.prompt