├── .gitignore ├── README.markdown ├── doc └── cljrefactor.txt └── plugin └── cljrefactor.vim /.gitignore: -------------------------------------------------------------------------------- 1 | /doc/tags 2 | *.pyc 3 | test-project 4 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # cljrefactor.vim 2 | 3 | Preliminary support, learning on the go. 4 | 5 | ## Installation 6 | 7 | First, set up [cider-nrepl](https://github.com/clojure-emacs/cider-nrepl) and [vim-fireplace](https://github.com/tpope/vim-fireplace) following their respective 8 | installation notes. As indicated in fireplace, cljrefactor.vim doesn't provide 9 | indenting or syntax highlighting, so you'll want [a set of Clojure runtime 10 | files](https://github.com/guns/vim-clojure-static) if you're on a version of 11 | Vim earlier than 7.4. You might also want [leiningen.vim](https://github.com/tpope/vim-leiningen) 12 | for assorted static project support. 13 | 14 | If you don't have a preferred installation method, I recommend 15 | installing [vundle.vim](https://github.com/gmarik/Vundle.vim) 16 | then simply add: 17 | 18 | Plugin 'reborg/vim-cljrefactor' 19 | 20 | Once help tags have been generated, you can view the manual with `:help cljrefactor`. 21 | 22 | ## Features 23 | 24 | ## License 25 | 26 | Copyright © Reborg (Renzo Borgatti). Distributed under the same terms as Vim itself. 27 | See `:help license`. 28 | -------------------------------------------------------------------------------- /doc/cljrefactor.txt: -------------------------------------------------------------------------------- 1 | *cljrefactor.txt* Clojure refactoring support 2 | 3 | Author: Renzo Borgtti 4 | License: Same terms as Vim itself (see |license|) 5 | 6 | This plugin is only available if 'compatible' is not set. 7 | 8 | *cljrefactor* 9 | This plugin requires the middleware 10 | provided by . 11 | 12 | RENAMING SYMBOLS *rename-sym* 13 | 14 | Awesome doc here. 15 | 16 | *more-awesome* 17 | More awesome 18 | 19 | ABOUT *cljrefator-about* 20 | 21 | Grab the latest version or report a bug on GitHub: 22 | 23 | http://github.com/reborg/vim-cljrefactor 24 | 25 | vim:tw=78:et:ft=help:norl: 26 | -------------------------------------------------------------------------------- /plugin/cljrefactor.vim: -------------------------------------------------------------------------------- 1 | " cljrefactor.vim - Clojure Refactoring Support 2 | " Maintainer: Frazer Irving () 3 | " Version: 0.1 4 | " GetLatestVimScripts: 9999 1 :AutoInstall: cljrefactor.vim 5 | 6 | function FindUsages() 7 | cgetex [] 8 | let word = expand('') 9 | let symbol = fireplace#info(word) 10 | let usages = fireplace#message({"op": "find-symbol", "ns": symbol.ns, "name": symbol.name, "dir": ".", "line": symbol.line, "serialization-format": "bencode", "file": expand('%:p'), "column": col('.')}, v:t_list) 11 | for usage in usages 12 | if has_key(usage, 'err') 13 | echoerr usage.err 14 | continue 15 | elseif !has_key(usage, 'occurrence') 16 | "echo "Not long enough: " 17 | "echo usage 18 | continue 19 | else 20 | let occ = usage.occurrence 21 | echo occ 22 | let msg = printf('%s:%d:%d:%s', occ['file'], occ['line-beg'], occ['col-beg'], occ['match']) 23 | caddex msg 24 | endif 25 | endfor 26 | copen 27 | endfunction 28 | 29 | function ExtractFunction() 30 | lgetex [] 31 | let word = expand('') 32 | let symbol = fireplace#info(word) 33 | let unbound = fireplace#message({"op": "find-unbound", "file": @%, "line": line('.'), "column": virtcol('.'), "serialization-format": "bencode"}) 34 | echo unbound 35 | 36 | let foo = GetCompleteContext() 37 | echo foo 38 | endfunction 39 | 40 | function ArtifactList() 41 | let artifacts = fireplace#message({"op": "artifact-list"}) 42 | echo artifacts 43 | endfunction 44 | 45 | function! s:paste(text) abort 46 | " Does charwise paste to current '[ and '] marks 47 | let @@ = a:text 48 | let reg_type = getregtype('@@') 49 | call setreg('@@', getreg('@@'), 'v') 50 | silent exe "normal! `[v`]p" 51 | call setreg('@@', getreg('@@'), reg_type)" 52 | endfunction 53 | 54 | 55 | function! CleanNs() 56 | 57 | let p = expand('%:p') 58 | normal! ggw 59 | 60 | let [line1, col1] = searchpairpos('(', '', ')', 'bc') 61 | let [line2, col2] = searchpairpos('(', '', ')', 'n') 62 | 63 | while col1 > 1 && getline(line1)[col1-2] =~# '[#''`~@]' 64 | let col1 -= 1 65 | endwhile 66 | call setpos("'[", [0, line1, col1, 0]) 67 | call setpos("']", [0, line2, col2, 0]) 68 | 69 | if expand('') ==? 'ns' 70 | let res = fireplace#message({ 'op': 'clean-ns', 'path': p, 'prefix-rewriting': 'false', 'ignore-paths': '[#"lifecycle", #"dev"]' }, v:t_list) 71 | let new_ns = get(res[0], 'ns') 72 | if type(new_ns) == type("") 73 | call s:paste(substitute(new_ns, '\n$', '', '')) 74 | silent exe "normal! `[v`]==" 75 | endif 76 | endif 77 | endfunction 78 | 79 | let fireplace#skip = 'synIDattr(synID(line("."),col("."),1),"name") =~? "comment\\|string\\|char\\|regexp"' 80 | 81 | function! GetCompleteContext() abort 82 | " Find toplevel form 83 | " If cursor is on start parenthesis we don't want to find the form 84 | " If cursor is on end parenthesis we want to find the form 85 | let [line1, col1] = searchpairpos('(', '', ')', 'Wrnb', g:fireplace#skip) 86 | let [line2, col2] = searchpairpos('(', '', ')', 'Wrnc', g:fireplace#skip) 87 | 88 | echo line1 89 | echo col1 90 | echo line2 91 | echo col2 92 | 93 | if (line1 == 0 && col1 == 0) || (line2 == 0 && col2 == 0) 94 | return "" 95 | endif 96 | 97 | if line1 == line2 98 | let expr = getline(line1)[col1-1 : col2-1] 99 | else 100 | let expr = getline(line1)[col1-1 : -1] . ' ' 101 | \ . join(getline(line1+1, line2-1), ' ') 102 | \ . getline(line2)[0 : col2-1] 103 | endif 104 | 105 | " Calculate the position of cursor inside the expr 106 | if line1 == line('.') 107 | let p = col('.') - col1 108 | else 109 | let p = strlen(getline(line1)[col1-1 : -1]) 110 | \ + strlen(join(getline(line1 + 1, line('.') - 1), ' ')) 111 | \ + col('.') 112 | endif 113 | echo p 114 | 115 | return strpart(expr, 0, p) . '__prefix__' . strpart(expr, p) 116 | endfunction 117 | 118 | 119 | command! -nargs=0 ExtractFunction call ExtractFunction() 120 | nmap cru :call FindUsages() 121 | nmap cral :call ArtifactList() 122 | nmap crc :call CleanNs() 123 | 124 | 125 | --------------------------------------------------------------------------------