Merge pull request #3231 from jhlink/add-astyle-for-c-formatting

Add astyle for C/C++ formatting
This commit is contained in:
Horacio Sanson 2020-07-29 22:08:46 +09:00 committed by GitHub
commit 5338dbf680
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 196 additions and 0 deletions

View file

@ -160,6 +160,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['php'],
\ 'description': 'Fix PHP files with php-cs-fixer.',
\ },
\ 'astyle': {
\ 'function': 'ale#fixers#astyle#Fix',
\ 'suggested_filetypes': ['c', 'cpp'],
\ 'description': 'Fix C/C++ with astyle.',
\ },
\ 'clangtidy': {
\ 'function': 'ale#fixers#clangtidy#Fix',
\ 'suggested_filetypes': ['c', 'cpp', 'objc'],

View file

@ -0,0 +1,59 @@
" Author: James Kim <jhlink@users.noreply.github.com>
" Description: Fix C/C++ files with astyle.
function! s:set_variables() abort
for l:ft in ['c', 'cpp']
call ale#Set(l:ft . '_astyle_executable', 'astyle')
call ale#Set(l:ft . '_astyle_project_options', '')
endfor
endfunction
call s:set_variables()
function! ale#fixers#astyle#Var(buffer, name) abort
let l:ft = getbufvar(str2nr(a:buffer), '&filetype')
let l:ft = l:ft =~# 'cpp' ? 'cpp' : 'c'
return ale#Var(a:buffer, l:ft . '_astyle_' . a:name)
endfunction
" Try to find a project options file.
function! ale#fixers#astyle#FindProjectOptions(buffer) abort
let l:proj_options = ale#fixers#astyle#Var(a:buffer, 'project_options')
" If user has set project options variable then use it and skip any searching.
" This would allow users to use project files named differently than .astylerc.
if !empty(l:proj_options)
return l:proj_options
endif
" Try to find nearest .astylerc file.
let l:proj_options = fnamemodify(ale#path#FindNearestFile(a:buffer, '.astylerc'), ':t')
if !empty(l:proj_options)
return l:proj_options
endif
" Try to find nearest _astylerc file.
let l:proj_options = fnamemodify(ale#path#FindNearestFile(a:buffer, '_astylerc'), ':t')
if !empty(l:proj_options)
return l:proj_options
endif
" If no project options file is found return an empty string.
return ''
endfunction
function! ale#fixers#astyle#Fix(buffer) abort
let l:executable = ale#fixers#astyle#Var(a:buffer, 'executable')
let l:proj_options = ale#fixers#astyle#FindProjectOptions(a:buffer)
let l:command = ' --stdin='
return {
\ 'command': ale#Escape(l:executable)
\ . (empty(l:proj_options) ? '' : ' --project=' . l:proj_options)
\ . l:command
\}
endfunction

View file

@ -59,6 +59,30 @@ g:ale_c_parse_makefile *g:ale_c_parse_makefile*
build flags to use for different files.
===============================================================================
astyle *ale-c-astyle*
g:ale_c_astyle_executable *g:ale_c_astyle_executable*
*b:ale_c_astyle_executable*
Type: |String|
Default: `'astyle'`
This variable can be changed to use a different executable for astyle.
g:ale_c_astyle_project_options *g:ale_c_astyle_project_options*
*b:ale_c_astyle_project_options*
Type: |String|
Default: `''`
This variable can be changed to use an option file for project level
configurations. Provide only the filename of the option file that should be
present at the project's root directory.
For example, if .astylrc is specified, the file is searched in the parent
directories of the source file's directory.
===============================================================================
clang *ale-c-clang*

View file

@ -13,6 +13,30 @@ The following C options also apply to some C++ linters too.
* |g:ale_c_parse_compile_commands|
===============================================================================
astyle *ale-cpp-astyle*
g:ale_cpp_astyle_executable *g:ale_cpp_astyle_executable*
*b:ale_cpp_astyle_executable*
Type: |String|
Default: `'astyle'`
This variable can be changed to use a different executable for astyle.
g:ale_cpp_astyle_project_options *g:ale_cpp_astyle_project_options*
*b:ale_cpp_astyle_project_options*
Type: |String|
Default: `''`
This variable can be changed to use an option file for project level
configurations. Provide only the filename of the option file that should be
present at the project's root directory.
For example, if .astylrc is specified, the file is searched in the parent
directories of the source file's directory.
===============================================================================
clang *ale-cpp-clang*

View file

@ -45,6 +45,7 @@ Notes:
* `shellcheck`
* `shfmt`
* C
* `astyle`
* `ccls`
* `clang`
* `clangd`
@ -62,6 +63,7 @@ Notes:
* `mcsc`!!
* `uncrustify`
* C++ (filetype cpp)
* `astyle`
* `ccls`
* `clang`
* `clangcheck`!!

View file

@ -2284,6 +2284,7 @@ documented in additional help files.
bib.....................................|ale-bib-options|
bibclean..............................|ale-bib-bibclean|
c.......................................|ale-c-options|
astyle................................|ale-c-astyle|
clang.................................|ale-c-clang|
clangd................................|ale-c-clangd|
clang-format..........................|ale-c-clangformat|
@ -2306,6 +2307,7 @@ documented in additional help files.
cmakelint.............................|ale-cmake-cmakelint|
cmake-format..........................|ale-cmake-cmakeformat|
cpp.....................................|ale-cpp-options|
astyle................................|ale-cpp-astyle|
clang.................................|ale-cpp-clang|
clangd................................|ale-cpp-clangd|
clangcheck............................|ale-cpp-clangcheck|

View file

@ -54,6 +54,7 @@ formatting.
* [shellcheck](https://www.shellcheck.net/)
* [shfmt](https://github.com/mvdan/sh)
* C
* [astyle](http://astyle.sourceforge.net/)
* [ccls](https://github.com/MaskRay/ccls)
* [clang](http://clang.llvm.org/)
* [clangd](https://clang.llvm.org/extra/clangd.html)
@ -71,6 +72,7 @@ formatting.
* [mcsc](http://www.mono-project.com/docs/about-mono/languages/csharp/) :floppy_disk: see:`help ale-cs-mcsc` for details and configuration
* [uncrustify](https://github.com/uncrustify/uncrustify)
* C++ (filetype cpp)
* [astyle](http://astyle.sourceforge.net/)
* [ccls](https://github.com/MaskRay/ccls)
* [clang](http://clang.llvm.org/)
* [clangcheck](http://clang.llvm.org/docs/ClangCheck.html) :floppy_disk:

View file

@ -0,0 +1,78 @@
Before:
Save g:ale_c_astyle_executable
Save g:ale_c_astyle_project_options
Save g:ale_cpp_astyle_project_options
" Use an invalid global executable, so we don't match it.
let g:ale_c_astyle_executable = 'xxxinvalid'
let g:ale_cpp_astyle_executable = 'invalidpp'
let g:ale_c_astyle_project_options = ''
let g:ale_cpp_astyle_project_options = ''
call ale#test#SetDirectory('/testplugin/test/fixers')
After:
Restore
call ale#test#RestoreDirectory()
Execute(The astyle callback should return the correct default values):
" Because this file doesn't exist, no astylrc config
" exists near it. Therefore, project_options is empty.
call ale#test#SetFilename('../c_files/testfile.c')
AssertEqual
\ {
\ 'command': ale#Escape(g:ale_c_astyle_executable)
\ . ' --stdin='
\ },
\ ale#fixers#astyle#Fix(bufnr(''))
Execute(The astyle callback should support cpp files):
" Because this file doesn't exist, no astylrc config
" exists near it. Therefore, project_options is empty.
call ale#test#SetFilename('../cpp_files/dummy.cpp')
set filetype=cpp " The test fails without this
AssertEqual
\ {
\ 'command': ale#Escape(g:ale_cpp_astyle_executable)
\ . ' --stdin='
\ },
\ ale#fixers#astyle#Fix(bufnr(''))
Execute(The astyle callback should support cpp files with option file set):
call ale#test#SetFilename('../cpp_files/dummy.cpp')
let g:ale_cpp_astyle_project_options = '.astylerc_cpp'
set filetype=cpp " The test fails without this
AssertEqual
\ {
\ 'command': ale#Escape('invalidpp')
\ . ' --project=' . g:ale_cpp_astyle_project_options
\ . ' --stdin='
\ },
\ ale#fixers#astyle#Fix(bufnr(''))
Execute(The astyle callback should return the correct default values with an option file set):
call ale#test#SetFilename('../c_files/testfile.c')
let g:ale_c_astyle_project_options = '.astylerc_c'
AssertEqual
\ {
\ 'command': ale#Escape('xxxinvalid')
\ . ' --project=' . g:ale_c_astyle_project_options
\ . ' --stdin='
\ },
\ ale#fixers#astyle#Fix(bufnr(''))
Execute(The astyle callback should find nearest default option file _astylrc):
call ale#test#SetFilename('../test_c_projects/makefile_project/subdir/file.c')
AssertEqual
\ {
\ 'command': ale#Escape('xxxinvalid')
\ . ' --project=_astylerc'
\ . ' --stdin='
\ },
\ ale#fixers#astyle#Fix(bufnr(''))