From c3b606035d30e89c5ecf29fc6c0b7c6ae34f221f Mon Sep 17 00:00:00 2001 From: Paul Fernandez Date: Wed, 11 May 2022 16:48:11 -0700 Subject: [PATCH 1/5] Clojure fomatting file --- zepl/contrib/clojure.vim | 67 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 zepl/contrib/clojure.vim diff --git a/zepl/contrib/clojure.vim b/zepl/contrib/clojure.vim new file mode 100644 index 0000000..49e0fed --- /dev/null +++ b/zepl/contrib/clojure.vim @@ -0,0 +1,67 @@ +" Description: Text formatter for Clojure code. +" File: zepl/contrib/clojure.vim +" Author: Paul Fernandez +" 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", 2) + + " Remove two leading spaces added by the Leiningen repl. + return substitute(line, '\m^\s\s', '', '') +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, "\n") . "\n" +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 +" let g:repl_config = { +" \ 'clojure': { +" \ 'cmd': filereadable('deps.edn') ? 'clj' : 'lein repl', +" \ 'formatter': function('zepl#contrib#clojure#formatter') +" \ } +" \ } From 0abfd95197a3c40df4c3b2cdb20921d05eb13280 Mon Sep 17 00:00:00 2001 From: Paul Fernandez Date: Wed, 11 May 2022 16:54:53 -0700 Subject: [PATCH 2/5] Clojure contrib doc --- doc/zepl-contrib.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/doc/zepl-contrib.txt b/doc/zepl-contrib.txt index 1dda596..fa1b653 100644 --- a/doc/zepl-contrib.txt +++ b/doc/zepl-contrib.txt @@ -66,6 +66,18 @@ Copy/adapt the code below to your |vimrc| to add improved F# support. \ 'formatter': function('zepl#contrib#fsharp#formatter') \ } < +------------------------------------------------------------------------------ + *zepl-clojure* + +Copy/adapt the code below to your |vimrc| to add improved Clojure support. +> + runtime zepl/contrib/clojure.vim + + autocmd! FileType clojure let b:repl_config = { + \ 'cmd': filereadable('deps.edn') ? 'clj' : 'lein repl', + \ 'formatter': function('zepl#contrib#clojure#formatter') + \ } +< ============================================================================== 3. EXTRA FEATURES *zepl-extra-features* From 0bd2b99051e17caf6ad472c784c90096c215f882 Mon Sep 17 00:00:00 2001 From: Paul Fernandez Date: Wed, 11 May 2022 17:30:42 -0700 Subject: [PATCH 3/5] Update comment --- zepl/contrib/clojure.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zepl/contrib/clojure.vim b/zepl/contrib/clojure.vim index 49e0fed..5cbc566 100644 --- a/zepl/contrib/clojure.vim +++ b/zepl/contrib/clojure.vim @@ -37,7 +37,7 @@ function s:processLine(...) abort " Remove trailing newlines. let line = trim(a:2, "\r\n", 2) - " Remove two leading spaces added by the Leiningen repl. + " Remove two leading spaces added by the Clojure repl. return substitute(line, '\m^\s\s', '', '') endfunction From 5b0a68f1997c354f89d6544f5df9c27d09531574 Mon Sep 17 00:00:00 2001 From: Paul Fernandez Date: Thu, 19 May 2022 11:22:52 -0700 Subject: [PATCH 4/5] Change \n to \ --- zepl/contrib/clojure.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zepl/contrib/clojure.vim b/zepl/contrib/clojure.vim index 49e0fed..4ed9f7e 100644 --- a/zepl/contrib/clojure.vim +++ b/zepl/contrib/clojure.vim @@ -52,7 +52,7 @@ function zepl#contrib#clojure#formatter(lines) let lines = s:normalizeIndents(lines) let lines = map(lines, function("s:processLine")) - return join(lines, "\n") . "\n" + return join(lines, "\") . "\" endfunction " EXAMPLE: Configure zepl.vim to use the Clojure formatter in Clojure buffers. From 1f4c91004c789b5b72921ac3026fdb7c7f338c34 Mon Sep 17 00:00:00 2001 From: Paul Fernandez Date: Thu, 19 May 2022 11:44:10 -0700 Subject: [PATCH 5/5] Update zepl/contrib/clojure.vim Co-authored-by: Alex Vear --- zepl/contrib/clojure.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zepl/contrib/clojure.vim b/zepl/contrib/clojure.vim index be703c3..3b952c8 100644 --- a/zepl/contrib/clojure.vim +++ b/zepl/contrib/clojure.vim @@ -35,7 +35,7 @@ endfunction function s:processLine(...) abort " Remove trailing newlines. - let line = trim(a:2, "\r\n", 2) + let line = trim(a:2, "\r\n\", 2) " Remove two leading spaces added by the Clojure repl. return substitute(line, '\m^\s\s', '', '')