Skip to content

Commit f46c6fe

Browse files
committed
Add perl formatting support using Perltidy
Signed-off-by: liuyehcf <[email protected]>
1 parent 6fa1616 commit f46c6fe

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ codefmt` if codefmt is installed (and helptags have been generated).
3535
* Markdown (prettier)
3636
* Nix (nixpkgs-fmt)
3737
* OCaml ([ocamlformat](https://github.com/ocaml-ppx/ocamlformat))
38+
* Perl ([perltidy](https://perltidy.sourceforge.netx))
3839
* Protocol Buffers (clang-format)
3940
* Python (Autopep8, Black, isort, or YAPF)
4041
* Ruby ([rubocop](https://rubocop.org))

Diff for: autoload/codefmt/perltidy.vim

+54
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

Diff for: instant/flags.vim

+4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ call s:plugin.Flag('gn_executable', 'gn')
107107
" The path to the buildifier executable.
108108
call s:plugin.Flag('buildifier_executable', 'buildifier')
109109

110+
""
111+
" The path to the perltidy executable.
112+
call s:plugin.Flag('perltidy_executable', 'perltidy')
113+
110114
""
111115
" The lint_mode for buildifier. passed to buildifier --lint parameter.
112116
"

Diff for: plugin/register.vim

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
" * lua: luaformatterfiveone
4545
" * nix: nixpkgs-fmt
4646
" * ocaml: ocamlformat
47+
" * perl: perltidy
4748
" * python: autopep8, black, yapf
4849
" * ruby: rubocop
4950
" * rust: rustfmt
@@ -80,6 +81,7 @@ call s:registry.AddExtension(codefmt#ktfmt#GetFormatter())
8081
call s:registry.AddExtension(codefmt#luaformatterfiveone#GetFormatter())
8182
call s:registry.AddExtension(codefmt#nixpkgs_fmt#GetFormatter())
8283
call s:registry.AddExtension(codefmt#autopep8#GetFormatter())
84+
call s:registry.AddExtension(codefmt#perltidy#GetFormatter())
8385
call s:registry.AddExtension(codefmt#isort#GetFormatter())
8486
call s:registry.AddExtension(codefmt#black#GetFormatter())
8587
call s:registry.AddExtension(codefmt#yapf#GetFormatter())

Diff for: vroom/perltidy.vroom

+38
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)