Skip to content

Commit

Permalink
Merge branch 'master' into pr/144
Browse files Browse the repository at this point in the history
  • Loading branch information
lelutin committed Sep 15, 2024
2 parents 228d1dd + c59aa99 commit a5f3ce8
Show file tree
Hide file tree
Showing 28 changed files with 772 additions and 602 deletions.
23 changes: 9 additions & 14 deletions .github/workflows/vader.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,34 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# There exists a vint github action but it can't install the alpha
# version. vint has not been moving since 2020 and we need to use the
# alpha release since it fixed some things wrt the latest stable release.
- uses: actions/setup-python@v1
- name: Install vint
run: pip install --pre vim-vint
- name: Lint vimscript files
run: vint --warning --enable-neovim ./after ./autoload ./compiler ./ftdetect ./ftplugin ./indent ./syntax
- name: Install shellcheck
run: sudo apt-get -y install shellcheck
- name: Lint shell scripts
run: "shellcheck **/*.sh"
- uses: lunarmodules/luacheck@v1
- uses: ludeeus/action-shellcheck@master

tests:
runs-on: ubuntu-latest
needs: lint
strategy:
fail-fast: false
matrix:
#neovim: [false, true]
# When running vader tests in neovim under github's CI, it coredumps
neovim: [false]
editor: ["vim", "nvim"]

steps:
- uses: actions/checkout@v2
- name: Setup Vim or Neovim
- name: Setup ${{ matrix.editor }}
uses: rhysd/action-setup-vim@v1
id: vim
with:
neovim: ${{ matrix.neovim }}
neovim: ${{ matrix.editor == 'nvim' }}
- name: Run Vader unit tests
run: |
if [ "${{ matrix.neovim }}" = "true" ]; then
TESTVIM=nvim
else
TESTVIM=vim
fi
TESTVIM=${{ matrix.editor }}
export TESTVIM
./test/run-tests.sh
17 changes: 17 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- vim: ft=lua tw=80

