venantius / vim-cljfmt

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

Replaces escape characters. #8

Closed kyrre closed 9 years ago

kyrre commented 9 years ago

For example if I have a regular expression like this:

(clojure.string/split (slurp "osquery.results.log") #"\n")

:Cljfmt will replace "\n" with "" and break the program.

venantius commented 9 years ago

I'll take a look at this, shouldn't be too hard to fix.

venantius commented 9 years ago

This should now be resolved; let me know if you're still experiencing trouble.

raymond-w-ko commented 9 years ago

Hello,

This escaping apparently isn't enough. I tried using this plugin on a file which contains the following nasty snippet.

https://gist.github.com/raymond-w-ko/691e033f4c530cb61526

However, the plugin would silently fail. Uncommenting the silent! in session_eval in s:GetFormattedFile() would mention "\s" and the like not escaping properly.

I tried fixing it by double escaping all backslashes and it seems to work with the files I had without messing up the escapes. I also tested the snippet by kyrre and it seems to preserve it also. I think this should cover almost all cases?

diff --git a/plugin/cljfmt.vim b/plugin/cljfmt.vim
index 0cf229e..a182b97 100644
--- a/plugin/cljfmt.vim
+++ b/plugin/cljfmt.vim
@@ -19,7 +19,7 @@ function! s:GetCurrentBufferContents()
     " Escape newlines
     let l:temp = []
     for l:line in getline(1, '$')
-        let l:line = substitute(l:line, '\\n', '\\\\n', 'g')
+        let l:line = substitute(l:line, '\', '\\\\', 'g')
         call add(l:temp, l:line)
     endfor
     let l:escaped_buffer_contents = join(l:temp, '\n')
venantius commented 9 years ago

Haha, that's quite a minefield to run through! Still, I'm glad for the diff. Give me a few minutes and I'll merge it in.

venantius commented 9 years ago

Okay, merged as of https://github.com/venantius/vim-cljfmt/commit/89a454e86f6be9c49e5503aa6049ce46d985cf10. Thanks for the work.