Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Split plug-in into command definitions / autoload functions

  • Loading branch information...
commit 527e1b76a06c04baf2b9b597f1fbd5e0ef02fca4 1 parent 7f9cbd6
Peter Odding authored August 12, 2010
253  autoload.vim
... ...
@@ -0,0 +1,253 @@
  1
+" Vim script.
  2
+" Author: Peter Odding <peter@peterodding.com>
  3
+" Last Change: August 12, 2010
  4
+" URL: http://peterodding.com/code/vim/lua-inspect/
  5
+" License: MIT
  6
+
  7
+let s:script = expand('<sfile>:p:~')
  8
+
  9
+function! luainspect#auto_enable() " {{{1
  10
+  if !&diff && !exists('b:luainspect_disabled')
  11
+    " Disable easytags.vim because it doesn't play nice with luainspect.vim!
  12
+    let b:easytags_nohl = 1
  13
+    " Define buffer local mappings for rename / goto definition features.
  14
+    inoremap <buffer> <silent> <F2> <C-o>:call luainspect#make_request('rename')<CR>
  15
+    nnoremap <buffer> <silent> <F2> :call luainspect#make_request('rename')<CR>
  16
+    nnoremap <buffer> <silent> gd :call luainspect#make_request('goto')<CR>
  17
+    " Enable balloon evaluation / dynamic tool tips.
  18
+    setlocal ballooneval balloonexpr=LuaInspectToolTip()
  19
+    " Install automatic commands to update the highlighting.
  20
+    for event in split(g:lua_inspect_events, ',')
  21
+      execute 'autocmd!' event '<buffer> LuaInspect'
  22
+    endfor
  23
+  endif
  24
+endfunction
  25
+
  26
+function! luainspect#highlight_cmd(disable) " {{{1
  27
+  if a:disable
  28
+    call s:clear_previous_matches()
  29
+    unlet! b:luainspect_input b:luainspect_output
  30
+    let b:luainspect_disabled = 1
  31
+  else
  32
+    unlet! b:luainspect_disabled
  33
+    let starttime = xolox#timer#start()
  34
+    call luainspect#make_request('highlight')
  35
+    let bufname = expand('%:p:~')
  36
+    let msg = "%s: Highlighted %s in %s."
  37
+    call xolox#timer#stop(msg, s:script, bufname, starttime)
  38
+  endif
  39
+endfunction
  40
+
  41
+function! luainspect#make_request(action) " {{{1
  42
+  if a:action == 'tooltip'
  43
+    let lines = getbufline(v:beval_bufnr, 1, "$")
  44
+    call insert(lines, v:beval_col)
  45
+    call insert(lines, v:beval_lnum)
  46
+  else
  47
+    let lines = getline(1, "$")
  48
+    call insert(lines, col('.'))
  49
+    call insert(lines, line('.'))
  50
+  endif
  51
+  call insert(lines, a:action)
  52
+  call s:parse_text(join(lines, "\n"), s:prepare_search_path())
  53
+  if !empty(b:luainspect_output)
  54
+    let response = b:luainspect_output[0]
  55
+    if response == 'syntax_error' && len(b:luainspect_output) >= 4
  56
+      " Never perform syntax error highlighting in non-Lua buffers!
  57
+      let linenum = b:luainspect_output[1] + 0
  58
+      let colnum = b:luainspect_output[2] + 0
  59
+      let linenum2 = b:luainspect_output[3] + 0
  60
+      let b:luainspect_syntax_error = b:luainspect_output[4]
  61
+      if a:action != 'tooltip' || v:beval_bufnr == bufnr('%')
  62
+        let error_cmd = 'syntax match luaInspectSyntaxError /\%%>%il\%%<%il.*/ containedin=ALLBUT,lua*Comment*'
  63
+        execute printf(error_cmd, linenum - 1, (linenum2 ? linenum2 : line('$')) + 1)
  64
+      endif
  65
+      " But always let the user know that a syntax error exists.
  66
+      let bufname = fnamemodify(bufname(a:action != 'tooltip' ? '%' : v:beval_bufnr), ':p:~')
  67