-- Global objects defined by the C code
read_globals = {
vim = {
other_fields = true,
fields = {
b = {
fields = {
epuppet_subtype = {
read_only = false,
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Provides

* Formatting based on the latest Puppetlabs Style Guide
* Syntax highlighting compatible with puppet 4.x
* by default, highlights errors: mixing spaces and tabs and bad names. If you
don't want this highlighting, add `let g:puppet_display_errors = 0` to your
vimrc.
* Automatic => alignment
* If you don't like that, add `let g:puppet_align_hashes = 0` to your vimrc.
* Ctags support
Expand Down
22 changes: 18 additions & 4 deletions autoload/puppet/align.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ function! puppet#align#LinesInBlock(lnum) abort
let marker = a:lnum - 1
while marker >= 1
let line_text = getline(marker)
let line_indent = puppet#align#IndentLevel(marker)

if line_text =~? '\v\S'
let line_indent = puppet#align#IndentLevel(marker)
if line_indent < indent_level
break
elseif line_indent == indent_level
Expand All @@ -25,9 +25,9 @@ function! puppet#align#LinesInBlock(lnum) abort
let marker = a:lnum
while marker <= line('$')
let line_text = getline(marker)
let line_indent = puppet#align#IndentLevel(marker)

if line_text =~? '\v\S'
let line_indent = puppet#align#IndentLevel(marker)
if line_indent < indent_level
break
elseif line_indent == indent_level
Expand All @@ -52,17 +52,31 @@ function! puppet#align#AlignHashrockets(...) abort
let indent_str = printf('%' . indent(l:lnum) . 's', '')

for line_num in lines_in_block
let data = matchlist(getline(line_num), '^\s*\(.\{-}\S\)\s*=>\s*\(.*\)$')
let text_line = getline(line_num)
" skip comment lines
if matchstr(text_line, '\S') ==# '#'
continue
endif

let data = matchlist(text_line, '^\s*\(.\{-}\S\)\s*=>\s*\(.*\)$')
if !empty(data)
let max_left_len = max([max_left_len, strlen(data[1])])
endif
endfor

for line_num in lines_in_block
let data = matchlist(getline(line_num), '^\s*\(.\{-}\S\)\s*=>\s*\(.*\)$')
let text_line = getline(line_num)
" skip comment lines
if matchstr(text_line, '\S') ==# '#'
continue
endif

let data = matchlist(text_line, '^\s*\(.\{-}\S\)\s*=>\s*\(.*\)$')
if !empty(data)
let new_line = printf('%s%-' . max_left_len . 's => %s', indent_str, data[1], data[2])
call setline(line_num, new_line)
endif
endfor

return lines_in_block
endfunction
32 changes: 22 additions & 10 deletions autoload/puppet/format.vim
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
"
" Simple format using puppet's l:indents and align hashrockets function
function! puppet#format#Format() abort
" only allow reformatting through the gq command
" (e.g. Vim is in normal mode)
if mode() !=# 'n'
" do not fall back to internal formatting
return 0
endif

let l:start_lnum = v:lnum
let l:end_lnum = v:lnum + v:count - 1
" Don't modify indentation or alignment if called by textwidth. We'll only
" let the fallback function do its thing in this case so that textwidth
" still performs the expected feature.
if mode() !~# '[iR]'
call puppet#format#Indention(l:start_lnum, l:end_lnum)
call puppet#format#Hashrocket(l:start_lnum, l:end_lnum)
endif

call puppet#format#Indention(l:start_lnum, l:end_lnum)
call puppet#format#Hashrocket(l:start_lnum, l:end_lnum)
call puppet#format#Fallback(l:start_lnum, l:end_lnum)

" explicitly avoid falling back to default formatting
return 0
endfunction
Expand All @@ -19,12 +23,20 @@ endfunction
" Format hashrockets expressions in every line in range start_lnum and
" end_lnum, both ends included
"
" TODO way of using AlignHashrockets function is ineffective, because it
" formats same lines again and again, find better way to do it
function! puppet#format#Hashrocket(start_lnum, end_lnum) abort
let l:lnum = a:start_lnum
let processed_lines = []
while l:lnum <= a:end_lnum
call puppet#align#AlignHashrockets(l:lnum)
if index(processed_lines, l:lnum) ==# -1
let line_text = getline(l:lnum)
if line_text =~? '\v\S'
let processed_lines += puppet#align#AlignHashrockets(l:lnum)
else
" empty lines make puppet#align#AlignHashrockets reprocess blocks with
" indentation that were already processed so we just skip them
call add(processed_lines, l:lnum)
endif
endif
let l:lnum += 1
endwhile
endfunction
Expand Down
16 changes: 16 additions & 0 deletions ftdetect/puppet.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Some epp files may get marked as "mason" type before this script is reached.
-- Vim's own scripts.vim forces the type if it detects a `<%` at the start of
-- the file. All files ending in .epp should be epuppet
vim.filetype.add({
extension = {
epp =
function(path, bufnr)
local path_wo_epp = path:sub(1,-5)
local matched = vim.filetype.match({ buf = bufnr, filename = path_wo_epp })
if matched ~= nil and matched ~= 'mason' then
vim.b.epuppet_subtype = matched
end
return 'epuppet'
end,
}
})
36 changes: 22 additions & 14 deletions ftdetect/puppet.vim
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
" vint: -ProhibitAutocmdWithNoGroup
" Vim's own filetypes.vim runs before all ftdetect scripts (why?) and matches
" detects the .pp extension as being a 'pascal' file. Since the script uses
" `setf`, we can nullify the filetype detection by removing all commands bound
" to BufRead and BufNewFile for .pp files with `au!`. Hopefully, if there were
" any other commands set they were associated with the pascal type and we want
" to get rid of them.
" However, this has the effect of completely nullifying pascal type detection
" for .pp files.
au! BufRead,BufNewFile *.pp setfiletype puppet
" Some epp files may get marked as "mason" type before this script is reached.
" Vim's own scripts.vim forces the type if it detects a `<%` at the start of
" the file. All files ending in .epp should be epuppet
au BufRead,BufNewFile *.epp setl ft=epuppet
au BufRead,BufNewFile Puppetfile setfiletype ruby
" Vim has fixed puppet vs pascal detection in patch 8.2.2334 so we can rely on
" their type detection from that point on.
if !has('patch-8.2.2334') && !has('nvim-0.5.0')
" Vim's own filetypes.vim runs before all ftdetect scripts (why?) and matches
" detects the .pp extension as being a 'pascal' file. Since the script uses
" `setf`, we can nullify the filetype detection by removing all commands bound
" to BufRead and BufNewFile for .pp files with `au!`. Hopefully, if there were
" any other commands set they were associated with the pascal type and we want
" to get rid of them.
" However, this has the effect of completely nullifying pascal type detection
" for .pp files.
au! BufRead,BufNewFile *.pp setfiletype puppet
endif
" Vim now has autodetection for epuppet and Puppetfile. We only need to add
" autocommands for older versions of vim / neovim
if !has('patch-8.2.2402') && !has('nvim-0.5.0')
" Some epp files may get marked as "mason" type before this script is reached.
" Vim's own scripts.vim forces the type if it detects a `<%` at the start of
" the file. All files ending in .epp should be epuppet
au! BufRead,BufNewFile *.epp setf epuppet
au BufRead,BufNewFile Puppetfile setfiletype ruby
endif
41 changes: 40 additions & 1 deletion ftplugin/epuppet.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,48 @@ set cpo-=C

" Define some defaults in case the included ftplugins don't set them.
let s:undo_ftplugin = ''
let s:browsefilter = "All Files (*.*)\t*.*\n"
if has('win32')
let s:browsefilter = "All Files (*.*)\t*.*\n"
else
let s:browsefilter = "All Files (*)\t*\n"
endif
let s:match_words = ''

if !exists('g:epuppet_default_subtype')
let g:epuppet_default_subtype = 'sh'
endif

if &filetype =~# '^epuppet\.'
let b:epuppet_subtype = matchstr(&filetype,'^epuppet\.\zs\w\+')
elseif !exists('b:epuppet_subtype')
let b:epuppet_subtype = matchstr(substitute(expand('%:t'),'\c\%(\.epp\)\+$','',''),'\.\zs\w\+\%(\ze+\w\+\)\=$')
" TODO instead of listing exceptions like this, can we instead recognize
" extension -> type mapping?
if b:epuppet_subtype ==? 'rhtml'
let b:epuppet_subtype = 'html'
elseif b:epuppet_subtype ==? 'rb'
let b:epuppet_subtype = 'ruby'
elseif b:epuppet_subtype ==? 'yml'
let b:epuppet_subtype = 'yaml'
elseif b:epuppet_subtype ==? 'js'
let b:epuppet_subtype = 'javascript'
elseif b:epuppet_subtype ==? 'txt'
" Conventional; not a real file type
let b:epuppet_subtype = 'text'
elseif b:epuppet_subtype ==? 'py'
let b:epuppet_subtype = 'python'
elseif b:epuppet_subtype ==? 'rs'
let b:epuppet_subtype = 'rust'
elseif b:epuppet_subtype ==?''
let b:epuppet_subtype = g:epuppet_default_subtype
endif
endif

if exists('b:epuppet_subtype') && b:epuppet_subtype != '' && b:epuppet_subtype !=? 'epuppet'
exe 'runtime! ftplugin/'.b:epuppet_subtype.'.vim ftplugin/'.b:epuppet_subtype.'_*.vim ftplugin/'.b:epuppet_subtype.'/*.vim'
endif
unlet! b:did_ftplugin

runtime! ftplugin/sh.vim
unlet! b:did_ftplugin

Expand Down
3 changes: 1 addition & 2 deletions ftplugin/puppet.vim
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ setlocal commentstring=#\ %s
" full resource name, however since : is used in many non-keyword contexts it
" is a bad idea to add it to the option.

setlocal formatoptions-=t formatoptions+=croql
setlocal formatexpr=puppet#format#Format()

let b:undo_ftplugin = '
\ setlocal tabstop< tabstop< softtabstop< shiftwidth< expandtab<
\| setlocal keywordprg< iskeyword< comments< commentstring<
\| setlocal formatoptions< formatexpr<
\| setlocal formatexpr<
\'
37 changes: 34 additions & 3 deletions syntax/epuppet.vim
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,40 @@ if exists('b:current_syntax')
finish
endif

runtime! syntax/sh.vim
unlet! b:current_syntax
if !exists('g:epuppet_default_subtype')
let g:epuppet_default_subtype = 'sh'
endif

if &filetype =~# '^epuppet\.'
let b:epuppet_subtype = matchstr(&filetype,'^epuppet\.\zs\w\+')
elseif !exists('b:epuppet_subtype')
let b:epuppet_subtype = matchstr(substitute(expand('%:t'),'\c\%(\.epp\)\+$','',''),'\.\zs\w\+\%(\ze+\w\+\)\=$')
" TODO instead of listing exceptions like this, can we instead recognize
" extension -> type mapping?
if b:epuppet_subtype ==? 'rhtml'
let b:epuppet_subtype = 'html'
elseif b:epuppet_subtype ==? 'rb'
let b:epuppet_subtype = 'ruby'
elseif b:epuppet_subtype ==? 'yml'
let b:epuppet_subtype = 'yaml'
elseif b:epuppet_subtype ==? 'js'
let b:epuppet_subtype = 'javascript'
elseif b:epuppet_subtype ==? 'txt'
" Conventional; not a real file type
let b:epuppet_subtype = 'text'
elseif b:epuppet_subtype ==? 'py'
let b:epuppet_subtype = 'python'
elseif b:epuppet_subtype ==? 'rs'
let b:epuppet_subtype = 'rust'
elseif b:epuppet_subtype ==? ''
let b:epuppet_subtype = g:epuppet_default_subtype
endif
endif

if exists('b:epuppet_subtype') && b:epuppet_subtype != '' && b:epuppet_subtype !=? 'epuppet'
exe 'runtime! syntax/'.b:epuppet_subtype.'.vim'
unlet! b:current_syntax
endif

syn include @puppetTop syntax/puppet.vim

Expand All @@ -26,4 +58,3 @@ hi def link ePuppetDelimiter PreProc
hi def link ePuppetComment Comment

let b:current_syntax = 'epuppet'

Loading

0 comments on commit a5f3ce8

Please sign in to comment.