-
Notifications
You must be signed in to change notification settings - Fork 104
feat: add zprint formatter for clojure #130
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
| " Copyright 2019 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. | ||
|
|
||
| "" | ||
| " @section Recommended zprint mappings, mappings-zprint | ||
| " @parentsection mappings | ||
| " | ||
| " Since zprint only works on top-level Clojure forms, it doesn't make sense to | ||
| " format line ranges that aren't complete forms. If you're using vim-sexp | ||
| " (https://github.com/guns/vim-sexp), the following mapping replaces the default | ||
| " "format the current line" with "format the current top-level form." > | ||
| " autocmd FileType clojure nmap <buffer> <silent> <leader>== <leader>=iF | ||
| " < | ||
|
|
||
|
|
||
| let s:plugin = maktaba#plugin#Get('codefmt') | ||
|
|
||
|
|
||
| "" | ||
| " @private | ||
| " Formatter: zprint | ||
| function! codefmt#zprint#GetFormatter() abort | ||
| let l:formatter = { | ||
| \ 'name': 'zprint', | ||
| \ 'setup_instructions': | ||
| \ 'Install zprint filter (https://github.com/kkinnear/zprint) ' . | ||
| \ 'and configure the zprint_executable flag'} | ||
|
|
||
| function l:formatter.IsAvailable() abort | ||
| return executable(s:plugin.Flag('zprint_executable')) | ||
| endfunction | ||
|
|
||
| function l:formatter.AppliesToBuffer() abort | ||
| return &filetype is# 'clojure' | ||
| endfunction | ||
|
|
||
| "" | ||
| " Reformat the current buffer with zprint or the binary named in | ||
| " @flag(zprint_executable), only targeting the range between {startline} and | ||
| " {endline}. | ||
| function l:formatter.FormatRange(startline, endline) abort | ||
| " Must be upper-cased to call as a function | ||
| let l:ZprintOptions = s:plugin.Flag('zprint_options') | ||
| if type(l:ZprintOptions) is# type([]) | ||
| " Assign upper-case to lower-case | ||
| let l:zprint_options = l:ZprintOptions | ||
| elseif maktaba#value#IsCallable(l:ZprintOptions) | ||
| " Call upper-case to assign lower-case | ||
| let l:zprint_options = maktaba#function#Call(l:ZprintOptions) | ||
| else | ||
| throw maktaba#error#WrongType( | ||
| \ 'zprint_options flag must be list or callable. Found %s', | ||
| \ string(l:ZprintOptions)) | ||
| endif | ||
| let l:cmd = [s:plugin.Flag('zprint_executable')] | ||
| call extend(l:cmd, l:zprint_options) | ||
|
|
||
| call maktaba#ensure#IsNumber(a:startline) | ||
| call maktaba#ensure#IsNumber(a:endline) | ||
| let l:lines = getline(1, line('$')) | ||
|
|
||
| " zprint doesn't support formatting a range of lines, so format the range | ||
agriffis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| " individually, ignoring context. This works well for top-level forms, although it's | ||
| " not ideal for inner forms because it loses the indentation. | ||
| let l:input = join(l:lines[a:startline - 1 : a:endline - 1], "\n") | ||
|
|
||
| " Prepare the syscall, changing to the containing directory in case the user | ||
| " has configured {:search-config? true} in ~/.zprintrc | ||
| let l:result = maktaba#syscall#Create(l:cmd).WithCwd(expand('%:p:h')).WithStdin(l:input).Call() | ||
| let l:formatted = split(l:result.stdout, "\n") | ||
|
|
||
| " Special case empty slice: neither l:lines[:0] nor l:lines[:-1] is right. | ||
| let l:before = a:startline > 1 ? l:lines[ : a:startline - 2] : [] | ||
| let l:full_formatted = l:before + l:formatted + l:lines[a:endline :] | ||
|
|
||
| call maktaba#buffer#Overwrite(1, line('$'), l:full_formatted) | ||
| endfunction | ||
|
|
||
| return l:formatter | ||
| endfunction | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,93 @@ | ||
| The zprint formatter knows how to format Clojure. | ||
| If you aren't familiar with basic codefmt usage yet, see main.vroom first. | ||
|
|
||
| We'll set up codefmt and configure the vroom environment, then jump into some | ||
| examples. | ||
|
|
||
| :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) | ||
|
|
||
| The zprint formatter expects the zprint executable to be installed on your system. | ||
|
|
||
| :FormatCode zprint | ||
| ! cd .* zprint .* | ||
|
|
||
| The name or path of the zprint executable can be configured via the | ||
| zprint_executable flag if the default of "zprint" doesn't work. | ||
|
|
||
| :Glaive codefmt zprint_executable='/usr/local/bin/zprint' | ||
| :FormatCode zprint | ||
| ! cd .* /usr/local/bin/zprint .* | ||
| :Glaive codefmt zprint_executable='zprint' | ||
|
|
||
| You can format an entire buffer with :FormatCode. | ||
|
|
||
| @clear | ||
| % (defn x [] (cond nil 1 :else 2))<CR> | ||
| |(defn y [] (cond nil 3 :else 4)) | ||
|
|
||
| :FormatCode zprint | ||
| ! cd .* zprint .* | ||
| $ (defn x | ||
| $ [] | ||
| $ (cond nil 1 | ||
| $ :else 2)) | ||
| $ (defn y | ||
| $ [] | ||
| $ (cond nil 3 | ||
| $ :else 4)) | ||
| (defn x | ||
| [] | ||
| (cond nil 1 | ||
| :else 2)) | ||
| (defn y | ||
| [] | ||
| (cond nil 3 | ||
| :else 4)) | ||
| @end | ||
|
|
||
| You can format specific line ranges using :FormatLines. (Since zprint works on | ||
| top-level forms, the range of lines should be one or more complete forms, | ||
| otherwise zprint will generate an error or incorrectly-formatted code.) | ||
|
|
||
| @clear | ||
| % (defn x [] (cond nil 1 :else 2))<CR> | ||
| |(defn y [] (cond nil 3 :else 4))<CR> | ||
| |(defn z [] (cond nil 5 :else 6)) | ||
|
|
||
| :2,2FormatLines zprint | ||
| ! cd .* zprint .* | ||
| $ (defn y | ||
| $ [] | ||
| $ (cond nil 3 | ||
| $ :else 4)) | ||
| (defn x [] (cond nil 1 :else 2)) | ||
| (defn y | ||
| [] | ||
| (cond nil 3 | ||
| :else 4)) | ||
| (defn z [] (cond nil 5 :else 6)) | ||
| @end | ||
|
|
||
| Zprint is the default formatter for the clojure file type, so calling | ||
| :FormatCode or :FormatLines will use it automatically. | ||
|
|
||
| :set filetype=clojure | ||
| :FormatCode | ||
| ! cd .* zprint .* | ||
| :set filetype& | ||
|
|
||
| The default setting of zprint_options propagates Vim's textwidth setting to | ||
| zprint's command-line. | ||
|
|
||
| :set textwidth=123 | ||
| :FormatCode zprint | ||
| ! cd .* zprint .*:width 123.* | ||
| :set textwidth& |
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.
Note to self: Build a new vimdoc release. I just found that the
@parentsectiondirective isn't supported yet in the latest vimdoc 0.6.0 release.