Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Completion of module names after typing require('

The following variants are currently supported:

 * require '...'
 * require "..."
 * require('...')
 * require("...")
  • Loading branch information...
commit ffaf36b6ddfa1e2fe764337ff13e613a153adc91 1 parent 6d68f3d
Peter Odding authored June 14, 2011
2  README.md
Source Rendered
@@ -12,7 +12,7 @@ The [Lua][lua] file type plug-in for [Vim][vim] makes it easier to work with Lua
12 12
 
13 13
  * The ['completefunc'][cfu] option is set to allow completion of Lua 5.1 keywords, global variables and library members using Control-X Control-U
14 14
 
15  
- * The ['omnifunc'][ofu] option is set to allow dynamic completion of all modules installed on the system using Control-X Control-O, however it needs to be explicitly enabled by setting the `lua_complete_omni` option because this functionality may have undesired side effects!
  15
+ * The ['omnifunc'][ofu] option is set to allow dynamic completion of the variables defined in all modules installed on the system using Control-X Control-O, however it needs to be explicitly enabled by setting the `lua_complete_omni` option because this functionality may have undesired side effects! When you invoke omni completion after typing `require '` or `require('` you get completion of module names
16 16
 
17 17
  * Several [text-objects][tob] are defined so you can jump between blocks and functions
18 18
 
66  autoload/xolox/lua.vim
@@ -263,24 +263,28 @@ function! xolox#lua#completefunc(init, base) " {{{1
263 263
 endfunction
264 264
 
265 265
 function! s:get_completion_prefix()
266  
-  let prefix = strpart(getline('.'), 0, col('.') - 2)
267  
-  return match(prefix, '\w\+\.\?\w*$')
  266
+  return match(strpart(getline('.'), 0, col('.') - 2), '\w\+\.\?\w*$')
268 267
 endfunction
269 268
 
270  
-function! xolox#lua#completedynamic() " {{{1
  269
+function! xolox#lua#completedynamic(type) " {{{1
271 270
   if xolox#lua#getopt('lua_complete_dynamic', 1)
272  
-    if s:getsynid(1) !~? 'string\|comment\|keyword'
  271
+    if (a:type == "'" || a:type == '"') && xolox#lua#getopt('lua_complete_omni', 0)
  272
+      if strpart(getline('.'), 0, col('.') - 1) =~ 'require[^''"]*$'
  273
+        return a:type . "\<C-x>\<C-o>"
  274
+      endif
  275
+    elseif a:type == '.' && s:getsynid(1) !~? 'string\|comment\|keyword'
273 276
       let column = col('.') - 1
274 277
       " Gotcha: even though '.' is remapped it counts as a column?
275 278
       if column && getline('.')[column - 1] =~ '\w'
276  
-        " This results in "Pattern not found" when no completion items
277  
-        " matched, which is kind of annoying. But I don't know an alternative
278  
-        " to :silent that can be used inside of <expr> mappings?!
279  
-        return ".\<C-x>\<C-u>"
  279
+        " This results in "Pattern not found" when no completion candidates
  280
+        " are available, which is kind of annoying. But I don't know of an
  281
+        " alternative to :silent that can be used inside of <expr>
  282
+        " mappings?!
  283
+        return a:type . "\<C-x>\<C-u>"
280 284
       endif
281 285
     endif
282 286
   endif
283  
-  return '.'
  287
+  return a:type
284 288
 endfunction
285 289
 
286 290
 function! xolox#lua#omnifunc(init, base) " {{{1
@@ -289,30 +293,48 @@ function! xolox#lua#omnifunc(init, base) " {{{1
289 293
   elseif !xolox#lua#getopt('lua_complete_omni', 0)
290 294
     throw printf("%s: omni completion needs to be explicitly enabled, see the readme!", s:script)
291 295
   endif
292  
-  if !exists('s:omnifunc_candidates')
293  
-    let s:omnifunc_candidates = xolox#lua#getomnicandidates()
  296
+  if !exists('s:omnifunc_modules')
  297
+    let s:omnifunc_modules = xolox#lua#getomnimodules()
  298
+  endif
  299
+  if !exists('s:omnifunc_variables')
  300
+    let s:omnifunc_variables = xolox#lua#getomnivariables(s:omnifunc_modules)
294 301
   endif
295  
-  if a:base == ''
296  
-    return s:omnifunc_candidates
  302
+  " FIXME When you type "require'" without a space in between
  303
+  " the getline('.') call below returns an empty string?!
  304
+  if getline('.') =~ 'require[^''"]*[''"]'
  305
+    let pattern = xolox#misc#escape#pattern(a:base)
  306
+    return filter(copy(s:omnifunc_modules), 'v:val =~ pattern')
  307
+  elseif a:base == ''
  308
+    return s:omnifunc_variables
297 309
   else
298 310
     let pattern = xolox#misc#escape#pattern(a:base)
299  
-    return filter(copy(s:omnifunc_candidates), 'v:val =~ pattern')
  311
+    return filter(copy(s:omnifunc_variables), 'v:val =~ pattern')
300 312
   endif
301 313
 endfunction
302 314
 
303  
-function! xolox#lua#getomnicandidates() " {{{1
  315
+function! xolox#lua#getomnimodules() " {{{1
304 316
   let starttime = xolox#misc#timer#start()
305  
-  let modules = {}
  317
+  let modulemap = {}
306 318
   let luapath = xolox#lua#getsearchpath('$LUA_PATH', 'package.path')
307 319
   let luacpath = xolox#lua#getsearchpath('$LUA_CPATH', 'package.cpath')
308 320
   for searchpath in [luapath, luacpath]
309  
-    call s:expandsearchpath(searchpath, modules)
  321
+    call s:expandsearchpath(searchpath, modulemap)
310 322
   endfor
311  
-  let output = xolox#lua#dofile(s:omnicomplete_script, keys(modules))
312  
-  let lines = split(output, "\n")
313  
-  call sort(lines, 1)
314  
-  call xolox#misc#timer#stop("%s: Collected omni completion candidates in %s", s:script, starttime)
315  
-  return lines
  323
+  let modules = keys(modulemap)
  324
+  call sort(modules)
  325
+  let msg = "%s: Collected %i module names for omni completion in %s"
  326
+  call xolox#misc#timer#stop(msg, s:script, len(modules), starttime)
  327
+  return modules
  328
+endfunction
  329
+
  330
+function! xolox#lua#getomnivariables(modules) " {{{1
  331
+  let starttime = xolox#misc#timer#start()
  332
+  let output = xolox#lua#dofile(s:omnicomplete_script, a:modules)
  333
+  let variables = split(output, "\n")
  334
+  call sort(variables, 1)
  335
+  let msg = "%s: Collected %i variables for omni completion in %s"
  336
+  call xolox#misc#timer#stop(msg, s:script, len(variables), starttime)
  337
+  return variables
316 338
 endfunction
317 339
 
318 340
 let s:omnicomplete_script = expand('<sfile>:p:h:h:h') . '/misc/lua-ftplugin/omnicomplete.lua'
10  doc/lua.txt
@@ -20,10 +20,12 @@ code in Vim by providing the following features:
20 20
  - The |'completefunc'| option is set to allow completion of Lua 5.1 keywords,
21 21
    global variables and library members using Control-X Control-U
22 22
 
23  
- - The |'omnifunc'| option is set to allow dynamic completion of all modules
24  
-   installed on the system using Control-X Control-O, however it needs to be
25  
-   explicitly enabled by setting the |lua_complete_omni| option because this
26  
-   functionality may have undesired side effects!
  23
+ - The |'omnifunc'| option is set to allow dynamic completion of the variables
  24
+   defined in all modules installed on the system using Control-X Control-O,
  25
+   however it needs to be explicitly enabled by setting the
  26
+   |lua_complete_omni| option because this functionality may have undesired
  27
+   side effects! When you invoke omni completion after typing 'require '' or
  28
+   'require('' you get completion of module names
27 29
 
28 30
  - Several |text-objects| are defined so you can jump between blocks and
29 31
    functions
10  ftplugin/lua.vim
@@ -3,7 +3,7 @@
3 3
 " Author: Peter Odding <peter@peterodding.com>
4 4
 " Last Change: June 14, 2011
5 5
 " URL: http://peterodding.com/code/vim/lua-ftplugin
6  
-" Version: 0.6.1
  6
+" Version: 0.6.2
7 7
 
8 8
 " Support for automatic update using the GLVS plug-in.
9 9
 " GetLatestVimScripts: 3625 1 :AutoInstall: lua.zip
@@ -74,9 +74,13 @@ if exists('loaded_matchit')
74 74
   call add(s:undo_ftplugin, 'unlet! b:match_ignorecase b:match_words b:match_skip')
75 75
 endif
76 76
 
77  
-" Enable dynamic completion on typing the "." operator? {{{1
78  
-imap <buffer> <silent> <expr> . xolox#lua#completedynamic()
  77
+" Enable dynamic completion on typing "require('" or "variable."? {{{1
  78
+inoremap <buffer> <silent> <expr> . xolox#lua#completedynamic('.')
79 79
 call add(s:undo_ftplugin, 'iunmap <buffer> .')
  80
+inoremap <buffer> <silent> <expr> ' xolox#lua#completedynamic("'")
  81
+call add(s:undo_ftplugin, "iunmap <buffer> '")
  82
+inoremap <buffer> <silent> <expr> " xolox#lua#completedynamic('"')
  83
+call add(s:undo_ftplugin, 'iunmap <buffer> "')
80 84
 
81 85
 " }}}1
82 86
 

0 notes on commit ffaf36b

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