Skip to content

Commit

Permalink
Add vim indent plugin (#8)
Browse files Browse the repository at this point in the history
* Define commentstring for vim plugin
* Define comments for vim plugin
* Correct commentstring
* Update vim Makefile to handle ftplugin directory
* Rough indentation plugin implementation
* Set indent level of first line
* Improve indentation inside sections
* Indent keywords
* Tidy echoes and comments
* Handle indent plugin in Makefile
* Add 'Stage' keyword
* Tidy comments
* Add handling of apptest section

Co-authored-by: Vanessasaurus <814322+vsoch@users.noreply.github.com>
Co-authored-by: Vanessasaurus <814322+vsoch@users.noreply.github.com>
  • Loading branch information
JimMadge and vsoch committed Sep 13, 2021
1 parent ca59280 commit 1090c0e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
4 changes: 4 additions & 0 deletions vim/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ install:
mkdir -p ~/.vim/syntax
mkdir -p ~/.vim/ftdetect
mkdir -p ~/.vim/ftplugin
mkdir -p ~/.vim/indent
cp syntax/singularity.vim ~/.vim/syntax/
cp ftdetect/singularity.vim ~/.vim/ftdetect/
cp ftplugin/singularity.vim ~/.vim/ftplugin/
cp indent/singularity.vim ~/.vim/indent/

uninstall:
rm ~/.vim/syntax/singularity.vim
rm ~/.vim/ftdetect/singularity.vim
rm ~/.vim/ftlugin/singularity.vim
rm ~/.vim/indent/singularity.vim
rmdir ~/.vim/syntax
rmdir ~/.vim/ftdetect
rmdir ~/.vim/ftplugin
rmdir ~/.vim/indent
70 changes: 70 additions & 0 deletions vim/indent/singularity.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
" Singularity indent file

" Only load this script if no other indent script has been loaded
if exists("b:did_indent")
finish
endif
let b:did_indent=1

" Configure other indent options
setlocal nocindent
setlocal nosmartindent
setlocal autoindent

" Declare function to determine indent level
setlocal indentexpr=GetSingularityIndent(v:lnum)

" Declare which keys trigger the indentation function
setlocal indentkeys=!^F,o,O,0%,<:>

" Indentation level function
" Returns the level of indentation as an integer
" -1 means to retain the current indentation level
function! GetSingularityIndent(lnum)
let lnum = a:lnum

" The first line is at indent level 0
if lnum == 1
return 0
endif

let this_line = getline(lnum)

" Catch indentation triggered when typing %, for section headings set
" indent to level 0
if this_line =~ '^\s*%'
return 0
endif

" Trigger indentation when typing :, if line matches a keyword set indent
" to level 0
if this_line =~ '^\s*\(Bootstrap\|From\|Stage\|OSVersion\|MirrorURL\|Library\|Registry\|Namespace\|IncludeCmd\|Include\):'
return 0
endif


" Search backwards for the section lnum belongs to
let clnum = lnum - 1
while clnum > 0
let current_line = getline(clnum)

" If parent section is one of
" %setup,%files,%post,%test,%environment,%startscript,%runscript,%labels,%help
" increase indent one step
if current_line =~ '^%\(setup\|files\|post\|test\|environment\|startscript\|runscript\|labels\|help\)'
return indent(clnum) + &shiftwidth
endif

" If parent section is one of
" %apprun *,%applabels *,%appinstall *,%appenv *,%apphelp *,%appfiles *
" increase indent one step
if current_line =~ '^%\(apprun\|applabels\|appinstall\|appenv\|apphelp\|appfiles\|apptest\)\s*[a-zA-Z]*'
return indent(clnum) + &shiftwidth
endif

let clnum = clnum - 1
endwhile

" Otherwise return same indent level
return -1
endfunction

0 comments on commit 1090c0e

Please sign in to comment.