venantius / vim-cljfmt

A Vim plugin for cljfmt, the Clojure formatting tool.
152 stars 22 forks source link

support reformatting clojurescript #14

Closed cedrictools closed 9 years ago

cedrictools commented 9 years ago

Can this plugin be used for .cljs files ? So far I've seen that the lein cljfmt plugin can reformat cljs, with command lein cljfmt fix However I did not find a way to make this work inside vim. At line https://github.com/venantius/vim-cljfmt/blob/master/plugin/cljfmt.vim#L83 I can of course change to include cljs, cljx extensions, but this is not sufficient. As I understand, fireplace will always wants to connect to a cljs repl with piggieback when opened file is cljs. Do you think a patch could allow reformatting cljs? Would this requires some changes in vim-fireplace ?

venantius commented 9 years ago

To be honest I don't know enough about vim-fireplace's interactions with the ClojureScript REPL to be able to answer that question.

You can test vim-cljfmt's functionality by calling :Cljfmt directly, without the write hooks; if you find that it works I'm happy to update the plugin to include cljs/cljx files as well.

cedrictools commented 9 years ago

Have tried those 2 changes so far which allows cljs to be formatted. May be there is a way to force only cljfmt command to go into clojure repl instead of piggieback. Did not find such a way so this hack in fireplace-vim will brakes piggieback.

diff --git a/plugin/cljfmt.vim b/plugin/cljfmt.vim
index 0cf229e..fa1607b 100644
--- a/plugin/cljfmt.vim
+++ b/plugin/cljfmt.vim
@@ -2,7 +2,7 @@
 " Maintainer:  Venantius <http://venanti.us>
 " Version:     0.3

-let g:clj_fmt_required = 0
+let g:clj_fmt_required = 1

 function! s:RequireCljfmt()
     let l:cmd = "(require 'cljfmt.core)"
@@ -45,6 +45,7 @@ endfunction

 function! s:GetFormattedFile()
     let l:bufcontents = s:GetCurrentBufferContents()
+    let b:fireplace_ns='cljfmt.core'
     redir => l:cljfmt_output
     try
         silent! call fireplace#session_eval(s:GetReformatString(l:bufcontents))
@@ -80,7 +81,7 @@ augroup vim-cljfmt

     " code formatting on save
     if get(g:, "clj_fmt_autosave", 1)
-        autocmd BufWritePre *.clj call cljfmt#Format()
+        autocmd BufWritePre *.clj,*.cljs,*.cljx call cljfmt#Format()
     endif

 augroup END
diff --git a/plugin/fireplace.vim b/plugin/fireplace.vim
index 6c32caa..9deb5a5 100644
--- a/plugin/fireplace.vim
+++ b/plugin/fireplace.vim
@@ -554,18 +554,6 @@ endfunction
 function! fireplace#client(...) abort
   let buf = a:0 ? a:1 : s:buf()
   let client = fireplace#platform(buf)
-  if fnamemodify(bufname(buf), ':e') ==# 'cljs'
-    if !has_key(client, 'connection')
-      throw 'Fireplace: no live REPL connection'
-    endif
-    if empty(client.piggiebacks)
-      let result = client.piggieback('')
-      if has_key(result, 'ex')
-        return result
-      endif
-    endif
-    return client.piggiebacks[0]
-  endif
   return client
 endfunction
venantius commented 9 years ago

I think that short of a fix that's already existing in Fireplace this is probably a non-starter. If you're able to find a way to get this working either (a) without having to modify Fireplace or (b) once a suitable patch to Fireplace has been accepted I'd be happy to merge either of those in, but otherwise I'm going to close this for now.