Skip to content

Commit 5d2a95f

Browse files
committed
Extract vim interaction to vim-exec
1 parent e7d54b6 commit 5d2a95f

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

clj/src/vim_clojure_static/test.clj

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,24 @@
66
[clojure.java.shell :as shell]
77
[clojure.string :as string]
88
[clojure.test :as test])
9-
(:import (java.util List)))
9+
(:import (java.io File)
10+
(java.util List)))
11+
12+
(defn vim-exec
13+
"Spit buf into file, then execute vim-expr after Vim loads the file. The
14+
value of vim-expr is evaluated as EDN and returned."
15+
[file buf vim-expr]
16+
(let [tmp (File/createTempFile "vim-clojure-static.test." ".out")]
17+
(try
18+
(io/make-parents file)
19+
(spit file buf)
20+
(shell/sh "vim" "-N" "-u" "vim/test-runtime.vim"
21+
"-c" (str "let @x = " vim-expr)
22+
"-c" (str "call writefile([@x], " (pr-str (str tmp)) ") | quitall!")
23+
file)
24+
(edn/read-string (slurp tmp))
25+
(finally
26+
(.delete tmp)))))
1027

1128
(defn syn-id-names
1229
"Map lines of clojure text to vim synID names at each column as keywords:
@@ -16,13 +33,9 @@
1633
First parameter is the file that is used to communicate with Vim. The file
1734
is not deleted to allow manual inspection."
1835
[file & lines]
19-
(io/make-parents file)
20-
(spit file (string/join \newline lines))
21-
(shell/sh "vim" "-u" "NONE" "-N" "-S" "vim/test-runtime.vim" file)
22-
;; The last line of the file will contain valid EDN
2336
(into {} (map (fn [l ids] [l (mapv keyword ids)])
2437
lines
25-
(edn/read-string (peek (string/split-lines (slurp file)))))))
38+
(vim-exec file (string/join \newline lines) "ClojureSynIDNames()"))))
2639

2740
(defn subfmt
2841
"Extract a subsequence of seq s corresponding to the character positions of

clj/vim/test-runtime.vim

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,12 @@ syntax on
66
set synmaxcol=0
77
setfiletype clojure
88

9-
if !exists('g:testing')
10-
let g:testing = 1
11-
endif
12-
13-
function! s:syn_id_names()
9+
function! ClojureSynIDNames()
1410
let names = []
1511
for lnum in range(1, line('$'))
1612
let f = 'synIDattr(synID(' . lnum . ', v:val, 0), "name")'
1713
call add(names, map(range(1, virtcol([lnum, '$']) - 1), f))
1814
endfor
19-
return names
15+
" Changing the quotes makes this valid EDN
16+
return tr(string(names), "'", '"')
2017
endfunction
21-
22-
if g:testing
23-
" Changing the quotes will make this valid EDN
24-
call append(line('$'), tr(string(s:syn_id_names()), "'", '"')) | write | quitall!
25-
endif

0 commit comments

Comments
 (0)