Skip to content

Commit

Permalink
Always include standard library modules in module name completion
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Jun 22, 2013
1 parent a959a1b commit 5157b16
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 81 deletions.
1 change: 1 addition & 0 deletions addon-info.json
@@ -0,0 +1 @@
{"vim_script_nr": 3625, "dependencies": {"vim-misc": {}}, "homepage": "http://peterodding.com/code/vim/lua-ftplugin", "name": "vim-lua-ftplugin"}
9 changes: 6 additions & 3 deletions autoload/xolox/lua.vim
@@ -1,9 +1,9 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: May 25, 2013
" Last Change: June 22, 2013
" URL: http://peterodding.com/code/vim/lua-ftplugin

let g:xolox#lua#version = '0.7.10'
let g:xolox#lua#version = '0.7.11'
let s:miscdir = expand('<sfile>:p:h:h:h') . '/misc/lua-ftplugin'
let s:omnicomplete_script = s:miscdir . '/omnicomplete.lua'
let s:globals_script = s:miscdir . '/globals.lua'
Expand Down Expand Up @@ -366,14 +366,17 @@ endfunction

function! xolox#lua#getomnimodules() " {{{1
let starttime = xolox#misc#timer#start()
" Find all source & binary modules available on the module search path.
let modulemap = {}
let luapath = xolox#lua#getsearchpath('$LUA_PATH', 'package.path')
let luacpath = xolox#lua#getsearchpath('$LUA_CPATH', 'package.cpath')
for searchpath in [luapath, luacpath]
call s:expandsearchpath(searchpath, modulemap)
endfor
let modules = keys(modulemap)
call sort(modules)
" Always include the standard library modules.
call extend(modules, ['coroutine', 'debug', 'io', 'math', 'os', 'package', 'string', 'table'])
call sort(modules, 1)
let msg = "lua.vim %s: Collected %i module names for omni completion in %s"
call xolox#misc#timer#stop(msg, g:xolox#lua#version, len(modules), starttime)
return modules
Expand Down
153 changes: 75 additions & 78 deletions doc/ft_lua.txt
@@ -1,12 +1,11 @@
*ft_lua.txt* Lua file type plug-in for the Vim text editor

===============================================================================
*ft_lua-contents*
Contents ~

1. Introduction |ft_lua-introduction|
2. Installation |ft_lua-installation|
3. Options |ft_lua-options|
1. Introduction |ft_lua-introduction|
2. Installation |ft_lua-installation|
3. Options |ft_lua-options|
1. The |lua_path| option
2. The |lua_check_syntax| option
3. The |lua_check_globals| option
Expand All @@ -19,94 +18,93 @@ Contents ~
10. The |lua_complete_dynamic| option
11. The |lua_complete_omni| option
12. The |lua_define_completion_mappings| option
4. Contact |ft_lua-contact|
5. License |ft_lua-license|
4. Contact |ft_lua-contact|
5. License |ft_lua-license|
6. References |ft_lua-references|

===============================================================================
*ft_lua-introduction*
*ft_lua-introduction*
Introduction ~

The Lua [1] file type plug-in for Vim makes it easier to work with Lua source
code in Vim by providing the following features:

- The |'includeexpr'| option is set so that the |gf| (go to file) mapping knows
how to resolve Lua module names using package.path [2]
- The |'includeexpr'| option is set so that the |gf| (go to file) mapping
knows how to resolve Lua module names using package.path [2]

- The |'include'| option is set so that Vim follows dofile() [3], loadfile() [4]
and require() [5] calls when looking for identifiers in included files
(this works together with the |'includeexpr'| option)
- The |'include'| option is set so that Vim follows dofile() [3], loadfile()
[4] and require() [5] calls when looking for identifiers in included files
(this works together with the |'includeexpr'| option)

