Skip to content

Commit

Permalink
Add perl formatting support using Perltidy
Browse files Browse the repository at this point in the history
Signed-off-by: liuyehcf <[email protected]>
  • Loading branch information
liuyehcf committed Jun 19, 2024
1 parent 6fa1616 commit f46c6fe
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ codefmt` if codefmt is installed (and helptags have been generated).
* Markdown (prettier)
* Nix (nixpkgs-fmt)
* OCaml ([ocamlformat](https://github.com/ocaml-ppx/ocamlformat))
* Perl ([perltidy](https://perltidy.sourceforge.netx))
* Protocol Buffers (clang-format)
* Python (Autopep8, Black, isort, or YAPF)
* Ruby ([rubocop](https://rubocop.org))
Expand Down
54 changes: 54 additions & 0 deletions autoload/codefmt/perltidy.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
" Copyright 2023 Google Inc. All rights reserved.
"
" Licensed under the Apache License, Version 2.0 (the "License");
" you may not use this file except in compliance with the License.
" You may obtain a copy of the License at
"
" http://www.apache.org/licenses/LICENSE-2.0
"
" Unless required by applicable law or agreed to in writing, software
" distributed under the License is distributed on an "AS IS" BASIS,
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
" See the License for the specific language governing permissions and
" limitations under the License.

let s:plugin = maktaba#plugin#Get('codefmt')


""
" @private
" Formatter: perltidy
function! codefmt#perltidy#GetFormatter() abort
let l:formatter = {
\ 'name': 'perltidy',
\ 'setup_instructions': 'Install perltidy ' .
\ '(https://perltidy.sourceforge.net/INSTALL.html).'}

function l:formatter.IsAvailable() abort
return executable(s:plugin.Flag('perltidy_executable'))
endfunction

function l:formatter.AppliesToBuffer() abort
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'perl')
endfunction

""
" Reformat the current buffer with perltidy or the binary named in
" @flag(perltidy_executable), only targeting the range between {startline} and
" {endline}.
" @throws ShellError
function l:formatter.FormatRange(startline, endline) abort
let l:executable = s:plugin.Flag('perltidy_executable')

call maktaba#ensure#IsNumber(a:startline)
call maktaba#ensure#IsNumber(a:endline)

" Perltidy does not support range formatting.
call codefmt#formatterhelpers#AttemptFakeRangeFormatting(
\ a:startline,
\ a:endline,
\ [l:executable, '-'])
endfunction

return l:formatter
endfunction
4 changes: 4 additions & 0 deletions instant/flags.vim
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ call s:plugin.Flag('gn_executable', 'gn')
" The path to the buildifier executable.
call s:plugin.Flag('buildifier_executable', 'buildifier')

""
" The path to the perltidy executable.
call s:plugin.Flag('perltidy_executable', 'perltidy')

""
" The lint_mode for buildifier. passed to buildifier --lint parameter.
"
Expand Down
2 changes: 2 additions & 0 deletions plugin/register.vim
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
" * lua: luaformatterfiveone
" * nix: nixpkgs-fmt
" * ocaml: ocamlformat
" * perl: perltidy
" * python: autopep8, black, yapf
" * ruby: rubocop
" * rust: rustfmt
Expand Down Expand Up @@ -80,6 +81,7 @@ call s:registry.AddExtension(codefmt#ktfmt#GetFormatter())
call s:registry.AddExtension(codefmt#luaformatterfiveone#GetFormatter())
call s:registry.AddExtension(codefmt#nixpkgs_fmt#GetFormatter())
call s:registry.AddExtension(codefmt#autopep8#GetFormatter())
call s:registry.AddExtension(codefmt#perltidy#GetFormatter())
call s:registry.AddExtension(codefmt#isort#GetFormatter())
call s:registry.AddExtension(codefmt#black#GetFormatter())
call s:registry.AddExtension(codefmt#yapf#GetFormatter())
Expand Down
38 changes: 38 additions & 0 deletions vroom/perltidy.vroom
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Perltidy is a linter and formatter for perl.
If you aren't familiar with basic codefmt usage yet, see main.vroom first.

First, set up the vroom environment.

:source $VROOMDIR/setupvroom.vim

:let g:repeat_calls = []
:function FakeRepeat(...)<CR>
| call add(g:repeat_calls, a:000)<CR>
:endfunction
:call maktaba#test#Override('repeat#set', 'FakeRepeat')

:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0)

By default, the perltidy executable is called. To use this plugin, perltidy
must be installed on your system. (But not for testing; vroom intercepts
system calls.)
:FormatCode perltidy
! perltidy .*



The name and path of the Perltidy executable can be configured with a flag:
:Glaive codefmt perltidy_executable=some_other_program
:FormatCode perltidy
! some_other_program .*
:Glaive codefmt perltidy_executable=perltidy



Perltidy does basic whitespace management.
% my @nums = (1 .. 5);<CR>
:FormatCode perltidy
! perltidy .*
$ =========
$ my @nums = ( 1 .. 5 );
my @nums = ( 1 .. 5 );

0 comments on commit f46c6fe

Please sign in to comment.