-
Notifications
You must be signed in to change notification settings - Fork 9
Clojure formatter #18
base: master
Are you sure you want to change the base?
Changes from all commits
c3b6060
0abfd95
0bd2b99
5b0a68f
f136008
1f4c910
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| " Description: Text formatter for Clojure code. | ||
| " File: zepl/contrib/clojure.vim | ||
| " Author: Paul Fernandez <paul4nandez@gmail.com> | ||
| " Help: :help zepl-clojure | ||
| " Legal: No rights reserved. Public domain. | ||
|
|
||
| function s:stripLeadingEmptyLines(lines) abort | ||
| while !empty(a:lines) | ||
| if a:lines[0] =~# '\m^\s*$' | ||
| call remove(a:lines, 0) | ||
| else | ||
| break | ||
| endif | ||
| endwhile | ||
| return a:lines | ||
| endfunction | ||
|
|
||
| function s:stripTrailingEmptyLines(lines) abort | ||
| let idx = len(a:lines) - 1 | ||
| while idx >= 0 | ||
| if a:lines[idx] =~# '\m^\s*$' | ||
| call remove(a:lines, idx) | ||
| let idx -= 1 | ||
| else | ||
| break | ||
| endif | ||
| endwhile | ||
| return a:lines | ||
| endfunction | ||
|
|
||
| function s:normalizeIndents(lines) abort | ||
| let depth = len(matchstr(a:lines[0], '\m\C^\s*')) | ||
| return map(a:lines, 'v:val[' . depth . ':]') | ||
| endfunction | ||
|
|
||
| function s:processLine(...) abort | ||
| " Remove trailing newlines. | ||
| let line = trim(a:2, "\r\n\<CR>", 2) | ||
|
|
||
| " Remove two leading spaces added by the Clojure repl. | ||
| return substitute(line, '\m^\s\s', '', '') | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using macOS Monterey 12.3.1, and run Vim inside iTerm: I'll go back and strip my vimrc to the bare essentials, and also try it with Gvim and on a Ubuntu machine I have lying around. While I'm at it, I'll dig deeper into the mysterious
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've been pretty busy at work, but today I removed everything except zepl from my vim config and tried it again. I found that the problem is the same in either case, but that my "eliminate two spaces" code only worked because I was always using a two-space indent inside comment forms. I was just covering up the fact that the indentation code that I borrowed from the F# formatter wasn't actually doing anything. I'll come back to this when I have more time and give it a fresh look.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, don't worry, there's no rush. I'm happy to help where needed, but I am quite busy too. |
||
| endfunction | ||
|
|
||
| function zepl#contrib#clojure#formatter(lines) | ||
| let lines = s:stripLeadingEmptyLines(a:lines) | ||
| let lines = s:stripTrailingEmptyLines(lines) | ||
|
|
||
| if empty(lines) | ||
| return '' | ||
| endif | ||
|
|
||
| let lines = s:normalizeIndents(lines) | ||
| let lines = map(lines, function("s:processLine")) | ||
|
|
||
| return join(lines, "\<CR>") . "\<CR>" | ||
| endfunction | ||
|
|
||
| " EXAMPLE: Configure zepl.vim to use the Clojure formatter in Clojure buffers. | ||
| " (Replace 'plugins/' with your plugin directory path.) | ||
| " | ||
| " runtime plugins/zepl.vim/zepl/contrib/clojure.vim | ||
|
Comment on lines
+59
to
+61
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious about this, does just Which plugin manager are you using?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm using vim-plug, which actually uses ~/.vim/plugged/. I wrote |
||
| " let g:repl_config = { | ||
| " \ 'clojure': { | ||
| " \ 'cmd': filereadable('deps.edn') ? 'clj' : 'lein repl', | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works to determine whether we're in a leiningen project or not, but assumes that the user is working from the project root. Nevertheless I think it's a useful example.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. Maybe this example could include the 'rlwrap' flag too? (You don't have to if you'd rather not.) " let g:repl_config = {
" \ 'clojure': {
" \ 'cmd': filereadable('deps.edn') ? 'clj' : 'lein repl',
" \ 'rlwrap': filereadable('deps.edn') ? 1 : 0
" \ 'formatter': function('zepl#contrib#clojure#formatter')
" \ }
" \ }(+ If you decide to update this, don't forget to update the example in the doc too.) |
||
| " \ 'formatter': function('zepl#contrib#clojure#formatter') | ||
| " \ } | ||
| " \ } | ||




Uh oh!
There was an error while loading. Please reload this page.
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.
The code in these three functions are ripped straight from the other formatters. At some point they could potentially be moved into a shared file like contrib/utils.vim.
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.
I agree. I've actually thought about this before, but haven't got around to it. I think they should probably live under
autoload/zepl/fmt.vimor similar (still not sure on the naming yet).Then they can be called like this:
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.
In fact, the default formatter should probably do some of this anyway. Maybe I'll get round to that at some point in the future.