misc: intro a vim plugin with better autocomplete
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 31 Jan 2015 16:40:20 +0000 (11:40 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 3 Feb 2015 02:46:48 +0000 (21:46 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

misc/README
misc/vim/plugin/nit.vim [new file with mode: 0644]

index 6f19e2c..3c8abfb 100644 (file)
@@ -11,10 +11,10 @@ Then can add the brush to your html page:
 
        <script type="text/javascript" src="shBrushNit.js"></script>
 
-# vim
+# Vim
 
-vim is a powerful text editor.
-The misc/vim directory contains the support of nit files to vim.
+Vim is a powerful text editor and a favorite of the Nit team.
+The `misc/vim` directory provides Vim support for Nit source files.
 
 ### Install
 
@@ -23,7 +23,7 @@ The simpler way to install nit for vim is with [pathogen][1].
     cd ~/.vim/bundle
     ln -s /full/path/to/nit/misc/vim nit
 
-Ensure that your .vimrc contains
+Ensure that `~/.vimrc` contains
 
     call pathogen#infect()
     syntax on
@@ -33,8 +33,32 @@ Ensure that your .vimrc contains
 
 ### Features
 
- * syntax highlighting
- * indentation
- * syntax checker (require [Syntastic][2]).
+ * Syntax highlighting
+ * Automatic indentation
+ * Syntax checker (require [Syntastic][2]).
+ * Autocomplete for whole projects using module importations
 
   [2]: https://github.com/scrooloose/syntastic
+
+### Autocomplete
+
+The Nit plugin offers better autocomplete by scanning all projects in the
+current directory, and their dependencies.
+
+Add the following code to `~/.vimrc`, then use `ctrl-n` to open the
+autocomplete popup.
+
+~~~
+" Compute Nit module dependencies for autocomplete on loading our first Nit module
+autocmd Filetype nit call NitComplete()
+
+" Map reloading Nit module dependencies to F2
+map <F2> :call ForceNitComplete()<enter>
+~~~
+
+The plugin is compatible with, and optimized for, [AutoComplPop][3].
+
+Look at the functions defined in `misc/vim/plugin/nit.vim` for all possible
+usages.
+
+  [3]: http://www.vim.org/scripts/script.php?script_id=1879
diff --git a/misc/vim/plugin/nit.vim b/misc/vim/plugin/nit.vim
new file mode 100644 (file)
index 0000000..d5921e0
--- /dev/null
@@ -0,0 +1,82 @@
+" This file is part of NIT ( http://www.nitlanguage.org ).
+"
+" Licensed under the Apache License, Version 2.0 (the "License");
+" you may not use this file except in compliance with the License.
+" You may obtain a copy of the License at
+"
+"               http://www.apache.org/licenses/LICENSE-2.0
+"
+" Unless required by applicable law or agreed to in writing, software
+" distributed under the License is distributed on an "AS IS" BASIS,
+" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+" See the License for the specific language governing permissions and
+" limitations under the License.
+
+" Nit plugin for Vim, provides some advanced features
+
+if v:version < 700
+       finish
+endif
+
+if exists("loaded_nit_plugin")
+       finish
+endif
+let loaded_nit_plugin = 1
+
+" Scan all relevant Nit modules for the current directory to autocomplete
+"
+" The guard `g:nit_complete_done` ensures that its body is executed only
+" once. The call to `nitls -M` analyzses the current directory. However,
+" updating the module list can be forced using ForceNitComplete.
+"
+" To activate, add the following line to ~/.vimrc
+"
+" autocmd Filetype nit call NitComplete()
+function NitComplete()
+       if !exists("g:nit_complete_done")
+               let g:nit_complete_done = 1
+
+               " Reset or backup the original complete
+               if !exists("g:nit_complete_backup")
+                       let g:nit_complete_backup = &complete
+               else
+                       silent let &complete = g:nit_complete_backup
+                       silent set complete?
+               endif
+
+               " This gives us better results for Nit
+               set noignorecase
+               set completeopt=longest,menuone
+
+               " Do not predict small 3 letters keywords (or their prefix), they slow down
+               " prediction and some also require double-enter on end of line.
+               let g:acp_behaviorKeywordIgnores = ['new', 'var', 'in', 'do', 'els', 'end', 'ret', 'for', 'fun']
+
+               " Use nitls to compute all interesting files from the current directory
+               for file in split(system('nitls -M', '\n'))
+                       silent let &complete = &complete . ',s' . file
+                       silent set complete?
+               endfor
+
+               " Compatibility with AutoComplPop
+               let g:acp_completeOption = &complete
+               let g:acp_ignorecaseOption = &ignorecase
+
+               " Redraw in case the user pressed some keys while waiting
+               redraw!
+       endif
+endfunction
+
+" Force updating the Nit modules used for autocomplete
+"
+" It is recommended to manually call this function as needed. It can be mapped
+" to a key with:
+"
+" map <F2> :call ForceNitComplete()
+"
+" For small projects (or fast computers) you might want to call it on each
+" file save or load.
+function ForceNitComplete()
+       unlet! g:nit_complete_done
+       call NitComplete()
+endfunction