+      call xolox#warning("Syntax error around line %i in %s: %s", linenum, bufname, b:luainspect_syntax_error)
  68
+      return
  69
+    endif
  70
+    unlet! b:luainspect_syntax_error
  71
+    if response == 'highlight'
  72
+      call s:define_default_styles()
  73
+      call s:clear_previous_matches()
  74
+      call s:highlight_variables()
  75
+    elseif response == 'goto'
  76
+      if len(b:luainspect_output) < 3
  77
+        call xolox#warning("No variable under cursor!")
  78
+      else
  79
+        let linenum = b:luainspect_output[1] + 0
  80
+        let colnum = b:luainspect_output[2] + 0
  81
+        call setpos('.', [0, linenum, colnum, 0])
  82
+        call xolox#message("") " Clear any previous message to avoid confusion.
  83
+      endif
  84
+    elseif response == 'tooltip'
  85
+      if len(b:luainspect_output) > 1
  86
+        return join(b:luainspect_output[1:-1], "\n")
  87
+      endif
  88
+    elseif response == 'rename'
  89
+      if len(b:luainspect_output) == 1
  90
+        call xolox#warning("No variable under cursor!")
  91
+      else
  92
+        call s:rename_variable()
  93
+      endif
  94
+    endif
  95
+  endif
  96
+endfunction
  97
+
  98
+function! s:prepare_search_path() " {{{1
  99
+  let code = ''
  100
+  if !(has('lua') && g:lua_inspect_internal && exists('s:changed_path'))
  101
+    let template = 'package.path = ''%s/?.lua;'' .. package.path'
  102
