Skip to content

Commit

Permalink
Add prompt editing for shell scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
w0rp committed Sep 17, 2023
1 parent 1b5ac4e commit f8b9a4d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
44 changes: 44 additions & 0 deletions autoload/neural/pre_process/sh.vim
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
36 changes: 36 additions & 0 deletions test/vim/pre_process/test_sh.vader
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

0 comments on commit f8b9a4d

Please sign in to comment.