Skip to content

Commit d7668cb

Browse files
committed
Add perl formatting support using Perltidy
Signed-off-by: liuyehcf <1559500551@qq.com>
1 parent 4c23304 commit d7668cb

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ helpfiles in the `doc/` directory. The helpfiles are also available via
2626
* JSON (js-beautify)
2727
* Kotlin ([ktfmt](https://github.com/facebookincubator/ktfmt))
2828
* OCaml ([ocamlformat](https://github.com/ocaml-ppx/ocamlformat))
29+
* Perl ([perltidy](https://perltidy.sourceforge.netx))
2930
* Proto (clang-format)
3031
* Python (Autopep8, Black, or YAPF)
3132
* Ruby ([rubocop](https://rubocop.org))

autoload/codefmt/perltidy.vim

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
" Copyright 2023 Google Inc. All rights reserved.
2+
"
3+
" Licensed under the Apache License, Version 2.0 (the "License");
4+
" you may not use this file except in compliance with the License.
5+
" You may obtain a copy of the License at
6+
"
7+
" http://www.apache.org/licenses/LICENSE-2.0
8+
"
9+
" Unless required by applicable law or agreed to in writing, software
10+
" distributed under the License is distributed on an "AS IS" BASIS,
11+
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
" See the License for the specific language governing permissions and
13+
" limitations under the License.
14+
15+
let s:plugin = maktaba#plugin#Get('codefmt')
16+
17+
18+
""
19+
" @private
20+
" Formatter: perltidy
21+
function! codefmt#perltidy#GetFormatter() abort
22+
let l:formatter = {
23+
\ 'name': 'perltidy',
24+
\ 'setup_instructions': 'Install perltidy ' .
25+
\ '(https://perltidy.sourceforge.net/INSTALL.html).'}
26+
27+
function l:formatter.IsAvailable() abort
28+
return executable(s:plugin.Flag('perltidy_executable'))
29+
endfunction
30+
31+
function l:formatter.AppliesToBuffer() abort
32+
return codefmt#formatterhelpers#FiletypeMatches(&filetype, 'perl')
33+
endfunction
34+
35+
""
36+
" Reformat the current buffer with perltidy or the binary named in
37+
" @flag(perltidy_executable), only targeting the range between {startline} and
38+
" {endline}.
39+
" @throws ShellError
40+
function l:formatter.FormatRange(startline, endline) abort
41+
let l:executable = s:plugin.Flag('perltidy_executable')
42+
43+
call maktaba#ensure#IsNumber(a:startline)
44+
call maktaba#ensure#IsNumber(a:endline)
45+
46+
" Perltidy does not support range formatting.
47+
call codefmt#formatterhelpers#AttemptFakeRangeFormatting(
48+
\ a:startline,
49+
\ a:endline,
50+
\ [l:executable, '-'])
51+
endfunction
52+
53+
return l:formatter
54+
endfunction

instant/flags.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ call s:plugin.Flag('gn_executable', 'gn')
105105
" The path to the buildifier executable.
106106
call s:plugin.Flag('buildifier_executable', 'buildifier')
107107

108+
""
109+
" The path to the perltidy executable.
110+
call s:plugin.Flag('perltidy_executable', 'perltidy')
111+
108112
""
109113
" The lint_mode for buildifier. passed to buildifier --lint parameter.
110114
"

plugin/register.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
" * lua: luaformatterfiveone
4444
" * nix: nixpkgs-fmt
4545
" * ocaml: ocamlformat
46+
" * perl: perltidy
4647
" * python: autopep8, black, yapf
4748
" * ruby: rubocop
4849
" * rust: rustfmt
@@ -78,6 +79,7 @@ call s:registry.AddExtension(codefmt#ktfmt#GetFormatter())
7879
call s:registry.AddExtension(codefmt#luaformatterfiveone#GetFormatter())
7980
call s:registry.AddExtension(codefmt#nixpkgs_fmt#GetFormatter())
8081
call s:registry.AddExtension(codefmt#autopep8#GetFormatter())
82+
call s:registry.AddExtension(codefmt#perltidy#GetFormatter())
8183
call s:registry.AddExtension(codefmt#isort#GetFormatter())
8284
call s:registry.AddExtension(codefmt#black#GetFormatter())
8385
call s:registry.AddExtension(codefmt#yapf#GetFormatter())

vroom/perltidy.vroom

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Perltidy is a linter and formatter for perl.
2+
If you aren't familiar with basic codefmt usage yet, see main.vroom first.
3+
4+
First, set up the vroom environment.
5+
6+
:source $VROOMDIR/setupvroom.vim
7+
8+
:let g:repeat_calls = []
9+
:function FakeRepeat(...)<CR>
10+
| call add(g:repeat_calls, a:000)<CR>
11+
:endfunction
12+
:call maktaba#test#Override('repeat#set', 'FakeRepeat')
13+
14+
:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0)
15+
16+
By default, the perltidy executable is called. To use this plugin, perltidy
17+
must be installed on your system. (But not for testing; vroom intercepts
18+
system calls.)
19+
:FormatCode perltidy
20+
! perltidy .*
21+
22+
23+
24+
The name and path of the Perltidy executable can be configured with a flag:
25+
:Glaive codefmt perltidy_executable=some_other_program
26+
:FormatCode perltidy
27+
! some_other_program .*
28+
:Glaive codefmt perltidy_executable=perltidy
29+
30+
31+
32+
Perltidy does basic whitespace management.
33+
% my @nums = (1 .. 5);<CR>
34+
:FormatCode perltidy
35+
! perltidy .*
36+
$ =========
37+
$ my @nums = ( 1 .. 5 );
38+
my @nums = ( 1 .. 5 );

0 commit comments

Comments
 (0)