-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add perl formatting support using Perltidy #227
Open
liuyehcf
wants to merge
2
commits into
google:master
Choose a base branch
from
liuyehcf:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
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 .* | ||
$ { "Cursor": 0 } | ||
|
||
|
||
|
||
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 ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to fake the command output like this:
vim-codefmt/vroom/clangformat.vroom
Line 46 in c6730fb
Otherwise the tests can't run without perltidy installed (and may get brittle, failing depending on the version of perltidy installed).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated, and I'm not sure if I'm doing right because I don't really understand the syntax of
.vroom
.