-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add prompt editing for shell scripts
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
" Author: w0rp <devw0rp@gmail.com> | ||
" 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 |
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,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 |