+    let code = printf(template, escape(expand(g:lua_inspect_path), '"\'''))
  103
+    if has('lua') && g:lua_inspect_internal
  104
+      execute 'lua' code
  105
+      let s:changed_path = 1
  106
+    endif
  107
+  endif
  108
+  return code
  109
+endfunction
  110
+
  111
+function! s:parse_text(input, search_path) " {{{1
  112
+  if !(exists('b:luainspect_input') && b:luainspect_input == a:input)
  113
+    if !(has('lua') && g:lua_inspect_internal)
  114
+      let template = 'lua -e "%s; require ''luainspect4vim'' (io.read ''*a'')"'
  115
+      let command = printf(template, a:search_path)
  116
+      try
  117
+        let b:luainspect_output = xolox#shell#execute(command, 1, a:input)
  118
+      catch /^Vim\%((\a\+)\)\=:E117/
  119
+        " Ignore missing shell.vim plug-in.
  120
+        let b:luainspect_output = split(system(command, a:input), "\n")
  121
+        if v:shell_error
  122
+          let msg = "Failed to execute luainspect as external process! %s"
  123
+          throw printf(msg, strtrans(join(b:luainspect_output, "\n")))
  124
+        endif
  125
+      endtry
  126
+    else
  127
+      redir => output
  128
+      silent lua << EOF
  129
+      if io == nil then
  130
+        -- The Lua interface for Vim previously didn't include io.*!
  131
+        io = { type = function() end }
  132
+      end
  133
+      require 'luainspect4vim' (vim.eval 'a:input')
  134
+EOF
  135
+      redir END
  136
+      let b:luainspect_output = split(output, "\n")
  137
+    endif
  138
+    " Remember the text that was just parsed.
  139
+    let b:luainspect_input = a:input
  140
+  endif
  141
+endfunction
  142
+
  143
+function! s:define_default_styles() " {{{1
  144
+  " Always define the default highlighting styles
  145
+  " (copied from /luainspect/scite.lua for consistency).
  146
+  for [group, styles] in items(s:groups)
  147
+    let group = 'luaInspect' . group
  148
+    if type(styles) == type('')
  149
+      let defgroup = styles
  150
+    else
  151
+      let defgroup = 'luaInspectDefault' . group
  152
+      let style = &bg == 'light' ? styles[0] : styles[1]
  153
+      execute 'highlight' defgroup style
  154
+    endif
  155
+    " Don't link the actual highlighting styles to the defaults if the user
  156
+    " has already defined or linked the highlighting group. This enables color
  157
+    " schemes and vimrc scripts to override the styles (see :help :hi-default).
  158
+    execute 'highlight def link' group defgroup
  159
+    unlet styles " to avoid E706.
  160
+  endfor
  161
+endfunction
  162
+
  163
+function! s:clear_previous_matches() " {{{1
  164
+  " Clear existing highlighting.
  165
+  for group in keys(s:groups)
  166
+    let group = 'luaInspect' . group
  167
+    if hlexists(group)
  168
+      execute 'syntax clear' group
  169
+    endif
  170
+  endfor
  171
+endfunction
  172
+
  173
+function! s:highlight_variables() " {{{1
  174
+  call clearmatches()
  175
+  for line in b:luainspect_output[1:-1]
  176
+    if s:check_output(line, '^\w\+\(\s\+\d\+\)\{3}$')
  177
+      let [group, linenum, firstcol, lastcol] = split(line)
  178
+      let pattern = s:highlight_position(linenum + 0, firstcol - 1, lastcol + 2)
  179
+      if group == 'luaInspectWrongArgCount'
  180
+        call matchadd(group, pattern)
  181
+      elseif group == 'luaInspectSelectedVariable' 
  182
+        call matchadd(group, pattern, 20)
  183
+      else
  184
+        execute 'syntax match' group '/' . pattern . '/'
  185
+      endif
  186
+    endif
  187
+  endfor
  188
+endfunction
  189
+
  190
+function! s:rename_variable() " {{{1
  191
+  " Highlight occurrences of variable before rename.
  192
+  let highlights = []
  193
+  for line in b:luainspect_output[1:-1]
  194
+    if s:check_output(line, '^\d\+\(\s\+\d\+\)\{2}$')
  195
+      let [linenum, firstcol, lastcol] = split(line)
  196
+      let pattern = s:highlight_position(linenum + 0, firstcol - 1, lastcol + 2)
  197
+      call add(highlights, matchadd('IncSearch', pattern))
  198
+    endif
  199
+  endfor
  200
+  redraw
  201
+  " Prompt for new name.
  202
+  let oldname = expand('<cword>')
  203
+  let prompt = "Please enter the new name for %s: "
  204
+  let newname = input(printf(prompt, oldname), oldname)
  205
+  " Clear highlighting of occurrences.
  206
+  call map(highlights, 'matchdelete(v:val)')
  207
+  " Perform rename?
  208
+  if newname != '' && newname != oldname
  209
+    let num_renamed = 0
  210
+    for fields in reverse(b:luainspect_output[1:-1])
  211
+      let [linenum, firstcol, lastcol] = split(fields)
  212
+      let linenum += 0
  213
+      let firstcol -= 2
  214
+      let lastcol += 0
  215
+      let line = getline(linenum)
  216
+      let prefix = firstcol > 0 ? line[0 : firstcol] : ''
  217
+      let suffix = lastcol < len(line) ? line[lastcol : -1] : ''
  218
+      call setline(linenum, prefix . newname . suffix)
  219
+      let num_renamed += 1
  220
+    endfor
  221
+    let msg = "Renamed %i occurrences of %s to %s"
  222
+    call xolox#message(msg, num_renamed, oldname, newname)
  223
+  endif
  224
+endfunction
  225
+
  226
+function! s:check_output(line, pattern) " {{{1
  227
+  if match(a:line, a:pattern) >= 0
  228
+    return 1
  229
+  else
  230
+    call xolox#warning("Invalid output from luainspect4vim.lua: '%s'", strtrans(a:line))
  231
+    return 0
  232
+  endif
  233
+endfunction
  234
+
  235
+function! s:highlight_position(linenum, firstcol, lastcol) " {{{1
  236
+  return printf('\%%%il\%%>%ic\<\w\+\>\%%<%ic', a:linenum, a:firstcol, a:lastcol)
  237
+endfunction
  238
+
  239
+" Highlighting groups and their default light/dark styles. {{{1
  240
+
  241
+let s:groups = {}
  242
+let s:groups['GlobalDefined'] = ['guifg=#600000', 'guifg=#ffc080']
  243
+let s:groups['GlobalUndefined'] = 'ErrorMsg'
  244
+let s:groups['LocalUnused'] = ['guifg=#ffffff guibg=#000080', 'guifg=#ffffff guibg=#000080']
  245
+let s:groups['LocalMutated'] = ['gui=italic guifg=#000080', 'gui=italic guifg=#c0c0ff']
  246
+let s:groups['UpValue'] = ['guifg=#0000ff', 'guifg=#e8e8ff']
  247
+let s:groups['Param'] = ['guifg=#000040', 'guifg=#8080ff']
  248
+let s:groups['Local'] = ['guifg=#000040', 'guifg=#c0c0ff']
  249
+let s:groups['FieldDefined'] = ['guifg=#600000', 'guifg=#ffc080']
  250
+let s:groups['FieldUndefined'] = ['guifg=#c00000', 'guifg=#ff0000']
  251
+let s:groups['SelectedVariable'] = 'CursorLine'
  252
+let s:groups['SyntaxError'] = 'SpellBad'
  253
+let s:groups['WrongArgCount'] = 'SpellLocal'
274  luainspect.vim
@@ -13,10 +13,6 @@ if &cp || exists('g:loaded_luainspect')
13 13
   finish
14 14
 endif
15 15
 
16  
-let s:script = expand('<sfile>:p:~')
17  
-
18  
-" Configuration defaults. {{{1
19  
-
20 16
 if !exists('g:lua_inspect_events')
21 17
   " Change this to enable semantic highlighting on your preferred events.
22 18
   let g:lua_inspect_events = 'CursorHold,CursorHoldI,BufWritePost'
@@ -41,278 +37,24 @@ if !exists('g:lua_inspect_internal')
41 37
   let g:lua_inspect_internal = has('lua')
42 38
 endif
43 39
 
44  
-" The highlight groups and default styles/links defined by this plug-in.
45  
-let s:groups = {}
46  
-let s:groups['GlobalDefined'] = ['guifg=#600000', 'guifg=#ffc080']
47  
-let s:groups['GlobalUndefined'] = 'ErrorMsg'
48  
-let s:groups['LocalUnused'] = ['guifg=#ffffff guibg=#000080', 'guifg=#ffffff guibg=#000080']
49  
-let s:groups['LocalMutated'] = ['gui=italic guifg=#000080', 'gui=italic guifg=#c0c0ff']
50  
-let s:groups['UpValue'] = ['guifg=#0000ff', 'guifg=#e8e8ff']
51  
-let s:groups['Param'] = ['guifg=#000040', 'guifg=#8080ff']
52  
-let s:groups['Local'] = ['guifg=#000040', 'guifg=#c0c0ff']
53  
-let s:groups['FieldDefined'] = ['guifg=#600000', 'guifg=#ffc080']
54  
-let s:groups['FieldUndefined'] = ['guifg=#c00000', 'guifg=#ff0000']
55  
-let s:groups['SelectedVariable'] = 'CursorLine'
56  
-let s:groups['SyntaxError'] = 'SpellBad'
57  
-let s:groups['WrongArgCount'] = 'SpellLocal'
58  
-
59  
-" (Automatic) command definitions. {{{1
60  
-
61  
-command! -bar -bang LuaInspect call s:luainspect_command(<q-bang> == '!')
  40
+" This command updates highlighting when automatic highlighting is disabled.
  41
+command! -bar -bang LuaInspect call luainspect#highlight_cmd(<q-bang> == '!')
62 42
 
  43
+" Automatically enable the plug-in in Lua buffers.
63 44
 augroup PluginLuaInspect
64  
-  " Clear existing automatic commands.
65  
-  autocmd!
66  
-  " Automatically enable the plug-in on these events.
67  
-  autocmd BufNewFile,BufReadPost,BufWritePost * call s:auto_enable_plugin()
68  
-  " Define the configured automatic commands for highlighting.
69  
-  for s:event in split(g:lua_inspect_events, ',')
70  
-    execute 'autocmd' s:event '* if s:check_plugin_valid() | LuaInspect | endif'
71  
-  endfor
72  
-  unlet s:event
  45
+  autocmd! FileType lua call luainspect#auto_enable()
73 46
 augroup END
74 47
 
75  
-" Script local functions. {{{1
76  
-
77  
-function! s:check_plugin_valid() " {{{2
78  
-  return &ft == 'lua' && !&diff && !exists('b:luainspect_disabled')
79  
-endfunction
80  
-
81  
-function! s:luainspect_command(disable) " {{{2
82  
-  if a:disable
83  
-    call s:clear_previous_matches()
84  
-    unlet! b:luainspect_input b:luainspect_output
85  
-    let b:luainspect_disabled = 1
86  
-  else
87  
-    unlet! b:luainspect_disabled
88  
-    let starttime = xolox#timer#start()
89  
-    call s:run_lua_inspect('highlight')
90  
-    let bufname = expand('%:p:~')
91  
-    let msg = "%s: Highlighted %s in %s."
92  
-    call xolox#timer#stop(msg, s:script, bufname, starttime)
93  
-  endif
94  
-endfunction
95  
-
96  
-function! s:auto_enable_plugin() " {{{2
97  
-  if s:check_plugin_valid()
98  
-    " Disable easytags.vim because it doesn't play nice with luainspect.vim!
99  
-    let b:easytags_nohl = 1
100  
-    inoremap <buffer> <silent> <F2> <C-o>:call <Sid>run_lua_inspect('rename')<CR>
101  
-    nnoremap <buffer> <silent> <F2> :call <Sid>run_lua_inspect('rename')<CR>
102  
-    nnoremap <buffer> <silent> gd :call <Sid>run_lua_inspect('goto')<CR>
103  
-    setlocal ballooneval balloonexpr=LuaInspectToolTip()
104  
-  endif
105  
-endfunction
106  
-
107  
-function! LuaInspectToolTip() " {{{2
108  
-  let text = s:run_lua_inspect('tooltip')
  48
+" The &balloonexpr option requires a global function.
  49
+function! LuaInspectToolTip()
  50
+  let result = luainspect#make_request('tooltip')
109 51
   if exists('b:luainspect_syntax_error')
110 52
     return b:luainspect_syntax_error
111 53
   else
112  
-    return type(text) == type('') ? text : ''
113  
-  endif
114  
-endfunction
115  
-
116  
-function! s:run_lua_inspect(action) " {{{2
117  
-  if a:action == 'tooltip'
118  
-    let lines = getbufline(v:beval_bufnr, 1, "$")
119  
-    call insert(lines, v:beval_col)
120  
-    call insert(lines, v:beval_lnum)
121  
-  else
122  
-    let lines = getline(1, "$")
123  
-    call insert(lines, col('.'))
124  
-    call insert(lines, line('.'))
125  
-  endif
126  
-  call insert(lines, a:action)
127  
-  call s:parse_text(join(lines, "\n"), s:prepare_search_path())
128  
-  if !empty(b:luainspect_output)
129  
-    let response = b:luainspect_output[0]
130  
-    if response == 'syntax_error' && len(b:luainspect_output) >= 4
131  
-      " Never perform syntax error highlighting in non-Lua buffers!
132  
-      let linenum = b:luainspect_output[1] + 0
133  
-      let colnum = b:luainspect_output[2] + 0
134  
-      let linenum2 = b:luainspect_output[3] + 0
135  
-      let b:luainspect_syntax_error = b:luainspect_output[4]
136  
-      if a:action != 'tooltip' || v:beval_bufnr == bufnr('%')
137  
-        let error_cmd = 'syntax match luaInspectSyntaxError /\%%>%il\%%<%il.*/ containedin=ALLBUT,lua*Comment*'
138  
-        execute printf(error_cmd, linenum - 1, (linenum2 ? linenum2 : line('$')) + 1)
139  
-      endif
140  
-      " But always let the user know that a syntax error exists.
141  
-      let bufname = fnamemodify(bufname(a:action != 'tooltip' ? '%' : v:beval_bufnr), ':p:~')
142  
-      call xolox#warning("Syntax error around line %i in %s: %s", linenum, bufname, b:luainspect_syntax_error)
143  
-      return
144  
-    endif
145  
-    unlet! b:luainspect_syntax_error
146  
-    if response == 'highlight'
147  
-      call s:define_default_styles()
148  
-      call s:clear_previous_matches()
149  
-      call s:highlight_variables()
150  
-    elseif response == 'goto'
151  
-      if len(b:luainspect_output) < 3
152  
-        call xolox#warning("No variable under cursor!")
153  
-      else
154  
-        let linenum = b:luainspect_output[1] + 0
155  
-        let colnum = b:luainspect_output[2] + 0
156  
-        call setpos('.', [0, linenum, colnum, 0])
157  
-        call xolox#message("") " Clear any previous message to avoid confusion.
158  
-      endif
159  
-    elseif response == 'tooltip'
160  
-      if len(b:luainspect_output) > 1
161  
-        return join(b:luainspect_output[1:-1], "\n")
162  
-      endif
163  
-    elseif response == 'rename'
164  
-      if len(b:luainspect_output) == 1
165  
-        call xolox#warning("No variable under cursor!")
166  
-      else
167  
-        call s:rename_variable()
168  
-      endif
169  
-    endif
  54
+    return type(result) == type('') ? result : ''
170 55
   endif
171 56
 endfunction
172 57
 
173  
-function! s:prepare_search_path() " {{{2
174  
-  let code = ''
175  
-  if !(has('lua') && g:lua_inspect_internal && exists('s:changed_path'))
176  
-    let template = 'package.path = ''%s/?.lua;'' .. package.path'
177  
-    let code = printf(template, escape(expand(g:lua_inspect_path), '"\'''))
178  
-    if has('lua') && g:lua_inspect_internal
179  
-      execute 'lua' code
180  
-      let s:changed_path = 1
181  
-    endif
182  
-  endif
183  
-  return code
184  
-endfunction
185  
-
186  
-function! s:parse_text(input, search_path) " {{{2
187  
-  if !(exists('b:luainspect_input') && b:luainspect_input == a:input)
188  
-    if !(has('lua') && g:lua_inspect_internal)
189  
-      let template = 'lua -e "%s; require ''luainspect4vim'' (io.read ''*a'')"'
190  
-      let command = printf(template, a:search_path)
191  
-      try
192  
-        let b:luainspect_output = xolox#shell#execute(command, 1, a:input)
193  
-      catch /^Vim\%((\a\+)\)\=:E117/
194  
-        " Ignore missing shell.vim plug-in.
195  
-        let b:luainspect_output = split(system(command, a:input), "\n")
196  
-        if v:shell_error
197  
-          let msg = "Failed to execute luainspect as external process! %s"
198  
-          throw printf(msg, strtrans(join(b:luainspect_output, "\n")))
199  
-        endif
200  
-      endtry
201  
-    else
202  
-      redir => output
203  
-      silent lua << EOF
204  
-      if io == nil then
205  
-        -- The Lua interface for Vim previously didn't include io.*!
206  
-        io = { type = function() end }
207  
-      end
208  
-      require 'luainspect4vim' (vim.eval 'a:input')
209  
-EOF
210  
-      redir END
211  
-      let b:luainspect_output = split(output, "\n")
212  
-    endif
213  
-    " Remember the text that was just parsed.
214  
-    let b:luainspect_input = a:input
215  
-  endif
216  
-endfunction
217  
-
218  
-function! s:define_default_styles() " {{{2
219  
-  " Always define the default highlighting styles
220  
-  " (copied from /luainspect/scite.lua for consistency).
221  
-  for [group, styles] in items(s:groups)
222  
-    let group = 'luaInspect' . group
223  
-    if type(styles) == type('')
224  
-      let defgroup = styles
225  
-    else
226  
-      let defgroup = 'luaInspectDefault' . group
227  
-      let style = &bg == 'light' ? styles[0] : styles[1]
228  
-      execute 'highlight' defgroup style
229  
-    endif
230  
-    " Don't link the actual highlighting styles to the defaults if the user
231  
-    " has already defined or linked the highlighting group. This enables color
232  
-    " schemes and vimrc scripts to override the styles (see :help :hi-default).
233  
-    execute 'highlight def link' group defgroup
234  
-    unlet styles " to avoid E706.
235  
-  endfor
236  
-endfunction
237  
-
238  
-function! s:clear_previous_matches() " {{{2
239  
-  " Clear existing highlighting.
240  
-  for group in keys(s:groups)
241  
-    let group = 'luaInspect' . group
242  
-    if hlexists(group)
243  
-      execute 'syntax clear' group
244  
-    endif
245  
-  endfor
246  
-endfunction
247  
-
248  
-function! s:highlight_variables() " {{{2
249  
-  call clearmatches()
250  
-  for line in b:luainspect_output[1:-1]
251  
-    if s:check_output(line, '^\w\+\(\s\+\d\+\)\{3}$')
252  
-      let [group, linenum, firstcol, lastcol] = split(line)
253  
-      let pattern = s:highlight_position(linenum + 0, firstcol - 1, lastcol + 2)
254  
-      if group == 'luaInspectWrongArgCount'
255  
-        call matchadd(group, pattern)
256  
-      elseif group == 'luaInspectSelectedVariable' 
257  
-        call matchadd(group, pattern, 20)
258  
-      else
259  
-        execute 'syntax match' group '/' . pattern . '/'
260  
-      endif
261  
-    endif
262  
-  endfor
263  
-endfunction
264  
-
265  
-function! s:rename_variable() " {{{2
266  
-  " Highlight occurrences of variable before rename.
267  
-  let highlights = []
268  
-  for line in b:luainspect_output[1:-1]
269  
-    if s:check_output(line, '^\d\+\(\s\+\d\+\)\{2}$')
270  
-      let [linenum, firstcol, lastcol] = split(line)
271  
-      let pattern = s:highlight_position(linenum + 0, firstcol - 1, lastcol + 2)
272  
-      call add(highlights, matchadd('IncSearch', pattern))
273  
-    endif
274  
-  endfor
275  
-  redraw
276  
-  " Prompt for new name.
277  
-  let oldname = expand('<cword>')
278  
-  let prompt = "Please enter the new name for %s: "
279  
-  let newname = input(printf(prompt, oldname), oldname)
280  
-  " Clear highlighting of occurrences.
281  
-  call map(highlights, 'matchdelete(v:val)')
282  
-  " Perform rename?
283  
-  if newname != '' && newname != oldname
284  
-    let num_renamed = 0
285  
-    for fields in reverse(b:luainspect_output[1:-1])
286  
-      let [linenum, firstcol, lastcol] = split(fields)
287  
-      let linenum += 0
288  
-      let firstcol -= 2
289  
-      let lastcol += 0
290  
-      let line = getline(linenum)
291  
-      let prefix = firstcol > 0 ? line[0 : firstcol] : ''
292  
-      let suffix = lastcol < len(line) ? line[lastcol : -1] : ''
293  
-      call setline(linenum, prefix . newname . suffix)
294  
-      let num_renamed += 1
295  
-    endfor
296  
-    let msg = "Renamed %i occurrences of %s to %s"
297  
-    call xolox#message(msg, num_renamed, oldname, newname)
298  
-  endif
299  
-endfunction
300  
-
301  
-function! s:check_output(line, pattern) " {{{2
302  
-  if match(a:line, a:pattern) >= 0
303  
-    return 1
304  
-  else
305  
-    call xolox#warning("Invalid output from luainspect4vim.lua: '%s'", strtrans(a:line))
306  
-    return 0
307  
-  endif
308  
-endfunction
309  
-
310  
-function! s:highlight_position(linenum, firstcol, lastcol) " {{{2
311  
-  return printf('\%%%il\%%>%ic\<\w\+\>\%%<%ic', a:linenum, a:firstcol, a:lastcol)
312  
-endfunction
313  
-
314  
-" }}}1
315  
-
316 58
 " Make sure the plug-in is only loaded once.
317 59
 let g:loaded_luainspect = 1
318 60
 

0 notes on commit 527e1b7

Please sign in to comment.
Something went wrong with that request. Please try again.