- An automatic command is installed that runs 'luac -p' when you save your Lua
scripts. If 'luac' reports any errors they are shown in the quick-fix list
and Vim jumps to the line of the first error. If 'luac -p' doesn't report
any errors a check for undefined global variables is performed by parsing
the output of 'luac -p -l'
- An automatic command is installed that runs 'luac -p' when you save your
Lua scripts. If 'luac' reports any errors they are shown in the quick-fix
list and Vim jumps to the line of the first error. If 'luac -p' doesn't
report any errors a check for undefined global variables is performed by
parsing the output of 'luac -p -l'

- '<F1>' on a Lua function or 'method' call will try to open the relevant
documentation in the Lua Reference for Vim [6]
- '<F1>' on a Lua function or 'method' call will try to open the relevant
documentation in the Lua Reference for Vim [6]

- The |'completefunc'| option is set to allow completion of Lua 5.1 keywords,
global variables and library members using Control-X Control-U
- The |'completefunc'| option is set to allow completion of Lua 5.1 keywords,
global variables and library members using Control-X Control-U

- The |'omnifunc'| 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
- The |'omnifunc'| 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

Screenshot of omni completion, see reference [7]
Image: Screenshot of omni completion (see reference [7])

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

- A pretty nifty hack of the matchit plug-in (see |matchit-install|) is
included: When the cursor is on a 'function' or 'return' keyword the '%'
mapping cycles between the relevant keywords ('function', 'return', 'end'),
this also works for branching statements ('if', 'elseif', 'else', 'end')
and looping statements ('for', 'while', 'repeat', 'until', 'end')
- A pretty nifty hack of the matchit plug-in (see |matchit-install|) is
included: When the cursor is on a 'function' or 'return' keyword the '%'
mapping cycles between the relevant keywords ('function', 'return', 'end'),
this also works for branching statements ('if', 'elseif', 'else', 'end')
and looping statements ('for', 'while', 'repeat', 'until', 'end')

===============================================================================
*ft_lua-installation*
*ft_lua-installation*
Installation ~

Please note that the vim-lua-ftplugin plug-in requires my vim-misc plug-in
which is separately distributed.
_Please note that the vim-lua-ftplugin plug-in requires my vim-misc plug-in
which is separately distributed._

Unzip the most recent ZIP archives of the vim-lua-ftplugin [8] and vim-misc
[9] plug-ins inside your Vim profile directory (usually this is '~/.vim' on
UNIX and '%USERPROFILE%\vimfiles' on Windows), restart Vim and execute the
command ':helptags ~/.vim/doc' (use ':helptags ~\vimfiles\doc' instead on
Windows).
Unzip the most recent ZIP archives of the vim-lua-ftplugin [8] and vim-misc [9]
plug-ins inside your Vim profile directory (usually this is '~/.vim' on UNIX
and '%USERPROFILE%\vimfiles' on Windows), restart Vim and execute the command
':helptags ~/.vim/doc' (use ':helptags ~\vimfiles\doc' instead on Windows).

If you prefer you can also use Pathogen [10], Vundle [11] or a similar tool to
install & update the vim-lua-ftplugin [12] and vim-misc [13] plug-ins using a
local clone of the git repository.

Now try it out: Edit a Lua script and try any of the features documented
above.
Now try it out: Edit a Lua script and try any of the features documented above.

Note that on Windows a command prompt window pops up whenever Lua is run as an
external process. If this bothers you then you can install my shell.vim [14]
plug-in which includes a DLL [15] that works around this issue. Once you've
installed both plug-ins it should work out of the box!

===============================================================================
*ft_lua-options*
*ft_lua-options*
Options ~

The Lua file type plug-in handles options as follows: First it looks at buffer
local variables, then it looks at global variables and if neither exists a
default is chosen. This means you can change how the plug-in works for
individual buffers. For example to change the location of the Lua compiler
used to check the syntax:
individual buffers. For example to change the location of the Lua compiler used
to check the syntax:
>
" This sets the default value for all buffers.
:let g:lua_compiler_name = '/usr/local/bin/luac'
" This is how you change the value for one buffer.
:let b:lua_compiler_name = '/usr/local/bin/lualint'
" This sets the default value for all buffers.
:let g:lua_compiler_name = '/usr/local/bin/luac'
" This is how you change the value for one buffer.
:let b:lua_compiler_name = '/usr/local/bin/lualint'
<
-------------------------------------------------------------------------------
The *lua_path* option

