Skip to content

Commit ffaf36b

Browse files
committed
Completion of module names after typing require('
The following variants are currently supported: * require '...' * require "..." * require('...') * require("...")
1 parent 6d68f3d commit ffaf36b

File tree

4 files changed

+58
-30
lines changed

4 files changed

+58
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The [Lua][lua] file type plug-in for [Vim][vim] makes it easier to work with Lua
1212

1313
* The ['completefunc'][cfu] option is set to allow completion of Lua 5.1 keywords, global variables and library members using Control-X Control-U
1414

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
1616

1717
* Several [text-objects][tob] are defined so you can jump between blocks and functions
1818

autoload/xolox/lua.vim

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -263,24 +263,28 @@ function! xolox#lua#completefunc(init, base) " {{{1
263263
endfunction
264264

265265
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*$')
268267
endfunction
269268

270-
function! xolox#lua#completedynamic() " {{{1
269+
function! xolox#lua#completedynamic(type) " {{{1
271270
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'
273276
let column = col('.') - 1
274277
" Gotcha: even though '.' is remapped it counts as a column?
275278
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>"
280284
endif
281285
endif
282286
endif
283-
return '.'
287+
return a:type
284288
endfunction
285289

286290
function! xolox#lua#omnifunc(init, base) " {{{1
@@ -289,30 +293,48 @@ function! xolox#lua#omnifunc(init, base) " {{{1
289293
elseif !xolox#lua#getopt('lua_complete_omni', 0)
290294
throw printf("%s: omni completion needs to be explicitly enabled, see the readme!", s:script)
291295
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)
294301
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
297309
else
298310
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')
300312
endif
301313
endfunction
302314

303-
function! xolox#lua#getomnicandidates() " {{{1
315+
function! xolox#lua#getomnimodules() " {{{1
304316
let starttime = xolox#misc#timer#start()
305-
let modules = {}
317+
let modulemap = {}
306318
let luapath = xolox#lua#getsearchpath('$LUA_PATH', 'package.path')
307319
let luacpath = xolox#lua#getsearchpath('$LUA_CPATH', 'package.cpath')
308320
for searchpath in [luapath, luacpath]
309-
call s:expandsearchpath(searchpath, modules)
321+
call s:expandsearchpath(searchpath, modulemap)
310322
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
316338
endfunction
317339

318340
let s:omnicomplete_script = expand('<sfile>:p:h:h:h') . '/misc/lua-ftplugin/omnicomplete.lua'

doc/lua.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ code in Vim by providing the following features:
2020
- The |'completefunc'| option is set to allow completion of Lua 5.1 keywords,
2121
global variables and library members using Control-X Control-U
2222

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
2729

2830
- Several |text-objects| are defined so you can jump between blocks and
2931
functions

ftplugin/lua.vim

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
" Author: Peter Odding <peter@peterodding.com>
44
" Last Change: June 14, 2011
55
" URL: http://peterodding.com/code/vim/lua-ftplugin
6-
" Version: 0.6.1
6+
" Version: 0.6.2
77

88
" Support for automatic update using the GLVS plug-in.
99
" GetLatestVimScripts: 3625 1 :AutoInstall: lua.zip
@@ -74,9 +74,13 @@ if exists('loaded_matchit')
7474
call add(s:undo_ftplugin, 'unlet! b:match_ignorecase b:match_words b:match_skip')
7575
endif
7676

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('.')
7979
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> "')
8084

8185
" }}}1
8286

0 commit comments

Comments
 (0)