Expand All @@ -122,8 +120,8 @@ When you write a Lua script to disk the plug-in automatically runs the Lua
compiler to check for syntax errors. To disable this behavior you can set this
option to false (0):
>
let g:lua_check_syntax = 0
let g:lua_check_syntax = 0
<
You can manually check the syntax using the ':CheckSyntax' command.

-------------------------------------------------------------------------------
Expand All @@ -133,16 +131,16 @@ When you write a Lua script to disk the plug-in automatically runs the Lua
compiler to check for undefined global variables. To disable this behavior you
can set this option to false (0):
>
let g:lua_check_globals = 0
let g:lua_check_globals = 0
<
You can manually check the globals using the ':CheckGlobals' command.

-------------------------------------------------------------------------------
The *lua_compiler_name* option

The name or path of the Lua compiler used to check for syntax errors (defaults
to 'luac'). You can set this option to run the Lua compiler from a
non-standard location or to run a dedicated syntax checker like lualint [16].
to 'luac'). You can set this option to run the Lua compiler from a non-standard
location or to run a dedicated syntax checker like lualint [16].

-------------------------------------------------------------------------------
The *lua_compiler_args* option
Expand All @@ -163,8 +161,7 @@ To disable completion of keywords you can set this option to false (0).
-------------------------------------------------------------------------------
The *lua_complete_globals* option

To disable completion of global functions you can set this option to false
(0).
To disable completion of global functions you can set this option to false (0).

-------------------------------------------------------------------------------
The *lua_complete_library* option
Expand All @@ -184,20 +181,20 @@ The *lua_complete_omni* option

This option is disabled by default for two reasons:

- The omni completion support works by enumerating and loading all installed
modules. If module loading has side effects this can have unintended
consequences!
- The omni completion support works by enumerating and loading all installed
modules. **If module loading has side effects this can have unintended
consequences!**

- Because all modules installed on the system are loaded, collecting the
completion candidates can be slow. After the first run the completion
candidates are cached so this will only bother you once (until you restart
Vim).
- Because all modules installed on the system are loaded, collecting the
completion candidates can be slow. After the first run the completion
candidates are cached so this will only bother you once (until you restart
Vim).

If you want to use the omni completion despite the warnings above, execute the
following command:
>
:let g:lua_complete_omni = 1
:let g:lua_complete_omni = 1
<
Now when you type Control-X Control-O Vim will hang for a moment, after which
you should be presented with an enormous list of completion candidates :-)

Expand All @@ -211,23 +208,23 @@ like these mappings you can set this option to zero (false). In that case the
mappings will not be defined.

===============================================================================
*ft_lua-contact*
*ft_lua-contact*
Contact ~

If you have questions, bug reports, suggestions, etc. the author can be
contacted at peter@peterodding.com. The latest version is available at
http://peterodding.com/code/vim/lua-ftplugin and http://github.com/xolox/vim-lua-ftplugin.
If you like this plug-in please vote for it on Vim Online [17].
http://peterodding.com/code/vim/lua-ftplugin and http://github.com/xolox/vim-
lua-ftplugin. If you like this plug-in please vote for it on Vim Online [17].

===============================================================================
*ft_lua-license*
*ft_lua-license*
License ~

This software is licensed under the MIT license [18]. Copyright 2013 Peter
Odding <peter@peterodding.com>.
This software is licensed under the MIT license [18]. © 2013 Peter Odding
<peter@peterodding.com>.

===============================================================================
*ft_lua-references*
*ft_lua-references*
References ~

[1] http://www.lua.org/
Expand Down

0 comments on commit 5157b16

Please sign in to comment.