1
0
Fork 0
mirror of synced 2024-11-22 16:55:34 -05:00

Added and updated some plugins

Added: vim-ruby, typescript-vim, vim-javascript
Updated: rust-vim
This commit is contained in:
Amir Salihefendic 2019-11-16 18:43:18 +01:00
parent ff0ede6b30
commit 97e3db7fe9
108 changed files with 12408 additions and 869 deletions

View file

@ -126,6 +126,9 @@ I recommend reading the docs of these plugins to understand them better. Each pl
* [vim-markdown](https://github.com/tpope/vim-markdown) * [vim-markdown](https://github.com/tpope/vim-markdown)
* [nginx.vim](https://github.com/vim-scripts/nginx.vim): Highlights configuration files for nginx * [nginx.vim](https://github.com/vim-scripts/nginx.vim): Highlights configuration files for nginx
* [vim-go](https://github.com/fatih/vim-go) * [vim-go](https://github.com/fatih/vim-go)
* [vim-ruby](https://github.com/vim-ruby/vim-ruby)
* [typescript-vim](https://github.com/leafgarland/typescript-vim)
* [vim-javascript](https://github.com/pangloss/vim-javascript)
## How to include your own stuff? ## How to include your own stuff?

View file

@ -0,0 +1,7 @@
---
sudo: required
services:
- docker
language: generic
script: |
cd test && ./run-tests

View file

@ -0,0 +1,10 @@
cmdargs:
# Checking more strictly
severity: style_problem
policies:
# Disable a violation
ProhibitUnnecessaryDoubleQuote:
enabled: false
ProhibitImplicitScopeVariable:
enabled: false

View file

@ -0,0 +1,27 @@
<!--
Hi, and thanks for reporting an issue with rust.vim.
Details about your environment will help us assist you.
Please edit this template!
-->
* rust.vim version: <!-- Describe if you use a Vim plugin manager, and you
can use it to tell which version of rust.vim you are running. -->
Steps to reproduce:
<!-- It's best to try to reproduce the issue with the master version of
rust.vim. The issue may already be fixed! -->
_?_
Expected vs. actual behavior:
_?_
Paste debugging info from the Rust Vim plugin via _one_ of the following
commands: `:RustInfo`, `:RustInfoToClipboard`, or `:RustInfoToFile <filename>`.
_?_

View file

@ -3,42 +3,47 @@
## Description ## Description
This is a Vim plugin that provides [Rust][r] file detection, syntax highlighting, formatting, This is a Vim plugin that provides [Rust][r] file detection, syntax highlighting, formatting,
[Syntastic][syn] integration, and more. [Syntastic][syn] integration, and more. It requires Vim 8 or higher for full functionality.
Some things may not work on earlier versions.
## Installation ## Installation
### Using [Vundle][v] Use one of the following package managers:
1. Add `Plugin 'rust-lang/rust.vim'` to `~/.vimrc` * [Vim8 packages][vim8pack]:
2. `:PluginInstall` or `$ vim +PluginInstall +qall` * `git clone https://github.com/rust-lang/rust.vim ~/.vim/pack/plugins/start/rust.vim`
* [Vundle][v]:
*Note:* Vundle will not automatically detect Rust files properly if `filetype * Add `Plugin 'rust-lang/rust.vim'` to `~/.vimrc`
* `:PluginInstall` or `$ vim +PluginInstall +qall`
* *Note:* Vundle will not automatically detect Rust files properly if `filetype
on` is executed before Vundle. Please check the [quickstart][vqs] for more on` is executed before Vundle. Please check the [quickstart][vqs] for more
details. details. Errors such as `Not an editor command: RustFmt` may occur if Vundle
is misconfigured with this plugin.
### Using [Pathogen][p] * [Pathogen][p]:
* `git clone --depth=1 https://github.com/rust-lang/rust.vim.git ~/.vim/bundle/rust.vim`
```shell * [vim-plug][vp]:
git clone --depth=1 https://github.com/rust-lang/rust.vim.git ~/.vim/bundle/rust.vim * Add `Plug 'rust-lang/rust.vim'` to `~/.vimrc`
``` * `:PlugInstall` or `$ vim +PlugInstall +qall`
* [dein.vim][d]:
### Using [NeoBundle][nb] * Add `call dein#add('rust-lang/rust.vim')` to `~/.vimrc`
* `:call dein#install()`
1. Add `NeoBundle 'rust-lang/rust.vim'` to `~/.vimrc` * [NeoBundle][nb]:
2. Re-open vim or execute `:source ~/.vimrc` * Add `NeoBundle 'rust-lang/rust.vim'` to `~/.vimrc`
* Re-open vim or execute `:source ~/.vimrc`
### Using [vim-plug][vp]
1. Add `Plug 'rust-lang/rust.vim'` to `~/.vimrc`
2. `:PlugInstall` or `$ vim +PlugInstall +qall`
## Features ## Features
### Error checking with [Syntastic][syn] ### Error checking with [Syntastic][syn]
`rust.vim` automatically registers `rustc` as a syntax checker `rust.vim` automatically registers `cargo` as a syntax checker with
with [Syntastic][syn]. Check Syntastic's documentation for [Syntastic][syn], if nothing else is specified. See `:help rust-syntastic`
information on how to customize its behaviour. for more details.
### Source browsing with [Tagbar][tgbr]
The installation of Tagbar along with [Universal Ctags][uctags] is recommended
for a good Tagbar experience. For other kinds of setups, `rust.vim` tries to
configure Tagbar to some degree.
### Formatting with [rustfmt][rfmt] ### Formatting with [rustfmt][rfmt]
@ -68,7 +73,10 @@ If you set g:rust_clip_command RustPlay will copy the url to the clipboard.
let g:rust_clip_command = 'xclip -selection clipboard' let g:rust_clip_command = 'xclip -selection clipboard'
[rfmt]: https://crates.io/crates/rustfmt/ ### Running a test under cursor
In cargo project, the `:RustTest` command will run a test under the cursor.
This is useful when your project is bigger and running all tests take longer time.
## Help ## Help
@ -91,7 +99,11 @@ LICENSE-MIT for details.
[p]: https://github.com/tpope/vim-pathogen [p]: https://github.com/tpope/vim-pathogen
[nb]: https://github.com/Shougo/neobundle.vim [nb]: https://github.com/Shougo/neobundle.vim
[vp]: https://github.com/junegunn/vim-plug [vp]: https://github.com/junegunn/vim-plug
[d]: https://github.com/Shougo/dein.vim
[rfmt]: https://github.com/rust-lang-nursery/rustfmt [rfmt]: https://github.com/rust-lang-nursery/rustfmt
[syn]: https://github.com/scrooloose/syntastic [syn]: https://github.com/scrooloose/syntastic
[tgbr]: https://github.com/majutsushi/tagbar
[uctags]: https://ctags.io
[wav]: https://github.com/mattn/webapi-vim [wav]: https://github.com/mattn/webapi-vim
[pp]: https://play.rust-lang.org/ [pp]: https://play.rust-lang.org/
[vim8pack]: http://vimhelp.appspot.com/repeat.txt.html#packages

View file

@ -1,9 +1,11 @@
if !exists('g:rust_conceal') || g:rust_conceal == 0 || !has('conceal') || &enc != 'utf-8' scriptencoding utf-8
if !get(g:, 'rust_conceal', 0) || !has('conceal') || &encoding !=# 'utf-8'
finish finish
endif endif
" For those who don't want to see `::`... " For those who don't want to see `::`...
if exists('g:rust_conceal_mod_path') && g:rust_conceal_mod_path != 0 if get(g:, 'rust_conceal_mod_path', 0)
syn match rustNiceOperator "::" conceal cchar= syn match rustNiceOperator "::" conceal cchar=
endif endif
@ -18,7 +20,7 @@ syn match rustNiceOperator "=>" contains=rustFatRightArrowHead,rustFatRightArrow
syn match rustNiceOperator /\<\@!_\(_*\>\)\@=/ conceal cchar= syn match rustNiceOperator /\<\@!_\(_*\>\)\@=/ conceal cchar=
" For those who don't want to see `pub`... " For those who don't want to see `pub`...
if exists('g:rust_conceal_pub') && g:rust_conceal_pub != 0 if get(g:, 'rust_conceal_pub', 0)
syn match rustPublicSigil contained "pu" conceal cchar= syn match rustPublicSigil contained "pu" conceal cchar=
syn match rustPublicRest contained "b" conceal cchar=  syn match rustPublicRest contained "b" conceal cchar= 
syn match rustNiceOperator "pub " contains=rustPublicSigil,rustPublicRest syn match rustNiceOperator "pub " contains=rustPublicSigil,rustPublicRest
@ -26,9 +28,14 @@ endif
hi link rustNiceOperator Operator hi link rustNiceOperator Operator
if !(exists('g:rust_conceal_mod_path') && g:rust_conceal_mod_path != 0) if !get(g:, 'rust_conceal_mod_path', 0)
hi! link Conceal Operator hi! link Conceal Operator
augroup rust.vim.after
autocmd!
" And keep it after a colorscheme change " And keep it after a colorscheme change
au ColorScheme <buffer> hi! link Conceal Operator autocmd ColorScheme <buffer> hi! link Conceal Operator
augroup END
endif endif
" vim: set et sw=4 sts=4 ts=8:

View file

@ -1,85 +1,115 @@
function! cargo#cmd(args) function! cargo#Load()
silent! clear " Utility call to get this script loaded, for debugging
if !a:args
execute "!" . "cargo ". a:args
else
echom "Missing arguments"
endif
endfunction endfunction
function! cargo#build(args) function! cargo#cmd(args)
silent! clear execute "! cargo" a:args
if !a:args endfunction
execute "!" . "cargo build " . a:args
function! s:nearest_cargo(...) abort
" If the second argument is not specified, the first argument determines
" whether we will start from the current directory or the directory of the
" current buffer, otherwise, we start with the provided path on the
" second argument.
let l:is_getcwd = get(a:, 1, 0)
if l:is_getcwd
let l:starting_path = get(a:, 2, getcwd())
else else
execute "!" . "cargo build" let l:starting_path = get(a:, 2, expand('%:p:h'))
endif endif
silent! clear
execute "!" . "cargo build" return findfile('Cargo.toml', l:starting_path . ';')
endfunction
function! cargo#nearestCargo(is_getcwd) abort
return s:nearest_cargo(a:is_getcwd)
endfunction
function! cargo#nearestWorkspaceCargo(is_getcwd) abort
let l:nearest = s:nearest_cargo(a:is_getcwd)
while l:nearest !=# ''
for l:line in readfile(l:nearest, '', 0x100)
if l:line =~# '\V[workspace]'
return l:nearest
endif
endfor
let l:next = fnamemodify(l:nearest, ':p:h:h')
let l:nearest = s:nearest_cargo(0, l:next)
endwhile
return ''
endfunction
function! cargo#nearestRootCargo(is_getcwd) abort
" Try to find a workspace Cargo.toml, and if not found, take the nearest
" regular Cargo.toml
let l:workspace_cargo = cargo#nearestWorkspaceCargo(a:is_getcwd)
if l:workspace_cargo !=# ''
return l:workspace_cargo
endif
return s:nearest_cargo(a:is_getcwd)
endfunction
function! cargo#build(args)
call cargo#cmd("build " . a:args)
endfunction endfunction
function! cargo#clean(args) function! cargo#clean(args)
silent! clear call cargo#cmd("clean " . a:args)
if !a:args
execute "!" . "cargo clean " . a:args
else
execute "!" . "cargo clean"
endif
silent! clear
execute "!" . "cargo clean"
endfunction endfunction
function! cargo#doc(args) function! cargo#doc(args)
silent! clear call cargo#cmd("doc " . a:args)
if !a:args
execute "!" . "cargo doc " . a:args
else
execute "!" . "cargo doc"
endif
endfunction endfunction
function! cargo#new(args) function! cargo#new(args)
silent! clear call cargo#cmd("new " . a:args)
if !a:args cd `=a:args`
execute "!cargo new " . a:args
:cd `=a:args`
else
echom "Missing arguments"
endif
endfunction endfunction
function! cargo#init(args) function! cargo#init(args)
silent! clear call cargo#cmd("init " . a:args)
if !a:args
execute "!" . "cargo init " . a:args
else
execute "!" . "cargo init"
endif
endfunction endfunction
function! cargo#run(args) function! cargo#run(args)
silent! clear call cargo#cmd("run " . a:args)
if !a:args
execute "!" . "cargo run " . a:args
else
execute "!" . "cargo run"
endif
endfunction endfunction
function! cargo#test(args) function! cargo#test(args)
silent! clear call cargo#cmd("test " . a:args)
if !a:args
execute "!" . "cargo test " . a:args
else
execute "!" . "cargo test"
endif
endfunction endfunction
function! cargo#bench(args) function! cargo#bench(args)
silent! clear call cargo#cmd("bench " . a:args)
if !a:args endfunction
execute "!" . "cargo bench " . a:args
else function! cargo#runtarget(args)
execute "!" . "cargo bench" let l:filename = expand('%:p')
let l:read_manifest = system('cargo read-manifest')
let l:metadata = json_decode(l:read_manifest)
let l:targets = get(l:metadata, 'targets', [])
let l:did_run = 0
for l:target in l:targets
let l:src_path = get(l:target, 'src_path', '')
let l:kinds = get(l:target, 'kind', [])
let l:name = get(l:target, 'name', '')
if l:src_path == l:filename
if index(l:kinds, 'example') != -1
let l:did_run = 1
call cargo#run("--example " . shellescape(l:name) . " " . a:args)
return
elseif index(l:kinds, 'bin') != -1
let l:did_run = 1
call cargo#run("--bin " . shellescape(l:name) . " " . a:args)
return
endif
endif
endfor
if l:did_run != 1
call cargo#run(a:args)
return
endif endif
endfunction endfunction
" vim: set et sw=4 sts=4 ts=8:

View file

@ -0,0 +1,26 @@
function! cargo#quickfix#CmdPre() abort
if &filetype ==# 'rust' && get(b:, 'current_compiler', '') ==# 'cargo'
" Preserve the current directory, and 'lcd' to the nearest Cargo file.
let b:rust_compiler_cargo_qf_has_lcd = haslocaldir()
let b:rust_compiler_cargo_qf_prev_cd = getcwd()
let b:rust_compiler_cargo_qf_prev_cd_saved = 1
let l:nearest = fnamemodify(cargo#nearestRootCargo(0), ':h')
execute 'lchdir! '.l:nearest
else
let b:rust_compiler_cargo_qf_prev_cd_saved = 0
endif
endfunction
function! cargo#quickfix#CmdPost() abort
if exists("b:rust_compiler_cargo_qf_prev_cd_saved") && b:rust_compiler_cargo_qf_prev_cd_saved
" Restore the current directory.
if b:rust_compiler_cargo_qf_has_lcd
execute 'lchdir! '.b:rust_compiler_cargo_qf_prev_cd
else
execute 'chdir! '.b:rust_compiler_cargo_qf_prev_cd
endif
let b:rust_compiler_cargo_qf_prev_cd_saved = 0
endif
endfunction
" vim: set et sw=4 sts=4 ts=8:

View file

@ -3,6 +3,58 @@
" Last Modified: May 27, 2014 " Last Modified: May 27, 2014
" For bugs, patches and license go to https://github.com/rust-lang/rust.vim " For bugs, patches and license go to https://github.com/rust-lang/rust.vim
function! rust#Load()
" Utility call to get this script loaded, for debugging
endfunction
function! rust#GetConfigVar(name, default)
" Local buffer variable with same name takes predeence over global
if has_key(b:, a:name)
return get(b:, a:name)
endif
if has_key(g:, a:name)
return get(g:, a:name)
endif
return a:default
endfunction
" Include expression {{{1
function! rust#IncludeExpr(fname) abort
" Remove leading 'crate::' to deal with 2018 edition style 'use'
" statements
let l:fname = substitute(a:fname, '^crate::', '', '')
" Remove trailing colons arising from lines like
"
" use foo::{Bar, Baz};
let l:fname = substitute(l:fname, ':\+$', '', '')
" Replace '::' with '/'
let l:fname = substitute(l:fname, '::', '/', 'g')
" When we have
"
" use foo::bar::baz;
"
" we can't tell whether baz is a module or a function; and we can't tell
" which modules correspond to files.
"
" So we work our way up, trying
"
" foo/bar/baz.rs
" foo/bar.rs
" foo.rs
while l:fname !=# '.'
let l:path = findfile(l:fname)
if !empty(l:path)
return l:fname
endif
let l:fname = fnamemodify(l:fname, ':h')
endwhile
return l:fname
endfunction
" Jump {{{1 " Jump {{{1
function! rust#Jump(mode, function) range function! rust#Jump(mode, function) range
@ -68,7 +120,7 @@ function! s:Run(dict, rustc_args, args)
let pwd = a:dict.istemp ? a:dict.tmpdir : '' let pwd = a:dict.istemp ? a:dict.tmpdir : ''
let output = s:system(pwd, shellescape(rustc) . " " . join(map(rustc_args, 'shellescape(v:val)'))) let output = s:system(pwd, shellescape(rustc) . " " . join(map(rustc_args, 'shellescape(v:val)')))
if output != '' if output !=# ''
echohl WarningMsg echohl WarningMsg
echo output echo output
echohl None echohl None
@ -135,7 +187,7 @@ function! s:Expand(dict, pretty, args)
endfunction endfunction
function! rust#CompleteExpand(lead, line, pos) function! rust#CompleteExpand(lead, line, pos)
if a:line[: a:pos-1] =~ '^RustExpand!\s*\S*$' if a:line[: a:pos-1] =~# '^RustExpand!\s*\S*$'
" first argument and it has a ! " first argument and it has a !
let list = ["normal", "expanded", "typed", "expanded,identified", "flowgraph=", "everybody_loops"] let list = ["normal", "expanded", "typed", "expanded,identified", "flowgraph=", "everybody_loops"]
if !empty(a:lead) if !empty(a:lead)
@ -164,7 +216,7 @@ function! s:Emit(dict, type, args)
let args = [relpath, '--emit', a:type, '-o', output_path] + a:args let args = [relpath, '--emit', a:type, '-o', output_path] + a:args
let pwd = a:dict.istemp ? a:dict.tmpdir : '' let pwd = a:dict.istemp ? a:dict.tmpdir : ''
let output = s:system(pwd, shellescape(rustc) . " " . join(map(args, 'shellescape(v:val)'))) let output = s:system(pwd, shellescape(rustc) . " " . join(map(args, 'shellescape(v:val)')))
if output != '' if output !=# ''
echohl WarningMsg echohl WarningMsg
echo output echo output
echohl None echohl None
@ -174,10 +226,10 @@ function! s:Emit(dict, type, args)
exe 'silent keepalt read' fnameescape(output_path) exe 'silent keepalt read' fnameescape(output_path)
1 1
d d
if a:type == "llvm-ir" if a:type ==# "llvm-ir"
setl filetype=llvm setl filetype=llvm
let extension = 'll' let extension = 'll'
elseif a:type == "asm" elseif a:type ==# "asm"
setl filetype=asm setl filetype=asm
let extension = 's' let extension = 's'
endif endif
@ -243,8 +295,8 @@ function! s:WithPath(func, ...)
let dict.tmpdir_relpath = filename let dict.tmpdir_relpath = filename
let dict.path = dict.tmpdir.'/'.filename let dict.path = dict.tmpdir.'/'.filename
let saved.mod = &mod let saved.mod = &modified
set nomod set nomodified
silent exe 'keepalt write! ' . fnameescape(dict.path) silent exe 'keepalt write! ' . fnameescape(dict.path)
if pathisempty if pathisempty
@ -325,7 +377,7 @@ function! s:ShellTokenize(text)
endif endif
let l:state = 3 let l:state = 3
elseif l:state == 5 " single-quoted elseif l:state == 5 " single-quoted
if l:c == "'" if l:c ==# "'"
let l:state = 1 let l:state = 1
else else
let l:current .= l:c let l:current .= l:c
@ -343,11 +395,20 @@ function! s:RmDir(path)
if empty(a:path) if empty(a:path)
echoerr 'Attempted to delete empty path' echoerr 'Attempted to delete empty path'
return 0 return 0
elseif a:path == '/' || a:path == $HOME elseif a:path ==# '/' || a:path ==# $HOME
let l:path = expand(a:path)
if l:path ==# '/' || l:path ==# $HOME
echoerr 'Attempted to delete protected path: ' . a:path echoerr 'Attempted to delete protected path: ' . a:path
return 0 return 0
endif endif
return system("rm -rf " . shellescape(a:path)) endif
if !isdirectory(a:path)
return 0
endif
" delete() returns 0 when removing file successfully
return delete(a:path, 'rf') == 0
endfunction endfunction
" Executes {cmd} with the cwd set to {pwd}, without changing Vim's cwd. " Executes {cmd} with the cwd set to {pwd}, without changing Vim's cwd.
@ -396,24 +457,101 @@ function! rust#Play(count, line1, line2, ...) abort
call setreg('"', save_regcont, save_regtype) call setreg('"', save_regcont, save_regtype)
endif endif
let body = l:rust_playpen_url."?code=".webapi#http#encodeURI(content) let url = l:rust_playpen_url."?code=".webapi#http#encodeURI(content)
if strlen(body) > 5000 if strlen(url) > 5000
echohl ErrorMsg | echomsg 'Buffer too large, max 5000 encoded characters ('.strlen(body).')' | echohl None echohl ErrorMsg | echomsg 'Buffer too large, max 5000 encoded characters ('.strlen(url).')' | echohl None
return return
endif endif
let payload = "format=simple&url=".webapi#http#encodeURI(body) let payload = "format=simple&url=".webapi#http#encodeURI(url)
let res = webapi#http#post(l:rust_shortener_url.'create.php', payload, {}) let res = webapi#http#post(l:rust_shortener_url.'create.php', payload, {})
if res.status[0] ==# '2'
let url = res.content let url = res.content
if exists('g:rust_clip_command')
call system(g:rust_clip_command, url)
endif endif
redraw | echomsg 'Done: '.url let footer = ''
if exists('g:rust_clip_command')
call system(g:rust_clip_command, url)
if !v:shell_error
let footer = ' (copied to clipboard)'
endif
endif
redraw | echomsg 'Done: '.url.footer
endfunction
" Run a test under the cursor or all tests {{{1
" Finds a test function name under the cursor. Returns empty string when a
" test function is not found.
function! s:SearchTestFunctionNameUnderCursor() abort
let cursor_line = line('.')
" Find #[test] attribute
if search('\m\C#\[test\]', 'bcW') is 0
return ''
endif
" Move to an opening brace of the test function
let test_func_line = search('\m\C^\s*fn\s\+\h\w*\s*(.\+{$', 'eW')
if test_func_line is 0
return ''
endif
" Search the end of test function (closing brace) to ensure that the
" cursor position is within function definition
normal! %
if line('.') < cursor_line
return ''
endif
return matchstr(getline(test_func_line), '\m\C^\s*fn\s\+\zs\h\w*')
endfunction
function! rust#Test(all, options) abort
let manifest = findfile('Cargo.toml', expand('%:p:h') . ';')
if manifest ==# ''
return rust#Run(1, '--test ' . a:options)
endif
if has('terminal')
let cmd = 'terminal '
elseif has('nvim')
let cmd = 'noautocmd new | terminal '
else
let cmd = '!'
let manifest = shellescape(manifest)
endif
if a:all
if a:options ==# ''
execute cmd . 'cargo test --manifest-path' manifest
else
execute cmd . 'cargo test --manifest-path' manifest a:options
endif
return
endif
let saved = getpos('.')
try
let func_name = s:SearchTestFunctionNameUnderCursor()
if func_name ==# ''
echohl ErrorMsg
echomsg 'No test function was found under the cursor. Please add ! to command if you want to run all tests'
echohl None
return
endif
if a:options ==# ''
execute cmd . 'cargo test --manifest-path' manifest func_name
else
execute cmd . 'cargo test --manifest-path' manifest func_name a:options
endif
return
finally
call setpos('.', saved)
endtry
endfunction endfunction
" }}}1 " }}}1
" vim: set noet sw=8 ts=8: " vim: set et sw=4 sts=4 ts=8:

View file

@ -0,0 +1,103 @@
" For debugging, inspired by https://github.com/w0rp/rust/blob/master/autoload/rust/debugging.vim
let s:global_variable_list = [
\ '_rustfmt_autosave_because_of_config',
\ 'ftplugin_rust_source_path',
\ 'loaded_syntastic_rust_cargo_checker',
\ 'loaded_syntastic_rust_filetype',
\ 'loaded_syntastic_rust_rustc_checker',
\ 'rust_bang_comment_leader',
\ 'rust_cargo_avoid_whole_workspace',
\ 'rust_clip_command',
\ 'rust_conceal',
\ 'rust_conceal_mod_path',
\ 'rust_conceal_pub',
\ 'rust_fold',
\ 'rust_last_args',
\ 'rust_last_rustc_args',
\ 'rust_original_delimitMate_excluded_regions',
\ 'rust_playpen_url',
\ 'rust_prev_delimitMate_quotes',
\ 'rust_recent_nearest_cargo_tol',
\ 'rust_recent_root_cargo_toml',
\ 'rust_recommended_style',
\ 'rust_set_conceallevel',
\ 'rust_set_conceallevel=1',
\ 'rust_set_foldmethod',
\ 'rust_set_foldmethod=1',
\ 'rust_shortener_url',
\ 'rustc_makeprg_no_percent',
\ 'rustc_path',
\ 'rustfmt_autosave',
\ 'rustfmt_autosave_if_config_present',
\ 'rustfmt_command',
\ 'rustfmt_emit_files',
\ 'rustfmt_fail_silently',
\ 'rustfmt_options',
\ 'syntastic_extra_filetypes',
\ 'syntastic_rust_cargo_fname',
\]
function! s:Echo(message) abort
execute 'echo a:message'
endfunction
function! s:EchoGlobalVariables() abort
for l:key in s:global_variable_list
if l:key !~# '^_'
call s:Echo('let g:' . l:key . ' = ' . string(get(g:, l:key, v:null)))
endif
if has_key(b:, l:key)
call s:Echo('let b:' . l:key . ' = ' . string(b:[l:key]))
endif
endfor
endfunction
function! rust#debugging#Info() abort
call cargo#Load()
call rust#Load()
call rustfmt#Load()
call s:Echo('rust.vim Global Variables:')
call s:Echo('')
call s:EchoGlobalVariables()
silent let l:output = system(g:rustfmt_command . ' --version')
echo l:output
let l:rustc = exists("g:rustc_path") ? g:rustc_path : "rustc"
silent let l:output = system(l:rustc . ' --version')
echo l:output
silent let l:output = system('cargo --version')
echo l:output
version
if exists(":SyntasticInfo")
echo "----"
echo "Info from Syntastic:"
execute "SyntasticInfo"
endif
endfunction
function! rust#debugging#InfoToClipboard() abort
redir @"
silent call rust#debugging#Info()
redir END
call s:Echo('RustInfo copied to your clipboard')
endfunction
function! rust#debugging#InfoToFile(filename) abort
let l:expanded_filename = expand(a:filename)
redir => l:output
silent call rust#debugging#Info()
redir END
call writefile(split(l:output, "\n"), l:expanded_filename)
call s:Echo('RustInfo written to ' . l:expanded_filename)
endfunction
" vim: set et sw=4 sts=4 ts=8:

View file

@ -0,0 +1,44 @@
let s:delimitMate_extra_excluded_regions = ',rustLifetimeCandidate,rustGenericLifetimeCandidate'
" For this buffer, when delimitMate issues the `User delimitMate_map`
" event in the autocommand system, add the above-defined extra excluded
" regions to delimitMate's state, if they have not already been added.
function! rust#delimitmate#onMap() abort
if &filetype !=# 'rust'
return
endif
if get(b:, "delimitMate_quotes")
let b:rust_prev_delimitMate_quotes = b:delimitMate_quotes
endif
let b:delimitMate_quotes = "\" `"
if match(delimitMate#Get("excluded_regions"),
\ s:delimitMate_extra_excluded_regions) == -1
call delimitMate#Set("excluded_regions",
\delimitMate#Get("excluded_regions").s:delimitMate_extra_excluded_regions)
endif
endfunction
" For this buffer, when delimitMate issues the `User delimitMate_unmap`
" event in the autocommand system, delete the above-defined extra excluded
" regions from delimitMate's state (the deletion being idempotent and
" having no effect if the extra excluded regions are not present in the
" targeted part of delimitMate's state).
function! rust#delimitmate#onUnmap() abort
if &filetype !=# 'rust'
return
endif
if get(b:, "rust_prev_delimitMate_quotes")
let b:delimitMate_quotes = b:rust_prev_delimitMate_quotes
endif
call delimitMate#Set("excluded_regions", substitute(
\ delimitMate#Get("excluded_regions"),
\ '\C\V' . s:delimitMate_extra_excluded_regions,
\ '', 'g'))
endfunction
" vim: set et sw=4 sts=4 ts=8:

View file

@ -0,0 +1,18 @@
" Tagbar support code, for the sake of not automatically overriding its
" configuration in case Universal Ctags is detected.
let s:ctags_is_uctags = 0
let s:checked_ctags = 0
function! rust#tags#IsUCtags() abort
if s:checked_ctags == 0
let l:ctags_bin = get(g:, 'tagbar_ctags_bin', 'ctags')
if system(l:ctags_bin.' --version') =~? 'universal ctags'
let s:ctags_is_uctags = 1
endif
let s:checked_ctags = 1
endif
return s:ctags_is_uctags
endfunction
" vim: set et sw=4 sts=4 ts=8:

View file

@ -19,89 +19,246 @@ if !exists("g:rustfmt_fail_silently")
let g:rustfmt_fail_silently = 0 let g:rustfmt_fail_silently = 0
endif endif
let s:got_fmt_error = 0 function! rustfmt#DetectVersion()
" Save rustfmt '--help' for feature inspection
silent let s:rustfmt_help = system(g:rustfmt_command . " --help")
let s:rustfmt_unstable_features = s:rustfmt_help =~# "--unstable-features"
function! s:RustfmtCommandRange(filename, line1, line2) " Build a comparable rustfmt version varible out of its `--version` output:
let l:arg = {"file": shellescape(a:filename), "range": [a:line1, a:line2]} silent let l:rustfmt_version_full = system(g:rustfmt_command . " --version")
return printf("%s %s --write-mode=overwrite --file-lines '[%s]'", g:rustfmt_command, g:rustfmt_options, json_encode(l:arg)) let l:rustfmt_version_list = matchlist(l:rustfmt_version_full,
endfunction \ '\vrustfmt ([0-9]+[.][0-9]+[.][0-9]+)')
if len(l:rustfmt_version_list) < 3
function! s:RustfmtCommand(filename) let s:rustfmt_version = "0"
return g:rustfmt_command . " --write-mode=overwrite " . g:rustfmt_options . " " . shellescape(a:filename)
endfunction
function! s:RunRustfmt(command, curw, tmpname)
if exists("*systemlist")
let out = systemlist(a:command)
else else
let out = split(system(a:command), '\r\?\n') let s:rustfmt_version = l:rustfmt_version_list[1]
endif
return s:rustfmt_version
endfunction
call rustfmt#DetectVersion()
if !exists("g:rustfmt_emit_files")
let g:rustfmt_emit_files = s:rustfmt_version >= "0.8.2"
endif endif
if v:shell_error == 0 || v:shell_error == 3 if !exists("g:rustfmt_file_lines")
let g:rustfmt_file_lines = s:rustfmt_help =~# "--file-lines JSON"
endif
let s:got_fmt_error = 0
function! rustfmt#Load()
" Utility call to get this script loaded, for debugging
endfunction
function! s:RustfmtWriteMode()
if g:rustfmt_emit_files
return "--emit=files"
else
return "--write-mode=overwrite"
endif
endfunction
function! s:RustfmtConfig()
let l:rustfmt_toml = findfile('rustfmt.toml', expand('%:p:h') . ';')
if l:rustfmt_toml !=# ''
return '--config-path '.l:rustfmt_toml
endif
let l:_rustfmt_toml = findfile('.rustfmt.toml', expand('%:p:h') . ';')
if l:_rustfmt_toml !=# ''
return '--config-path '.l:_rustfmt_toml
endif
return ''
endfunction
function! s:RustfmtCommandRange(filename, line1, line2)
if g:rustfmt_file_lines == 0
echo "--file-lines is not supported in the installed `rustfmt` executable"
return
endif
let l:arg = {"file": shellescape(a:filename), "range": [a:line1, a:line2]}
let l:write_mode = s:RustfmtWriteMode()
let l:rustfmt_config = s:RustfmtConfig()
" FIXME: When --file-lines gets to be stable, add version range checking
" accordingly.
let l:unstable_features = s:rustfmt_unstable_features ? '--unstable-features' : ''
let l:cmd = printf("%s %s %s %s %s --file-lines '[%s]' %s", g:rustfmt_command,
\ l:write_mode, g:rustfmt_options,
\ l:unstable_features, l:rustfmt_config,
\ json_encode(l:arg), shellescape(a:filename))
return l:cmd
endfunction
function! s:RustfmtCommand()
if g:rustfmt_emit_files
let l:write_mode = "--emit=stdout"
else
let l:write_mode = "--write-mode=display"
endif
" rustfmt will pick on the right config on its own due to the
" current directory change.
return g:rustfmt_command . " ". l:write_mode . " " . g:rustfmt_options
endfunction
function! s:DeleteLines(start, end) abort
silent! execute a:start . ',' . a:end . 'delete _'
endfunction
function! s:RunRustfmt(command, tmpname, from_writepre)
mkview!
let l:stderr_tmpname = tempname()
call writefile([], l:stderr_tmpname)
let l:command = a:command . ' 2> ' . l:stderr_tmpname
if a:tmpname ==# ''
" Rustfmt in stdin/stdout mode
" chdir to the directory of the file
let l:has_lcd = haslocaldir()
let l:prev_cd = getcwd()
execute 'lchdir! '.expand('%:h')
let l:buffer = getline(1, '$')
if exists("*systemlist")
silent let out = systemlist(l:command, l:buffer)
else
silent let out = split(system(l:command,
\ join(l:buffer, "\n")), '\r\?\n')
endif
else
if exists("*systemlist")
silent let out = systemlist(l:command)
else
silent let out = split(system(l:command), '\r\?\n')
endif
endif
let l:stderr = readfile(l:stderr_tmpname)
call delete(l:stderr_tmpname)
let l:open_lwindow = 0
if v:shell_error == 0
if a:from_writepre
" remove undo point caused via BufWritePre " remove undo point caused via BufWritePre
try | silent undojoin | catch | endtry try | silent undojoin | catch | endtry
endif
" Replace current file with temp file, then reload buffer if a:tmpname ==# ''
call rename(a:tmpname, expand('%')) let l:content = l:out
silent edit! else
let &syntax = &syntax " take the tmpfile's content, this is better than rename
" because it preserves file modes.
let l:content = readfile(a:tmpname)
endif
call s:DeleteLines(len(l:content), line('$'))
call setline(1, l:content)
" only clear location list if it was previously filled to prevent " only clear location list if it was previously filled to prevent
" clobbering other additions " clobbering other additions
if s:got_fmt_error if s:got_fmt_error
let s:got_fmt_error = 0 let s:got_fmt_error = 0
call setloclist(0, []) call setloclist(0, [])
lwindow let l:open_lwindow = 1
endif endif
elseif g:rustfmt_fail_silently == 0 elseif g:rustfmt_fail_silently == 0 && !a:from_writepre
" otherwise get the errors and put them in the location list " otherwise get the errors and put them in the location list
let errors = [] let l:errors = []
for line in out let l:prev_line = ""
" src/lib.rs:13:5: 13:10 error: expected `,`, or `}`, found `value` for l:line in l:stderr
let tokens = matchlist(line, '^\(.\{-}\):\(\d\+\):\(\d\+\):\s*\(\d\+:\d\+\s*\)\?\s*error: \(.*\)') " error: expected one of `;` or `as`, found `extern`
" --> src/main.rs:2:1
let tokens = matchlist(l:line, '^\s\+-->\s\(.\{-}\):\(\d\+\):\(\d\+\)$')
if !empty(tokens) if !empty(tokens)
call add(errors, {"filename": @%, call add(l:errors, {"filename": @%,
\"lnum": tokens[2], \"lnum": tokens[2],
\"col": tokens[3], \"col": tokens[3],
\"text": tokens[5]}) \"text": l:prev_line})
endif endif
let l:prev_line = l:line
endfor endfor
if empty(errors) if !empty(l:errors)
% | " Couldn't detect rustfmt error format, output errors call setloclist(0, l:errors, 'r')
endif
if !empty(errors)
call setloclist(0, errors, 'r')
echohl Error | echomsg "rustfmt returned error" | echohl None echohl Error | echomsg "rustfmt returned error" | echohl None
else
echo "rust.vim: was not able to parse rustfmt messages. Here is the raw output:"
echo "\n"
for l:line in l:stderr
echo l:line
endfor
endif endif
let s:got_fmt_error = 1 let s:got_fmt_error = 1
lwindow let l:open_lwindow = 1
" We didn't use the temp file, so clean up
call delete(a:tmpname)
endif endif
call winrestview(a:curw) " Restore the current directory if needed
if a:tmpname ==# ''
if l:has_lcd
execute 'lchdir! '.l:prev_cd
else
execute 'chdir! '.l:prev_cd
endif
endif
" Open lwindow after we have changed back to the previous directory
if l:open_lwindow == 1
lwindow
endif
silent! loadview
endfunction endfunction
function! rustfmt#FormatRange(line1, line2) function! rustfmt#FormatRange(line1, line2)
let l:curw = winsaveview() let l:tmpname = tempname()
let l:tmpname = expand("%:p:h") . "/." . expand("%:p:t") . ".rustfmt"
call writefile(getline(1, '$'), l:tmpname) call writefile(getline(1, '$'), l:tmpname)
let command = s:RustfmtCommandRange(l:tmpname, a:line1, a:line2) let command = s:RustfmtCommandRange(l:tmpname, a:line1, a:line2)
call s:RunRustfmt(command, l:tmpname, v:false)
call s:RunRustfmt(command, l:curw, l:tmpname) call delete(l:tmpname)
endfunction endfunction
function! rustfmt#Format() function! rustfmt#Format()
let l:curw = winsaveview() call s:RunRustfmt(s:RustfmtCommand(), '', v:false)
let l:tmpname = expand("%:p:h") . "/." . expand("%:p:t") . ".rustfmt"
call writefile(getline(1, '$'), l:tmpname)
let command = s:RustfmtCommand(l:tmpname)
call s:RunRustfmt(command, l:curw, l:tmpname)
endfunction endfunction
function! rustfmt#Cmd()
" Mainly for debugging
return s:RustfmtCommand()
endfunction
function! rustfmt#PreWrite()
if !filereadable(expand("%@"))
return
endif
if rust#GetConfigVar('rustfmt_autosave_if_config_present', 0)
if findfile('rustfmt.toml', '.;') !=# '' || findfile('.rustfmt.toml', '.;') !=# ''
let b:rustfmt_autosave = 1
let b:_rustfmt_autosave_because_of_config = 1
endif
else
if has_key(b:, '_rustfmt_autosave_because_of_config')
unlet b:_rustfmt_autosave_because_of_config
unlet b:rustfmt_autosave
endif
endif
if !rust#GetConfigVar("rustfmt_autosave", 0)
return
endif
call s:RunRustfmt(s:RustfmtCommand(), '', v:true)
endfunction
" vim: set et sw=4 sts=4 ts=8:

View file

@ -10,8 +10,10 @@ endif
runtime compiler/rustc.vim runtime compiler/rustc.vim
let current_compiler = "cargo" let current_compiler = "cargo"
" vint: -ProhibitAbbreviationOption
let s:save_cpo = &cpo let s:save_cpo = &cpo
set cpo&vim set cpo&vim
" vint: +ProhibitAbbreviationOption
if exists(':CompilerSet') != 2 if exists(':CompilerSet') != 2
command -nargs=* CompilerSet setlocal <args> command -nargs=* CompilerSet setlocal <args>
@ -23,6 +25,12 @@ else
CompilerSet makeprg=cargo\ $* CompilerSet makeprg=cargo\ $*
endif endif
augroup RustCargoQuickFixHooks
autocmd!
autocmd QuickFixCmdPre make call cargo#quickfix#CmdPre()
autocmd QuickFixCmdPost make call cargo#quickfix#CmdPost()
augroup END
" Ignore general cargo progress messages " Ignore general cargo progress messages
CompilerSet errorformat+= CompilerSet errorformat+=
\%-G%\\s%#Downloading%.%#, \%-G%\\s%#Downloading%.%#,
@ -31,7 +39,11 @@ CompilerSet errorformat+=
\%-G%\\s%#error:\ Could\ not\ compile\ %.%#, \%-G%\\s%#error:\ Could\ not\ compile\ %.%#,
\%-G%\\s%#To\ learn\ more\\,%.%#, \%-G%\\s%#To\ learn\ more\\,%.%#,
\%-Gnote:\ Run\ with\ \`RUST_BACKTRACE=%.%#, \%-Gnote:\ Run\ with\ \`RUST_BACKTRACE=%.%#,
\%.%#panicked\ at\ \\'%m\\'\\,\ %f:%l \%.%#panicked\ at\ \\'%m\\'\\,\ %f:%l:%c
" vint: -ProhibitAbbreviationOption
let &cpo = s:save_cpo let &cpo = s:save_cpo
unlet s:save_cpo unlet s:save_cpo
" vint: +ProhibitAbbreviationOption
" vim: set et sw=4 sts=4 ts=8:

View file

@ -9,30 +9,27 @@ if exists("current_compiler")
endif endif
let current_compiler = "rustc" let current_compiler = "rustc"
let s:cpo_save = &cpo " vint: -ProhibitAbbreviationOption
let s:save_cpo = &cpo
set cpo&vim set cpo&vim
" vint: +ProhibitAbbreviationOption
if exists(":CompilerSet") != 2 if exists(":CompilerSet") != 2
command -nargs=* CompilerSet setlocal <args> command -nargs=* CompilerSet setlocal <args>
endif endif
if exists("g:rustc_makeprg_no_percent") && g:rustc_makeprg_no_percent != 0 if get(g:, 'rustc_makeprg_no_percent', 0)
CompilerSet makeprg=rustc CompilerSet makeprg=rustc
else
if has('patch-7.4.191')
CompilerSet makeprg=rustc\ \%:S
else else
CompilerSet makeprg=rustc\ \% CompilerSet makeprg=rustc\ \%
endif endif
endif
" Old errorformat (before nightly 2016/08/10)
CompilerSet errorformat=
\%f:%l:%c:\ %t%*[^:]:\ %m,
\%f:%l:%c:\ %*\\d:%*\\d\ %t%*[^:]:\ %m,
\%-G%f:%l\ %s,
\%-G%*[\ ]^,
\%-G%*[\ ]^%*[~],
\%-G%*[\ ]...
" New errorformat (after nightly 2016/08/10) " New errorformat (after nightly 2016/08/10)
CompilerSet errorformat+= CompilerSet errorformat=
\%-G, \%-G,
\%-Gerror:\ aborting\ %.%#, \%-Gerror:\ aborting\ %.%#,
\%-Gerror:\ Could\ not\ compile\ %.%#, \%-Gerror:\ Could\ not\ compile\ %.%#,
@ -40,7 +37,21 @@ CompilerSet errorformat+=
\%Eerror[E%n]:\ %m, \%Eerror[E%n]:\ %m,
\%Wwarning:\ %m, \%Wwarning:\ %m,
\%Inote:\ %m, \%Inote:\ %m,
\%C\ %#-->\ %f:%l:%c \%C\ %#-->\ %f:%l:%c,
\%E\ \ left:%m,%C\ right:%m\ %f:%l:%c,%Z
let &cpo = s:cpo_save " Old errorformat (before nightly 2016/08/10)
unlet s:cpo_save CompilerSet errorformat+=
\%f:%l:%c:\ %t%*[^:]:\ %m,
\%f:%l:%c:\ %*\\d:%*\\d\ %t%*[^:]:\ %m,
\%-G%f:%l\ %s,
\%-G%*[\ ]^,
\%-G%*[\ ]^%*[~],
\%-G%*[\ ]...
" vint: -ProhibitAbbreviationOption
let &cpo = s:save_cpo
unlet s:save_cpo
" vint: +ProhibitAbbreviationOption
" vim: set et sw=4 sts=4 ts=8:

View file

@ -0,0 +1,11 @@
--langdef=Rust
--langmap=Rust:.rs
--regex-Rust=/^[ \t]*(#\[[^\]]\][ \t]*)*(pub[ \t]+)?(extern[ \t]+)?("[^"]+"[ \t]+)?(unsafe[ \t]+)?fn[ \t]+([a-zA-Z0-9_]+)/\6/f,functions,function definitions/
--regex-Rust=/^[ \t]*(pub[ \t]+)?type[ \t]+([a-zA-Z0-9_]+)/\2/T,types,type definitions/
--regex-Rust=/^[ \t]*(pub[ \t]+)?enum[ \t]+([a-zA-Z0-9_]+)/\2/g,enum,enumeration names/
--regex-Rust=/^[ \t]*(pub[ \t]+)?struct[ \t]+([a-zA-Z0-9_]+)/\2/s,structure names/
--regex-Rust=/^[ \t]*(pub[ \t]+)?mod[ \t]+([a-zA-Z0-9_]+)/\2/m,modules,module names/
--regex-Rust=/^[ \t]*(pub[ \t]+)?(static|const)[ \t]+([a-zA-Z0-9_]+)/\3/c,consts,static constants/
--regex-Rust=/^[ \t]*(pub[ \t]+)?trait[ \t]+([a-zA-Z0-9_]+)/\2/t,traits,traits/
--regex-Rust=/^[ \t]*(pub[ \t]+)?impl([ \t\n]*<[^>]*>)?[ \t]+(([a-zA-Z0-9_:]+)[ \t]*(<[^>]*>)?[ \t]+(for)[ \t]+)?([a-zA-Z0-9_]+)/\4 \6 \7/i,impls,trait implementations/
--regex-Rust=/^[ \t]*macro_rules![ \t]+([a-zA-Z0-9_]+)/\1/d,macros,macro definitions/

View file

@ -12,7 +12,8 @@ CONTENTS *rust*
INTRODUCTION *rust-intro* INTRODUCTION *rust-intro*
This plugin provides syntax and supporting functionality for the Rust This plugin provides syntax and supporting functionality for the Rust
filetype. filetype. It requires Vim 8 or higher for full functionality. Some commands
will not work on earlier versions.
============================================================================== ==============================================================================
SETTINGS *rust-settings* SETTINGS *rust-settings*
@ -20,6 +21,9 @@ SETTINGS *rust-settings*
This plugin has a few variables you can define in your vimrc that change the This plugin has a few variables you can define in your vimrc that change the
behavior of the plugin. behavior of the plugin.
Some variables can be set buffer local (`:b` prefix), and the buffer local
will take precedence over the global `g:` counterpart.
*g:rustc_path* *g:rustc_path*
g:rustc_path~ g:rustc_path~
Set this option to the path to rustc for use in the |:RustRun| and Set this option to the path to rustc for use in the |:RustRun| and
@ -81,6 +85,25 @@ g:rust_bang_comment_leader~
let g:rust_bang_comment_leader = 1 let g:rust_bang_comment_leader = 1
< <
*g:rust_use_custom_ctags_defs*
g:rust_use_custom_ctags_defs~
Set this option to 1 if you have customized ctags definitions for Rust
and do not wish for those included with rust.vim to be used: >
let g:rust_use_custom_ctags_defs = 1
<
NOTE: rust.vim's built-in definitions are only used for the Tagbar Vim
plugin, if you have it installed, AND if Universal Ctags is not
detected. This is because Universal Ctags already has built-in
support for Rust when used with Tagbar.
Also, note that when using ctags other than Universal Ctags, it is not
automatically used when generating |tags| files that Vim can use to
navigate to definitions across different source files. Feel free to
copy `rust.vim/ctags/rust.ctags` into your own `~/.ctags` if you wish
to generate |tags| files.
*g:ftplugin_rust_source_path* *g:ftplugin_rust_source_path*
g:ftplugin_rust_source_path~ g:ftplugin_rust_source_path~
Set this option to a path that should be prepended to 'path' for Rust Set this option to a path that should be prepended to 'path' for Rust
@ -100,6 +123,23 @@ g:rustfmt_autosave~
buffer. If not specified it defaults to 0 : > buffer. If not specified it defaults to 0 : >
let g:rustfmt_autosave = 0 let g:rustfmt_autosave = 0
< <
There is also a buffer-local b:rustfmt_autosave that can be set for
the same purpose, and can override the global setting.
*g:rustfmt_autosave_if_config_present*
g:rustfmt_autosave_if_config_present~
Set this option to 1 to to have *b:rustfmt_autosave* be set automatically
if a `rustfmt.toml` file is present in any parent directly leading to
the file being edited. If not set, default to 0: >
let g:rustfmt_autosave_if_config_present = 0
<
This is useful to have `rustfmt` only execute on save, on projects
that have `rustfmt.toml` configuration.
There is also a buffer-local b:rustfmt_autosave_if_config_present
that can be set for the same purpose, which can overrides the global
setting.
*g:rustfmt_fail_silently* *g:rustfmt_fail_silently*
g:rustfmt_fail_silently~ g:rustfmt_fail_silently~
Set this option to 1 to prevent 'rustfmt' from populating the Set this option to 1 to prevent 'rustfmt' from populating the
@ -113,6 +153,13 @@ g:rustfmt_options~
defaults to '' : > defaults to '' : >
let g:rustfmt_options = '' let g:rustfmt_options = ''
< <
*g:rustfmt_emit_files*
g:rustfmt_emit_files~
If not specified rust.vim tries to detect the right parameter to
pass to rustfmt based on its reported version. Otherwise, it
determines whether to run rustfmt with '--emit=files' (when 1 is
provided) instead of '--write-mode=overwrite'. >
let g:rustfmt_emit_files = 0
*g:rust_playpen_url* *g:rust_playpen_url*
g:rust_playpen_url~ g:rust_playpen_url~
@ -133,9 +180,189 @@ g:rust_clip_command~
let g:rust_clip_command = 'xclip -selection clipboard' let g:rust_clip_command = 'xclip -selection clipboard'
< <
*g:cargo_makeprg_params*
g:cargo_makeprg_params~
Set this option to the string of parameters to pass to cargo. If not
specified it defaults to '$*' : >
let g:cargo_makeprg_params = 'build'
<
Integration with Syntastic *rust-syntastic*
--------------------------
This plugin automatically integrates with the Syntastic checker. There are two
checkers provided: 'rustc', and 'cargo'. The later invokes 'Cargo' in order to
build code, and the former delivers a single edited '.rs' file as a compilation
target directly to the Rust compiler, `rustc`.
Because Cargo is almost exclusively being used for building Rust code these
days, 'cargo' is the default checker. >
let g:syntastic_rust_checkers = ['cargo']
<
If you would like to change it, you can set `g:syntastic_rust_checkers` to a
different value.
*g:rust_cargo_avoid_whole_workspace*
*b:rust_cargo_avoid_whole_workspace*
g:rust_cargo_avoid_whole_workspace~
When editing a crate that is part of a Cargo workspace, and this
option is set to 1 (the default), then 'cargo' will be executed
directly in that crate directory instead of in the workspace
directory. Setting 0 prevents this behavior - however be aware that if
you are working in large workspace, Cargo commands may take more time,
plus the Syntastic error list may include all the crates in the
workspace. >
let g:rust_cargo_avoid_whole_workspace = 0
<
*g:rust_cargo_check_all_targets*
*b:rust_cargo_check_all_targets*
g:rust_cargo_check_all_targets~
When set to 1, the `--all-targets` option will be passed to cargo when
Syntastic executes it, allowing the linting of all targets under the
package.
The default is 0.
*g:rust_cargo_check_all_features*
*b:rust_cargo_check_all_features*
g:rust_cargo_check_all_features~
When set to 1, the `--all-features` option will be passed to cargo when
Syntastic executes it, allowing the linting of all features of the
package.
The default is 0.
*g:rust_cargo_check_examples*
*b:rust_cargo_check_examples*
g:rust_cargo_check_examples~
When set to 1, the `--examples` option will be passed to cargo when
Syntastic executes it, to prevent the exclusion of examples from
linting. The examples are normally under the `examples/` directory of
the crate.
The default is 0.
*g:rust_cargo_check_tests*
*b:rust_cargo_check_tests*
g:rust_cargo_check_tests~
When set to 1, the `--tests` option will be passed to cargo when
Syntastic executes it, to prevent the exclusion of tests from linting.
The tests are normally under the `tests/` directory of the crate.
The default is 0.
*g:rust_cargo_check_benches*
*b:rust_cargo_check_benches*
g:rust_cargo_check_benches~
When set to 1, the `--benches` option will be passed to cargo when
Syntastic executes it. The benches are normally under the `benches/`
directory of the crate.
The default is 0.
Integration with auto-pairs *rust-auto-pairs*
---------------------------
This plugin automatically configures the auto-pairs plugin not to duplicate
single quotes, which are used more often for lifetime annotations than for
single character literals.
*g:rust_keep_autopairs_default*
g:rust_keep_autopairs_default~
Don't override auto-pairs default for the Rust filetype. The default
is 0.
============================================================================== ==============================================================================
COMMANDS *rust-commands* COMMANDS *rust-commands*
Invoking Cargo
--------------
This plug defines very simple shortcuts for invoking Cargo from with Vim.
:Cargo <args> *:Cargo*
Runs 'cargo' with the provided arguments.
:Cbuild <args> *:Cbuild*
Shortcut for 'cargo build`.
:Cclean <args> *:Cclean*
Shortcut for 'cargo clean`.
:Cdoc <args> *:Cdoc*
Shortcut for 'cargo doc`.
:Cinit <args> *:Cinit*
Shortcut for 'cargo init`.
:Crun <args> *:Crun*
Shortcut for 'cargo run`.
:Ctest <args> *:Ctest*
Shortcut for 'cargo test`.
:Cupdate <args> *:Cupdate*
Shortcut for 'cargo update`.
:Cbench <args> *:Cbench*
Shortcut for 'cargo bench`.
:Csearch <args> *:Csearch*
Shortcut for 'cargo search`.
:Cpublish <args> *:Cpublish*
Shortcut for 'cargo publish`.
:Cinstall <args> *:Cinstall*
Shortcut for 'cargo install`.
:Cruntarget <args> *:Cruntarget*
Shortcut for 'cargo run --bin' or 'cargo run --example',
depending on the currently open buffer.
Formatting
----------
:RustFmt *:RustFmt*
Runs |g:rustfmt_command| on the current buffer. If
|g:rustfmt_options| is set then those will be passed to the
executable.
If |g:rustfmt_fail_silently| is 0 (the default) then it
will populate the |location-list| with the errors from
|g:rustfmt_command|. If |g:rustfmt_fail_silently| is set to 1
then it will not populate the |location-list|.
:RustFmtRange *:RustFmtRange*
Runs |g:rustfmt_command| with selected range. See
|:RustFmt| for any other information.
Playpen integration
-------------------
:RustPlay *:RustPlay*
This command will only work if you have web-api.vim installed
(available at https://github.com/mattn/webapi-vim). It sends the
current selection, or if nothing is selected, the entirety of the
current buffer to the Rust playpen, and emits a message with the
shortened URL to the playpen.
|g:rust_playpen_url| is the base URL to the playpen, by default
"https://play.rust-lang.org/".
|g:rust_shortener_url| is the base url for the shorterner, by
default "https://is.gd/"
|g:rust_clip_command| is the command to run to copy the
playpen url to the clipboard of your system.
Evaulation of a single Rust file
--------------------------------
NOTE: These commands are useful only when working with standalone Rust files,
which is usually not the case for common Rust development. If you wish to
building Rust crates from with Vim can should use Vim's make, Syntastic, or
functionality from other plugins.
:RustRun [args] *:RustRun* :RustRun [args] *:RustRun*
:RustRun! [rustc-args] [--] [args] :RustRun! [rustc-args] [--] [args]
Compiles and runs the current file. If it has unsaved changes, Compiles and runs the current file. If it has unsaved changes,
@ -195,52 +422,47 @@ COMMANDS *rust-commands*
If |g:rustc_path| is defined, it is used as the path to rustc. If |g:rustc_path| is defined, it is used as the path to rustc.
Otherwise it is assumed rustc can be found in $PATH. Otherwise it is assumed rustc can be found in $PATH.
:RustPlay *:RustPlay*
This command will only work if you have web-api.vim installed
(available at https://github.com/mattn/webapi-vim). It sends the
current selection, or if nothing is selected, the entirety of the
current buffer to the Rust playpen, and emits a message with the
shortened URL to the playpen.
|g:rust_playpen_url| is the base URL to the playpen, by default Running test(s)
"https://play.rust-lang.org/". ---------------
|g:rust_shortener_url| is the base url for the shorterner, by :RustTest[!] [options] *:RustTest*
default "https://is.gd/" Runs a test under the cursor when the current buffer is in a
cargo project with "cargo test" command. If the command did
not find any test function under the cursor, it stops with an
error message.
|g:rust_clip_command| is the command to run to copy the When ! is given, runs all tests regardless of current cursor
playpen url to the clipboard of your system. position.
:RustFmt *:RustFmt* When [options] is given, it is passed to "cargo" command
Runs |g:rustfmt_command| on the current buffer. If arguments.
|g:rustfmt_options| is set then those will be passed to the
executable.
If |g:rustfmt_fail_silently| is 0 (the default) then it When the current buffer is outside cargo project, the command
will populate the |location-list| with the errors from runs "rustc --test" command instead of "cargo test" as
|g:rustfmt_command|. If |g:rustfmt_fail_silently| is set to 1 fallback. All tests are run regardless of adding ! since there
then it will not populate the |location-list|. is no way to run specific test function with rustc. [options]
is passed to "rustc" command arguments in the case.
:RustFmtRange *:RustFmtRange*
Runs |g:rustfmt_command| with selected range. See rust.vim Debugging
|:RustFmt| for any other information. ------------------
:RustInfo *:RustInfo*
Emits debugging info of the Vim Rust plugin.
:RustInfoToClipboard *:RustInfoClipboard*
Saves debugging info of the Vim Rust plugin to the default
register.
:RustInfoToFile [filename] *:RustInfoToFile*
Saves debugging info of the Vim Rust plugin to the the given
file, overwritting it.
============================================================================== ==============================================================================
MAPPINGS *rust-mappings* MAPPINGS *rust-mappings*
This plugin defines mappings for |[[| and |]]| to support hanging indents. This plugin defines mappings for |[[| and |]]| to support hanging indents.
It also has a few other mappings:
*rust_<D-r>*
<D-r> Executes |:RustRun| with no arguments.
Note: This binding is only available in MacVim.
*rust_<D-R>*
<D-R> Populates the command line with |:RustRun|! using the
arguments given to the last invocation, but does not
execute it.
Note: This binding is only available in MacVim.
============================================================================== ==============================================================================
vim:tw=78:sw=4:noet:ts=8:ft=help:norl: vim:tw=78:sw=4:noet:ts=8:ft=help:norl:

View file

@ -1 +1,15 @@
au BufRead,BufNewFile *.rs set filetype=rust " vint: -ProhibitAutocmdWithNoGroup
autocmd BufRead,BufNewFile *.rs call s:set_rust_filetype()
if has('patch-8.0.613')
autocmd BufRead,BufNewFile Cargo.toml setf FALLBACK cfg
endif
function! s:set_rust_filetype() abort
if &filetype !=# 'rust'
set filetype=rust
endif
endfunction
" vim: set et sw=4 sts=4 ts=8:

View file

@ -10,11 +10,18 @@ if exists("b:did_ftplugin")
endif endif
let b:did_ftplugin = 1 let b:did_ftplugin = 1
" vint: -ProhibitAbbreviationOption
let s:save_cpo = &cpo let s:save_cpo = &cpo
set cpo&vim set cpo&vim
" vint: +ProhibitAbbreviationOption
augroup rust.vim if get(b:, 'current_compiler', '') ==# ''
autocmd! if strlen(findfile('Cargo.toml', '.;')) > 0
compiler cargo
else
compiler rustc
endif
endif
" Variables {{{1 " Variables {{{1
@ -22,13 +29,13 @@ autocmd!
" comments, so we'll use that as our default, but make it easy to switch. " comments, so we'll use that as our default, but make it easy to switch.
" This does not affect indentation at all (I tested it with and without " This does not affect indentation at all (I tested it with and without
" leader), merely whether a leader is inserted by default or not. " leader), merely whether a leader is inserted by default or not.
if exists("g:rust_bang_comment_leader") && g:rust_bang_comment_leader != 0 if get(g:, 'rust_bang_comment_leader', 0)
" Why is the `,s0:/*,mb:\ ,ex:*/` there, you ask? I don't understand why, " Why is the `,s0:/*,mb:\ ,ex:*/` there, you ask? I don't understand why,
" but without it, */ gets indented one space even if there were no " but without it, */ gets indented one space even if there were no
" leaders. I'm fairly sure that's a Vim bug. " leaders. I'm fairly sure that's a Vim bug.
setlocal comments=s1:/*,mb:*,ex:*/,s0:/*,mb:\ ,ex:*/,:///,://!,:// setlocal comments=s1:/*,mb:*,ex:*/,s0:/*,mb:\ ,ex:*/,:///,://!,://
else else
setlocal comments=s0:/*!,m:\ ,ex:*/,s1:/*,mb:*,ex:*/,:///,://!,:// setlocal comments=s0:/*!,ex:*/,s1:/*,mb:*,ex:*/,:///,://!,://
endif endif
setlocal commentstring=//%s setlocal commentstring=//%s
setlocal formatoptions-=t formatoptions+=croqnl setlocal formatoptions-=t formatoptions+=croqnl
@ -39,13 +46,14 @@ silent! setlocal formatoptions+=j
" otherwise it's better than nothing. " otherwise it's better than nothing.
setlocal smartindent nocindent setlocal smartindent nocindent
if !exists("g:rust_recommended_style") || g:rust_recommended_style != 0 if get(g:, 'rust_recommended_style', 1)
setlocal tabstop=4 shiftwidth=4 softtabstop=4 expandtab let b:rust_set_style = 1
setlocal tabstop=8 shiftwidth=4 softtabstop=4 expandtab
setlocal textwidth=99 setlocal textwidth=99
endif endif
" This includeexpr isn't perfect, but it's a good start setlocal include=\\v^\\s*(pub\\s+)?use\\s+\\zs(\\f\|:)+
setlocal includeexpr=substitute(v:fname,'::','/','g') setlocal includeexpr=rust#IncludeExpr(v:fname)
setlocal suffixesadd=.rs setlocal suffixesadd=.rs
@ -58,35 +66,20 @@ if exists("g:loaded_delimitMate")
let b:rust_original_delimitMate_excluded_regions = b:delimitMate_excluded_regions let b:rust_original_delimitMate_excluded_regions = b:delimitMate_excluded_regions
endif endif
let s:delimitMate_extra_excluded_regions = ',rustLifetimeCandidate,rustGenericLifetimeCandidate' augroup rust.vim.DelimitMate
autocmd!
" For this buffer, when delimitMate issues the `User delimitMate_map` autocmd User delimitMate_map :call rust#delimitmate#onMap()
" event in the autocommand system, add the above-defined extra excluded autocmd User delimitMate_unmap :call rust#delimitmate#onUnmap()
" regions to delimitMate's state, if they have not already been added. augroup END
autocmd User <buffer>
\ if expand('<afile>') ==# 'delimitMate_map' && match(
\ delimitMate#Get("excluded_regions"),
\ s:delimitMate_extra_excluded_regions) == -1
\| let b:delimitMate_excluded_regions =
\ delimitMate#Get("excluded_regions")
\ . s:delimitMate_extra_excluded_regions
\|endif
" For this buffer, when delimitMate issues the `User delimitMate_unmap`
" event in the autocommand system, delete the above-defined extra excluded
" regions from delimitMate's state (the deletion being idempotent and
" having no effect if the extra excluded regions are not present in the
" targeted part of delimitMate's state).
autocmd User <buffer>
\ if expand('<afile>') ==# 'delimitMate_unmap'
\| let b:delimitMate_excluded_regions = substitute(
\ delimitMate#Get("excluded_regions"),
\ '\C\V' . s:delimitMate_extra_excluded_regions,
\ '', 'g')
\|endif
endif endif
if has("folding") && exists('g:rust_fold') && g:rust_fold != 0 " Integration with auto-pairs (https://github.com/jiangmiao/auto-pairs)
if exists("g:AutoPairsLoaded") && !get(g:, 'rust_keep_autopairs_default', 0)
let b:AutoPairs = {'(':')', '[':']', '{':'}','"':'"', '`':'`'}
endif
if has("folding") && get(g:, 'rust_fold', 0)
let b:rust_set_foldmethod=1 let b:rust_set_foldmethod=1
setlocal foldmethod=syntax setlocal foldmethod=syntax
if g:rust_fold == 2 if g:rust_fold == 2
@ -96,7 +89,7 @@ if has("folding") && exists('g:rust_fold') && g:rust_fold != 0
endif endif
endif endif
if has('conceal') && exists('g:rust_conceal') && g:rust_conceal != 0 if has('conceal') && get(g:, 'rust_conceal', 0)
let b:rust_set_conceallevel=1 let b:rust_set_conceallevel=1
setlocal conceallevel=2 setlocal conceallevel=2
endif endif
@ -134,12 +127,17 @@ command! -buffer RustFmt call rustfmt#Format()
" See |:RustFmtRange| for docs " See |:RustFmtRange| for docs
command! -range -buffer RustFmtRange call rustfmt#FormatRange(<line1>, <line2>) command! -range -buffer RustFmtRange call rustfmt#FormatRange(<line1>, <line2>)
" Mappings {{{1 " See |:RustInfo| for docs
command! -bar RustInfo call rust#debugging#Info()
" Bind ⌘R in MacVim to :RustRun " See |:RustInfoToClipboard| for docs
nnoremap <silent> <buffer> <D-r> :RustRun<CR> command! -bar RustInfoToClipboard call rust#debugging#InfoToClipboard()
" Bind ⌘⇧R in MacVim to :RustRun! pre-filled with the last args
nnoremap <buffer> <D-R> :RustRun! <C-r>=join(b:rust_last_rustc_args)<CR><C-\>erust#AppendCmdLine(' -- ' . join(b:rust_last_args))<CR> " See |:RustInfoToFile| for docs
command! -bar -nargs=1 RustInfoToFile call rust#debugging#InfoToFile(<f-args>)
" See |:RustTest| for docs
command! -buffer -nargs=* -bang RustTest call rust#Test(<bang>0, <q-args>)
if !exists("b:rust_last_rustc_args") || !exists("b:rust_last_args") if !exists("b:rust_last_rustc_args") || !exists("b:rust_last_args")
let b:rust_last_rustc_args = [] let b:rust_last_rustc_args = []
@ -149,8 +147,10 @@ endif
" Cleanup {{{1 " Cleanup {{{1
let b:undo_ftplugin = " let b:undo_ftplugin = "
\ setlocal formatoptions< comments< commentstring< includeexpr< suffixesadd< \ setlocal formatoptions< comments< commentstring< include< includeexpr< suffixesadd<
\|if exists('b:rust_set_style')
\|setlocal tabstop< shiftwidth< softtabstop< expandtab< textwidth< \|setlocal tabstop< shiftwidth< softtabstop< expandtab< textwidth<
\|endif
\|if exists('b:rust_original_delimitMate_excluded_regions') \|if exists('b:rust_original_delimitMate_excluded_regions')
\|let b:delimitMate_excluded_regions = b:rust_original_delimitMate_excluded_regions \|let b:delimitMate_excluded_regions = b:rust_original_delimitMate_excluded_regions
\|unlet b:rust_original_delimitMate_excluded_regions \|unlet b:rust_original_delimitMate_excluded_regions
@ -171,32 +171,31 @@ let b:undo_ftplugin = "
\|delcommand RustEmitIr \|delcommand RustEmitIr
\|delcommand RustEmitAsm \|delcommand RustEmitAsm
\|delcommand RustPlay \|delcommand RustPlay
\|nunmap <buffer> <D-r>
\|nunmap <buffer> <D-R>
\|nunmap <buffer> [[ \|nunmap <buffer> [[
\|nunmap <buffer> ]] \|nunmap <buffer> ]]
\|xunmap <buffer> [[ \|xunmap <buffer> [[
\|xunmap <buffer> ]] \|xunmap <buffer> ]]
\|ounmap <buffer> [[ \|ounmap <buffer> [[
\|ounmap <buffer> ]] \|ounmap <buffer> ]]
\|set matchpairs-=<:> \|setlocal matchpairs-=<:>
\|unlet b:match_skip \|unlet b:match_skip
\" \"
" }}}1 " }}}1
" Code formatting on save " Code formatting on save
if get(g:, "rustfmt_autosave", 0) augroup rust.vim.PreWrite
autocmd BufWritePre *.rs silent! call rustfmt#Format() autocmd!
endif autocmd BufWritePre *.rs silent! call rustfmt#PreWrite()
augroup END augroup END
set matchpairs+=<:> setlocal matchpairs+=<:>
" For matchit.vim (rustArrow stops `Fn() -> X` messing things up) " For matchit.vim (rustArrow stops `Fn() -> X` messing things up)
let b:match_skip = 's:comment\|string\|rustArrow' let b:match_skip = 's:comment\|string\|rustCharacter\|rustArrow'
" vint: -ProhibitAbbreviationOption
let &cpo = s:save_cpo let &cpo = s:save_cpo
unlet s:save_cpo unlet s:save_cpo
" vint: +ProhibitAbbreviationOption
" vim: set noet sw=8 ts=8: " vim: set et sw=4 sts=4 ts=8:

View file

@ -0,0 +1,40 @@
"
" Support for Tagbar -- https://github.com/majutsushi/tagbar
"
if !exists(':Tagbar') || rust#tags#IsUCtags()
finish
endif
" vint: -ProhibitAbbreviationOption
let s:save_cpo = &cpo
set cpo&vim
" vint: +ProhibitAbbreviationOption
if !exists('g:tagbar_type_rust')
let g:tagbar_type_rust = {
\ 'ctagstype' : 'rust',
\ 'kinds' : [
\'T:types',
\'f:functions',
\'g:enumerations',
\'s:structures',
\'m:modules',
\'c:constants',
\'t:traits',
\'i:trait implementations',
\ ]
\ }
endif
" In case you've updated/customized your ~/.ctags and prefer to use it.
if !get(g:, 'rust_use_custom_ctags_defs', 0)
let g:tagbar_type_rust.deffile = expand('<sfile>:p:h:h:h') . '/ctags/rust.ctags'
endif
" vint: -ProhibitAbbreviationOption
let &cpo = s:save_cpo
unlet s:save_cpo
" vint: +ProhibitAbbreviationOption
" vim: set et sw=4 sts=4 ts=8:

View file

@ -1,7 +1,7 @@
" Vim indent file " Vim indent file
" Language: Rust " Language: Rust
" Author: Chris Morgan <me@chrismorgan.info> " Author: Chris Morgan <me@chrismorgan.info>
" Last Change: 2017 Mar 21 " Last Change: 2018 Jan 10
" For bugs, patches and license go to https://github.com/rust-lang/rust.vim " For bugs, patches and license go to https://github.com/rust-lang/rust.vim
" Only load this indent file when no other was loaded. " Only load this indent file when no other was loaded.
@ -11,8 +11,8 @@ endif
let b:did_indent = 1 let b:did_indent = 1
setlocal cindent setlocal cindent
setlocal cinoptions=L0,(0,Ws,J1,j1 setlocal cinoptions=L0,(s,Ws,J1,j1,m1
setlocal cinkeys=0{,0},!^F,o,O,0[,0] setlocal cinkeys=0{,0},!^F,o,O,0[,0],0(,0)
" Don't think cinwords will actually do anything at all... never mind " Don't think cinwords will actually do anything at all... never mind
setlocal cinwords=for,if,else,while,loop,impl,mod,unsafe,trait,struct,enum,fn,extern,macro setlocal cinwords=for,if,else,while,loop,impl,mod,unsafe,trait,struct,enum,fn,extern,macro
@ -20,7 +20,7 @@ setlocal cinwords=for,if,else,while,loop,impl,mod,unsafe,trait,struct,enum,fn,ex
setlocal nolisp " Make sure lisp indenting doesn't supersede us setlocal nolisp " Make sure lisp indenting doesn't supersede us
setlocal autoindent " indentexpr isn't much help otherwise setlocal autoindent " indentexpr isn't much help otherwise
" Also do indentkeys, otherwise # gets shoved to column 0 :-/ " Also do indentkeys, otherwise # gets shoved to column 0 :-/
setlocal indentkeys=0{,0},!^F,o,O,0[,0] setlocal indentkeys=0{,0},!^F,o,O,0[,0],0(,0)
setlocal indentexpr=GetRustIndent(v:lnum) setlocal indentexpr=GetRustIndent(v:lnum)
@ -29,8 +29,10 @@ if exists("*GetRustIndent")
finish finish
endif endif
" vint: -ProhibitAbbreviationOption
let s:save_cpo = &cpo let s:save_cpo = &cpo
set cpo&vim set cpo&vim
" vint: +ProhibitAbbreviationOption
" Come here when loading the script the first time. " Come here when loading the script the first time.
@ -44,12 +46,12 @@ function! s:get_line_trimmed(lnum)
" If the last character in the line is a comment, do a binary search for " If the last character in the line is a comment, do a binary search for
" the start of the comment. synID() is slow, a linear search would take " the start of the comment. synID() is slow, a linear search would take
" too long on a long line. " too long on a long line.
if synIDattr(synID(a:lnum, line_len, 1), "name") =~ 'Comment\|Todo' if synIDattr(synID(a:lnum, line_len, 1), "name") =~? 'Comment\|Todo'
let min = 1 let min = 1
let max = line_len let max = line_len
while min < max while min < max
let col = (min + max) / 2 let col = (min + max) / 2
if synIDattr(synID(a:lnum, col, 1), "name") =~ 'Comment\|Todo' if synIDattr(synID(a:lnum, col, 1), "name") =~? 'Comment\|Todo'
let max = col let max = col
else else
let min = col + 1 let min = col + 1
@ -69,7 +71,7 @@ function! s:is_string_comment(lnum, col)
if has('syntax_items') if has('syntax_items')
for id in synstack(a:lnum, a:col) for id in synstack(a:lnum, a:col)
let synname = synIDattr(id, "name") let synname = synIDattr(id, "name")
if synname == "rustString" || synname =~ "^rustComment" if synname ==# "rustString" || synname =~# "^rustComment"
return 1 return 1
endif endif
endfor endfor
@ -79,8 +81,17 @@ function! s:is_string_comment(lnum, col)
endif endif
endfunction endfunction
function GetRustIndent(lnum) if exists('*shiftwidth')
function! s:shiftwidth()
return shiftwidth()
endfunc
else
function! s:shiftwidth()
return &shiftwidth
endfunc
endif
function GetRustIndent(lnum)
" Starting assumption: cindent (called at the end) will do it right " Starting assumption: cindent (called at the end) will do it right
" normally. We just want to fix up a few cases. " normally. We just want to fix up a few cases.
@ -88,13 +99,13 @@ function GetRustIndent(lnum)
if has('syntax_items') if has('syntax_items')
let synname = synIDattr(synID(a:lnum, 1, 1), "name") let synname = synIDattr(synID(a:lnum, 1, 1), "name")
if synname == "rustString" if synname ==# "rustString"
" If the start of the line is in a string, don't change the indent " If the start of the line is in a string, don't change the indent
return -1 return -1
elseif synname =~ '\(Comment\|Todo\)' elseif synname =~? '\(Comment\|Todo\)'
\ && line !~ '^\s*/\*' " not /* opening line \ && line !~# '^\s*/\*' " not /* opening line
if synname =~ "CommentML" " multi-line if synname =~? "CommentML" " multi-line
if line !~ '^\s*\*' && getline(a:lnum - 1) =~ '^\s*/\*' if line !~# '^\s*\*' && getline(a:lnum - 1) =~# '^\s*/\*'
" This is (hopefully) the line after a /*, and it has no " This is (hopefully) the line after a /*, and it has no
" leader, so the correct indentation is that of the " leader, so the correct indentation is that of the
" previous line. " previous line.
@ -121,22 +132,73 @@ function GetRustIndent(lnum)
" Search backwards for the previous non-empty line. " Search backwards for the previous non-empty line.
let prevlinenum = prevnonblank(a:lnum - 1) let prevlinenum = prevnonblank(a:lnum - 1)
let prevline = s:get_line_trimmed(prevlinenum) let prevline = s:get_line_trimmed(prevlinenum)
while prevlinenum > 1 && prevline !~ '[^[:blank:]]' while prevlinenum > 1 && prevline !~# '[^[:blank:]]'
let prevlinenum = prevnonblank(prevlinenum - 1) let prevlinenum = prevnonblank(prevlinenum - 1)
let prevline = s:get_line_trimmed(prevlinenum) let prevline = s:get_line_trimmed(prevlinenum)
endwhile endwhile
" A standalone '{', '}', or 'where'
let l:standalone_open = line =~# '\V\^\s\*{\s\*\$'
let l:standalone_close = line =~# '\V\^\s\*}\s\*\$'
let l:standalone_where = line =~# '\V\^\s\*where\s\*\$'
if l:standalone_open || l:standalone_close || l:standalone_where
" ToDo: we can search for more items than 'fn' and 'if'.
let [l:found_line, l:col, l:submatch] =
\ searchpos('\<\(fn\)\|\(if\)\>', 'bnWp')
if l:found_line !=# 0
" Now we count the number of '{' and '}' in between the match
" locations and the current line (there is probably a better
" way to compute this).
let l:i = l:found_line
let l:search_line = strpart(getline(l:i), l:col - 1)
let l:opens = 0
let l:closes = 0
while l:i < a:lnum
let l:search_line2 = substitute(l:search_line, '\V{', '', 'g')
let l:opens += strlen(l:search_line) - strlen(l:search_line2)
let l:search_line3 = substitute(l:search_line2, '\V}', '', 'g')
let l:closes += strlen(l:search_line2) - strlen(l:search_line3)
let l:i += 1
let l:search_line = getline(l:i)
endwhile
if l:standalone_open || l:standalone_where
if l:opens ==# l:closes
return indent(l:found_line)
endif
else
" Expect to find just one more close than an open
if l:opens ==# l:closes + 1
return indent(l:found_line)
endif
endif
endif
endif
" A standalone 'where' adds a shift.
let l:standalone_prevline_where = prevline =~# '\V\^\s\*where\s\*\$'
if l:standalone_prevline_where
return indent(prevlinenum) + 4
endif
" Handle where clauses nicely: subsequent values should line up nicely. " Handle where clauses nicely: subsequent values should line up nicely.
if prevline[len(prevline) - 1] == "," if prevline[len(prevline) - 1] ==# ","
\ && prevline =~# '^\s*where\s' \ && prevline =~# '^\s*where\s'
return indent(prevlinenum) + 6 return indent(prevlinenum) + 6
endif endif
if prevline[len(prevline) - 1] == "," let l:last_prevline_character = prevline[len(prevline) - 1]
\ && s:get_line_trimmed(a:lnum) !~ '^\s*[\[\]{}]'
\ && prevline !~ '^\s*fn\s' " A line that ends with '.<expr>;' is probably an end of a long list
\ && prevline !~ '([^()]\+,$' " of method operations.
\ && s:get_line_trimmed(a:lnum) !~ '^\s*\S\+\s*=>' if prevline =~# '\V\^\s\*.' && l:last_prevline_character ==# ';'
return indent(prevlinenum) - s:shiftwidth()
endif
if l:last_prevline_character ==# ","
\ && s:get_line_trimmed(a:lnum) !~# '^\s*[\[\]{})]'
\ && prevline !~# '^\s*fn\s'
\ && prevline !~# '([^()]\+,$'
\ && s:get_line_trimmed(a:lnum) !~# '^\s*\S\+\s*=>'
" Oh ho! The previous line ended in a comma! I bet cindent will try to " Oh ho! The previous line ended in a comma! I bet cindent will try to
" take this too far... For now, let's normally use the previous line's " take this too far... For now, let's normally use the previous line's
" indent. " indent.
@ -195,7 +257,7 @@ function GetRustIndent(lnum)
else else
" At the module scope, inside square brackets only " At the module scope, inside square brackets only
"if getline(a:lnum)[0] == ']' || search('\[', '', '\]', 'nW') == a:lnum "if getline(a:lnum)[0] == ']' || search('\[', '', '\]', 'nW') == a:lnum
if line =~ "^\\s*]" if line =~# "^\\s*]"
" It's the closing line, dedent it " It's the closing line, dedent it
return 0 return 0
else else
@ -209,5 +271,9 @@ function GetRustIndent(lnum)
return cindent(a:lnum) return cindent(a:lnum)
endfunction endfunction
" vint: -ProhibitAbbreviationOption
let &cpo = s:save_cpo let &cpo = s:save_cpo
unlet s:save_cpo unlet s:save_cpo
" vint: +ProhibitAbbreviationOption
" vim: set et sw=4 sts=4 ts=8:

View file

@ -0,0 +1,26 @@
if exists('g:loaded_rust_vim_plugin_cargo')
finish
endif
let g:loaded_rust_vim_plugin_cargo = 1
let s:save_cpo = &cpoptions
set cpoptions&vim
command! -nargs=+ Cargo call cargo#cmd(<q-args>)
command! -nargs=* Cbuild call cargo#build(<q-args>)
command! -nargs=* Cclean call cargo#clean(<q-args>)
command! -nargs=* Cdoc call cargo#doc(<q-args>)
command! -nargs=+ Cnew call cargo#new(<q-args>)
command! -nargs=* Cinit call cargo#init(<q-args>)
command! -nargs=* Crun call cargo#run(<q-args>)
command! -nargs=* Ctest call cargo#test(<q-args>)
command! -nargs=* Cbench call cargo#bench(<q-args>)
command! -nargs=* Cupdate call cargo#update(<q-args>)
command! -nargs=* Csearch call cargo#search(<q-args>)
command! -nargs=* Cpublish call cargo#publish(<q-args>)
command! -nargs=* Cinstall call cargo#install(<q-args>)
command! -nargs=* Cruntarget call cargo#runtarget(<q-args>)
let &cpoptions = s:save_cpo
unlet s:save_cpo
" vim: set et sw=4 sts=4 ts=8:

View file

@ -2,12 +2,12 @@
" Language: Rust " Language: Rust
" Maintainer: Andrew Gallant <jamslam@gmail.com> " Maintainer: Andrew Gallant <jamslam@gmail.com>
if exists("g:loaded_syntastic_rust_filetype") if exists('g:loaded_rust_vim')
finish finish
endif endif
let g:loaded_syntastic_rust_filetype = 1 let g:loaded_rust_vim = 1
let s:save_cpo = &cpo let s:save_cpo = &cpoptions
set cpo&vim set cpoptions&vim
" This is to let Syntastic know about the Rust filetype. " This is to let Syntastic know about the Rust filetype.
" It enables tab completion for the 'SyntasticInfo' command. " It enables tab completion for the 'SyntasticInfo' command.
@ -18,19 +18,11 @@ else
let g:syntastic_extra_filetypes = ['rust'] let g:syntastic_extra_filetypes = ['rust']
endif endif
let &cpo = s:save_cpo if !exists('g:syntastic_rust_checkers')
let g:syntastic_rust_checkers = ['cargo']
endif
let &cpoptions = s:save_cpo
unlet s:save_cpo unlet s:save_cpo
command! -nargs=* Cargo call cargo#cmd(<q-args>) " vim: set et sw=4 sts=4 ts=8:
command! -nargs=* Cbuild call cargo#build(<q-args>)
command! -nargs=* Cclean call cargo#clean(<q-args>)
command! -nargs=* Cdoc call cargo#doc(<q-args>)
command! -nargs=* Cnew call cargo#new(<q-args>)
command! -nargs=* Cinit call cargo#init(<q-args>)
command! -nargs=* Crun call cargo#run(<q-args>)
command! -nargs=* Ctest call cargo#test(<q-args>)
command! -nargs=* Cbench call cargo#bench(<q-args>)
command! -nargs=* Cupdate call cargo#update(<q-args>)
command! -nargs=* Csearch call cargo#search(<q-args>)
command! -nargs=* Cpublish call cargo#publish(<q-args>)
command! -nargs=* Cinstall call cargo#install(<q-args>)

View file

@ -15,21 +15,31 @@ endif
" Syntax definitions {{{1 " Syntax definitions {{{1
" Basic keywords {{{2 " Basic keywords {{{2
syn keyword rustConditional match if else syn keyword rustConditional match if else
syn keyword rustRepeat for loop while syn keyword rustRepeat loop while
" `:syn match` must be used to prioritize highlighting `for` keyword.
syn match rustRepeat /\<for\>/
" Highlight `for` keyword in `impl ... for ... {}` statement. This line must
" be put after previous `syn match` line to overwrite it.
syn match rustKeyword /\%(\<impl\>.\+\)\@<=\<for\>/
syn keyword rustRepeat in
syn keyword rustTypedef type nextgroup=rustIdentifier skipwhite skipempty syn keyword rustTypedef type nextgroup=rustIdentifier skipwhite skipempty
syn keyword rustStructure struct enum nextgroup=rustIdentifier skipwhite skipempty syn keyword rustStructure struct enum nextgroup=rustIdentifier skipwhite skipempty
syn keyword rustUnion union nextgroup=rustIdentifier skipwhite skipempty contained syn keyword rustUnion union nextgroup=rustIdentifier skipwhite skipempty contained
syn match rustUnionContextual /\<union\_s\+\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*/ transparent contains=rustUnion syn match rustUnionContextual /\<union\_s\+\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*/ transparent contains=rustUnion
syn keyword rustOperator as syn keyword rustOperator as
syn keyword rustExistential existential nextgroup=rustTypedef skipwhite skipempty contained
syn match rustExistentialContextual /\<existential\_s\+type/ transparent contains=rustExistential,rustTypedef
syn match rustAssert "\<assert\(\w\)*!" contained syn match rustAssert "\<assert\(\w\)*!" contained
syn match rustPanic "\<panic\(\w\)*!" contained syn match rustPanic "\<panic\(\w\)*!" contained
syn match rustAsync "\<async\%(\s\|\n\)\@="
syn keyword rustKeyword break syn keyword rustKeyword break
syn keyword rustKeyword box nextgroup=rustBoxPlacement skipwhite skipempty syn keyword rustKeyword box
syn keyword rustKeyword continue syn keyword rustKeyword continue
syn keyword rustKeyword crate
syn keyword rustKeyword extern nextgroup=rustExternCrate,rustObsoleteExternMod skipwhite skipempty syn keyword rustKeyword extern nextgroup=rustExternCrate,rustObsoleteExternMod skipwhite skipempty
syn keyword rustKeyword fn nextgroup=rustFuncName skipwhite skipempty syn keyword rustKeyword fn nextgroup=rustFuncName skipwhite skipempty
syn keyword rustKeyword in impl let syn keyword rustKeyword impl let
syn keyword rustKeyword macro syn keyword rustKeyword macro
syn keyword rustKeyword pub nextgroup=rustPubScope skipwhite skipempty syn keyword rustKeyword pub nextgroup=rustPubScope skipwhite skipempty
syn keyword rustKeyword return syn keyword rustKeyword return
@ -42,8 +52,7 @@ syn keyword rustKeyword use nextgroup=rustModPath skipwhite skipempty
syn keyword rustKeyword mod trait nextgroup=rustIdentifier skipwhite skipempty syn keyword rustKeyword mod trait nextgroup=rustIdentifier skipwhite skipempty
syn keyword rustStorage move mut ref static const syn keyword rustStorage move mut ref static const
syn match rustDefault /\<default\ze\_s\+\(impl\|fn\|type\|const\)\>/ syn match rustDefault /\<default\ze\_s\+\(impl\|fn\|type\|const\)\>/
syn keyword rustAwait await
syn keyword rustInvalidBareKeyword crate
syn keyword rustPubScopeCrate crate contained syn keyword rustPubScopeCrate crate contained
syn match rustPubScopeDelim /[()]/ contained syn match rustPubScopeDelim /[()]/ contained
@ -57,20 +66,12 @@ syn keyword rustObsoleteExternMod mod contained nextgroup=rustIdentifier skipw
syn match rustIdentifier contains=rustIdentifierPrime "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained syn match rustIdentifier contains=rustIdentifierPrime "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained
syn match rustFuncName "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained syn match rustFuncName "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained
syn region rustBoxPlacement matchgroup=rustBoxPlacementParens start="(" end=")" contains=TOP contained
" Ideally we'd have syntax rules set up to match arbitrary expressions. Since
" we don't, we'll just define temporary contained rules to handle balancing
" delimiters.
syn region rustBoxPlacementBalance start="(" end=")" containedin=rustBoxPlacement transparent
syn region rustBoxPlacementBalance start="\[" end="\]" containedin=rustBoxPlacement transparent
" {} are handled by rustFoldBraces
syn region rustMacroRepeat matchgroup=rustMacroRepeatDelimiters start="$(" end=")" contains=TOP nextgroup=rustMacroRepeatCount syn region rustMacroRepeat matchgroup=rustMacroRepeatDelimiters start="$(" end=")" contains=TOP nextgroup=rustMacroRepeatCount
syn match rustMacroRepeatCount ".\?[*+]" contained syn match rustMacroRepeatCount ".\?[*+]" contained
syn match rustMacroVariable "$\w\+" syn match rustMacroVariable "$\w\+"
" Reserved (but not yet used) keywords {{{2 " Reserved (but not yet used) keywords {{{2
syn keyword rustReservedKeyword alignof become do offsetof priv pure sizeof typeof unsized abstract virtual final override syn keyword rustReservedKeyword become do priv typeof unsized abstract virtual final override
" Built-in types {{{2 " Built-in types {{{2
syn keyword rustType isize usize char bool u8 u16 u32 u64 u128 f32 syn keyword rustType isize usize char bool u8 u16 u32 u64 u128 f32
@ -143,16 +144,35 @@ syn match rustEscapeError display contained /\\./
syn match rustEscape display contained /\\\([nrt0\\'"]\|x\x\{2}\)/ syn match rustEscape display contained /\\\([nrt0\\'"]\|x\x\{2}\)/
syn match rustEscapeUnicode display contained /\\u{\%(\x_*\)\{1,6}}/ syn match rustEscapeUnicode display contained /\\u{\%(\x_*\)\{1,6}}/
syn match rustStringContinuation display contained /\\\n\s*/ syn match rustStringContinuation display contained /\\\n\s*/
syn region rustString start=+b"+ skip=+\\\\\|\\"+ end=+"+ contains=rustEscape,rustEscapeError,rustStringContinuation syn region rustString matchgroup=rustStringDelimiter start=+b"+ skip=+\\\\\|\\"+ end=+"+ contains=rustEscape,rustEscapeError,rustStringContinuation
syn region rustString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=rustEscape,rustEscapeUnicode,rustEscapeError,rustStringContinuation,@Spell syn region rustString matchgroup=rustStringDelimiter start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=rustEscape,rustEscapeUnicode,rustEscapeError,rustStringContinuation,@Spell
syn region rustString start='b\?r\z(#*\)"' end='"\z1' contains=@Spell syn region rustString matchgroup=rustStringDelimiter start='b\?r\z(#*\)"' end='"\z1' contains=@Spell
syn region rustAttribute start="#!\?\[" end="\]" contains=rustString,rustDerive,rustCommentLine,rustCommentBlock,rustCommentLineDocError,rustCommentBlockDocError " Match attributes with either arbitrary syntax or special highlighting for
" derives. We still highlight strings and comments inside of the attribute.
syn region rustAttribute start="#!\?\[" end="\]" contains=@rustAttributeContents,rustAttributeParenthesizedParens,rustAttributeParenthesizedCurly,rustAttributeParenthesizedBrackets,rustDerive
syn region rustAttributeParenthesizedParens matchgroup=rustAttribute start="\w\%(\w\)*("rs=e end=")"re=s transparent contained contains=rustAttributeBalancedParens,@rustAttributeContents
syn region rustAttributeParenthesizedCurly matchgroup=rustAttribute start="\w\%(\w\)*{"rs=e end="}"re=s transparent contained contains=rustAttributeBalancedCurly,@rustAttributeContents
syn region rustAttributeParenthesizedBrackets matchgroup=rustAttribute start="\w\%(\w\)*\["rs=e end="\]"re=s transparent contained contains=rustAttributeBalancedBrackets,@rustAttributeContents
syn region rustAttributeBalancedParens matchgroup=rustAttribute start="("rs=e end=")"re=s transparent contained contains=rustAttributeBalancedParens,@rustAttributeContents
syn region rustAttributeBalancedCurly matchgroup=rustAttribute start="{"rs=e end="}"re=s transparent contained contains=rustAttributeBalancedCurly,@rustAttributeContents
syn region rustAttributeBalancedBrackets matchgroup=rustAttribute start="\["rs=e end="\]"re=s transparent contained contains=rustAttributeBalancedBrackets,@rustAttributeContents
syn cluster rustAttributeContents contains=rustString,rustCommentLine,rustCommentBlock,rustCommentLineDocError,rustCommentBlockDocError
syn region rustDerive start="derive(" end=")" contained contains=rustDeriveTrait syn region rustDerive start="derive(" end=")" contained contains=rustDeriveTrait
" This list comes from src/libsyntax/ext/deriving/mod.rs " This list comes from src/libsyntax/ext/deriving/mod.rs
" Some are deprecated (Encodable, Decodable) or to be removed after a new snapshot (Show). " Some are deprecated (Encodable, Decodable) or to be removed after a new snapshot (Show).
syn keyword rustDeriveTrait contained Clone Hash RustcEncodable RustcDecodable Encodable Decodable PartialEq Eq PartialOrd Ord Rand Show Debug Default FromPrimitive Send Sync Copy syn keyword rustDeriveTrait contained Clone Hash RustcEncodable RustcDecodable Encodable Decodable PartialEq Eq PartialOrd Ord Rand Show Debug Default FromPrimitive Send Sync Copy
" dyn keyword: It's only a keyword when used inside a type expression, so
" we make effort here to highlight it only when Rust identifiers follow it
" (not minding the case of pre-2018 Rust where a path starting with :: can
" follow).
"
" This is so that uses of dyn variable names such as in 'let &dyn = &2'
" and 'let dyn = 2' will not get highlighted as a keyword.
syn match rustKeyword "\<dyn\ze\_s\+\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)" contains=rustDynKeyword
syn keyword rustDynKeyword dyn contained
" Number literals " Number literals
syn match rustDecNumber display "\<[0-9][0-9_]*\%([iu]\%(size\|8\|16\|32\|64\|128\)\)\=" syn match rustDecNumber display "\<[0-9][0-9_]*\%([iu]\%(size\|8\|16\|32\|64\|128\)\)\="
syn match rustHexNumber display "\<0x[a-fA-F0-9_]\+\%([iu]\%(size\|8\|16\|32\|64\|128\)\)\=" syn match rustHexNumber display "\<0x[a-fA-F0-9_]\+\%([iu]\%(size\|8\|16\|32\|64\|128\)\)\="
@ -172,12 +192,13 @@ syn match rustFloat display "\<[0-9][0-9_]*\%(\.[0-9][0-9_]*\)\=\%([eE
" For the benefit of delimitMate " For the benefit of delimitMate
syn region rustLifetimeCandidate display start=/&'\%(\([^'\\]\|\\\(['nrt0\\\"]\|x\x\{2}\|u{\%(\x_*\)\{1,6}}\)\)'\)\@!/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustSigil,rustLifetime syn region rustLifetimeCandidate display start=/&'\%(\([^'\\]\|\\\(['nrt0\\\"]\|x\x\{2}\|u{\%(\x_*\)\{1,6}}\)\)'\)\@!/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustSigil,rustLifetime
syn region rustGenericRegion display start=/<\%('\|[^[cntrl:][:space:][:punct:]]\)\@=')\S\@=/ end=/>/ contains=rustGenericLifetimeCandidate syn region rustGenericRegion display start=/<\%('\|[^[:cntrl:][:space:][:punct:]]\)\@=')\S\@=/ end=/>/ contains=rustGenericLifetimeCandidate
syn region rustGenericLifetimeCandidate display start=/\%(<\|,\s*\)\@<='/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustSigil,rustLifetime syn region rustGenericLifetimeCandidate display start=/\%(<\|,\s*\)\@<='/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustSigil,rustLifetime
"rustLifetime must appear before rustCharacter, or chars will get the lifetime highlighting "rustLifetime must appear before rustCharacter, or chars will get the lifetime highlighting
syn match rustLifetime display "\'\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" syn match rustLifetime display "\'\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*"
syn match rustLabel display "\'\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*:" syn match rustLabel display "\'\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*:"
syn match rustLabel display "\%(\<\%(break\|continue\)\s*\)\@<=\'\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*"
syn match rustCharacterInvalid display contained /b\?'\zs[\n\r\t']\ze'/ syn match rustCharacterInvalid display contained /b\?'\zs[\n\r\t']\ze'/
" The groups negated here add up to 0-255 but nothing else (they do not seem to go beyond ASCII). " The groups negated here add up to 0-255 but nothing else (they do not seem to go beyond ASCII).
syn match rustCharacterInvalidUnicode display contained /b'\zs[^[:cntrl:][:graph:][:alnum:][:space:]]\ze'/ syn match rustCharacterInvalidUnicode display contained /b'\zs[^[:cntrl:][:graph:][:alnum:][:space:]]\ze'/
@ -189,11 +210,12 @@ syn region rustCommentLine star
syn region rustCommentLineDoc start="//\%(//\@!\|!\)" end="$" contains=rustTodo,@Spell syn region rustCommentLineDoc start="//\%(//\@!\|!\)" end="$" contains=rustTodo,@Spell
syn region rustCommentLineDocError start="//\%(//\@!\|!\)" end="$" contains=rustTodo,@Spell contained syn region rustCommentLineDocError start="//\%(//\@!\|!\)" end="$" contains=rustTodo,@Spell contained
syn region rustCommentBlock matchgroup=rustCommentBlock start="/\*\%(!\|\*[*/]\@!\)\@!" end="\*/" contains=rustTodo,rustCommentBlockNest,@Spell syn region rustCommentBlock matchgroup=rustCommentBlock start="/\*\%(!\|\*[*/]\@!\)\@!" end="\*/" contains=rustTodo,rustCommentBlockNest,@Spell
syn region rustCommentBlockDoc matchgroup=rustCommentBlockDoc start="/\*\%(!\|\*[*/]\@!\)" end="\*/" contains=rustTodo,rustCommentBlockDocNest,@Spell syn region rustCommentBlockDoc matchgroup=rustCommentBlockDoc start="/\*\%(!\|\*[*/]\@!\)" end="\*/" contains=rustTodo,rustCommentBlockDocNest,rustCommentBlockDocRustCode,@Spell
syn region rustCommentBlockDocError matchgroup=rustCommentBlockDocError start="/\*\%(!\|\*[*/]\@!\)" end="\*/" contains=rustTodo,rustCommentBlockDocNestError,@Spell contained syn region rustCommentBlockDocError matchgroup=rustCommentBlockDocError start="/\*\%(!\|\*[*/]\@!\)" end="\*/" contains=rustTodo,rustCommentBlockDocNestError,@Spell contained
syn region rustCommentBlockNest matchgroup=rustCommentBlock start="/\*" end="\*/" contains=rustTodo,rustCommentBlockNest,@Spell contained transparent syn region rustCommentBlockNest matchgroup=rustCommentBlock start="/\*" end="\*/" contains=rustTodo,rustCommentBlockNest,@Spell contained transparent
syn region rustCommentBlockDocNest matchgroup=rustCommentBlockDoc start="/\*" end="\*/" contains=rustTodo,rustCommentBlockDocNest,@Spell contained transparent syn region rustCommentBlockDocNest matchgroup=rustCommentBlockDoc start="/\*" end="\*/" contains=rustTodo,rustCommentBlockDocNest,@Spell contained transparent
syn region rustCommentBlockDocNestError matchgroup=rustCommentBlockDocError start="/\*" end="\*/" contains=rustTodo,rustCommentBlockDocNestError,@Spell contained transparent syn region rustCommentBlockDocNestError matchgroup=rustCommentBlockDocError start="/\*" end="\*/" contains=rustTodo,rustCommentBlockDocNestError,@Spell contained transparent
" FIXME: this is a really ugly and not fully correct implementation. Most " FIXME: this is a really ugly and not fully correct implementation. Most
" importantly, a case like ``/* */*`` should have the final ``*`` not being in " importantly, a case like ``/* */*`` should have the final ``*`` not being in
" a comment, but in practice at present it leaves comments open two levels " a comment, but in practice at present it leaves comments open two levels
@ -213,6 +235,39 @@ syn keyword rustTodo contained TODO FIXME XXX NB NOTE
" FIXME: use the AST to make really good folding " FIXME: use the AST to make really good folding
syn region rustFoldBraces start="{" end="}" transparent fold syn region rustFoldBraces start="{" end="}" transparent fold
if !exists("b:current_syntax_embed")
let b:current_syntax_embed = 1
syntax include @RustCodeInComment <sfile>:p:h/rust.vim
unlet b:current_syntax_embed
" Currently regions marked as ```<some-other-syntax> will not get
" highlighted at all. In the future, we can do as vim-markdown does and
" highlight with the other syntax. But for now, let's make sure we find
" the closing block marker, because the rules below won't catch it.
syn region rustCommentLinesDocNonRustCode matchgroup=rustCommentDocCodeFence start='^\z(\s*//[!/]\s*```\).\+$' end='^\z1$' keepend contains=rustCommentLineDoc
" We borrow the rules from rusts src/librustdoc/html/markdown.rs, so that
" we only highlight as Rust what it would perceive as Rust (almost; its
" possible to trick it if you try hard, and indented code blocks arent
" supported because Markdown is a menace to parse and only mad dogs and
" Englishmen would try to handle that case correctly in this syntax file).
syn region rustCommentLinesDocRustCode matchgroup=rustCommentDocCodeFence start='^\z(\s*//[!/]\s*```\)[^A-Za-z0-9_-]*\%(\%(should_panic\|no_run\|ignore\|allow_fail\|rust\|test_harness\|compile_fail\|E\d\{4}\|edition201[58]\)\%([^A-Za-z0-9_-]\+\|$\)\)*$' end='^\z1$' keepend contains=@RustCodeInComment,rustCommentLineDocLeader
syn region rustCommentBlockDocRustCode matchgroup=rustCommentDocCodeFence start='^\z(\%(\s*\*\)\?\s*```\)[^A-Za-z0-9_-]*\%(\%(should_panic\|no_run\|ignore\|allow_fail\|rust\|test_harness\|compile_fail\|E\d\{4}\|edition201[58]\)\%([^A-Za-z0-9_-]\+\|$\)\)*$' end='^\z1$' keepend contains=@RustCodeInComment,rustCommentBlockDocStar
" Strictly, this may or may not be correct; this code, for example, would
" mishighlight:
"
" /**
" ```rust
" println!("{}", 1
" * 1);
" ```
" */
"
" … but I dont care. Balance of probability, and all that.
syn match rustCommentBlockDocStar /^\s*\*\s\?/ contained
syn match rustCommentLineDocLeader "^\s*//\%(//\@!\|!\)" contained
endif
" Default highlighting {{{1 " Default highlighting {{{1
hi def link rustDecNumber rustNumber hi def link rustDecNumber rustNumber
hi def link rustHexNumber rustNumber hi def link rustHexNumber rustNumber
@ -231,6 +286,7 @@ hi def link rustEscapeUnicode rustEscape
hi def link rustEscapeError Error hi def link rustEscapeError Error
hi def link rustStringContinuation Special hi def link rustStringContinuation Special
hi def link rustString String hi def link rustString String
hi def link rustStringDelimiter String
hi def link rustCharacterInvalid Error hi def link rustCharacterInvalid Error
hi def link rustCharacterInvalidUnicode rustCharacterInvalid hi def link rustCharacterInvalidUnicode rustCharacterInvalid
hi def link rustCharacter Character hi def link rustCharacter Character
@ -244,9 +300,11 @@ hi def link rustFloat Float
hi def link rustArrowCharacter rustOperator hi def link rustArrowCharacter rustOperator
hi def link rustOperator Operator hi def link rustOperator Operator
hi def link rustKeyword Keyword hi def link rustKeyword Keyword
hi def link rustDynKeyword rustKeyword
hi def link rustTypedef Keyword " More precise is Typedef, but it doesn't feel right for Rust hi def link rustTypedef Keyword " More precise is Typedef, but it doesn't feel right for Rust
hi def link rustStructure Keyword " More precise is Structure hi def link rustStructure Keyword " More precise is Structure
hi def link rustUnion rustStructure hi def link rustUnion rustStructure
hi def link rustExistential rustKeyword
hi def link rustPubScopeDelim Delimiter hi def link rustPubScopeDelim Delimiter
hi def link rustPubScopeCrate rustKeyword hi def link rustPubScopeCrate rustKeyword
hi def link rustSuper rustKeyword hi def link rustSuper rustKeyword
@ -264,10 +322,13 @@ hi def link rustFuncCall Function
hi def link rustShebang Comment hi def link rustShebang Comment
hi def link rustCommentLine Comment hi def link rustCommentLine Comment
hi def link rustCommentLineDoc SpecialComment hi def link rustCommentLineDoc SpecialComment
hi def link rustCommentLineDocLeader rustCommentLineDoc
hi def link rustCommentLineDocError Error hi def link rustCommentLineDocError Error
hi def link rustCommentBlock rustCommentLine hi def link rustCommentBlock rustCommentLine
hi def link rustCommentBlockDoc rustCommentLineDoc hi def link rustCommentBlockDoc rustCommentLineDoc
hi def link rustCommentBlockDocStar rustCommentBlockDoc
hi def link rustCommentBlockDocError Error hi def link rustCommentBlockDocError Error
hi def link rustCommentDocCodeFence rustCommentLineDoc
hi def link rustAssert PreCondit hi def link rustAssert PreCondit
hi def link rustPanic PreCondit hi def link rustPanic PreCondit
hi def link rustMacro Macro hi def link rustMacro Macro
@ -280,11 +341,11 @@ hi def link rustStorage StorageClass
hi def link rustObsoleteStorage Error hi def link rustObsoleteStorage Error
hi def link rustLifetime Special hi def link rustLifetime Special
hi def link rustLabel Label hi def link rustLabel Label
hi def link rustInvalidBareKeyword Error
hi def link rustExternCrate rustKeyword hi def link rustExternCrate rustKeyword
hi def link rustObsoleteExternMod Error hi def link rustObsoleteExternMod Error
hi def link rustBoxPlacementParens Delimiter
hi def link rustQuestionMark Special hi def link rustQuestionMark Special
hi def link rustAsync rustKeyword
hi def link rustAwait rustKeyword
" Other Suggestions: " Other Suggestions:
" hi rustAttribute ctermfg=cyan " hi rustAttribute ctermfg=cyan
@ -297,3 +358,5 @@ syn sync minlines=200
syn sync maxlines=500 syn sync maxlines=500
let b:current_syntax = "rust" let b:current_syntax = "rust"
" vim: set et sw=4 sts=4 ts=8:

View file

@ -18,12 +18,50 @@ let s:save_cpo = &cpo
set cpo&vim set cpo&vim
function! SyntaxCheckers_rust_cargo_IsAvailable() dict function! SyntaxCheckers_rust_cargo_IsAvailable() dict
if exists("*syntastic#util#getVersion")
echom "rust.vim: version of Syntastic is too old. Needs to be at least 3.7.0."
return v:false
endif
return executable(self.getExec()) && return executable(self.getExec()) &&
\ syntastic#util#versionIsAtLeast(self.getVersion(), [0, 16, 0]) \ syntastic#util#versionIsAtLeast(self.getVersion(), [0, 16, 0])
endfunction endfunction
function! SyntaxCheckers_rust_cargo_GetLocList() dict function! SyntaxCheckers_rust_cargo_GetLocList() dict
let makeprg = self.makeprgBuild({ "args": "check" }) let makeprg = self.makeprgBuild({ "args": "check" })
let l:root_cargo_toml = cargo#nearestRootCargo(0)
let l:nearest_cargo_toml = cargo#nearestCargo(0)
let b:rust_recent_root_cargo_toml = l:root_cargo_toml
let b:rust_recent_nearest_cargo_toml = l:nearest_cargo_toml
" All pathname prints are relative to the Cargo.toml of the workspace, if
" there is a workspace, otherwise they are relative to the Cargo.toml of
" the single crate. Where to actually execute under these varying
" circumtances 'cargo' is determined here, and controlled by
" configuration.
if rust#GetConfigVar('rust_cargo_avoid_whole_workspace', 1)
if l:root_cargo_toml !=# l:nearest_cargo_toml
let makeprg = "cd " . fnamemodify(l:nearest_cargo_toml, ":p:h")
\ . " && " . makeprg
endif
else
let makeprg = "cd " . fnamemodify(l:root_cargo_toml, ":p:h")
\ . " && " . makeprg
endif
let l:check_all_targets = rust#GetConfigVar('rust_cargo_check_all_targets', 0)
let l:check_all_features = rust#GetConfigVar('rust_cargo_check_all_features', 0)
let l:check_examples = rust#GetConfigVar('rust_cargo_check_examples', 0)
let l:check_tests = rust#GetConfigVar('rust_cargo_check_tests', 0)
let l:check_benches = rust#GetConfigVar('rust_cargo_check_benches', 0)
let makeprg = makeprg. ' '
\ . (l:check_all_targets ? ' --all-targets' : '')
\ . (l:check_all_features ? ' --all-features' : '')
\ . (l:check_benches ? ' --benches' : '')
\ . (l:check_examples ? ' --examples' : '')
\ . (l:check_tests ? ' --tests' : '')
" Ignored patterns, and blank lines " Ignored patterns, and blank lines
let errorformat = let errorformat =
@ -41,7 +79,7 @@ function! SyntaxCheckers_rust_cargo_GetLocList() dict
return SyntasticMake({ return SyntasticMake({
\ 'makeprg': makeprg, \ 'makeprg': makeprg,
\ 'cwd': expand('%:p:h'), \ 'cwd': fnamemodify(l:root_cargo_toml, ":p:h:."),
\ 'errorformat': errorformat }) \ 'errorformat': errorformat })
endfunction endfunction
@ -51,3 +89,5 @@ call g:SyntasticRegistry.CreateAndRegisterChecker({
let &cpo = s:save_cpo let &cpo = s:save_cpo
unlet s:save_cpo unlet s:save_cpo
" vim: set et sw=4 sts=4 ts=8:

View file

@ -10,8 +10,10 @@ if exists("g:loaded_syntastic_rust_rustc_checker")
endif endif
let g:loaded_syntastic_rust_rustc_checker = 1 let g:loaded_syntastic_rust_rustc_checker = 1
" vint: -ProhibitAbbreviationOption
let s:save_cpo = &cpo let s:save_cpo = &cpo
set cpo&vim set cpo&vim
" vint: +ProhibitAbbreviationOption
function! SyntaxCheckers_rust_rustc_GetLocList() dict function! SyntaxCheckers_rust_rustc_GetLocList() dict
let makeprg = self.makeprgBuild({}) let makeprg = self.makeprgBuild({})
@ -44,5 +46,9 @@ call g:SyntasticRegistry.CreateAndRegisterChecker({
\ 'filetype': 'rust', \ 'filetype': 'rust',
\ 'name': 'rustc'}) \ 'name': 'rustc'})
" vint: -ProhibitAbbreviationOption
let &cpo = s:save_cpo let &cpo = s:save_cpo
unlet s:save_cpo unlet s:save_cpo
" vint: +ProhibitAbbreviationOption
" vim: set et sw=4 sts=4 ts=8:

View file

View file

@ -0,0 +1,34 @@
# This is brought as reference, to be able to reproduce a new image
FROM alonid/vim-testbed:10
RUN install_vim -tag v7.4.052 -name vim74-trusty -build \
-tag v8.0.1850 -name vim80 -build \
-tag v8.1.0105 -name vim81 -build \
-tag neovim:v0.1.7 -build \
-tag neovim:v0.2.2 -build
ENV PACKAGES="\
bash \
git \
python \
python2-pip \
curl \
"
RUN dnf install -y $PACKAGES
RUN pip install vim-vint==0.3.19
RUN export HOME=/rust ; mkdir $HOME ; curl https://sh.rustup.rs -sSf | sh -s -- -y
RUN chown vimtest.vimtest -R /rust
RUN (dnf remove -y gcc \*-devel ; \
dnf install -y gpm msgpack libvterm libtermkey unibilium ) || true
RUN dnf clean all
RUN echo "export PATH=~/.cargo/bin:$PATH" >> ~/.bashrc
RUN git clone https://github.com/da-x/vader.vim vader && \
cd vader && git checkout v2017-12-26

View file

@ -0,0 +1,24 @@
Given rust (Some Rust code):
fn main() {
println!("Hello World\n")
}
Execute (RustInfo - call it to see that it works):
redir => m
silent RustInfo
redir END
Log m
Execute (RustEmitAsm - see that we actually get assembly output):
silent! w test.rs
silent! e! test.rs
redir => m
silent! RustEmitAsm
redir END
AssertEqual 'asm', &filetype
normal! ggVGy:q<CR>
AssertEqual 1,(@" =~# '\V.section')
bd
call delete('test.rs')
# TODO: a lot more tests

View file

@ -0,0 +1,247 @@
Given rust:
fn main() {
let a = 2;
println!("Hello World\n")
}
Do:
vip=
Expect rust (very basic indentation result):
fn main() {
let a = 2;
println!("Hello World\n")
}
############################################
# Issue #195
Given rust:
fn main() {
let paths: Vec<_> = ({
fs::read_dir("test_data")
.unwrap()
.cloned()
})
.collect();
println!("Hello World\n");
}
Do:
/collect\<cr>
ostatement();\<ESC>\<ESC>
Expect rust (issue #195):
fn main() {
let paths: Vec<_> = ({
fs::read_dir("test_data")
.unwrap()
.cloned()
})
.collect();
statement();
println!("Hello World\n");
}
############################################
# Issue #189
Given rust:
impl X for {
pub fn get<Q>(&self, key: &Q) -> Option<Entry<K, V>>
where
K: Borrow<Q>,
Q: Ord + ?Sized,
{
self.inner.get(key).map(Entry::new)
}
}
Do:
vip=
Expect rust (issue #189):
impl X for {
pub fn get<Q>(&self, key: &Q) -> Option<Entry<K, V>>
where
K: Borrow<Q>,
Q: Ord + ?Sized,
{
self.inner.get(key).map(Entry::new)
}
}
############################################
# Issue #189b
Given rust:
impl X for {
pub fn get<Q>(&self, key: &Q) -> Option<Entry<K, V>>
where
K: Borrow<Q>,
Q: Ord + ?Sized
{
self.inner.get(key).map(Entry::new)
}
}
Do:
vip=
Expect rust (issue #189b):
impl X for {
pub fn get<Q>(&self, key: &Q) -> Option<Entry<K, V>>
where
K: Borrow<Q>,
Q: Ord + ?Sized
{
self.inner.get(key).map(Entry::new)
}
}
############################################
# Issue #189c
Given rust:
impl X for {
pub fn get<Q>(&self, key: &Q) -> Option<Entry<K, V>>
where K: Borrow<Q>, Q: Ord + ?Sized
{
self.inner.get(key).map(Entry::new)
}
}
Do:
vip=
Expect rust (issue #189b):
impl X for {
pub fn get<Q>(&self, key: &Q) -> Option<Entry<K, V>>
where K: Borrow<Q>, Q: Ord + ?Sized
{
self.inner.get(key).map(Entry::new)
}
}
############################################
# Issue #149
Given rust:
fn test() {
let t = "a
wah";
}
Do:
/wah\<cr>
i#\<ESC>\<ESC>
/;\<cr>o
statement();\<ESC>\<ESC>
# Disabled
# Expect rust (issue #149):
fn test() {
let t = "a
#wah";
statement();
}
############################################
# Issue #77
Given rust:
fn test() {
}
Do:
of(x, y,\<CR>z);\<CR>
f((x, y),\<CR>z);\<CR>
# Disabled
# Expect rust (issue #77):
fn test() {
f(x, y,
z);
f((x, y),
z);
}
############################################
# Issue #44
Given rust:
fn main() {
a
let philosophers = vec![
Philosopher::new("Judith Butler"),
Philosopher::new("Gilles Deleuze"),
Philosopher::new("Karl Marx"),
Philosopher::new("Emma Goldman"),
Philosopher::new("Michel Foucault"),
];
}
Do:
/let\<CR>
vip=
# Disabled
# Expect rust (issue #44):
fn main() {
a
let philosophers = vec![
Philosopher::new("Judith Butler"),
Philosopher::new("Gilles Deleuze"),
Philosopher::new("Karl Marx"),
Philosopher::new("Emma Goldman"),
Philosopher::new("Michel Foucault"),
];
}
############################################
# Issue #5
Given rust:
fn f() {
if x &&
y {
}
}
Do:
vip=
Expect rust (issue #5):
fn f() {
if x &&
y {
}
}
############################################
Given rust:
fn f() {
let mut state = State::new(
backend,
header.clone(),
).expect("Something");
}
Do:
vip=
Expect rust (Trailing comma in call):
fn f() {
let mut state = State::new(
backend,
header.clone(),
).expect("Something");
}

View file

@ -0,0 +1,105 @@
#!/usr/bin/env python
import os
import sys
REPO = "alonid/vim-testbed"
TAG = "10-rust.vim"
IMAGE = "%s:%s" % (REPO, TAG)
class Error(Exception):
pass
def system(cmd, capture=False, ok_fail=False):
if capture:
f = os.popen(cmd)
d = f.read()
return d
res = os.system(cmd)
if res != 0:
if ok_fail:
return res
raise Error("Error executing: %s" % (cmd, ))
return 0
def root():
return os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
def prep():
d = os.path.join(root(), "test")
for i in [".cargo", ".rustup", ".multirust"]:
l = os.path.join(d, i)
if not os.path.lexists(l):
os.symlink("/rust/" + i, l)
l = os.path.join(root(), "test/.vimrc")
if not os.path.lexists(l):
os.symlink("vimrc", l)
if not os.path.exists(os.path.join(d, ".profile")):
f = open(os.path.join(d, ".profile"), "w")
f.write('export PATH="$HOME/.cargo/bin:$PATH"\n')
f.close()
def docker_run(cmd, interactive=False, ok_fail=False):
prep()
d = root()
params = "-v %s:/testplugin -v %s/test:/home/vimtest" % (d, d)
params += " -e HOME=/home/vimtest"
if not interactive:
params += " -a stderr"
params += " -e VADER_OUTPUT_FILE=/dev/stderr"
params += " -u %s" % (os.getuid(), )
params += " -w /testplugin"
if interactive:
interactive_str = "-it"
else:
interactive_str = ""
return system("docker run %s --rm %s %s %s" % (interactive_str, params, IMAGE, cmd),
ok_fail=ok_fail)
def image_exists():
r = system("docker images -q %s" % (IMAGE, ), capture=True)
return len(r.strip().splitlines()) >= 1
def tests_on_docker():
res = docker_run("bash -lc 'python /home/vimtest/run-tests inside-docker'", ok_fail=True)
if res == 0:
print "Tests OK"
else:
print "Tests Failed"
sys.exit(1)
def inside_docker():
res = system("/vim-build/bin/vim80 --not-a-term '+Vader! test/*.vader'", ok_fail=True)
if res != 0:
sys.exit(1)
def run_with_vimrc(vimrc):
res = system("vim -u %s --not-a-term '+Vader! test/*.vader'" % (vimrc, ), ok_fail=True)
if res != 0:
sys.exit(1)
def main():
if sys.argv[1:] == ["inside-docker"]:
inside_docker()
return
if sys.argv[1:2] == ["run-with-vimrc"]:
run_with_vimrc(sys.argv[2])
return
if not image_exists():
print "Need to take image from remote"
system("docker pull %s" % (IMAGE, ))
if "-i" in sys.argv[1:]:
docker_run("bash -l", interactive=True)
return
tests_on_docker()
if __name__ == "__main__":
main()

View file

@ -0,0 +1,30 @@
" vint: -ProhibitSetNoCompatible
"
set nocompatible
filetype off
" This script is currently designed to be run from within Docker, the
" following paths are intrinsic to the container:
source /rtp.vim
" Paths need prepending (instead of what is originally done
" in vim-testbed) in order to supersede the rust.vim that is
" supplied with Vim.
exec 'set runtimepath=/vader,/testplugin,' . &runtimepath
cd /testplugin
filetype plugin indent on
syntax on
set nocompatible
set tabstop=8
set softtabstop=4
set shiftwidth=4
set expandtab
set backspace=2
set nofoldenable
set foldmethod=syntax
set foldlevelstart=10
set foldnestmax=10
set ttimeoutlen=0

View file

@ -0,0 +1,131 @@
Typescript Syntax for Vim
=========================
Syntax file and other settings for [TypeScript](http://typescriptlang.org). The
syntax file is taken from this [blog
post](http://blogs.msdn.com/b/interoperability/archive/2012/10/01/sublime-text-vi-emacs-typescript-enabled.aspx).
Checkout [Tsuquyomi](https://github.com/Quramy/tsuquyomi) for omni-completion
and other features for TypeScript editing.
Install
-------
From Vim 8 onward, the plugin can be installed as simply as:
```
git clone https://github.com/leafgarland/typescript-vim.git ~/.vim/pack/typescript/start/typescript-vim
```
For older versions of Vim, the simplest way to install is via a Vim add-in manager such as
[Plug](https://github.com/junegunn/vim-plug),
[Vundle](https://github.com/gmarik/vundle) or
[Pathogen](https://github.com/tpope/vim-pathogen/).
_See the [Installation Wiki](https://github.com/leafgarland/typescript-vim/wiki/Installation)_
### Pathogen
```
git clone https://github.com/leafgarland/typescript-vim.git ~/.vim/bundle/typescript-vim
```
If you want to install manually then you need to copy the files from this
repository into your vim path, see the vim docs for [:help
runtimepath](http://vimdoc.sourceforge.net/htmldoc/options.html#'runtimepath')
for more information. This might be as simple as copying the files and
directories to `~/.vim/` but it depends on your Vim install and operating
system.
Usage
-----
Once the files are installed the syntax highlighting and other settings will be
automatically enabled anytime you edit a `.ts` file.
Indenting
---------
This plugin includes a custom indenter (based on [pangloss/vim-javascript's
indenter](https://github.com/pangloss/vim-javascript/blob/master/indent/javascript.vim)),
it works pretty well but there are cases where it fails. If these bother you or
want to use other indent settings you can disable it by setting a flag in your
`.vimrc`:
```vim
let g:typescript_indent_disable = 1
```
If you want the indenter to automatically indent chained method calls as you type.
```typescript
something
.foo()
.bar();
```
Then add something like `setlocal indentkeys+=0.` to your `.vimrc`, see `:help
'indentkeys'` in vim for more information.
If you use the `=` operator to re-indent code it will always indent
chained method calls - this can be disabled by changing the regex the
indent script uses to identify indented lines. In this case removing '.'
from the regex means that it wont indent lines starting with '.'. Note,
this is not ideal as the regex may change making your setting out of date.
```vim
let g:typescript_opfirst='\%([<>=,?^%|*/&]\|\([-:+]\)\1\@!\|!=\|in\%(stanceof\)\=\>\)'
```
Compiler settings
-----------------
This plugin contains compiler settings to set `makeprg` and `errorformat`.
The compiler settings enable you to call the `tsc` compiler directly from Vim
and display any errors or warnings in Vim's QuickFix window.
To run the compiler, enter `:make`, this will run `tsc` against the last saved
version of your currently edited file.
The default for `makeprg` is `tsc $* %`. You can enter other compiler options into your `:make`
command line and they will be inserted in place of `$*`.
There are options to change the compiler name and to insert default options.
```vim
let g:typescript_compiler_binary = 'tsc'
let g:typescript_compiler_options = ''
```
These options will be passed to the binary as command arguments. For example,
if `g:typescript_compiler_binary = 'tsc'` and `g:typescript_compiler_options = '--lib es6'`,
`l:makeprg` will be: `tsc --lib es6 $* %`.
You can completely override this plugin's compiler settings with something like
this in your `.vimrc`, where you can set makeprg to whatever you want.
```vim
autocmd FileType typescript :set makeprg=tsc
```
Note, this plugin's compiler settings are not used by Syntastic which has its own
way of changing the options. See https://github.com/scrooloose/syntastic#faqargs.
You can use something like this in your `.vimrc` to make the QuickFix
window automatically appear if `:make` has any errors.
```vim
autocmd QuickFixCmdPost [^l]* nested cwindow
autocmd QuickFixCmdPost l* nested lwindow
```
Syntax highlighting
-------------------
Syntax highlighting for TypeScript can be customized by following variables.
- `g:typescript_ignore_typescriptdoc`: When this variable is defined, doccomments will not be
highlighted.
- `g:typescript_ignore_browserwords`: When this variable is set to `1`, browser API names such as
`window` or `document` will not be highlighted. (default to `0`)
![Obligatory screenshot](https://raw.github.com/leafgarland/typescript-vim/master/vimshot01.png)

View file

@ -0,0 +1,30 @@
if exists("current_compiler")
finish
endif
let current_compiler = "typescript"
if !exists("g:typescript_compiler_binary")
let g:typescript_compiler_binary = "tsc"
endif
if !exists("g:typescript_compiler_options")
let g:typescript_compiler_options = ""
endif
if exists(":CompilerSet") != 2
command! -nargs=* CompilerSet setlocal <args>
endif
let s:cpo_save = &cpo
set cpo-=C
execute 'CompilerSet makeprg='
\ . escape(g:typescript_compiler_binary, ' ')
\ . '\ '
\ . escape(g:typescript_compiler_options, ' ')
\ . '\ $*\ %'
CompilerSet errorformat=%+A\ %#%f\ %#(%l\\\,%c):\ %m,%C%m
let &cpo = s:cpo_save
unlet s:cpo_save

View file

@ -0,0 +1,4 @@
" use `set filetype` to override default filetype=xml for *.ts files
autocmd BufNewFile,BufRead *.ts set filetype=typescript
" use `setfiletype` to not override any other plugins like ianks/vim-tsx
autocmd BufNewFile,BufRead *.tsx setfiletype typescript

View file

@ -0,0 +1,21 @@
if exists("b:did_ftplugin")
finish
endif
let b:did_ftplugin = 1
let s:cpo_save = &cpo
set cpo-=C
compiler typescript
setlocal commentstring=//\ %s
" Set 'formatoptions' to break comment lines but not other lines,
" " and insert the comment leader when hitting <CR> or using "o".
setlocal formatoptions-=t formatoptions+=croql
setlocal suffixesadd+=.ts,.tsx
let b:undo_ftplugin = "setl fo< ofu< com< cms<"
let &cpo = s:cpo_save
unlet s:cpo_save

View file

@ -0,0 +1,359 @@
" Vim indent file
" Language: Typescript
" Acknowledgement: Almost direct copy from https://github.com/pangloss/vim-javascript
" Only load this indent file when no other was loaded.
if exists('b:did_indent') || get(g:, 'typescript_indent_disable', 0)
finish
endif
let b:did_indent = 1
" Now, set up our indentation expression and keys that trigger it.
setlocal indentexpr=GetTypescriptIndent()
setlocal autoindent nolisp nosmartindent
setlocal indentkeys+=0],0)
let b:undo_indent = 'setlocal indentexpr< smartindent< autoindent< indentkeys<'
" Only define the function once.
if exists('*GetTypescriptIndent')
finish
endif
let s:cpo_save = &cpo
set cpo&vim
" Get shiftwidth value
if exists('*shiftwidth')
function s:sw()
return shiftwidth()
endfunction
else
function s:sw()
return &sw
endfunction
endif
" searchpair() wrapper
if has('reltime')
function s:GetPair(start,end,flags,skip,time,...)
return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 2000,0] + a:000),a:time)
endfunction
else
function s:GetPair(start,end,flags,skip,...)
return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,max([prevnonblank(v:lnum) - 1000,get(a:000,1)]))
endfunction
endif
" Regex of syntax group names that are or delimit string or are comments.
let s:syng_strcom = 'string\|comment\|regex\|special\|doc\|template\%(braces\)\@!'
let s:syng_str = 'string\|template\|special'
let s:syng_com = 'comment\|doc'
" Expression used to check whether we should skip a match with searchpair().
let s:skip_expr = "synIDattr(synID(line('.'),col('.'),0),'name') =~? '".s:syng_strcom."'"
function s:skip_func()
if !s:free || search('\m`\|\${\|\*\/','nW',s:looksyn)
let s:free = !eval(s:skip_expr)
let s:looksyn = line('.')
return !s:free
endif
let s:looksyn = line('.')
return getline('.') =~ '\%<'.col('.').'c\/.\{-}\/\|\%>'.col('.').'c[''"]\|\\$' &&
\ eval(s:skip_expr)
endfunction
function s:alternatePair(stop)
let pos = getpos('.')[1:2]
while search('\m[][(){}]','bW',a:stop)
if !s:skip_func()
let idx = stridx('])}',s:looking_at())
if idx + 1
if s:GetPair(['\[','(','{'][idx], '])}'[idx],'bW','s:skip_func()',2000,a:stop) <= 0
break
endif
else
return
endif
endif
endwhile
call call('cursor',pos)
endfunction
function s:save_pos(f,...)
let l:pos = getpos('.')[1:2]
let ret = call(a:f,a:000)
call call('cursor',l:pos)
return ret
endfunction
function s:syn_at(l,c)
return synIDattr(synID(a:l,a:c,0),'name')
endfunction
function s:looking_at()
return getline('.')[col('.')-1]
endfunction
function s:token()
return s:looking_at() =~ '\k' ? expand('<cword>') : s:looking_at()
endfunction
function s:previous_token()
let l:n = line('.')
if (s:looking_at() !~ '\k' || search('\m\<','cbW')) && search('\m\S','bW')
if (getline('.')[col('.')-2:col('.')-1] == '*/' || line('.') != l:n &&
\ getline('.') =~ '\%<'.col('.').'c\/\/') && s:syn_at(line('.'),col('.')) =~? s:syng_com
while search('\m\/\ze[/*]','cbW')
if !search('\m\S','bW')
break
elseif s:syn_at(line('.'),col('.')) !~? s:syng_com
return s:token()
endif
endwhile
else
return s:token()
endif
endif
return ''
endfunction
function s:others(p)
return "((line2byte(line('.')) + col('.')) <= ".(line2byte(a:p[0]) + a:p[1]).") || ".s:skip_expr
endfunction
function s:tern_skip(p)
return s:GetPair('{','}','nbW',s:others(a:p),200,a:p[0]) > 0
endfunction
function s:tern_col(p)
return s:GetPair('?',':\@<!::\@!','nbW',s:others(a:p)
\ .' || s:tern_skip('.string(a:p).')',200,a:p[0]) > 0
endfunction
function s:label_col()
let pos = getpos('.')[1:2]
let [s:looksyn,s:free] = pos
call s:alternatePair(0)
if s:save_pos('s:IsBlock')
let poss = getpos('.')[1:2]
return call('cursor',pos) || !s:tern_col(poss)
elseif s:looking_at() == ':'
return !s:tern_col([0,0])
endif
endfunction
" configurable regexes that define continuation lines, not including (, {, or [.
let s:opfirst = '^' . get(g:,'typescript_opfirst',
\ '\%([<>=,?^%|*/&]\|\([-.:+]\)\1\@!\|!=\|in\%(stanceof\)\=\>\)')
let s:continuation = get(g:,'typescript_continuation',
\ '\%([-+<>=,.~!?/*^%|&:]\|\<\%(typeof\|delete\|void\|in\|instanceof\)\)') . '$'
function s:continues(ln,con)
return !cursor(a:ln, match(' '.a:con,s:continuation)) &&
\ eval( (['s:syn_at(line("."),col(".")) !~? "regex"'] +
\ repeat(['getline(".")[col(".")-2] != tr(s:looking_at(),">","=")'],3) +
\ repeat(['s:previous_token() != "."'],5) + [1])[
\ index(split('/ > - + typeof in instanceof void delete'),s:token())])
endfunction
" get the line of code stripped of comments and move cursor to the last
" non-comment char.
function s:Trim(ln)
call cursor(a:ln+1,1)
call s:previous_token()
return strpart(getline('.'),0,col('.'))
endfunction
" Find line above 'lnum' that isn't empty or in a comment
function s:PrevCodeLine(lnum)
let l:n = prevnonblank(a:lnum)
while l:n
if getline(l:n) =~ '^\s*\/[/*]'
if (stridx(getline(l:n),'`') > 0 || getline(l:n-1)[-1:] == '\') &&
\ s:syn_at(l:n,1) =~? s:syng_str
return l:n
endif
let l:n = prevnonblank(l:n-1)
elseif getline(l:n) =~ '\([/*]\)\1\@![/*]' && s:syn_at(l:n,1) =~? s:syng_com
let l:n = s:save_pos('eval',
\ 'cursor('.l:n.',1) + search(''\m\/\*'',"bW")')
else
return l:n
endif
endwhile
endfunction
" Check if line 'lnum' has a balanced amount of parentheses.
function s:Balanced(lnum)
let l:open = 0
let l:line = getline(a:lnum)
let pos = match(l:line, '[][(){}]', 0)
while pos != -1
if s:syn_at(a:lnum,pos + 1) !~? s:syng_strcom
let l:open += match(' ' . l:line[pos],'[[({]')
if l:open < 0
return
endif
endif
let pos = match(l:line, '[][(){}]', pos + 1)
endwhile
return !l:open
endfunction
function s:OneScope(lnum)
let pline = s:Trim(a:lnum)
let kw = 'else do'
if pline[-1:] == ')' && s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0
call s:previous_token()
let kw = 'for if let while with'
if index(split('await each'),s:token()) + 1
call s:previous_token()
let kw = 'for'
endif
endif
return pline[-2:] == '=>' || index(split(kw),s:token()) + 1 &&
\ s:save_pos('s:previous_token') != '.'
endfunction
" returns braceless levels started by 'i' and above lines * &sw. 'num' is the
" lineNr which encloses the entire context, 'cont' if whether line 'i' + 1 is
" a continued expression, which could have started in a braceless context
function s:iscontOne(i,num,cont)
let [l:i, l:num, bL] = [a:i, a:num + !a:num, 0]
let pind = a:num ? indent(l:num) + s:W : 0
let ind = indent(l:i) + (a:cont ? 0 : s:W)
while l:i >= l:num && (ind > pind || l:i == l:num)
if indent(l:i) < ind && s:OneScope(l:i)
let bL += s:W
let l:i = line('.')
elseif !a:cont || bL || ind < indent(a:i)
break
endif
let ind = min([ind, indent(l:i)])
let l:i = s:PrevCodeLine(l:i - 1)
endwhile
return bL
endfunction
" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader
function s:IsBlock()
if s:looking_at() == '{'
let l:n = line('.')
let char = s:previous_token()
if match(s:stack,'xml\|jsx') + 1 && s:syn_at(line('.'),col('.')-1) =~? 'xml\|jsx'
return char != '{'
elseif char =~ '\k'
return index(split('return const let import export yield default delete var await void typeof throw case new in instanceof')
\ ,char) < (line('.') != l:n) || s:previous_token() == '.'
elseif char == '>'
return getline('.')[col('.')-2] == '=' || s:syn_at(line('.'),col('.')) =~? '^jsflow'
elseif char == ':'
return getline('.')[col('.')-2] != ':' && s:label_col()
elseif char == '/'
return s:syn_at(line('.'),col('.')) =~? 'regex'
endif
return char !~ '[=~!<*,?^%|&([]' &&
\ (char !~ '[-+]' || l:n != line('.') && getline('.')[col('.')-2] == char)
endif
endfunction
function GetTypescriptIndent()
let b:js_cache = get(b:,'js_cache',[0,0,0])
" Get the current line.
call cursor(v:lnum,1)
let l:line = getline('.')
" use synstack as it validates syn state and works in an empty line
let s:stack = synstack(v:lnum,1)
let syns = synIDattr(get(s:stack,-1),'name')
" start with strings,comments,etc.
if syns =~? s:syng_com
if l:line =~ '^\s*\*'
return cindent(v:lnum)
elseif l:line !~ '^\s*\/[/*]'
return -1
endif
elseif syns =~? s:syng_str && l:line !~ '^[''"]'
if b:js_cache[0] == v:lnum - 1 && s:Balanced(v:lnum-1)
let b:js_cache[0] = v:lnum
endif
return -1
endif
let l:lnum = s:PrevCodeLine(v:lnum - 1)
if !l:lnum
return
endif
let l:line = substitute(l:line,'^\s*','','')
if l:line[:1] == '/*'
let l:line = substitute(l:line,'^\%(\/\*.\{-}\*\/\s*\)*','','')
endif
if l:line =~ '^\/[/*]'
let l:line = ''
endif
" the containing paren, bracket, or curly. Many hacks for performance
let idx = index([']',')','}'],l:line[0])
if b:js_cache[0] >= l:lnum && b:js_cache[0] < v:lnum &&
\ (b:js_cache[0] > l:lnum || s:Balanced(l:lnum))
call call('cursor',b:js_cache[1:])
else
let [s:looksyn, s:free, top] = [v:lnum - 1, 1, (!indent(l:lnum) &&
\ s:syn_at(l:lnum,1) !~? s:syng_str) * l:lnum]
if idx + 1
call s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:skip_func()',2000,top)
elseif getline(v:lnum) !~ '^\S' && syns =~? 'block'
call s:GetPair('{','}','bW','s:skip_func()',2000,top)
else
call s:alternatePair(top)
endif
endif
let b:js_cache = [v:lnum] + (line('.') == v:lnum ? [0,0] : getpos('.')[1:2])
let num = b:js_cache[1]
let [s:W, isOp, bL, switch_offset] = [s:sw(),0,0,0]
if !num || s:IsBlock()
let ilnum = line('.')
let pline = s:save_pos('s:Trim',l:lnum)
if num && s:looking_at() == ')' && s:GetPair('(', ')', 'bW', s:skip_expr, 100) > 0
let num = ilnum == num ? line('.') : num
if idx < 0 && s:previous_token() ==# 'switch' && s:previous_token() != '.'
if &cino !~ ':'
let switch_offset = s:W
else
let cinc = matchlist(&cino,'.*:\zs\(-\)\=\(\d*\)\(\.\d\+\)\=\(s\)\=\C')
let switch_offset = max([cinc[0] is '' ? 0 : (cinc[1].1) *
\ ((strlen(cinc[2].cinc[3]) ? str2nr(cinc[2].str2nr(cinc[3][1])) : 10) *
\ (cinc[4] is '' ? 1 : s:W)) / 10, -indent(num)])
endif
if pline[-1:] != '.' && l:line =~# '^\%(default\|case\)\>'
return indent(num) + switch_offset
endif
endif
endif
if idx < 0 && pline !~ '[{;]$'
if pline =~# ':\@<!:$'
call cursor(l:lnum,strlen(pline))
let isOp = s:tern_col(b:js_cache[1:2]) * s:W
else
let isOp = (l:line =~# s:opfirst || s:continues(l:lnum,pline)) * s:W
endif
let bL = s:iscontOne(l:lnum,b:js_cache[1],isOp)
let bL -= (bL && l:line[0] == '{') * s:W
endif
endif
" main return
if idx + 1 || l:line[:1] == '|}'
return indent(num)
elseif num
return indent(num) + s:W + switch_offset + bL + isOp
endif
return bL + isOp
endfunction
let &cpo = s:cpo_save
unlet s:cpo_save

View file

@ -0,0 +1,339 @@
" Vim syntax file
" Language: typescript
" Author: MicroSoft Open Technologies Inc.
" Version: 0.1
" Credits: Zhao Yi, Claudio Fleiner, Scott Shattuck, Jose Elera Campana
if !exists("main_syntax")
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
let main_syntax = "typescript"
endif
" Drop fold if it set but vim doesn't support it.
if version < 600 && exists("typescript_fold")
unlet typescript_fold
endif
"" dollar sign is permitted anywhere in an identifier
setlocal iskeyword+=$
syntax sync fromstart
"" syntax coloring for Node.js shebang line
syn match shebang "^#!.*/bin/env\s\+node\>"
hi link shebang Comment
"" typescript comments"{{{
syn keyword typescriptCommentTodo TODO FIXME XXX TBD contained
syn match typescriptLineComment "\/\/.*" contains=@Spell,typescriptCommentTodo,typescriptRef
syn match typescriptRefComment /\/\/\/<\(reference\|amd-\(dependency\|module\)\)\s\+.*\/>$/ contains=typescriptRefD,typescriptRefS
syn region typescriptRefD start=+"+ skip=+\\\\\|\\"+ end=+"\|$+
syn region typescriptRefS start=+'+ skip=+\\\\\|\\'+ end=+'\|$+
syn match typescriptCommentSkip "^[ \t]*\*\($\|[ \t]\+\)"
syn region typescriptComment start="/\*" end="\*/" contains=@Spell,typescriptCommentTodo extend
"}}}
"" JSDoc support start"{{{
if !exists("typescript_ignore_typescriptdoc")
syntax case ignore
" syntax coloring for JSDoc comments (HTML)
"unlet b:current_syntax
syntax region typescriptDocComment start="/\*\*\s*$" end="\*/" contains=typescriptDocTags,typescriptCommentTodo,typescriptCvsTag,@typescriptHtml,@Spell fold extend
syntax match typescriptDocTags contained "@\(param\|argument\|requires\|exception\|throws\|type\|class\|extends\|see\|link\|member\|module\|method\|title\|namespace\|optional\|default\|base\|file\|returns\=\)\>" nextgroup=typescriptDocParam,typescriptDocSeeTag skipwhite
syntax match typescriptDocTags contained "@\(beta\|deprecated\|description\|fileoverview\|author\|license\|version\|constructor\|private\|protected\|final\|ignore\|addon\|exec\)\>"
syntax match typescriptDocParam contained "\%(#\|\w\|\.\|:\|\/\)\+"
syntax region typescriptDocSeeTag contained matchgroup=typescriptDocSeeTag start="{" end="}" contains=typescriptDocTags
syntax case match
endif "" JSDoc end
"}}}
syntax case match
"" Syntax in the typescript code"{{{
syn match typescriptSpecial "\\\d\d\d\|\\x\x\{2\}\|\\u\x\{4\}" contained containedin=typescriptStringD,typescriptStringS,typescriptStringB display
syn region typescriptStringD start=+"+ skip=+\\\\\|\\"+ end=+"\|$+ contains=typescriptSpecial,@htmlPreproc extend
syn region typescriptStringS start=+'+ skip=+\\\\\|\\'+ end=+'\|$+ contains=typescriptSpecial,@htmlPreproc extend
syn region typescriptStringB start=+`+ skip=+\\\\\|\\`+ end=+`+ contains=typescriptInterpolation,typescriptSpecial,@htmlPreproc extend
syn region typescriptInterpolation matchgroup=typescriptInterpolationDelimiter
\ start=/${/ end=/}/ contained
\ contains=@typescriptExpression
syn match typescriptNumber "-\=\<\d[0-9_]*L\=\>" display
syn match typescriptNumber "-\=\<0[xX][0-9a-fA-F][0-9a-fA-F_]*\>" display
syn match typescriptNumber "-\=\<0[bB][01][01_]*\>" display
syn match typescriptNumber "-\=\<0[oO]\o[0-7_]*\>" display
syn region typescriptRegexpString start=+/[^/*]+me=e-1 skip=+\\\\\|\\/+ end=+/[gi]\{0,2\}\s*$+ end=+/[gi]\{0,2\}\s*[;.,)\]}]+me=e-1 contains=@htmlPreproc oneline
" syntax match typescriptSpecial "\\\d\d\d\|\\x\x\{2\}\|\\u\x\{4\}\|\\."
" syntax region typescriptStringD start=+"+ skip=+\\\\\|\\$"+ end=+"+ contains=typescriptSpecial,@htmlPreproc
" syntax region typescriptStringS start=+'+ skip=+\\\\\|\\$'+ end=+'+ contains=typescriptSpecial,@htmlPreproc
" syntax region typescriptRegexpString start=+/\(\*\|/\)\@!+ skip=+\\\\\|\\/+ end=+/[gim]\{,3}+ contains=typescriptSpecial,@htmlPreproc oneline
" syntax match typescriptNumber /\<-\=\d\+L\=\>\|\<0[xX]\x\+\>/
syntax match typescriptFloat /\<-\=\%(\d[0-9_]*\.\d[0-9_]*\|\d[0-9_]*\.\|\.\d[0-9]*\)\%([eE][+-]\=\d[0-9_]*\)\=\>/
" syntax match typescriptLabel /\(?\s*\)\@<!\<\w\+\(\s*:\)\@=/
syn match typescriptDecorators /@\([_$a-zA-Z][_$a-zA-Z0-9]*\.\)*[_$a-zA-Z][_$a-zA-Z0-9]*\>/
"}}}
"" typescript Prototype"{{{
syntax keyword typescriptPrototype contained prototype
"}}}
" DOM, Browser and Ajax Support {{{
""""""""""""""""""""""""
if get(g:, 'typescript_ignore_browserwords', 0)
syntax keyword typescriptBrowserObjects window navigator screen history location
syntax keyword typescriptDOMObjects document event HTMLElement Anchor Area Base Body Button Form Frame Frameset Image Link Meta Option Select Style Table TableCell TableRow Textarea
syntax keyword typescriptDOMMethods contained createTextNode createElement insertBefore replaceChild removeChild appendChild hasChildNodes cloneNode normalize isSupported hasAttributes getAttribute setAttribute removeAttribute getAttributeNode setAttributeNode removeAttributeNode getElementsByTagName hasAttribute getElementById adoptNode close compareDocumentPosition createAttribute createCDATASection createComment createDocumentFragment createElementNS createEvent createExpression createNSResolver createProcessingInstruction createRange createTreeWalker elementFromPoint evaluate getBoxObjectFor getElementsByClassName getSelection getUserData hasFocus importNode
syntax keyword typescriptDOMProperties contained nodeName nodeValue nodeType parentNode childNodes firstChild lastChild previousSibling nextSibling attributes ownerDocument namespaceURI prefix localName tagName
syntax keyword typescriptAjaxObjects XMLHttpRequest
syntax keyword typescriptAjaxProperties contained readyState responseText responseXML statusText
syntax keyword typescriptAjaxMethods contained onreadystatechange abort getAllResponseHeaders getResponseHeader open send setRequestHeader
syntax keyword typescriptPropietaryObjects ActiveXObject
syntax keyword typescriptPropietaryMethods contained attachEvent detachEvent cancelBubble returnValue
syntax keyword typescriptHtmlElemProperties contained className clientHeight clientLeft clientTop clientWidth dir href id innerHTML lang length offsetHeight offsetLeft offsetParent offsetTop offsetWidth scrollHeight scrollLeft scrollTop scrollWidth style tabIndex target title
syntax keyword typescriptEventListenerKeywords contained blur click focus mouseover mouseout load item
syntax keyword typescriptEventListenerMethods contained scrollIntoView addEventListener dispatchEvent removeEventListener preventDefault stopPropagation
endif
" }}}
"" Programm Keywords"{{{
syntax keyword typescriptSource import export from as
syntax keyword typescriptIdentifier arguments this void
syntax keyword typescriptStorageClass let var const
syntax keyword typescriptOperator delete new instanceof typeof
syntax keyword typescriptBoolean true false
syntax keyword typescriptNull null undefined
syntax keyword typescriptMessage alert confirm prompt status
syntax keyword typescriptGlobal self top parent
syntax keyword typescriptDeprecated escape unescape all applets alinkColor bgColor fgColor linkColor vlinkColor xmlEncoding
"}}}
"" Statement Keywords"{{{
syntax keyword typescriptConditional if else switch
syntax keyword typescriptRepeat do while for in of
syntax keyword typescriptBranch break continue yield await
syntax keyword typescriptLabel case default async readonly
syntax keyword typescriptStatement return with
syntax keyword typescriptGlobalObjects Array Boolean Date Function Infinity JSON Math Number NaN Object Packages RegExp String Symbol netscape
syntax keyword typescriptExceptions try catch throw finally Error EvalError RangeError ReferenceError SyntaxError TypeError URIError
syntax keyword typescriptReserved constructor declare as interface module abstract enum int short export interface static byte extends long super char final native synchronized class float package throws goto private transient debugger implements protected volatile double import public type namespace from get set keyof
"}}}
"" typescript/DOM/HTML/CSS specified things"{{{
" typescript Objects"{{{
syn match typescriptFunction "(super\s*|constructor\s*)" contained nextgroup=typescriptVars
syn region typescriptVars start="(" end=")" contained contains=typescriptParameters transparent keepend
syn match typescriptParameters "([a-zA-Z0-9_?.$][\w?.$]*)\s*:\s*([a-zA-Z0-9_?.$][\w?.$]*)" contained skipwhite
"}}}
" DOM2 Objects"{{{
syntax keyword typescriptType DOMImplementation DocumentFragment Node NodeList NamedNodeMap CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction void any string boolean number symbol never object unknown
syntax keyword typescriptExceptions DOMException
"}}}
" DOM2 CONSTANT"{{{
syntax keyword typescriptDomErrNo INDEX_SIZE_ERR DOMSTRING_SIZE_ERR HIERARCHY_REQUEST_ERR WRONG_DOCUMENT_ERR INVALID_CHARACTER_ERR NO_DATA_ALLOWED_ERR NO_MODIFICATION_ALLOWED_ERR NOT_FOUND_ERR NOT_SUPPORTED_ERR INUSE_ATTRIBUTE_ERR INVALID_STATE_ERR SYNTAX_ERR INVALID_MODIFICATION_ERR NAMESPACE_ERR INVALID_ACCESS_ERR
syntax keyword typescriptDomNodeConsts ELEMENT_NODE ATTRIBUTE_NODE TEXT_NODE CDATA_SECTION_NODE ENTITY_REFERENCE_NODE ENTITY_NODE PROCESSING_INSTRUCTION_NODE COMMENT_NODE DOCUMENT_NODE DOCUMENT_TYPE_NODE DOCUMENT_FRAGMENT_NODE NOTATION_NODE
"}}}
" HTML events and internal variables"{{{
syntax case ignore
syntax keyword typescriptHtmlEvents onblur onclick oncontextmenu ondblclick onfocus onkeydown onkeypress onkeyup onmousedown onmousemove onmouseout onmouseover onmouseup onresize onload onsubmit
syntax case match
"}}}
" Follow stuff should be highligh within a special context
" While it can't be handled with context depended with Regex based highlight
" So, turn it off by default
if exists("typescript_enable_domhtmlcss")
" DOM2 things"{{{
syntax match typescriptDomElemAttrs contained /\%(nodeName\|nodeValue\|nodeType\|parentNode\|childNodes\|firstChild\|lastChild\|previousSibling\|nextSibling\|attributes\|ownerDocument\|namespaceURI\|prefix\|localName\|tagName\)\>/
syntax match typescriptDomElemFuncs contained /\%(insertBefore\|replaceChild\|removeChild\|appendChild\|hasChildNodes\|cloneNode\|normalize\|isSupported\|hasAttributes\|getAttribute\|setAttribute\|removeAttribute\|getAttributeNode\|setAttributeNode\|removeAttributeNode\|getElementsByTagName\|getAttributeNS\|setAttributeNS\|removeAttributeNS\|getAttributeNodeNS\|setAttributeNodeNS\|getElementsByTagNameNS\|hasAttribute\|hasAttributeNS\)\>/ nextgroup=typescriptParen skipwhite
"}}}
" HTML things"{{{
syntax match typescriptHtmlElemAttrs contained /\%(className\|clientHeight\|clientLeft\|clientTop\|clientWidth\|dir\|id\|innerHTML\|lang\|length\|offsetHeight\|offsetLeft\|offsetParent\|offsetTop\|offsetWidth\|scrollHeight\|scrollLeft\|scrollTop\|scrollWidth\|style\|tabIndex\|title\)\>/
syntax match typescriptHtmlElemFuncs contained /\%(blur\|click\|focus\|scrollIntoView\|addEventListener\|dispatchEvent\|removeEventListener\|item\)\>/ nextgroup=typescriptParen skipwhite
"}}}
" CSS Styles in typescript"{{{
syntax keyword typescriptCssStyles contained color font fontFamily fontSize fontSizeAdjust fontStretch fontStyle fontVariant fontWeight letterSpacing lineBreak lineHeight quotes rubyAlign rubyOverhang rubyPosition
syntax keyword typescriptCssStyles contained textAlign textAlignLast textAutospace textDecoration textIndent textJustify textJustifyTrim textKashidaSpace textOverflowW6 textShadow textTransform textUnderlinePosition
syntax keyword typescriptCssStyles contained unicodeBidi whiteSpace wordBreak wordSpacing wordWrap writingMode
syntax keyword typescriptCssStyles contained bottom height left position right top width zIndex
syntax keyword typescriptCssStyles contained border borderBottom borderLeft borderRight borderTop borderBottomColor borderLeftColor borderTopColor borderBottomStyle borderLeftStyle borderRightStyle borderTopStyle borderBottomWidth borderLeftWidth borderRightWidth borderTopWidth borderColor borderStyle borderWidth borderCollapse borderSpacing captionSide emptyCells tableLayout
syntax keyword typescriptCssStyles contained margin marginBottom marginLeft marginRight marginTop outline outlineColor outlineStyle outlineWidth padding paddingBottom paddingLeft paddingRight paddingTop
syntax keyword typescriptCssStyles contained listStyle listStyleImage listStylePosition listStyleType
syntax keyword typescriptCssStyles contained background backgroundAttachment backgroundColor backgroundImage backgroundPosition backgroundPositionX backgroundPositionY backgroundRepeat
syntax keyword typescriptCssStyles contained clear clip clipBottom clipLeft clipRight clipTop content counterIncrement counterReset cssFloat cursor direction display filter layoutGrid layoutGridChar layoutGridLine layoutGridMode layoutGridType
syntax keyword typescriptCssStyles contained marks maxHeight maxWidth minHeight minWidth opacity MozOpacity overflow overflowX overflowY verticalAlign visibility zoom cssText
syntax keyword typescriptCssStyles contained scrollbar3dLightColor scrollbarArrowColor scrollbarBaseColor scrollbarDarkShadowColor scrollbarFaceColor scrollbarHighlightColor scrollbarShadowColor scrollbarTrackColor
"}}}
endif "DOM/HTML/CSS
" Highlight ways"{{{
syntax match typescriptDotNotation "\." nextgroup=typescriptPrototype,typescriptDomElemAttrs,typescriptDomElemFuncs,typescriptDOMMethods,typescriptDOMProperties,typescriptHtmlElemAttrs,typescriptHtmlElemFuncs,typescriptHtmlElemProperties,typescriptAjaxProperties,typescriptAjaxMethods,typescriptPropietaryMethods,typescriptEventListenerMethods skipwhite skipnl
syntax match typescriptDotNotation "\.style\." nextgroup=typescriptCssStyles
"}}}
"" end DOM/HTML/CSS specified things""}}}
"" Code blocks
syntax cluster typescriptAll contains=typescriptComment,typescriptLineComment,typescriptDocComment,typescriptStringD,typescriptStringS,typescriptStringB,typescriptRegexpString,typescriptNumber,typescriptFloat,typescriptDecorators,typescriptLabel,typescriptSource,typescriptType,typescriptOperator,typescriptBoolean,typescriptNull,typescriptFuncKeyword,typescriptConditional,typescriptGlobal,typescriptRepeat,typescriptBranch,typescriptStatement,typescriptGlobalObjects,typescriptMessage,typescriptIdentifier,typescriptStorageClass,typescriptExceptions,typescriptReserved,typescriptDeprecated,typescriptDomErrNo,typescriptDomNodeConsts,typescriptHtmlEvents,typescriptDotNotation,typescriptBrowserObjects,typescriptDOMObjects,typescriptAjaxObjects,typescriptPropietaryObjects,typescriptDOMMethods,typescriptHtmlElemProperties,typescriptDOMProperties,typescriptEventListenerKeywords,typescriptEventListenerMethods,typescriptAjaxProperties,typescriptAjaxMethods,typescriptFuncArg
if main_syntax == "typescript"
syntax sync clear
syntax sync ccomment typescriptComment minlines=200
" syntax sync match typescriptHighlight grouphere typescriptBlock /{/
endif
syntax keyword typescriptFuncKeyword function
"syntax region typescriptFuncDef start="function" end="\(.*\)" contains=typescriptFuncKeyword,typescriptFuncArg keepend
"syntax match typescriptFuncArg "\(([^()]*)\)" contains=typescriptParens,typescriptFuncComma contained
"syntax match typescriptFuncComma /,/ contained
" syntax region typescriptFuncBlock contained matchgroup=typescriptFuncBlock start="{" end="}" contains=@typescriptAll,typescriptParensErrA,typescriptParensErrB,typescriptParen,typescriptBracket,typescriptBlock fold
syn match typescriptBraces "[{}\[\]]"
syn match typescriptParens "[()]"
syn match typescriptOpSymbols "=\{1,3}\|!==\|!=\|<\|>\|>=\|<=\|++\|+=\|--\|-="
syn match typescriptEndColons "[;,]"
syn match typescriptLogicSymbols "\(&&\)\|\(||\)\|\(!\)"
" typescriptFold Function {{{
" function! typescriptFold()
" skip curly braces inside RegEx's and comments
syn region foldBraces start=/{/ skip=/\(\/\/.*\)\|\(\/.*\/\)/ end=/}/ transparent fold keepend extend
" setl foldtext=FoldText()
" endfunction
" au FileType typescript call typescriptFold()
" }}}
" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
" For version 5.8 and later: only when an item doesn't have highlighting yet
if version >= 508 || !exists("did_typescript_syn_inits")
if version < 508
let did_typescript_syn_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
"typescript highlighting
HiLink typescriptParameters Operator
HiLink typescriptSuperBlock Operator
HiLink typescriptEndColons Exception
HiLink typescriptOpSymbols Operator
HiLink typescriptLogicSymbols Boolean
HiLink typescriptBraces Function
HiLink typescriptParens Operator
HiLink typescriptComment Comment
HiLink typescriptLineComment Comment
HiLink typescriptRefComment Include
HiLink typescriptRefS String
HiLink typescriptRefD String
HiLink typescriptDocComment Comment
HiLink typescriptCommentTodo Todo
HiLink typescriptCvsTag Function
HiLink typescriptDocTags Special
HiLink typescriptDocSeeTag Function
HiLink typescriptDocParam Function
HiLink typescriptStringS String
HiLink typescriptStringD String
HiLink typescriptStringB String
HiLink typescriptInterpolationDelimiter Delimiter
HiLink typescriptRegexpString String
HiLink typescriptGlobal Constant
HiLink typescriptCharacter Character
HiLink typescriptPrototype Type
HiLink typescriptConditional Conditional
HiLink typescriptBranch Conditional
HiLink typescriptIdentifier Identifier
HiLink typescriptStorageClass StorageClass
HiLink typescriptRepeat Repeat
HiLink typescriptStatement Statement
HiLink typescriptFuncKeyword Function
HiLink typescriptMessage Keyword
HiLink typescriptDeprecated Exception
HiLink typescriptError Error
HiLink typescriptParensError Error
HiLink typescriptParensErrA Error
HiLink typescriptParensErrB Error
HiLink typescriptParensErrC Error
HiLink typescriptReserved Keyword
HiLink typescriptOperator Operator
HiLink typescriptType Type
HiLink typescriptNull Type
HiLink typescriptNumber Number
HiLink typescriptFloat Number
HiLink typescriptDecorators Special
HiLink typescriptBoolean Boolean
HiLink typescriptLabel Label
HiLink typescriptSpecial Special
HiLink typescriptSource Special
HiLink typescriptGlobalObjects Special
HiLink typescriptExceptions Special
HiLink typescriptDomErrNo Constant
HiLink typescriptDomNodeConsts Constant
HiLink typescriptDomElemAttrs Label
HiLink typescriptDomElemFuncs PreProc
HiLink typescriptHtmlElemAttrs Label
HiLink typescriptHtmlElemFuncs PreProc
HiLink typescriptCssStyles Label
" Ajax Highlighting
HiLink typescriptBrowserObjects Constant
HiLink typescriptDOMObjects Constant
HiLink typescriptDOMMethods Function
HiLink typescriptDOMProperties Special
HiLink typescriptAjaxObjects Constant
HiLink typescriptAjaxMethods Function
HiLink typescriptAjaxProperties Special
HiLink typescriptFuncDef Title
HiLink typescriptFuncArg Special
HiLink typescriptFuncComma Operator
HiLink typescriptHtmlEvents Special
HiLink typescriptHtmlElemProperties Special
HiLink typescriptEventListenerKeywords Keyword
HiLink typescriptNumber Number
HiLink typescriptPropietaryObjects Constant
delcommand HiLink
endif
" Define the htmltypescript for HTML syntax html.vim
"syntax clear htmltypescript
"syntax clear typescriptExpression
syntax cluster htmltypescript contains=@typescriptAll,typescriptBracket,typescriptParen,typescriptBlock,typescriptParenError
syntax cluster typescriptExpression contains=@typescriptAll,typescriptBracket,typescriptParen,typescriptBlock,typescriptParenError,@htmlPreproc
let b:current_syntax = "typescript"
if main_syntax == 'typescript'
unlet main_syntax
endif
" vim: ts=4

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

View file

@ -0,0 +1,13 @@
*Requisite minimal reproducible example, formatted as plain text :*
<hr>
#### Optional: concerning jsx.
PLEASE PLEASE PLEASE make sure you have properly
setup and are sourcing this plugin https://github.com/mxw/vim-jsx
WE DO NOT support JSX automatically, you need another plugin to add get this
functionality.
Make sure the bug still exists if you disable all other javascript plugins
except the one noted above, mxw/vim-jsx

View file

@ -0,0 +1,125 @@
# vim-javascript
JavaScript bundle for vim, this bundle provides syntax highlighting and
improved indentation.
## Installation
### Install with native package manager
git clone https://github.com/pangloss/vim-javascript.git ~/.vim/pack/vim-javascript/start/vim-javascript
since Vim 8.
### Install with [pathogen](https://github.com/tpope/vim-pathogen)
git clone https://github.com/pangloss/vim-javascript.git ~/.vim/bundle/vim-javascript
alternatively, use a package manager like [vim-plug](https://github.com/junegunn/vim-plug)
## Configuration Variables
The following variables control certain syntax highlighting plugins. You can
add them to your `.vimrc` to enable their features.
-----------------
```
let g:javascript_plugin_jsdoc = 1
```
Enables syntax highlighting for [JSDocs](http://usejsdoc.org/).
Default Value: 0
-----------------
```
let g:javascript_plugin_ngdoc = 1
```
Enables some additional syntax highlighting for NGDocs. Requires JSDoc plugin
to be enabled as well.
Default Value: 0
-----------------
```
let g:javascript_plugin_flow = 1
```
Enables syntax highlighting for [Flow](https://flowtype.org/).
Default Value: 0
-----------------
```vim
augroup javascript_folding
au!
au FileType javascript setlocal foldmethod=syntax
augroup END
```
Enables code folding for javascript based on our syntax file.
Please note this can have a dramatic effect on performance.
## Concealing Characters
You can customize concealing characters, if your font provides the glyph you want, by defining one or more of the following
variables:
let g:javascript_conceal_function = "ƒ"
let g:javascript_conceal_null = "ø"
let g:javascript_conceal_this = "@"
let g:javascript_conceal_return = "⇚"
let g:javascript_conceal_undefined = "¿"
let g:javascript_conceal_NaN = ""
let g:javascript_conceal_prototype = "¶"
let g:javascript_conceal_static = "•"
let g:javascript_conceal_super = "Ω"
let g:javascript_conceal_arrow_function = "⇒"
let g:javascript_conceal_noarg_arrow_function = "🞅"
let g:javascript_conceal_underscore_arrow_function = "🞅"
You can enable concealing within VIM with:
set conceallevel=1
OR if you wish to toggle concealing you may wish to bind a command such as the following which will map `<LEADER>l` (leader is usually the `\` key) to toggling conceal mode:
map <leader>l :exec &conceallevel ? "set conceallevel=0" : "set conceallevel=1"<CR>
## Indentation Specific
* `:h cino-:`
* `:h cino-=`
* `:h cino-star`
* `:h cino-(`
* `:h cino-w`
* `:h cino-W`
* `:h cino-U`
* `:h cino-m`
* `:h cino-M`
* `:h 'indentkeys'`
## Contributing
Please follow the general code style
guides (read the code) and in your pull request explain the reason for the
proposed change and how it is valuable. All p.r.'s will be reviewed by a
maintainer(s) then, hopefully, merged.
Thank you!
## License
Distributed under the same terms as Vim itself. See `:help license`.

View file

@ -0,0 +1,12 @@
" Vim filetype plugin file
" Language: JavaScript
" Maintainer: vim-javascript community
" URL: https://github.com/pangloss/vim-javascript
setlocal iskeyword+=$ suffixesadd+=.js
if exists('b:undo_ftplugin')
let b:undo_ftplugin .= ' | setlocal iskeyword< suffixesadd<'
else
let b:undo_ftplugin = 'setlocal iskeyword< suffixesadd<'
endif

View file

@ -0,0 +1,16 @@
" Vim compiler plugin
" Language: JavaScript
" Maintainer: vim-javascript community
" URL: https://github.com/pangloss/vim-javascript
if exists("current_compiler")
finish
endif
let current_compiler = "eslint"
if exists(":CompilerSet") != 2
command! -nargs=* CompilerSet setlocal <args>
endif
CompilerSet makeprg=eslint\ -f\ compact\ %
CompilerSet errorformat=%f:\ line\ %l\\,\ col\ %c\\,\ %m

View file

@ -0,0 +1,8 @@
--langdef=js
--langmap=js:.js
--regex-js=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\{/\1/,object/
--regex-js=/([A-Za-z0-9._$()]+)[ \t]*[:=][ \t]*function[ \t]*\(/\1/,function/
--regex-js=/function[ \t]+([A-Za-z0-9._$]+)[ \t]*([^)])/\1/,function/
--regex-js=/([A-Za-z0-9._$]+)[ \t]*[:=][ \t]*\[/\1/,array/
--regex-js=/([^= ]+)[ \t]*=[ \t]*[^"]'[^']*/\1/,string/
--regex-js=/([^= ]+)[ \t]*=[ \t]*[^']"[^"]*/\1/,string/

View file

@ -0,0 +1,109 @@
syntax region jsFlowDefinition contained start=/:/ end=/\%(\s*[,=;)\n]\)\@=/ contains=@jsFlowCluster containedin=jsParen
syntax region jsFlowArgumentDef contained start=/:/ end=/\%(\s*[,)]\|=>\@!\)\@=/ contains=@jsFlowCluster
syntax region jsFlowArray contained matchgroup=jsFlowNoise start=/\[/ end=/\]/ contains=@jsFlowCluster,jsComment fold
syntax region jsFlowObject contained matchgroup=jsFlowNoise start=/{/ end=/}/ contains=@jsFlowCluster,jsComment fold
syntax region jsFlowExactObject contained matchgroup=jsFlowNoise start=/{|/ end=/|}/ contains=@jsFlowCluster,jsComment fold
syntax region jsFlowParens contained matchgroup=jsFlowNoise start=/(/ end=/)/ contains=@jsFlowCluster nextgroup=jsFlowArrow skipwhite keepend extend fold
syntax match jsFlowNoise contained /[:;,<>]/
syntax keyword jsFlowType contained boolean number string null void any mixed JSON array Function object array bool class
syntax keyword jsFlowTypeof contained typeof skipempty skipwhite nextgroup=jsFlowTypeCustom,jsFlowType
syntax match jsFlowTypeCustom contained /[0-9a-zA-Z_.]*/ skipwhite skipempty nextgroup=jsFlowGeneric
syntax region jsFlowGeneric matchgroup=jsFlowNoise start=/\k\@<=</ end=/>/ contains=@jsFlowCluster containedin=@jsExpression,jsFlowDeclareBlock
syntax region jsFlowGeneric contained matchgroup=jsFlowNoise start=/</ end=/>(\@=/ oneline contains=@jsFlowCluster containedin=@jsExpression,jsFlowDeclareBlock
syntax region jsFlowObjectGeneric contained matchgroup=jsFlowNoise start=/\k\@<=</ end=/>/ contains=@jsFlowCluster nextgroup=jsFuncArgs
syntax match jsFlowArrow contained /=>/ skipwhite skipempty nextgroup=jsFlowType,jsFlowTypeCustom,jsFlowParens
syntax match jsFlowObjectKey contained /[0-9a-zA-Z_$?]*\(\s*:\)\@=/ contains=jsFunctionKey,jsFlowMaybe skipwhite skipempty nextgroup=jsObjectValue containedin=jsObject
syntax match jsFlowOrOperator contained /|/ skipwhite skipempty nextgroup=@jsFlowCluster
syntax keyword jsFlowImportType contained type typeof skipwhite skipempty nextgroup=jsModuleAsterisk,jsModuleKeyword,jsModuleGroup
syntax match jsFlowWildcard contained /*/
syntax match jsFlowReturn contained /:\s*/ contains=jsFlowNoise skipwhite skipempty nextgroup=@jsFlowReturnCluster,jsFlowArrow,jsFlowReturnParens
syntax region jsFlowReturnObject contained matchgroup=jsFlowNoise start=/{/ end=/}/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsFuncBlock,jsFlowReturnOrOp extend fold
syntax region jsFlowReturnArray contained matchgroup=jsFlowNoise start=/\[/ end=/\]/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsFuncBlock,jsFlowReturnOrOp fold
syntax region jsFlowReturnParens contained matchgroup=jsFlowNoise start=/(/ end=/)/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsFuncBlock,jsFlowReturnOrOp,jsFlowReturnArrow fold
syntax match jsFlowReturnArrow contained /=>/ skipwhite skipempty nextgroup=@jsFlowReturnCluster
syntax match jsFlowReturnKeyword contained /\k\+/ contains=jsFlowType,jsFlowTypeCustom skipwhite skipempty nextgroup=jsFlowReturnGroup,jsFuncBlock,jsFlowReturnOrOp,jsFlowReturnArray
syntax match jsFlowReturnMaybe contained /?/ skipwhite skipempty nextgroup=jsFlowReturnKeyword,jsFlowReturnObject,jsFlowReturnParens
syntax region jsFlowReturnGroup contained matchgroup=jsFlowNoise start=/</ end=/>/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsFuncBlock,jsFlowReturnOrOp
syntax match jsFlowReturnOrOp contained /\s*|\s*/ skipwhite skipempty nextgroup=@jsFlowReturnCluster
syntax match jsFlowWildcardReturn contained /*/ skipwhite skipempty nextgroup=jsFuncBlock
syntax keyword jsFlowTypeofReturn contained typeof skipempty skipwhite nextgroup=@jsFlowReturnCluster
syntax region jsFlowFunctionGroup contained matchgroup=jsFlowNoise start=/</ end=/>/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsFuncArgs
syntax region jsFlowClassGroup contained matchgroup=jsFlowNoise start=/</ end=/>/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsClassBlock
syntax region jsFlowClassFunctionGroup contained matchgroup=jsFlowNoise start=/</ end=/>/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsFuncArgs
syntax match jsFlowObjectFuncName contained /\<\K\k*<\@=/ skipwhite skipempty nextgroup=jsFlowObjectGeneric containedin=jsObject
syntax region jsFlowTypeStatement start=/\(opaque\s\+\)\?type\%(\s\+\k\)\@=/ end=/=\@=/ contains=jsFlowTypeOperator oneline skipwhite skipempty nextgroup=jsFlowTypeValue keepend
syntax region jsFlowTypeValue contained matchgroup=jsFlowNoise start=/=/ end=/\%(;\|\n\%(\s*|\)\@!\)/ contains=@jsFlowCluster,jsFlowGeneric,jsFlowMaybe
syntax match jsFlowTypeOperator contained /=/ containedin=jsFlowTypeValue
syntax match jsFlowTypeOperator contained /=/
syntax keyword jsFlowTypeKeyword contained type
syntax keyword jsFlowDeclare declare skipwhite skipempty nextgroup=jsFlowTypeStatement,jsClassDefinition,jsStorageClass,jsFlowModule,jsFlowInterface
syntax match jsFlowClassProperty contained /\<[0-9a-zA-Z_$]*\>:\@=/ skipwhite skipempty nextgroup=jsFlowClassDef containedin=jsClassBlock
syntax region jsFlowClassDef contained start=/:/ end=/\%(\s*[,=;)\n]\)\@=/ contains=@jsFlowCluster skipwhite skipempty nextgroup=jsClassValue
syntax region jsFlowModule contained start=/module/ end=/\%({\|:\)\@=/ skipempty skipwhite nextgroup=jsFlowDeclareBlock contains=jsString
syntax region jsFlowInterface contained start=/interface/ end=/{\@=/ skipempty skipwhite nextgroup=jsFlowInterfaceBlock contains=@jsFlowCluster
syntax region jsFlowDeclareBlock contained matchgroup=jsFlowNoise start=/{/ end=/}/ contains=jsFlowDeclare,jsFlowNoise fold
syntax match jsFlowMaybe contained /?/
syntax region jsFlowInterfaceBlock contained matchgroup=jsFlowNoise start=/{/ end=/}/ contains=jsObjectKey,jsObjectKeyString,jsObjectKeyComputed,jsObjectSeparator,jsObjectFuncName,jsFlowObjectFuncName,jsObjectMethodType,jsGenerator,jsComment,jsObjectStringKey,jsSpreadExpression,jsFlowNoise,jsFlowParens,jsFlowGeneric keepend fold
syntax region jsFlowParenAnnotation contained start=/:/ end=/[,=)]\@=/ containedin=jsParen contains=@jsFlowCluster
syntax cluster jsFlowReturnCluster contains=jsFlowNoise,jsFlowReturnObject,jsFlowReturnArray,jsFlowReturnKeyword,jsFlowReturnGroup,jsFlowReturnMaybe,jsFlowReturnOrOp,jsFlowWildcardReturn,jsFlowReturnArrow,jsFlowTypeofReturn
syntax cluster jsFlowCluster contains=jsFlowArray,jsFlowObject,jsFlowExactObject,jsFlowNoise,jsFlowTypeof,jsFlowType,jsFlowGeneric,jsFlowMaybe,jsFlowParens,jsFlowOrOperator,jsFlowWildcard
if version >= 508 || !exists("did_javascript_syn_inits")
if version < 508
let did_javascript_syn_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink jsFlowDefinition PreProc
HiLink jsFlowClassDef jsFlowDefinition
HiLink jsFlowArgumentDef jsFlowDefinition
HiLink jsFlowType Type
HiLink jsFlowTypeCustom PreProc
HiLink jsFlowTypeof PreProc
HiLink jsFlowTypeofReturn PreProc
HiLink jsFlowArray PreProc
HiLink jsFlowObject PreProc
HiLink jsFlowExactObject PreProc
HiLink jsFlowParens PreProc
HiLink jsFlowGeneric PreProc
HiLink jsFlowObjectGeneric jsFlowGeneric
HiLink jsFlowReturn PreProc
HiLink jsFlowParenAnnotation PreProc
HiLink jsFlowReturnObject jsFlowReturn
HiLink jsFlowReturnArray jsFlowArray
HiLink jsFlowReturnParens jsFlowParens
HiLink jsFlowReturnGroup jsFlowGeneric
HiLink jsFlowFunctionGroup PreProc
HiLink jsFlowClassGroup PreProc
HiLink jsFlowClassFunctionGroup PreProc
HiLink jsFlowArrow PreProc
HiLink jsFlowReturnArrow PreProc
HiLink jsFlowTypeStatement PreProc
HiLink jsFlowTypeKeyword PreProc
HiLink jsFlowTypeOperator Operator
HiLink jsFlowMaybe PreProc
HiLink jsFlowReturnMaybe PreProc
HiLink jsFlowClassProperty jsClassProperty
HiLink jsFlowDeclare PreProc
HiLink jsFlowModule PreProc
HiLink jsFlowInterface PreProc
HiLink jsFlowNoise Noise
HiLink jsFlowObjectKey jsObjectKey
HiLink jsFlowOrOperator jsOperator
HiLink jsFlowReturnOrOp jsFlowOrOperator
HiLink jsFlowWildcard PreProc
HiLink jsFlowWildcardReturn PreProc
HiLink jsFlowImportType PreProc
HiLink jsFlowTypeValue PreProc
HiLink jsFlowObjectFuncName jsObjectFuncName
delcommand HiLink
endif

View file

@ -0,0 +1,39 @@
"" syntax coloring for javadoc comments (HTML)
syntax region jsComment matchgroup=jsComment start="/\*\s*" end="\*/" contains=jsDocTags,jsCommentTodo,jsCvsTag,@jsHtml,@Spell fold
" tags containing a param
syntax match jsDocTags contained "@\(alias\|api\|augments\|borrows\|class\|constructs\|default\|defaultvalue\|emits\|exception\|exports\|extends\|fires\|kind\|link\|listens\|member\|member[oO]f\|mixes\|module\|name\|namespace\|requires\|template\|throws\|var\|variation\|version\)\>" skipwhite nextgroup=jsDocParam
" tags containing type and param
syntax match jsDocTags contained "@\(arg\|argument\|cfg\|param\|property\|prop\|typedef\)\>" skipwhite nextgroup=jsDocType
" tags containing type but no param
syntax match jsDocTags contained "@\(callback\|define\|enum\|external\|implements\|this\|type\|return\|returns\|yields\)\>" skipwhite nextgroup=jsDocTypeNoParam
" tags containing references
syntax match jsDocTags contained "@\(lends\|see\|tutorial\)\>" skipwhite nextgroup=jsDocSeeTag
" other tags (no extra syntax)
syntax match jsDocTags contained "@\(abstract\|access\|accessor\|async\|author\|classdesc\|constant\|const\|constructor\|copyright\|deprecated\|desc\|description\|dict\|event\|example\|file\|file[oO]verview\|final\|function\|global\|ignore\|inherit[dD]oc\|inner\|instance\|interface\|license\|localdoc\|method\|mixin\|nosideeffects\|override\|overview\|preserve\|private\|protected\|public\|readonly\|since\|static\|struct\|todo\|summary\|undocumented\|virtual\)\>"
syntax region jsDocType contained matchgroup=jsDocTypeBrackets start="{" end="}" contains=jsDocTypeRecord oneline skipwhite nextgroup=jsDocParam
syntax match jsDocType contained "\%(#\|\"\|\w\|\.\|:\|\/\)\+" skipwhite nextgroup=jsDocParam
syntax region jsDocTypeRecord contained start=/{/ end=/}/ contains=jsDocTypeRecord extend
syntax region jsDocTypeRecord contained start=/\[/ end=/\]/ contains=jsDocTypeRecord extend
syntax region jsDocTypeNoParam contained start="{" end="}" oneline
syntax match jsDocTypeNoParam contained "\%(#\|\"\|\w\|\.\|:\|\/\)\+"
syntax match jsDocParam contained "\%(#\|\$\|-\|'\|\"\|{.\{-}}\|\w\|\.\|:\|\/\|\[.\{-}]\|=\)\+"
syntax region jsDocSeeTag contained matchgroup=jsDocSeeTag start="{" end="}" contains=jsDocTags
if version >= 508 || !exists("did_javascript_syn_inits")
if version < 508
let did_javascript_syn_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink jsDocTags Special
HiLink jsDocSeeTag Function
HiLink jsDocType Type
HiLink jsDocTypeBrackets jsDocType
HiLink jsDocTypeRecord jsDocType
HiLink jsDocTypeNoParam Type
HiLink jsDocParam Label
delcommand HiLink
endif

View file

@ -0,0 +1,3 @@
syntax match jsDocTags contained /@\(link\|method[oO]f\|ngdoc\|ng[iI]nject\|restrict\)/ nextgroup=jsDocParam skipwhite
syntax match jsDocType contained "\%(#\|\$\|\w\|\"\|-\|\.\|:\|\/\)\+" nextgroup=jsDocParam skipwhite
syntax match jsDocParam contained "\%(#\|\$\|\w\|\"\|-\|\.\|:\|{\|}\|\/\|\[\|]\|=\)\+"

View file

@ -0,0 +1 @@
autocmd BufNewFile,BufRead *.flow setfiletype flow

View file

@ -0,0 +1,8 @@
fun! s:SelectJavascript()
if getline(1) =~# '^#!.*/bin/\%(env\s\+\)\?node\>'
set ft=javascript
endif
endfun
autocmd BufNewFile,BufRead *.{js,mjs,jsm,es,es6},Jakefile setfiletype javascript
autocmd BufNewFile,BufRead * call s:SelectJavascript()

View file

@ -0,0 +1,477 @@
" Vim indent file
" Language: Javascript
" Maintainer: Chris Paul ( https://github.com/bounceme )
" URL: https://github.com/pangloss/vim-javascript
" Last Change: December 4, 2017
" Only load this indent file when no other was loaded.
if exists('b:did_indent')
finish
endif
let b:did_indent = 1
" Now, set up our indentation expression and keys that trigger it.
setlocal indentexpr=GetJavascriptIndent()
setlocal autoindent nolisp nosmartindent
setlocal indentkeys+=0],0)
" Testable with something like:
" vim -eNs "+filetype plugin indent on" "+syntax on" "+set ft=javascript" \
" "+norm! gg=G" '+%print' '+:q!' testfile.js \
" | diff -uBZ testfile.js -
let b:undo_indent = 'setlocal indentexpr< smartindent< autoindent< indentkeys<'
" Only define the function once.
if exists('*GetJavascriptIndent')
finish
endif
let s:cpo_save = &cpo
set cpo&vim
" indent correctly if inside <script>
" vim/vim@690afe1 for the switch from cindent
" overridden with b:html_indent_script1
call extend(g:,{'html_indent_script1': 'inc'},'keep')
" Regex of syntax group names that are or delimit string or are comments.
let s:bvars = {
\ 'syng_strcom': 'string\|comment\|regex\|special\|doc\|template\%(braces\)\@!',
\ 'syng_str': 'string\|template\|special' }
" template strings may want to be excluded when editing graphql:
" au! Filetype javascript let b:syng_str = '^\%(.*template\)\@!.*string\|special'
" au! Filetype javascript let b:syng_strcom = '^\%(.*template\)\@!.*string\|comment\|regex\|special\|doc'
function s:GetVars()
call extend(b:,extend(s:bvars,{'js_cache': [0,0,0]}),'keep')
endfunction
" Get shiftwidth value
if exists('*shiftwidth')
function s:sw()
return shiftwidth()
endfunction
else
function s:sw()
return &l:shiftwidth ? &l:shiftwidth : &l:tabstop
endfunction
endif
" Performance for forwards search(): start search at pos rather than masking
" matches before pos.
let s:z = has('patch-7.4.984') ? 'z' : ''
" Expression used to check whether we should skip a match with searchpair().
let s:skip_expr = "s:SynAt(line('.'),col('.')) =~? b:syng_strcom"
let s:in_comm = s:skip_expr[:-14] . "'comment\\|doc'"
let s:rel = has('reltime')
" searchpair() wrapper
if s:rel
function s:GetPair(start,end,flags,skip)
return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1,a:skip ==# 's:SkipFunc()' ? 2000 : 200)
endfunction
else
function s:GetPair(start,end,flags,skip)
return searchpair('\m'.a:start,'','\m'.a:end,a:flags,a:skip,s:l1)
endfunction
endif
function s:SynAt(l,c)
let byte = line2byte(a:l) + a:c - 1
let pos = index(s:synid_cache[0], byte)
if pos == -1
let s:synid_cache[:] += [[byte], [synIDattr(synID(a:l, a:c, 0), 'name')]]
endif
return s:synid_cache[1][pos]
endfunction
function s:ParseCino(f)
let [s, n, divider] = [strridx(&cino, a:f)+1, '', 0]
while s && &cino[ s ] =~ '[^,]'
if &cino[ s ] == '.'
let divider = 1
elseif &cino[ s ] ==# 's'
if n !~ '\d'
return n . s:sw() + 0
endif
let n = str2nr(n) * s:sw()
break
else
let [n, divider] .= [&cino[ s ], 0]
endif
let s += 1
endwhile
return str2nr(n) / max([divider, 1])
endfunction
" Optimized {skip} expr, only callable from the search loop which
" GetJavascriptIndent does to find the containing [[{(] (side-effects)
function s:SkipFunc()
if s:top_col == 1
throw 'out of bounds'
elseif s:check_in
if eval(s:skip_expr)
return 1
endif
let s:check_in = 0
elseif getline('.') =~ '\%<'.col('.').'c\/.\{-}\/\|\%>'.col('.').'c[''"]\|\\$'
if eval(s:skip_expr)
return 1
endif
elseif search('\m`\|\${\|\*\/','nW'.s:z,s:looksyn)
if eval(s:skip_expr)
let s:check_in = 1
return 1
endif
else
let s:synid_cache[:] += [[line2byte('.') + col('.') - 1], ['']]
endif
let [s:looksyn, s:top_col] = getpos('.')[1:2]
endfunction
function s:AlternatePair()
let [pat, l:for] = ['[][(){};]', 2]
while s:SearchLoop(pat,'bW','s:SkipFunc()')
if s:LookingAt() == ';'
if !l:for
if s:GetPair('{','}','bW','s:SkipFunc()')
return
endif
break
else
let [pat, l:for] = ['[{}();]', l:for - 1]
endif
else
let idx = stridx('])}',s:LookingAt())
if idx == -1
return
elseif !s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()')
break
endif
endif
endwhile
throw 'out of bounds'
endfunction
function s:Nat(int)
return a:int * (a:int > 0)
endfunction
function s:LookingAt()
return getline('.')[col('.')-1]
endfunction
function s:Token()
return s:LookingAt() =~ '\k' ? expand('<cword>') : s:LookingAt()
endfunction
function s:PreviousToken(...)
let [l:pos, tok] = [getpos('.'), '']
if search('\m\k\{1,}\|\S','ebW')
if getline('.')[col('.')-2:col('.')-1] == '*/'
if eval(s:in_comm) && !s:SearchLoop('\S\ze\_s*\/[/*]','bW',s:in_comm)
call setpos('.',l:pos)
else
let tok = s:Token()
endif
else
let two = a:0 || line('.') != l:pos[1] ? strridx(getline('.')[:col('.')],'//') + 1 : 0
if two && eval(s:in_comm)
call cursor(0,two)
let tok = s:PreviousToken(1)
if tok is ''
call setpos('.',l:pos)
endif
else
let tok = s:Token()
endif
endif
endif
return tok
endfunction
function s:Pure(f,...)
return eval("[call(a:f,a:000),cursor(a:firstline,".col('.').")][0]")
endfunction
function s:SearchLoop(pat,flags,expr)
return s:GetPair(a:pat,'\_$.',a:flags,a:expr)
endfunction
function s:ExprCol()
if getline('.')[col('.')-2] == ':'
return 1
endif
let bal = 0
while s:SearchLoop('[{}?:]','bW',s:skip_expr)
if s:LookingAt() == ':'
let bal -= !search('\m:\%#','bW')
elseif s:LookingAt() == '?'
if getline('.')[col('.'):col('.')+1] =~ '^\.\d\@!'
" ?. conditional chain, not ternary start
elseif !bal
return 1
else
let bal += 1
endif
elseif s:LookingAt() == '{'
return !s:IsBlock()
elseif !s:GetPair('{','}','bW',s:skip_expr)
break
endif
endwhile
endfunction
" configurable regexes that define continuation lines, not including (, {, or [.
let s:opfirst = '^' . get(g:,'javascript_opfirst',
\ '\C\%([<>=,.?^%|/&]\|\([-:+]\)\1\@!\|\*\+\|!=\|in\%(stanceof\)\=\>\)')
let s:continuation = get(g:,'javascript_continuation',
\ '\C\%([<=,.~!?/*^%|&:]\|+\@<!+\|-\@<!-\|=\@<!>\|\<\%(typeof\|new\|delete\|void\|in\|instanceof\|await\)\)') . '$'
function s:Continues()
let tok = matchstr(strpart(getline('.'),col('.')-15,15),s:continuation)
if tok =~ '[a-z:]'
return tok == ':' ? s:ExprCol() : s:PreviousToken() != '.'
elseif tok !~ '[/>]'
return tok isnot ''
endif
return s:SynAt(line('.'),col('.')) !~? (tok == '>' ? 'jsflow\|^html' : 'regex')
endfunction
" Check if line 'lnum' has a balanced amount of parentheses.
function s:Balanced(lnum,line)
let l:open = 0
let pos = match(a:line, '[][(){}]')
while pos != -1
if s:SynAt(a:lnum,pos + 1) !~? b:syng_strcom
let l:open += matchend(a:line[pos],'[[({]')
if l:open < 0
return
endif
endif
let pos = match(a:line, !l:open ? '[][(){}]' : '()' =~ a:line[pos] ?
\ '[()]' : '{}' =~ a:line[pos] ? '[{}]' : '[][]', pos + 1)
endwhile
return !l:open
endfunction
function s:OneScope()
if s:LookingAt() == ')' && s:GetPair('(', ')', 'bW', s:skip_expr)
let tok = s:PreviousToken()
return (count(split('for if let while with'),tok) ||
\ tok =~# '^await$\|^each$' && s:PreviousToken() ==# 'for') &&
\ s:Pure('s:PreviousToken') != '.' && !(tok == 'while' && s:DoWhile())
elseif s:Token() =~# '^else$\|^do$'
return s:Pure('s:PreviousToken') != '.'
elseif strpart(getline('.'),col('.')-2,2) == '=>'
call cursor(0,col('.')-1)
return s:PreviousToken() != ')' || s:GetPair('(', ')', 'bW', s:skip_expr)
endif
endfunction
function s:DoWhile()
let cpos = searchpos('\m\<','cbW')
while s:SearchLoop('\C[{}]\|\<\%(do\|while\)\>','bW',s:skip_expr)
if s:LookingAt() =~ '\a'
if s:Pure('s:IsBlock')
if s:LookingAt() ==# 'd'
return 1
endif
break
endif
elseif s:LookingAt() != '}' || !s:GetPair('{','}','bW',s:skip_expr)
break
endif
endwhile
call call('cursor',cpos)
endfunction
" returns total offset from braceless contexts. 'num' is the lineNr which
" encloses the entire context, 'cont' if whether a:firstline is a continued
" expression, which could have started in a braceless context
function s:IsContOne(cont)
let [l:num, pind] = b:js_cache[1] ?
\ [b:js_cache[1], indent(b:js_cache[1]) + s:sw()] : [1,0]
let [ind, b_l] = [indent('.') + !a:cont, 0]
while line('.') > l:num && ind > pind || line('.') == l:num
if indent('.') < ind && s:OneScope()
let b_l += 1
elseif !a:cont || b_l || ind < indent(a:firstline)
break
else
call cursor(0,1)
endif
let ind = min([ind, indent('.')])
if s:PreviousToken() is ''
break
endif
endwhile
return b_l
endfunction
function s:IsSwitch()
return search(printf('\m\C\%%%dl\%%%dc%s',b:js_cache[1],b:js_cache[2],
\ '{\_s*\%(\%(\/\/.*\_$\|\/\*\_.\{-}\*\/\)\@>\_s*\)*\%(case\|default\)\>'),'nW'.s:z)
endfunction
" https://github.com/sweet-js/sweet.js/wiki/design#give-lookbehind-to-the-reader
function s:IsBlock()
let tok = s:PreviousToken()
if join(s:stack) =~? 'xml\|jsx' && s:SynAt(line('.'),col('.')-1) =~? 'xml\|jsx'
let s:in_jsx = 1
return tok != '{'
elseif tok =~ '\k'
if tok ==# 'type'
return s:Pure('eval',"s:PreviousToken() !~# '^\\%(im\\|ex\\)port$' || s:PreviousToken() == '.'")
elseif tok ==# 'of'
return s:Pure('eval',"!s:GetPair('[[({]','[])}]','bW',s:skip_expr) || s:LookingAt() != '(' ||"
\ ."s:{s:PreviousToken() ==# 'await' ? 'Previous' : ''}Token() !=# 'for' || s:PreviousToken() == '.'")
endif
return index(split('return const let import export extends yield default delete var await void typeof throw case new in instanceof')
\ ,tok) < (line('.') != a:firstline) || s:Pure('s:PreviousToken') == '.'
elseif tok == '>'
return getline('.')[col('.')-2] == '=' || s:SynAt(line('.'),col('.')) =~? 'jsflow\|^html'
elseif tok == '*'
return s:Pure('s:PreviousToken') == ':'
elseif tok == ':'
return s:Pure('eval',"s:PreviousToken() =~ '^\\K\\k*$' && !s:ExprCol()")
elseif tok == '/'
return s:SynAt(line('.'),col('.')) =~? 'regex'
elseif tok !~ '[=~!<,.?^%|&([]'
return tok !~ '[-+]' || line('.') != a:firstline && getline('.')[col('.')-2] == tok
endif
endfunction
function GetJavascriptIndent()
call s:GetVars()
let s:synid_cache = [[],[]]
let l:line = getline(v:lnum)
" use synstack as it validates syn state and works in an empty line
let s:stack = [''] + map(synstack(v:lnum,1),"synIDattr(v:val,'name')")
" start with strings,comments,etc.
if s:stack[-1] =~? 'comment\|doc'
if l:line !~ '^\s*\/[/*]'
return l:line =~ '^\s*\*' ? cindent(v:lnum) : -1
endif
elseif s:stack[-1] =~? b:syng_str
if b:js_cache[0] == v:lnum - 1 && s:Balanced(v:lnum-1,getline(v:lnum-1))
let b:js_cache[0] = v:lnum
endif
return -1
endif
let nest = get(get(b:,'hi_indent',{}),'blocklnr')
let s:l1 = max([0, prevnonblank(v:lnum) - (s:rel ? 2000 : 1000), nest])
call cursor(v:lnum,1)
if s:PreviousToken() is ''
return
endif
let [l:lnum, lcol, pline] = getpos('.')[1:2] + [getline('.')[:col('.')-1]]
let l:line = substitute(l:line,'^\s*','','')
let l:line_s = l:line[0]
if l:line[:1] == '/*'
let l:line = substitute(l:line,'^\%(\/\*.\{-}\*\/\s*\)*','','')
endif
if l:line =~ '^\/[/*]'
let l:line = ''
endif
" the containing paren, bracket, or curly. Many hacks for performance
call cursor(v:lnum,1)
let idx = index([']',')','}'],l:line[0])
if b:js_cache[0] > l:lnum && b:js_cache[0] < v:lnum ||
\ b:js_cache[0] == l:lnum && s:Balanced(l:lnum,pline)
call call('cursor',b:js_cache[1:])
else
let [s:looksyn, s:top_col, s:check_in, s:l1] = [v:lnum - 1,0,0,
\ max([s:l1, &smc ? search('\m^.\{'.&smc.',}','nbW',s:l1 + 1) + 1 : 0])]
try
if idx != -1
call s:GetPair(['\[','(','{'][idx],'])}'[idx],'bW','s:SkipFunc()')
elseif getline(v:lnum) !~ '^\S' && s:stack[-1] =~? 'block\|^jsobject$'
if !s:GetPair('{','}','bW','s:SkipFunc()') && s:stack[-1] ==# 'jsObject'
return indent(l:lnum)
endif
else
call s:AlternatePair()
endif
catch /^\Cout of bounds$/
call cursor(v:lnum,1)
endtry
let b:js_cache[1:] = line('.') == v:lnum ? [0,0] : getpos('.')[1:2]
endif
let [b:js_cache[0], num] = [v:lnum, b:js_cache[1]]
let [num_ind, is_op, b_l, l:switch_offset, s:in_jsx] = [s:Nat(indent(num)),0,0,0,0]
if !num || s:LookingAt() == '{' && s:IsBlock()
let ilnum = line('.')
if num && !s:in_jsx && s:LookingAt() == ')' && s:GetPair('(',')','bW',s:skip_expr)
if ilnum == num
let [num, num_ind] = [line('.'), indent('.')]
endif
if idx == -1 && s:PreviousToken() ==# 'switch' && s:IsSwitch()
let l:switch_offset = &cino !~ ':' ? s:sw() : s:ParseCino(':')
if pline[-1:] != '.' && l:line =~# '^\%(default\|case\)\>'
return s:Nat(num_ind + l:switch_offset)
elseif &cino =~ '='
let l:case_offset = s:ParseCino('=')
endif
endif
endif
if idx == -1 && pline[-1:] !~ '[{;]'
call cursor(l:lnum, lcol)
let sol = matchstr(l:line,s:opfirst)
if sol is '' || sol == '/' && s:SynAt(v:lnum,
\ 1 + len(getline(v:lnum)) - len(l:line)) =~? 'regex'
if s:Continues()
let is_op = s:sw()
endif
elseif num && sol =~# '^\%(in\%(stanceof\)\=\|\*\)$' &&
\ s:LookingAt() == '}' && s:GetPair('{','}','bW',s:skip_expr) &&
\ s:PreviousToken() == ')' && s:GetPair('(',')','bW',s:skip_expr) &&
\ (s:PreviousToken() == ']' || s:LookingAt() =~ '\k' &&
\ s:{s:PreviousToken() == '*' ? 'Previous' : ''}Token() !=# 'function')
return num_ind + s:sw()
else
let is_op = s:sw()
endif
call cursor(l:lnum, lcol)
let b_l = s:Nat(s:IsContOne(is_op) - (!is_op && l:line =~ '^{')) * s:sw()
endif
elseif idx == -1 && s:LookingAt() == '(' && &cino =~ '(' &&
\ (search('\m\S','nbW',num) || s:ParseCino('U'))
let pval = s:ParseCino('(')
if !pval
let [Wval, vcol] = [s:ParseCino('W'), virtcol('.')]
if search('\m'.get(g:,'javascript_indent_W_pat','\S'),'W',num)
return s:ParseCino('w') ? vcol : virtcol('.')-1
endif
return Wval ? s:Nat(num_ind + Wval) : vcol
endif
return s:Nat(num_ind + pval + searchpair('\m(','','\m)','nbrmW',s:skip_expr,num) * s:sw())
endif
" main return
if l:line =~ '^[])}]\|^|}'
if l:line_s == ')'
if s:ParseCino('M')
return indent(l:lnum)
elseif num && &cino =~# 'm' && !s:ParseCino('m')
return virtcol('.') - 1
endif
endif
return num_ind
elseif num
return s:Nat(num_ind + get(l:,'case_offset',s:sw()) + l:switch_offset + b_l + is_op)
elseif nest
return indent(nextnonblank(nest+1)) + b_l + is_op
endif
return b_l + is_op
endfunction
let &cpo = s:cpo_save
unlet s:cpo_save

View file

@ -0,0 +1,2 @@
runtime syntax/javascript.vim
runtime extras/flow.vim

View file

@ -0,0 +1,393 @@
" Vim syntax file
" Language: JavaScript
" Maintainer: vim-javascript community
" URL: https://github.com/pangloss/vim-javascript
if !exists("main_syntax")
if version < 600
syntax clear
elseif exists("b:current_syntax")
finish
endif
let main_syntax = 'javascript'
endif
" Dollar sign is permitted anywhere in an identifier
if (v:version > 704 || v:version == 704 && has('patch1142')) && main_syntax == 'javascript'
syntax iskeyword @,48-57,_,192-255,$
else
setlocal iskeyword+=$
endif
syntax sync fromstart
" TODO: Figure out what type of casing I need
" syntax case ignore
syntax case match
syntax match jsNoise /[:,;]/
syntax match jsDot /\./ skipwhite skipempty nextgroup=jsObjectProp,jsFuncCall,jsPrototype,jsTaggedTemplate
syntax match jsObjectProp contained /\<\K\k*/
syntax match jsFuncCall /\<\K\k*\ze\s*(/
syntax match jsParensError /[)}\]]/
" Program Keywords
syntax keyword jsStorageClass const var let skipwhite skipempty nextgroup=jsDestructuringBlock,jsDestructuringArray,jsVariableDef
syntax match jsVariableDef contained /\<\K\k*/ skipwhite skipempty nextgroup=jsFlowDefinition
syntax keyword jsOperatorKeyword delete instanceof typeof void new in of skipwhite skipempty nextgroup=@jsExpression
syntax match jsOperator "[-!|&+<>=%/*~^]" skipwhite skipempty nextgroup=@jsExpression
syntax match jsOperator /::/ skipwhite skipempty nextgroup=@jsExpression
syntax keyword jsBooleanTrue true
syntax keyword jsBooleanFalse false
" Modules
syntax keyword jsImport import skipwhite skipempty nextgroup=jsModuleAsterisk,jsModuleKeyword,jsModuleGroup,jsFlowImportType
syntax keyword jsExport export skipwhite skipempty nextgroup=@jsAll,jsModuleGroup,jsExportDefault,jsModuleAsterisk,jsModuleKeyword,jsFlowTypeStatement
syntax match jsModuleKeyword contained /\<\K\k*/ skipwhite skipempty nextgroup=jsModuleAs,jsFrom,jsModuleComma
syntax keyword jsExportDefault contained default skipwhite skipempty nextgroup=@jsExpression
syntax keyword jsExportDefaultGroup contained default skipwhite skipempty nextgroup=jsModuleAs,jsFrom,jsModuleComma
syntax match jsModuleAsterisk contained /\*/ skipwhite skipempty nextgroup=jsModuleKeyword,jsModuleAs,jsFrom
syntax keyword jsModuleAs contained as skipwhite skipempty nextgroup=jsModuleKeyword,jsExportDefaultGroup
syntax keyword jsFrom contained from skipwhite skipempty nextgroup=jsString
syntax match jsModuleComma contained /,/ skipwhite skipempty nextgroup=jsModuleKeyword,jsModuleAsterisk,jsModuleGroup,jsFlowTypeKeyword
" Strings, Templates, Numbers
syntax region jsString start=+\z(["']\)+ skip=+\\\%(\z1\|$\)+ end=+\z1+ end=+$+ contains=jsSpecial,@Spell extend
syntax region jsTemplateString start=+`+ skip=+\\`+ end=+`+ contains=jsTemplateExpression,jsSpecial,@Spell extend
syntax match jsTaggedTemplate /\<\K\k*\ze`/ nextgroup=jsTemplateString
syntax match jsNumber /\c\<\%(\d\+\%(e[+-]\=\d\+\)\=\|0b[01]\+\|0o\o\+\|0x\x\+\)\>/
syntax keyword jsNumber Infinity
syntax match jsFloat /\c\<\%(\d\+\.\d\+\|\d\+\.\|\.\d\+\)\%(e[+-]\=\d\+\)\=\>/
" Regular Expressions
syntax match jsSpecial contained "\v\\%(x\x\x|u%(\x{4}|\{\x{4,5}})|c\u|.)"
syntax region jsTemplateExpression contained matchgroup=jsTemplateBraces start=+${+ end=+}+ contains=@jsExpression keepend
syntax region jsRegexpCharClass contained start=+\[+ skip=+\\.+ end=+\]+ contains=jsSpecial extend
syntax match jsRegexpBoundary contained "\v\c[$^]|\\b"
syntax match jsRegexpBackRef contained "\v\\[1-9]\d*"
syntax match jsRegexpQuantifier contained "\v[^\\]%([?*+]|\{\d+%(,\d*)?})\??"lc=1
syntax match jsRegexpOr contained "|"
syntax match jsRegexpMod contained "\v\(\?[:=!>]"lc=1
syntax region jsRegexpGroup contained start="[^\\]("lc=1 skip="\\.\|\[\(\\.\|[^]]\+\)\]" end=")" contains=jsRegexpCharClass,@jsRegexpSpecial keepend
syntax region jsRegexpString start=+\%(\%(\<return\|\<typeof\|\_[^)\]'"[:blank:][:alnum:]_$]\)\s*\)\@<=/\ze[^*/]+ skip=+\\.\|\[[^]]\{1,}\]+ end=+/[gimyus]\{,6}+ contains=jsRegexpCharClass,jsRegexpGroup,@jsRegexpSpecial oneline keepend extend
syntax cluster jsRegexpSpecial contains=jsSpecial,jsRegexpBoundary,jsRegexpBackRef,jsRegexpQuantifier,jsRegexpOr,jsRegexpMod
" Objects
syntax match jsObjectShorthandProp contained /\<\k*\ze\s*/ skipwhite skipempty nextgroup=jsObjectSeparator
syntax match jsObjectKey contained /\<\k*\ze\s*:/ contains=jsFunctionKey skipwhite skipempty nextgroup=jsObjectValue
syntax region jsObjectKeyString contained start=+\z(["']\)+ skip=+\\\%(\z1\|$\)+ end=+\z1\|$+ contains=jsSpecial,@Spell skipwhite skipempty nextgroup=jsObjectValue
syntax region jsObjectKeyComputed contained matchgroup=jsBrackets start=/\[/ end=/]/ contains=@jsExpression skipwhite skipempty nextgroup=jsObjectValue,jsFuncArgs extend
syntax match jsObjectSeparator contained /,/
syntax region jsObjectValue contained matchgroup=jsObjectColon start=/:/ end=/[,}]\@=/ contains=@jsExpression extend
syntax match jsObjectFuncName contained /\<\K\k*\ze\_s*(/ skipwhite skipempty nextgroup=jsFuncArgs
syntax match jsFunctionKey contained /\<\K\k*\ze\s*:\s*function\>/
syntax match jsObjectMethodType contained /\<[gs]et\ze\s\+\K\k*/ skipwhite skipempty nextgroup=jsObjectFuncName
syntax region jsObjectStringKey contained start=+\z(["']\)+ skip=+\\\%(\z1\|$\)+ end=+\z1\|$+ contains=jsSpecial,@Spell extend skipwhite skipempty nextgroup=jsFuncArgs,jsObjectValue
exe 'syntax keyword jsNull null '.(exists('g:javascript_conceal_null') ? 'conceal cchar='.g:javascript_conceal_null : '')
exe 'syntax keyword jsReturn return contained '.(exists('g:javascript_conceal_return') ? 'conceal cchar='.g:javascript_conceal_return : '').' skipwhite nextgroup=@jsExpression'
exe 'syntax keyword jsUndefined undefined '.(exists('g:javascript_conceal_undefined') ? 'conceal cchar='.g:javascript_conceal_undefined : '')
exe 'syntax keyword jsNan NaN '.(exists('g:javascript_conceal_NaN') ? 'conceal cchar='.g:javascript_conceal_NaN : '')
exe 'syntax keyword jsPrototype prototype '.(exists('g:javascript_conceal_prototype') ? 'conceal cchar='.g:javascript_conceal_prototype : '')
exe 'syntax keyword jsThis this '.(exists('g:javascript_conceal_this') ? 'conceal cchar='.g:javascript_conceal_this : '')
exe 'syntax keyword jsSuper super contained '.(exists('g:javascript_conceal_super') ? 'conceal cchar='.g:javascript_conceal_super : '')
" Statement Keywords
syntax match jsBlockLabel /\<\K\k*\s*::\@!/ contains=jsNoise skipwhite skipempty nextgroup=jsBlock
syntax match jsBlockLabelKey contained /\<\K\k*\ze\s*\_[;]/
syntax keyword jsStatement contained with yield debugger
syntax keyword jsStatement contained break continue skipwhite skipempty nextgroup=jsBlockLabelKey
syntax keyword jsConditional if skipwhite skipempty nextgroup=jsParenIfElse
syntax keyword jsConditional else skipwhite skipempty nextgroup=jsCommentIfElse,jsIfElseBlock
syntax keyword jsConditional switch skipwhite skipempty nextgroup=jsParenSwitch
syntax keyword jsRepeat while for skipwhite skipempty nextgroup=jsParenRepeat,jsForAwait
syntax keyword jsDo do skipwhite skipempty nextgroup=jsRepeatBlock
syntax region jsSwitchCase contained matchgroup=jsLabel start=/\<\%(case\|default\)\>/ end=/:\@=/ contains=@jsExpression,jsLabel skipwhite skipempty nextgroup=jsSwitchColon keepend
syntax keyword jsTry try skipwhite skipempty nextgroup=jsTryCatchBlock
syntax keyword jsFinally contained finally skipwhite skipempty nextgroup=jsFinallyBlock
syntax keyword jsCatch contained catch skipwhite skipempty nextgroup=jsParenCatch
syntax keyword jsException throw
syntax keyword jsAsyncKeyword async await
syntax match jsSwitchColon contained /::\@!/ skipwhite skipempty nextgroup=jsSwitchBlock
" Keywords
syntax keyword jsGlobalObjects ArrayBuffer Array BigInt64Array BigUint64Array Float32Array Float64Array Int16Array Int32Array Int8Array Uint16Array Uint32Array Uint8Array Uint8ClampedArray Boolean Buffer Collator DataView Date DateTimeFormat Function Intl Iterator JSON Map Set WeakMap WeakSet Math Number NumberFormat Object ParallelArray Promise Proxy Reflect RegExp String Symbol Uint8ClampedArray WebAssembly console document fetch window
syntax keyword jsGlobalNodeObjects module exports global process __dirname __filename
syntax match jsGlobalNodeObjects /\<require\>/ containedin=jsFuncCall
syntax keyword jsExceptions Error EvalError InternalError RangeError ReferenceError StopIteration SyntaxError TypeError URIError
syntax keyword jsBuiltins decodeURI decodeURIComponent encodeURI encodeURIComponent eval isFinite isNaN parseFloat parseInt uneval
" DISCUSS: How imporant is this, really? Perhaps it should be linked to an error because I assume the keywords are reserved?
syntax keyword jsFutureKeys abstract enum int short boolean interface byte long char final native synchronized float package throws goto private transient implements protected volatile double public
" DISCUSS: Should we really be matching stuff like this?
" DOM2 Objects
syntax keyword jsGlobalObjects DOMImplementation DocumentFragment Document Node NodeList NamedNodeMap CharacterData Attr Element Text Comment CDATASection DocumentType Notation Entity EntityReference ProcessingInstruction
syntax keyword jsExceptions DOMException
" DISCUSS: Should we really be matching stuff like this?
" DOM2 CONSTANT
syntax keyword jsDomErrNo INDEX_SIZE_ERR DOMSTRING_SIZE_ERR HIERARCHY_REQUEST_ERR WRONG_DOCUMENT_ERR INVALID_CHARACTER_ERR NO_DATA_ALLOWED_ERR NO_MODIFICATION_ALLOWED_ERR NOT_FOUND_ERR NOT_SUPPORTED_ERR INUSE_ATTRIBUTE_ERR INVALID_STATE_ERR SYNTAX_ERR INVALID_MODIFICATION_ERR NAMESPACE_ERR INVALID_ACCESS_ERR
syntax keyword jsDomNodeConsts ELEMENT_NODE ATTRIBUTE_NODE TEXT_NODE CDATA_SECTION_NODE ENTITY_REFERENCE_NODE ENTITY_NODE PROCESSING_INSTRUCTION_NODE COMMENT_NODE DOCUMENT_NODE DOCUMENT_TYPE_NODE DOCUMENT_FRAGMENT_NODE NOTATION_NODE
" DISCUSS: Should we really be special matching on these props?
" HTML events and internal variables
syntax keyword jsHtmlEvents onblur onclick oncontextmenu ondblclick onfocus onkeydown onkeypress onkeyup onmousedown onmousemove onmouseout onmouseover onmouseup onresize
" Code blocks
syntax region jsBracket matchgroup=jsBrackets start=/\[/ end=/\]/ contains=@jsExpression,jsSpreadExpression extend fold
syntax region jsParen matchgroup=jsParens start=/(/ end=/)/ contains=@jsExpression extend fold nextgroup=jsFlowDefinition
syntax region jsParenDecorator contained matchgroup=jsParensDecorator start=/(/ end=/)/ contains=@jsAll extend fold
syntax region jsParenIfElse contained matchgroup=jsParensIfElse start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsCommentIfElse,jsIfElseBlock,jsReturn extend fold
syntax region jsParenRepeat contained matchgroup=jsParensRepeat start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsCommentRepeat,jsRepeatBlock,jsReturn extend fold
syntax region jsParenSwitch contained matchgroup=jsParensSwitch start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsSwitchBlock extend fold
syntax region jsParenCatch contained matchgroup=jsParensCatch start=/(/ end=/)/ skipwhite skipempty nextgroup=jsTryCatchBlock extend fold
syntax region jsFuncArgs contained matchgroup=jsFuncParens start=/(/ end=/)/ contains=jsFuncArgCommas,jsComment,jsFuncArgExpression,jsDestructuringBlock,jsDestructuringArray,jsRestExpression,jsFlowArgumentDef skipwhite skipempty nextgroup=jsCommentFunction,jsFuncBlock,jsFlowReturn extend fold
syntax region jsClassBlock contained matchgroup=jsClassBraces start=/{/ end=/}/ contains=jsClassFuncName,jsClassMethodType,jsArrowFunction,jsArrowFuncArgs,jsComment,jsGenerator,jsDecorator,jsClassProperty,jsClassPropertyComputed,jsClassStringKey,jsAsyncKeyword,jsNoise extend fold
syntax region jsFuncBlock contained matchgroup=jsFuncBraces start=/{/ end=/}/ contains=@jsAll,jsBlock extend fold
syntax region jsIfElseBlock contained matchgroup=jsIfElseBraces start=/{/ end=/}/ contains=@jsAll,jsBlock extend fold
syntax region jsTryCatchBlock contained matchgroup=jsTryCatchBraces start=/{/ end=/}/ contains=@jsAll,jsBlock skipwhite skipempty nextgroup=jsCatch,jsFinally extend fold
syntax region jsFinallyBlock contained matchgroup=jsFinallyBraces start=/{/ end=/}/ contains=@jsAll,jsBlock extend fold
syntax region jsSwitchBlock contained matchgroup=jsSwitchBraces start=/{/ end=/}/ contains=@jsAll,jsBlock,jsSwitchCase extend fold
syntax region jsRepeatBlock contained matchgroup=jsRepeatBraces start=/{/ end=/}/ contains=@jsAll,jsBlock extend fold
syntax region jsDestructuringBlock contained matchgroup=jsDestructuringBraces start=/{/ end=/}/ contains=jsDestructuringProperty,jsDestructuringAssignment,jsDestructuringNoise,jsDestructuringPropertyComputed,jsSpreadExpression,jsComment nextgroup=jsFlowDefinition extend fold
syntax region jsDestructuringArray contained matchgroup=jsDestructuringBraces start=/\[/ end=/\]/ contains=jsDestructuringPropertyValue,jsDestructuringNoise,jsDestructuringProperty,jsSpreadExpression,jsDestructuringBlock,jsDestructuringArray,jsComment nextgroup=jsFlowDefinition extend fold
syntax region jsObject contained matchgroup=jsObjectBraces start=/{/ end=/}/ contains=jsObjectKey,jsObjectKeyString,jsObjectKeyComputed,jsObjectShorthandProp,jsObjectSeparator,jsObjectFuncName,jsObjectMethodType,jsGenerator,jsComment,jsObjectStringKey,jsSpreadExpression,jsDecorator,jsAsyncKeyword,jsTemplateString extend fold
syntax region jsBlock matchgroup=jsBraces start=/{/ end=/}/ contains=@jsAll,jsSpreadExpression extend fold
syntax region jsModuleGroup contained matchgroup=jsModuleBraces start=/{/ end=/}/ contains=jsModuleKeyword,jsModuleComma,jsModuleAs,jsComment,jsFlowTypeKeyword skipwhite skipempty nextgroup=jsFrom fold
syntax region jsSpreadExpression contained matchgroup=jsSpreadOperator start=/\.\.\./ end=/[,}\]]\@=/ contains=@jsExpression
syntax region jsRestExpression contained matchgroup=jsRestOperator start=/\.\.\./ end=/[,)]\@=/
syntax region jsTernaryIf matchgroup=jsTernaryIfOperator start=/?:\@!/ end=/\%(:\|}\@=\)/ contains=@jsExpression extend skipwhite skipempty nextgroup=@jsExpression
" These must occur here or they will be override by jsTernaryIf
syntax match jsOperator /?\.\ze\_D/
syntax match jsOperator /??/ skipwhite skipempty nextgroup=@jsExpression
syntax match jsGenerator contained /\*/ skipwhite skipempty nextgroup=jsFuncName,jsFuncArgs,jsFlowFunctionGroup
syntax match jsFuncName contained /\<\K\k*/ skipwhite skipempty nextgroup=jsFuncArgs,jsFlowFunctionGroup
syntax region jsFuncArgExpression contained matchgroup=jsFuncArgOperator start=/=/ end=/[,)]\@=/ contains=@jsExpression extend
syntax match jsFuncArgCommas contained ','
syntax keyword jsArguments contained arguments
syntax keyword jsForAwait contained await skipwhite skipempty nextgroup=jsParenRepeat
" Matches a single keyword argument with no parens
syntax match jsArrowFuncArgs /\<\K\k*\ze\s*=>/ skipwhite contains=jsFuncArgs skipwhite skipempty nextgroup=jsArrowFunction extend
" Matches a series of arguments surrounded in parens
syntax match jsArrowFuncArgs /([^()]*)\ze\s*=>/ contains=jsFuncArgs skipempty skipwhite nextgroup=jsArrowFunction extend
exe 'syntax match jsFunction /\<function\>/ skipwhite skipempty nextgroup=jsGenerator,jsFuncName,jsFuncArgs,jsFlowFunctionGroup skipwhite '.(exists('g:javascript_conceal_function') ? 'conceal cchar='.g:javascript_conceal_function : '')
exe 'syntax match jsArrowFunction /=>/ skipwhite skipempty nextgroup=jsFuncBlock,jsCommentFunction '.(exists('g:javascript_conceal_arrow_function') ? 'conceal cchar='.g:javascript_conceal_arrow_function : '')
exe 'syntax match jsArrowFunction /()\ze\s*=>/ skipwhite skipempty nextgroup=jsArrowFunction '.(exists('g:javascript_conceal_noarg_arrow_function') ? 'conceal cchar='.g:javascript_conceal_noarg_arrow_function : '')
exe 'syntax match jsArrowFunction /_\ze\s*=>/ skipwhite skipempty nextgroup=jsArrowFunction '.(exists('g:javascript_conceal_underscore_arrow_function') ? 'conceal cchar='.g:javascript_conceal_underscore_arrow_function : '')
" Classes
syntax keyword jsClassKeyword contained class
syntax keyword jsExtendsKeyword contained extends skipwhite skipempty nextgroup=@jsExpression
syntax match jsClassNoise contained /\./
syntax match jsClassFuncName contained /\<\K\k*\ze\s*[(<]/ skipwhite skipempty nextgroup=jsFuncArgs,jsFlowClassFunctionGroup
syntax match jsClassMethodType contained /\<\%([gs]et\|static\)\ze\s\+\K\k*/ skipwhite skipempty nextgroup=jsAsyncKeyword,jsClassFuncName,jsClassProperty
syntax region jsClassDefinition start=/\<class\>/ end=/\(\<extends\>\s\+\)\@<!{\@=/ contains=jsClassKeyword,jsExtendsKeyword,jsClassNoise,@jsExpression,jsFlowClassGroup skipwhite skipempty nextgroup=jsCommentClass,jsClassBlock,jsFlowClassGroup
syntax match jsClassProperty contained /\<\K\k*\ze\s*=/ skipwhite skipempty nextgroup=jsClassValue,jsFlowClassDef
syntax region jsClassValue contained start=/=/ end=/\_[;}]\@=/ contains=@jsExpression
syntax region jsClassPropertyComputed contained matchgroup=jsBrackets start=/\[/ end=/]/ contains=@jsExpression skipwhite skipempty nextgroup=jsFuncArgs,jsClassValue extend
syntax region jsClassStringKey contained start=+\z(["']\)+ skip=+\\\%(\z1\|$\)+ end=+\z1\|$+ contains=jsSpecial,@Spell extend skipwhite skipempty nextgroup=jsFuncArgs
" Destructuring
syntax match jsDestructuringPropertyValue contained /\k\+/
syntax match jsDestructuringProperty contained /\k\+\ze\s*=/ skipwhite skipempty nextgroup=jsDestructuringValue
syntax match jsDestructuringAssignment contained /\k\+\ze\s*:/ skipwhite skipempty nextgroup=jsDestructuringValueAssignment
syntax region jsDestructuringValue contained start=/=/ end=/[,}\]]\@=/ contains=@jsExpression extend
syntax region jsDestructuringValueAssignment contained start=/:/ end=/[,}=]\@=/ contains=jsDestructuringPropertyValue,jsDestructuringBlock,jsNoise,jsDestructuringNoise skipwhite skipempty nextgroup=jsDestructuringValue extend
syntax match jsDestructuringNoise contained /[,[\]]/
syntax region jsDestructuringPropertyComputed contained matchgroup=jsDestructuringBraces start=/\[/ end=/]/ contains=@jsExpression skipwhite skipempty nextgroup=jsDestructuringValue,jsDestructuringValueAssignment,jsDestructuringNoise extend fold
" Comments
syntax keyword jsCommentTodo contained TODO FIXME XXX TBD
syntax region jsComment start=+//+ end=/$/ contains=jsCommentTodo,@Spell extend keepend
syntax region jsComment start=+/\*+ end=+\*/+ contains=jsCommentTodo,@Spell fold extend keepend
syntax region jsEnvComment start=/\%^#!/ end=/$/ display
" Specialized Comments - These are special comment regexes that are used in
" odd places that maintain the proper nextgroup functionality. It sucks we
" can't make jsComment a skippable type of group for nextgroup
syntax region jsCommentFunction contained start=+//+ end=/$/ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsFuncBlock,jsFlowReturn extend keepend
syntax region jsCommentFunction contained start=+/\*+ end=+\*/+ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsFuncBlock,jsFlowReturn fold extend keepend
syntax region jsCommentClass contained start=+//+ end=/$/ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsClassBlock,jsFlowClassGroup extend keepend
syntax region jsCommentClass contained start=+/\*+ end=+\*/+ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsClassBlock,jsFlowClassGroup fold extend keepend
syntax region jsCommentIfElse contained start=+//+ end=/$/ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsIfElseBlock extend keepend
syntax region jsCommentIfElse contained start=+/\*+ end=+\*/+ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsIfElseBlock fold extend keepend
syntax region jsCommentRepeat contained start=+//+ end=/$/ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsRepeatBlock extend keepend
syntax region jsCommentRepeat contained start=+/\*+ end=+\*/+ contains=jsCommentTodo,@Spell skipwhite skipempty nextgroup=jsRepeatBlock fold extend keepend
" Decorators
syntax match jsDecorator /^\s*@/ nextgroup=jsDecoratorFunction
syntax match jsDecoratorFunction contained /\h[a-zA-Z0-9_.]*/ nextgroup=jsParenDecorator
if exists("javascript_plugin_jsdoc")
runtime extras/jsdoc.vim
" NGDoc requires JSDoc
if exists("javascript_plugin_ngdoc")
runtime extras/ngdoc.vim
endif
endif
if exists("javascript_plugin_flow")
runtime extras/flow.vim
endif
syntax cluster jsExpression contains=jsBracket,jsParen,jsObject,jsTernaryIf,jsTaggedTemplate,jsTemplateString,jsString,jsRegexpString,jsNumber,jsFloat,jsOperator,jsOperatorKeyword,jsBooleanTrue,jsBooleanFalse,jsNull,jsFunction,jsArrowFunction,jsGlobalObjects,jsExceptions,jsFutureKeys,jsDomErrNo,jsDomNodeConsts,jsHtmlEvents,jsFuncCall,jsUndefined,jsNan,jsPrototype,jsBuiltins,jsNoise,jsClassDefinition,jsArrowFunction,jsArrowFuncArgs,jsParensError,jsComment,jsArguments,jsThis,jsSuper,jsDo,jsForAwait,jsAsyncKeyword,jsStatement,jsDot
syntax cluster jsAll contains=@jsExpression,jsStorageClass,jsConditional,jsRepeat,jsReturn,jsException,jsTry,jsNoise,jsBlockLabel
" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
" For version 5.8 and later: only when an item doesn't have highlighting yet
if version >= 508 || !exists("did_javascript_syn_inits")
if version < 508
let did_javascript_syn_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink jsComment Comment
HiLink jsEnvComment PreProc
HiLink jsParensIfElse jsParens
HiLink jsParensRepeat jsParens
HiLink jsParensSwitch jsParens
HiLink jsParensCatch jsParens
HiLink jsCommentTodo Todo
HiLink jsString String
HiLink jsObjectKeyString String
HiLink jsTemplateString String
HiLink jsObjectStringKey String
HiLink jsClassStringKey String
HiLink jsTaggedTemplate StorageClass
HiLink jsTernaryIfOperator Operator
HiLink jsRegexpString String
HiLink jsRegexpBoundary SpecialChar
HiLink jsRegexpQuantifier SpecialChar
HiLink jsRegexpOr Conditional
HiLink jsRegexpMod SpecialChar
HiLink jsRegexpBackRef SpecialChar
HiLink jsRegexpGroup jsRegexpString
HiLink jsRegexpCharClass Character
HiLink jsCharacter Character
HiLink jsPrototype Special
HiLink jsConditional Conditional
HiLink jsBranch Conditional
HiLink jsLabel Label
HiLink jsReturn Statement
HiLink jsRepeat Repeat
HiLink jsDo Repeat
HiLink jsStatement Statement
HiLink jsException Exception
HiLink jsTry Exception
HiLink jsFinally Exception
HiLink jsCatch Exception
HiLink jsAsyncKeyword Keyword
HiLink jsForAwait Keyword
HiLink jsArrowFunction Type
HiLink jsFunction Type
HiLink jsGenerator jsFunction
HiLink jsArrowFuncArgs jsFuncArgs
HiLink jsFuncName Function
HiLink jsFuncCall Function
HiLink jsClassFuncName jsFuncName
HiLink jsObjectFuncName Function
HiLink jsArguments Special
HiLink jsError Error
HiLink jsParensError Error
HiLink jsOperatorKeyword jsOperator
HiLink jsOperator Operator
HiLink jsOf Operator
HiLink jsStorageClass StorageClass
HiLink jsClassKeyword Keyword
HiLink jsExtendsKeyword Keyword
HiLink jsThis Special
HiLink jsSuper Constant
HiLink jsNan Number
HiLink jsNull Type
HiLink jsUndefined Type
HiLink jsNumber Number
HiLink jsFloat Float
HiLink jsBooleanTrue Boolean
HiLink jsBooleanFalse Boolean
HiLink jsObjectColon jsNoise
HiLink jsNoise Noise
HiLink jsDot Noise
HiLink jsBrackets Noise
HiLink jsParens Noise
HiLink jsBraces Noise
HiLink jsFuncBraces Noise
HiLink jsFuncParens Noise
HiLink jsClassBraces Noise
HiLink jsClassNoise Noise
HiLink jsIfElseBraces Noise
HiLink jsTryCatchBraces Noise
HiLink jsModuleBraces Noise
HiLink jsObjectBraces Noise
HiLink jsObjectSeparator Noise
HiLink jsFinallyBraces Noise
HiLink jsRepeatBraces Noise
HiLink jsSwitchBraces Noise
HiLink jsSpecial Special
HiLink jsTemplateBraces Noise
HiLink jsGlobalObjects Constant
HiLink jsGlobalNodeObjects Constant
HiLink jsExceptions Constant
HiLink jsBuiltins Constant
HiLink jsImport Include
HiLink jsExport Include
HiLink jsExportDefault StorageClass
HiLink jsExportDefaultGroup jsExportDefault
HiLink jsModuleAs Include
HiLink jsModuleComma jsNoise
HiLink jsModuleAsterisk Noise
HiLink jsFrom Include
HiLink jsDecorator Special
HiLink jsDecoratorFunction Function
HiLink jsParensDecorator jsParens
HiLink jsFuncArgOperator jsFuncArgs
HiLink jsClassProperty jsObjectKey
HiLink jsObjectShorthandProp jsObjectKey
HiLink jsSpreadOperator Operator
HiLink jsRestOperator Operator
HiLink jsRestExpression jsFuncArgs
HiLink jsSwitchColon Noise
HiLink jsClassMethodType Type
HiLink jsObjectMethodType Type
HiLink jsClassDefinition jsFuncName
HiLink jsBlockLabel Identifier
HiLink jsBlockLabelKey jsBlockLabel
HiLink jsDestructuringBraces Noise
HiLink jsDestructuringProperty jsFuncArgs
HiLink jsDestructuringAssignment jsObjectKey
HiLink jsDestructuringNoise Noise
HiLink jsCommentFunction jsComment
HiLink jsCommentClass jsComment
HiLink jsCommentIfElse jsComment
HiLink jsCommentRepeat jsComment
HiLink jsDomErrNo Constant
HiLink jsDomNodeConsts Constant
HiLink jsDomElemAttrs Label
HiLink jsDomElemFuncs PreProc
HiLink jsHtmlEvents Special
HiLink jsHtmlElemAttrs Label
HiLink jsHtmlElemFuncs PreProc
HiLink jsCssStyles Label
delcommand HiLink
endif
" Define the htmlJavaScript for HTML syntax html.vim
syntax cluster htmlJavaScript contains=@jsAll,jsImport,jsExport
syntax cluster javaScriptExpression contains=@jsAll
" Vim's default html.vim highlights all javascript as 'Special'
hi! def link javaScript NONE
let b:current_syntax = "javascript"
if main_syntax == 'javascript'
unlet main_syntax
endif

View file

@ -0,0 +1 @@
doc/tags

View file

@ -0,0 +1 @@
--color

View file

@ -0,0 +1,19 @@
Maintainers:
Mark Guzman <segfault@hasno.info>
Doug Kearns <dougkearns@gmail.com>
Tim Pope <vim@NOSPAMtpope.org>
Andrew Radev <andrey.radev@gmail.com>
Nikolai Weibull <now@bitwi.se>
Other contributors:
Michael Brailsford <brailsmt@yahoo.com>
Sean Flanagan <sdflanagan@ozemail.com.au>
Tim Hammerquist <timh@rubyforge.org>
Ken Miller <ken.miller@gmail.com>
Hugh Sasse <hgs@dmu.ac.uk>
Tilman Sauerbeck <tilman@code-monkey.de>
Bertram Scharpf <info@bertram-scharpf.de>
Gavin Sinclair <gsinclair@gmail.com>
Aaron Son <aaronson@uiuc.edu>
Ned Konz <ned@bike-nomad.com>
Pan Thomakos <pan.thomakos@gmail.com>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,4 @@
source 'http://rubygems.org'
gem 'rspec'
gem 'vimrunner'

View file

@ -0,0 +1,28 @@
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.3)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
vimrunner (0.3.0)
PLATFORMS
ruby
DEPENDENCIES
rspec
vimrunner
BUNDLED WITH
1.13.7

View file

@ -0,0 +1,35 @@
Installation
============
In general, your favorite method works. Here are some options.
With pathogen.vim
-----------------
Install [pathogen.vim](https://github.com/tpope/vim-pathogen),
then copy and paste:
git clone git://github.com/vim-ruby/vim-ruby.git ~/.vim/bundle/vim-ruby
With Vundle
-----------
Install [Vundle](https://github.com/gmarik/vundle), then add the
following to your vimrc:
Bundle 'vim-ruby/vim-ruby'
With patience
-------------
Wait for an upgrade to Vim and install it. Vim ships with the latest
version of vim-ruby at the time of its release. (Remember this when
choosing another installation method. The version you download will
supersede the version that ships with Vim, so you will now be
responsible for keeping it up-to-date.)
Manually
--------
[Download](https://github.com/vim-ruby/vim-ruby/archives/master) and
extract the relevant files to `~/.vim` (or `$HOME/vimfiles` on Windows).

View file

@ -0,0 +1,243 @@
This file is no longer maintained. Consult the Git log for newer changes.
= 2008.07.XX
== Filetype Detection
The IRB RC file (.irbrc) is now detected as being a Ruby file.
= 2007.05.07
== Ruby Syntax Highlighting
Highlight OPTIMIZE alongside FIXME and TODO.
Multiline array literals can now be folded.
== Ruby Filetype Support
Added mappings for [[, ]], [], ][, [m, ]m, [M, and ]M. The first four bounce
between class and module declarations, and the last four between method
declarations.
== eRuby Syntax Highlighting
Tim Pope has taken over maintenance of the eRuby syntax file. The subtype of
the file is now determined dynamically from the filename, rather than being
hardwired to HTML. It can be overridden with b:eruby_subtype.
== eRuby Filetype Support
Tim Pope has taken over maintenance of the eRuby filetype plugin. Like with
the syntax file, the subtype is now determined dynamically.
== eRuby Indenting
As with the syntax file and filetype plugin, the subtype is now determined
dynamically.
== Bug Fixes
Ruby syntax file
- when ruby_operators is set, highlight scope and range operators, and don't
match '>' in =>'
- regexp literals are highlighted after the 'else' keyword
- don't match [!=?] as part of a sigil prefixed symbol name
- allow text to appear after, and on the same line, as '=begin' in
rubyDocumentation regions
- highlight %s() ans a symbol, not a string
- eliminated some false positves for here docs, symbols, ASCII codes, and
conditionals as statement modifiers
- added "neus" to regexp flags
- Highlight punctuation variables in string interpolation, and flag invalid
ones as errors
- removed : from rubyOptionalDoLine (falsely matches on symbols)
Ruby filetype plugin
- eliminated some false positives with the matchit patterns
Ruby indent plugin
- ignore instance, class, and global variables named "end"
= 2007.03.02
== Omni Completion
Fall back to syntax highlighting completion if Vim lacks the Ruby interface.
RubyGems is now loaded by default if available.
Classes are detected using ObjectSpace. Kernel methods are included in method
completion.
Added completion in Rails views. Rails helpers are included. Rails migrations
now have completion.
== Ruby Syntax Highlighting
Ruby code is highlighted inside interpolation regions.
Symbols are now highlighted with the Constant highlight group; Constants and
class names with the Type highlight group.
Symbol names specified with a string recognise interpolation and escape
sequences.
Alias statements receive special highlighting similar to other 'definitions'.
== Ruby Filetype Support
Matchit support has been improved to include (), {}, and [] in the list of
patterns so that these will be appropriately skipped when included in comments.
ri has been added as the 'keywordprg' and 'balloonexpr' is set to return the
output of ri.
== eRuby Indenting
Tim Pope has taken over maintenance of the eRuby indent file. Ruby code is now
indented appropriately.
== Bug Fixes
Ruby syntax file
- trailing whitespace is no longer included with the def, class, module
keywords.
- escaped interpolation regions should now be ignored in all cases.
- conditional and loop statements are now highlighted correctly in more
locations (where they're used as expressions).
eRuby syntax file
- '-' trim mode block delimiters are now recognised.
Omni Completion
- more robustness; failure to parse buffer no longer errors or prevents
completion.
= 2006.07.11
== Omni Completion
A new omni completion function is now included which offers IntelliSense-like
functionality. See :help ft-ruby-omni for further information.
Note: This will only work with Vim 7.x, compiled with the Ruby interface
(+ruby), and Ruby 1.8.x
== Ruby Filetype Support
Matchit support has been improved to include (), {}, and [] in the list of
patterns meaning these will be appropriately skipped when included in comments.
== Ruby Syntax Highlighting
Operators can now be highlighted by defining the Vim global variable
"ruby_operators".
Multiline comments will now be folded. This can be disabled by defining the
"ruby_no_comment_fold" Vim variable.
== Filetype Detection
RJS and RXML templates are now detected as being 'filetype=ruby'.
== FAQ
There is a new FAQ document included. This is a work in progress and any
feedback would be appreciated.
== Bug Fixes
Ruby syntax file - if/unless modifiers after a method name ending with [?!=]
should now be highlighted correctly.
= 2005.10.07
== Vim 6.4
This release is included in Vim 6.4.
== Bug Fixes
Ruby filetype plugin - symbols were incorrectly being matched as match_words
causing the matchit motion command to jump to an incorrect location in some
circumstances.
= 2005.10.05
== Bug Fixes
Ruby syntax file - allow for comments directly after module/class/def lines
without intervening whitespace (fold markers were breaking syntax highlighting).
Ruby filetype plugin - improve ordering of 'path' elements.
eRuby syntax file - make use of ruby_no_expensive local to the buffer.
= 2005.09.24
== Filetype Detection
The eruby filetype is now detected solely based on the file's extension. This
was being overridden by the scripts.vim detection script.
Note: Only files ending in *.rhtml are detected as filetype eruby since these
are currently assumed to be Ruby embedded in (X)HTML only. Other filetypes
could be supported if requested.
== eRuby Indent File
There is a new eRuby indent file which simply sources the HTML indent file for
now.
== eRuby Compiler Plugin
This now supports erb as the default 'makeprg'. To use eruby set the
eruby_compiler variable to "eruby" in your .vimrc
== Test::Unit Compiler Plugin
This has been improved and should now display messages similar to, though more
detailed than, the GUI test runners.
== Bug Fixes
A few minor bugs have been fixed in the Ruby syntax and indent files.
= 2005.09.15
== eRuby Support
There are new syntax, compiler, and ftplugin files for eRuby. This support is
incomplete and we're keen to hear of any problems or suggestions you may have
to improve it.
== Ruby Filetype Support
The Ruby filetype plugin has been improved to include as many useful settings
as possible without intruding too much on an individual user's preferences.
Matchit support has been improved, and the following options are now set to
appropriate values: comments, commentstring, formatoptions, include,
includeexpr, path, and suffixesadd
== Filetype Detection
The new ftdetect mechanism of Vim 6.3 is being utilized to enable filetype
detection of eRuby files until this is officially added to the next release of
Vim.
== Installation Directories
The installer script now, where possible, automatically determines both the
user and system-wide preferences directory.
== Bug Fixes
A large number of bugs have been fixed in the Ruby syntax and indent files.

View file

@ -0,0 +1,73 @@
+---------------------------------+
| vim-ruby github project README |
+---------------------------------+
Summary:
This project contains Vim configuration files for editing and compiling Ruby
within Vim. See the project homepage for more details.
Web links:
Homepage: https://github.com/vim-ruby
Explanation: https://github.com/vim-ruby/vim-ruby/wiki
For regular users:
- The project page should have two tarballs for download:
- vim-ruby-YYYY.MM.DD.tar.gz (the current stable release)
- vim-ruby-devel-YYYY.MM.DD.tar.gz (cutting-edge features we'd like you
to test)
- Please give feedback through the bug tracking and feature request features
of GitHub.
- Feel free to join discussions on the vim-ruby-devel mailing list:
http://rubyforge.org/mail/?group_id=16
For would-be contributors:
- Please get the latest from Git.
- Please join the mailing list and discuss changes, submit patches, etc.
- Thank you very much for taking an interest.
Contents of the project:
- The autoload, compiler, ftdetect, ftplugin, indent and syntax directories
contain the ruby*.vim files that are to be copied to a location somewhere
in the Vim 'runtimepath'.
How you get these files into Vim:
- By downloading the project via a snapshot or Git, you can keep up with
the latest, make changes, and install the files to a Vim directory.
- By downloading one of the tarballs, you can easily install the latest
stable or development version wherever you like on your machine. No
README etc. just Vim files. You would typically install these into either
$VIM/vimfiles, for system-wide use, or $HOME/.vim ($HOME/vimfiles on
Windows) for personal use.
- Remember that when you install Vim in the first place, all of these files
are present. The purpose of downloading and installing them from
GitHub is to get the latest version of them.
Git topics:
- Project was migrated from CVS in August, 2008.
- Files are tagged according to which version of Vim they are released in.
- The project was initiated in July 2003, when the current version of Vim
was 6.2. Thus every file began its life tagged as vim6.2.
- Modifications to the files are made in the expectation that they need to
be tested by interested users. They therefore (probably) don't have a
tag, and are available via "git pull --rebase", or a development snapshot.
- When a modification is considered stable, it is given a tag.
Everything that is stable gets released in vim-ruby-YYY.MM.DD.tar.gz files.
- When a new version of Vim is about to be released, the stable tarball is
contributed to it. After it has been released, the files are tagged
accordingly.
- MORAL OF THE STORY: modifications are committed to the head of the tree;
when they are ready for release into userland, they are tagged "stable".
Any questions or suggestions?
- If there's something about the project or its concepts that you don't
understand, send an email to the release coordinator, Doug Kearns
(dougkearns at gmail.com).
- To ask about the contents of the configuration files, open a GitHub issue
or ask on the mailing list, as different people maintain the different
files.
Project gossip:
- While the individual effort to maintain these files has a long history,
this actual project began in late July 2003.
--= End of Document =--

View file

@ -0,0 +1,876 @@
" Vim completion script
" Language: Ruby
" Maintainer: Mark Guzman <segfault@hasno.info>
" URL: https://github.com/vim-ruby/vim-ruby
" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
" ----------------------------------------------------------------------------
"
" Ruby IRB/Complete author: Keiju ISHITSUKA(keiju@ishitsuka.com)
" ----------------------------------------------------------------------------
" {{{ requirement checks
function! s:ErrMsg(msg)
echohl ErrorMsg
echo a:msg
echohl None
endfunction
if !has('ruby')
call s:ErrMsg( "Error: Rubycomplete requires vim compiled with +ruby" )
call s:ErrMsg( "Error: falling back to syntax completion" )
" lets fall back to syntax completion
setlocal omnifunc=syntaxcomplete#Complete
finish
endif
if version < 700
call s:ErrMsg( "Error: Required vim >= 7.0" )
finish
endif
" }}} requirement checks
" {{{ configuration failsafe initialization
if !exists("g:rubycomplete_rails")
let g:rubycomplete_rails = 0
endif
if !exists("g:rubycomplete_classes_in_global")
let g:rubycomplete_classes_in_global = 0
endif
if !exists("g:rubycomplete_buffer_loading")
let g:rubycomplete_buffer_loading = 0
endif
if !exists("g:rubycomplete_include_object")
let g:rubycomplete_include_object = 0
endif
if !exists("g:rubycomplete_include_objectspace")
let g:rubycomplete_include_objectspace = 0
endif
" }}} configuration failsafe initialization
" {{{ regex patterns
" Regex that defines the start-match for the 'end' keyword.
let s:end_start_regex =
\ '\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' .
\ '\<\%(module\|class\|if\|for\|while\|until\|case\|unless\|begin' .
\ '\|\%(\K\k*[!?]\?\s\+\)\=def\):\@!\>' .
\ '\|\%(^\|[^.:@$]\)\@<=\<do:\@!\>'
" Regex that defines the middle-match for the 'end' keyword.
let s:end_middle_regex = '\<\%(ensure\|else\|\%(\%(^\|;\)\s*\)\@<=\<rescue:\@!\>\|when\|elsif\):\@!\>'
" Regex that defines the end-match for the 'end' keyword.
let s:end_end_regex = '\%(^\|[^.:@$]\)\@<=\<end:\@!\>'
" }}} regex patterns
" {{{ vim-side support functions
let s:rubycomplete_debug = 0
function! s:dprint(msg)
if s:rubycomplete_debug == 1
echom a:msg
endif
endfunction
function! s:GetBufferRubyModule(name, ...)
if a:0 == 1
let [snum,enum] = s:GetBufferRubyEntity(a:name, "module", a:1)
else
let [snum,enum] = s:GetBufferRubyEntity(a:name, "module")
endif
return snum . '..' . enum
endfunction
function! s:GetBufferRubyClass(name, ...)
if a:0 >= 1
let [snum,enum] = s:GetBufferRubyEntity(a:name, "class", a:1)
else
let [snum,enum] = s:GetBufferRubyEntity(a:name, "class")
endif
return snum . '..' . enum
endfunction
function! s:GetBufferRubySingletonMethods(name)
endfunction
function! s:GetBufferRubyEntity( name, type, ... )
let lastpos = getpos(".")
let lastline = lastpos
if (a:0 >= 1)
let lastline = [ 0, a:1, 0, 0 ]
call cursor( a:1, 0 )
endif
let stopline = 1
let crex = '^\s*\<' . a:type . '\>\s*\<' . escape(a:name, '*') . '\>\s*\(<\s*.*\s*\)\?'
let [lnum,lcol] = searchpos( crex, 'w' )
"let [lnum,lcol] = searchpairpos( crex . '\zs', '', '\(end\|}\)', 'w' )
if lnum == 0 && lcol == 0
call cursor(lastpos[1], lastpos[2])
return [0,0]
endif
let curpos = getpos(".")
let [enum,ecol] = searchpairpos( s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'W' )
call cursor(lastpos[1], lastpos[2])
if lnum > enum
return [0,0]
endif
" we found a the class def
return [lnum,enum]
endfunction
function! s:IsInClassDef()
return s:IsPosInClassDef( line('.') )
endfunction
function! s:IsPosInClassDef(pos)
let [snum,enum] = s:GetBufferRubyEntity( '.*', "class" )
let ret = 'nil'
if snum < a:pos && a:pos < enum
let ret = snum . '..' . enum
endif
return ret
endfunction
function! s:IsInComment(pos)
let stack = synstack(a:pos[0], a:pos[1])
if !empty(stack)
return synIDattr(stack[0], 'name') =~ 'ruby\%(.*Comment\|Documentation\)'
else
return 0
endif
endfunction
function! s:GetRubyVarType(v)
let stopline = 1
let vtp = ''
let curpos = getpos('.')
let sstr = '^\s*#\s*@var\s*'.escape(a:v, '*').'\>\s\+[^ \t]\+\s*$'
let [lnum,lcol] = searchpos(sstr,'nb',stopline)
if lnum != 0 && lcol != 0
call setpos('.',curpos)
let str = getline(lnum)
let vtp = substitute(str,sstr,'\1','')
return vtp
endif
call setpos('.',curpos)
let ctors = '\(now\|new\|open\|get_instance'
if exists('g:rubycomplete_rails') && g:rubycomplete_rails == 1 && s:rubycomplete_rails_loaded == 1
let ctors = ctors.'\|find\|create'
else
endif
let ctors = ctors.'\)'
let fstr = '=\s*\([^ \t]\+.' . ctors .'\>\|[\[{"''/]\|%[xwQqr][(\[{@]\|[A-Za-z0-9@:\-()\.]\+...\?\|lambda\|&\)'
let sstr = ''.escape(a:v, '*').'\>\s*[+\-*/]*'.fstr
let pos = searchpos(sstr,'bW')
while pos != [0,0] && s:IsInComment(pos)
let pos = searchpos(sstr,'bW')
endwhile
if pos != [0,0]
let [lnum, col] = pos
let str = matchstr(getline(lnum),fstr,col)
let str = substitute(str,'^=\s*','','')
call setpos('.',pos)
if str == '"' || str == '''' || stridx(tolower(str), '%q[') != -1
return 'String'
elseif str == '[' || stridx(str, '%w[') != -1
return 'Array'
elseif str == '{'
return 'Hash'
elseif str == '/' || str == '%r{'
return 'Regexp'
elseif strlen(str) >= 4 && stridx(str,'..') != -1
return 'Range'
elseif stridx(str, 'lambda') != -1 || str == '&'
return 'Proc'
elseif strlen(str) > 4
let l = stridx(str,'.')
return str[0:l-1]
end
return ''
endif
call setpos('.',curpos)
return ''
endfunction
"}}} vim-side support functions
"{{{ vim-side completion function
function! rubycomplete#Init()
execute "ruby VimRubyCompletion.preload_rails"
endfunction
function! rubycomplete#Complete(findstart, base)
"findstart = 1 when we need to get the text length
if a:findstart
let line = getline('.')
let idx = col('.')
while idx > 0
let idx -= 1
let c = line[idx-1]
if c =~ '\w'
continue
elseif ! c =~ '\.'
let idx = -1
break
else
break
endif
endwhile
return idx
"findstart = 0 when we need to return the list of completions
else
let g:rubycomplete_completions = []
execute "ruby VimRubyCompletion.get_completions('" . a:base . "')"
return g:rubycomplete_completions
endif
endfunction
"}}} vim-side completion function
"{{{ ruby-side code
function! s:DefRuby()
ruby << RUBYEOF
# {{{ ruby completion
begin
require 'rubygems' # let's assume this is safe...?
rescue Exception
#ignore?
end
class VimRubyCompletion
# {{{ constants
@@debug = false
@@ReservedWords = [
"BEGIN", "END",
"alias", "and",
"begin", "break",
"case", "class",
"def", "defined", "do",
"else", "elsif", "end", "ensure",
"false", "for",
"if", "in",
"module",
"next", "nil", "not",
"or",
"redo", "rescue", "retry", "return",
"self", "super",
"then", "true",
"undef", "unless", "until",
"when", "while",
"yield",
]
@@Operators = [ "%", "&", "*", "**", "+", "-", "/",
"<", "<<", "<=", "<=>", "==", "===", "=~", ">", ">=", ">>",
"[]", "[]=", "^", ]
# }}} constants
# {{{ buffer analysis magic
def load_requires
custom_paths = VIM::evaluate("get(g:, 'rubycomplete_load_paths', [])")
if !custom_paths.empty?
$LOAD_PATH.concat(custom_paths).uniq!
end
buf = VIM::Buffer.current
enum = buf.line_number
nums = Range.new( 1, enum )
nums.each do |x|
ln = buf[x]
begin
if /.*require_relative\s*(.*)$/.match( ln )
eval( "require %s" % File.expand_path($1) )
elsif /.*require\s*(["'].*?["'])/.match( ln )
eval( "require %s" % $1 )
end
rescue Exception => e
dprint e.inspect
end
end
end
def load_gems
fpath = VIM::evaluate("get(g:, 'rubycomplete_gemfile_path', 'Gemfile')")
return unless File.file?(fpath) && File.readable?(fpath)
want_bundler = VIM::evaluate("get(g:, 'rubycomplete_use_bundler')")
parse_file = !want_bundler
begin
require 'bundler'
Bundler.setup
Bundler.require
rescue Exception
parse_file = true
end
if parse_file
File.new(fpath).each_line do |line|
begin
require $1 if /\s*gem\s*['"]([^'"]+)/.match(line)
rescue Exception
end
end
end
end
def load_buffer_class(name)
dprint "load_buffer_class(%s) START" % name
classdef = get_buffer_entity(name, 's:GetBufferRubyClass("%s")')
return if classdef == nil
pare = /^\s*class\s*(.*)\s*<\s*(.*)\s*\n/.match( classdef )
load_buffer_class( $2 ) if pare != nil && $2 != name # load parent class if needed
mixre = /.*\n\s*(include|prepend)\s*(.*)\s*\n/.match( classdef )
load_buffer_module( $2 ) if mixre != nil && $2 != name # load mixins if needed
begin
eval classdef
rescue Exception
VIM::evaluate( "s:ErrMsg( 'Problem loading class \"%s\", was it already completed?' )" % name )
end
dprint "load_buffer_class(%s) END" % name
end
def load_buffer_module(name)
dprint "load_buffer_module(%s) START" % name
classdef = get_buffer_entity(name, 's:GetBufferRubyModule("%s")')
return if classdef == nil
begin
eval classdef
rescue Exception
VIM::evaluate( "s:ErrMsg( 'Problem loading module \"%s\", was it already completed?' )" % name )
end
dprint "load_buffer_module(%s) END" % name
end
def get_buffer_entity(name, vimfun)
loading_allowed = VIM::evaluate("exists('g:rubycomplete_buffer_loading') && g:rubycomplete_buffer_loading")
return nil if loading_allowed.to_i.zero?
return nil if /(\"|\')+/.match( name )
buf = VIM::Buffer.current
nums = eval( VIM::evaluate( vimfun % name ) )
return nil if nums == nil
return nil if nums.min == nums.max && nums.min == 0
dprint "get_buffer_entity START"
visited = []
clscnt = 0
bufname = VIM::Buffer.current.name
classdef = ""
cur_line = VIM::Buffer.current.line_number
while (nums != nil && !(nums.min == 0 && nums.max == 0) )
dprint "visited: %s" % visited.to_s
break if visited.index( nums )
visited << nums
nums.each do |x|
if x != cur_line
next if x == 0
ln = buf[x]
is_const = false
if /^\s*(module|class|def|include)\s+/.match(ln) || is_const = /^\s*?[A-Z]([A-z]|[1-9])*\s*?[|]{0,2}=\s*?.+\s*?/.match(ln)
clscnt += 1 if /class|module/.match($1)
# We must make sure to load each constant only once to avoid errors
if is_const
ln.gsub!(/\s*?[|]{0,2}=\s*?/, '||=')
end
#dprint "\$1$1
classdef += "%s\n" % ln
classdef += "end\n" if /def\s+/.match(ln)
dprint ln
end
end
end
nm = "%s(::.*)*\", %s, \"" % [ name, nums.last ]
nums = eval( VIM::evaluate( vimfun % nm ) )
dprint "nm: \"%s\"" % nm
dprint "vimfun: %s" % (vimfun % nm)
dprint "got nums: %s" % nums.to_s
end
if classdef.length > 1
classdef += "end\n"*clscnt
# classdef = "class %s\n%s\nend\n" % [ bufname.gsub( /\/|\\/, "_" ), classdef ]
end
dprint "get_buffer_entity END"
dprint "classdef====start"
lns = classdef.split( "\n" )
lns.each { |x| dprint x }
dprint "classdef====end"
return classdef
end
def get_var_type( receiver )
if /(\"|\')+/.match( receiver )
"String"
else
VIM::evaluate("s:GetRubyVarType('%s')" % receiver)
end
end
def dprint( txt )
print txt if @@debug
end
def escape_vim_singlequote_string(str)
str.to_s.gsub(/'/,"\\'")
end
def get_buffer_entity_list( type )
# this will be a little expensive.
loading_allowed = VIM::evaluate("exists('g:rubycomplete_buffer_loading') && g:rubycomplete_buffer_loading")
allow_aggressive_load = VIM::evaluate("exists('g:rubycomplete_classes_in_global') && g:rubycomplete_classes_in_global")
return [] if allow_aggressive_load.to_i.zero? || loading_allowed.to_i.zero?
buf = VIM::Buffer.current
eob = buf.length
ret = []
rg = 1..eob
re = eval( "/^\s*%s\s*([A-Za-z0-9_:-]*)(\s*<\s*([A-Za-z0-9_:-]*))?\s*/" % type )
rg.each do |x|
if re.match( buf[x] )
next if type == "def" && eval( VIM::evaluate("s:IsPosInClassDef(%s)" % x) ) != nil
ret.push $1
end
end
return ret
end
def get_buffer_modules
return get_buffer_entity_list( "modules" )
end
def get_buffer_methods
return get_buffer_entity_list( "def" )
end
def get_buffer_classes
return get_buffer_entity_list( "class" )
end
def load_rails
allow_rails = VIM::evaluate("exists('g:rubycomplete_rails') && g:rubycomplete_rails")
return if allow_rails.to_i.zero?
buf_path = VIM::evaluate('expand("%:p")')
file_name = VIM::evaluate('expand("%:t")')
vim_dir = VIM::evaluate('getcwd()')
file_dir = buf_path.gsub( file_name, '' )
file_dir.gsub!( /\\/, "/" )
vim_dir.gsub!( /\\/, "/" )
vim_dir << "/"
dirs = [ vim_dir, file_dir ]
sdirs = [ "", "./", "../", "../../", "../../../", "../../../../" ]
rails_base = nil
dirs.each do |dir|
sdirs.each do |sub|
trail = "%s%s" % [ dir, sub ]
tcfg = "%sconfig" % trail
if File.exists?( tcfg )
rails_base = trail
break
end
end
break if rails_base
end
return if rails_base == nil
$:.push rails_base unless $:.index( rails_base )
rails_config = rails_base + "config/"
rails_lib = rails_base + "lib/"
$:.push rails_config unless $:.index( rails_config )
$:.push rails_lib unless $:.index( rails_lib )
bootfile = rails_config + "boot.rb"
envfile = rails_config + "environment.rb"
if File.exists?( bootfile ) && File.exists?( envfile )
begin
require bootfile
require envfile
begin
require 'console_app'
require 'console_with_helpers'
rescue Exception
dprint "Rails 1.1+ Error %s" % $!
# assume 1.0
end
#eval( "Rails::Initializer.run" ) #not necessary?
VIM::command('let s:rubycomplete_rails_loaded = 1')
dprint "rails loaded"
rescue Exception
dprint "Rails Error %s" % $!
VIM::evaluate( "s:ErrMsg('Error loading rails environment')" )
end
end
end
def get_rails_helpers
allow_rails = VIM::evaluate("exists('g:rubycomplete_rails') && g:rubycomplete_rails")
rails_loaded = VIM::evaluate('s:rubycomplete_rails_loaded')
return [] if allow_rails.to_i.zero? || rails_loaded.to_i.zero?
buf_path = VIM::evaluate('expand("%:p")')
buf_path.gsub!( /\\/, "/" )
path_elm = buf_path.split( "/" )
dprint "buf_path: %s" % buf_path
types = [ "app", "db", "lib", "test", "components", "script" ]
i = nil
ret = []
type = nil
types.each do |t|
i = path_elm.index( t )
break if i
end
type = path_elm[i]
type.downcase!
dprint "type: %s" % type
case type
when "app"
i += 1
subtype = path_elm[i]
subtype.downcase!
dprint "subtype: %s" % subtype
case subtype
when "views"
ret += ActionView::Base.instance_methods
ret += ActionView::Base.methods
when "controllers"
ret += ActionController::Base.instance_methods
ret += ActionController::Base.methods
when "models"
ret += ActiveRecord::Base.instance_methods
ret += ActiveRecord::Base.methods
end
when "db"
ret += ActiveRecord::ConnectionAdapters::SchemaStatements.instance_methods
ret += ActiveRecord::ConnectionAdapters::SchemaStatements.methods
end
return ret
end
def add_rails_columns( cls )
allow_rails = VIM::evaluate("exists('g:rubycomplete_rails') && g:rubycomplete_rails")
rails_loaded = VIM::evaluate('s:rubycomplete_rails_loaded')
return [] if allow_rails.to_i.zero? || rails_loaded.to_i.zero?
begin
eval( "#{cls}.establish_connection" )
return [] unless eval( "#{cls}.ancestors.include?(ActiveRecord::Base).to_s" )
col = eval( "#{cls}.column_names" )
return col if col
rescue
dprint "add_rails_columns err: (cls: %s) %s" % [ cls, $! ]
return []
end
return []
end
def clean_sel(sel, msg)
ret = sel.reject{|x|x.nil?}.uniq
ret = ret.grep(/^#{Regexp.quote(msg)}/) if msg != nil
ret
end
def get_rails_view_methods
allow_rails = VIM::evaluate("exists('g:rubycomplete_rails') && g:rubycomplete_rails")
rails_loaded = VIM::evaluate('s:rubycomplete_rails_loaded')
return [] if allow_rails.to_i.zero? || rails_loaded.to_i.zero?
buf_path = VIM::evaluate('expand("%:p")')
buf_path.gsub!( /\\/, "/" )
pelm = buf_path.split( "/" )
idx = pelm.index( "views" )
return [] unless idx
idx += 1
clspl = pelm[idx].camelize.pluralize
cls = clspl.singularize
ret = []
begin
ret += eval( "#{cls}.instance_methods" )
ret += eval( "#{clspl}Helper.instance_methods" )
rescue Exception
dprint "Error: Unable to load rails view helpers for %s: %s" % [ cls, $! ]
end
return ret
end
# }}} buffer analysis magic
# {{{ main completion code
def self.preload_rails
a = VimRubyCompletion.new
if VIM::evaluate("has('nvim')") == 0
require 'thread'
Thread.new(a) do |b|
begin
b.load_rails
rescue
end
end
end
a.load_rails
rescue
end
def self.get_completions(base)
b = VimRubyCompletion.new
b.get_completions base
end
def get_completions(base)
loading_allowed = VIM::evaluate("exists('g:rubycomplete_buffer_loading') && g:rubycomplete_buffer_loading")
if loading_allowed.to_i == 1
load_requires
load_rails
end
want_gems = VIM::evaluate("get(g:, 'rubycomplete_load_gemfile')")
load_gems unless want_gems.to_i.zero?
input = VIM::Buffer.current.line
cpos = VIM::Window.current.cursor[1] - 1
input = input[0..cpos]
input += base
input.sub!(/.*[ \t\n\"\\'`><=;|&{(]/, '') # Readline.basic_word_break_characters
input.sub!(/self\./, '')
input.sub!(/.*((\.\.[\[(]?)|([\[(]))/, '')
dprint 'input %s' % input
message = nil
receiver = nil
methods = []
variables = []
classes = []
constants = []
case input
when /^(\/[^\/]*\/)\.([^.]*)$/ # Regexp
receiver = $1
message = Regexp.quote($2)
methods = Regexp.instance_methods(true)
when /^([^\]]*\])\.([^.]*)$/ # Array
receiver = $1
message = Regexp.quote($2)
methods = Array.instance_methods(true)
when /^([^\}]*\})\.([^.]*)$/ # Proc or Hash
receiver = $1
message = Regexp.quote($2)
methods = Proc.instance_methods(true) | Hash.instance_methods(true)
when /^(:[^:.]*)$/ # Symbol
dprint "symbol"
if Symbol.respond_to?(:all_symbols)
receiver = $1
message = $1.sub( /:/, '' )
methods = Symbol.all_symbols.collect{|s| s.id2name}
methods.delete_if { |c| c.match( /'/ ) }
end
when /^::([A-Z][^:\.\(]*)?$/ # Absolute Constant or class methods
dprint "const or cls"
receiver = $1
methods = Object.constants.collect{ |c| c.to_s }.grep(/^#{receiver}/)
when /^(((::)?[A-Z][^:.\(]*)+?)::?([^:.]*)$/ # Constant or class methods
receiver = $1
message = Regexp.quote($4)
dprint "const or cls 2 [recv: \'%s\', msg: \'%s\']" % [ receiver, message ]
load_buffer_class( receiver )
load_buffer_module( receiver )
begin
constants = eval("#{receiver}.constants").collect{ |c| c.to_s }.grep(/^#{message}/)
methods = eval("#{receiver}.methods").collect{ |m| m.to_s }.grep(/^#{message}/)
rescue Exception
dprint "exception: %s" % $!
constants = []
methods = []
end
when /^(:[^:.]+)\.([^.]*)$/ # Symbol
dprint "symbol"
receiver = $1
message = Regexp.quote($2)
methods = Symbol.instance_methods(true)
when /^([0-9_]+(\.[0-9_]+)?(e[0-9]+)?)\.([^.]*)$/ # Numeric
dprint "numeric"
receiver = $1
message = Regexp.quote($4)
begin
methods = eval(receiver).methods
rescue Exception
methods = []
end
when /^(\$[^.]*)$/ #global
dprint "global"
methods = global_variables.grep(Regexp.new(Regexp.quote($1)))
when /^((\.?[^.]+)+?)\.([^.]*)$/ # variable
dprint "variable"
receiver = $1
message = Regexp.quote($3)
load_buffer_class( receiver )
cv = eval("self.class.constants")
vartype = get_var_type( receiver )
dprint "vartype: %s" % vartype
invalid_vartype = ['', "gets"]
if !invalid_vartype.include?(vartype)
load_buffer_class( vartype )
begin
methods = eval("#{vartype}.instance_methods")
variables = eval("#{vartype}.instance_variables")
rescue Exception
dprint "load_buffer_class err: %s" % $!
end
elsif (cv).include?(receiver)
# foo.func and foo is local var.
methods = eval("#{receiver}.methods")
vartype = receiver
elsif /^[A-Z]/ =~ receiver and /\./ !~ receiver
vartype = receiver
# Foo::Bar.func
begin
methods = eval("#{receiver}.methods")
rescue Exception
end
else
# func1.func2
ObjectSpace.each_object(Module){|m|
next if m.name != "IRB::Context" and
/^(IRB|SLex|RubyLex|RubyToken)/ =~ m.name
methods.concat m.instance_methods(false)
}
end
variables += add_rails_columns( "#{vartype}" ) if vartype && !invalid_vartype.include?(vartype)
when /^\(?\s*[A-Za-z0-9:^@.%\/+*\(\)]+\.\.\.?[A-Za-z0-9:^@.%\/+*\(\)]+\s*\)?\.([^.]*)/
message = $1
methods = Range.instance_methods(true)
when /^\.([^.]*)$/ # unknown(maybe String)
message = Regexp.quote($1)
methods = String.instance_methods(true)
else
dprint "default/other"
inclass = eval( VIM::evaluate("s:IsInClassDef()") )
if inclass != nil
dprint "inclass"
classdef = "%s\n" % VIM::Buffer.current[ inclass.min ]
found = /^\s*class\s*([A-Za-z0-9_-]*)(\s*<\s*([A-Za-z0-9_:-]*))?\s*\n$/.match( classdef )
if found != nil
receiver = $1
message = input
load_buffer_class( receiver )
begin
methods = eval( "#{receiver}.instance_methods" )
variables += add_rails_columns( "#{receiver}" )
rescue Exception
found = nil
end
end
end
if inclass == nil || found == nil
dprint "inclass == nil"
methods = get_buffer_methods
methods += get_rails_view_methods
cls_const = Class.constants
constants = cls_const.select { |c| /^[A-Z_-]+$/.match( c ) }
classes = eval("self.class.constants") - constants
classes += get_buffer_classes
classes += get_buffer_modules
include_objectspace = VIM::evaluate("exists('g:rubycomplete_include_objectspace') && g:rubycomplete_include_objectspace")
ObjectSpace.each_object(Class) { |cls| classes << cls.to_s } if include_objectspace == "1"
message = receiver = input
end
methods += get_rails_helpers
methods += Kernel.public_methods
end
include_object = VIM::evaluate("exists('g:rubycomplete_include_object') && g:rubycomplete_include_object")
methods = clean_sel( methods, message )
methods = (methods-Object.instance_methods) if include_object == "0"
rbcmeth = (VimRubyCompletion.instance_methods-Object.instance_methods) # lets remove those rubycomplete methods
methods = (methods-rbcmeth)
variables = clean_sel( variables, message )
classes = clean_sel( classes, message ) - ["VimRubyCompletion"]
constants = clean_sel( constants, message )
valid = []
valid += methods.collect { |m| { :name => m.to_s, :type => 'm' } }
valid += variables.collect { |v| { :name => v.to_s, :type => 'v' } }
valid += classes.collect { |c| { :name => c.to_s, :type => 't' } }
valid += constants.collect { |d| { :name => d.to_s, :type => 'd' } }
valid.sort! { |x,y| x[:name] <=> y[:name] }
outp = ""
rg = 0..valid.length
rg.step(150) do |x|
stpos = 0+x
enpos = 150+x
valid[stpos..enpos].each { |c| outp += "{'word':'%s','item':'%s','kind':'%s'}," % [ c[:name], c[:name], c[:type] ].map{|x|escape_vim_singlequote_string(x)} }
outp.sub!(/,$/, '')
VIM::command("call extend(g:rubycomplete_completions, [%s])" % outp)
outp = ""
end
end
# }}} main completion code
end # VimRubyCompletion
# }}} ruby completion
RUBYEOF
endfunction
let s:rubycomplete_rails_loaded = 0
call s:DefRuby()
"}}} ruby-side code
" vim:tw=78:sw=4:ts=8:et:fdm=marker:ft=vim:norl:

View file

@ -0,0 +1,39 @@
" Vim compiler file
" Language: eRuby
" Maintainer: Doug Kearns <dougkearns@gmail.com>
" URL: https://github.com/vim-ruby/vim-ruby
" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
if exists("current_compiler")
finish
endif
let current_compiler = "eruby"
if exists(":CompilerSet") != 2 " older Vim always used :setlocal
command -nargs=* CompilerSet setlocal <args>
endif
let s:cpo_save = &cpo
set cpo-=C
if exists("eruby_compiler") && eruby_compiler == "eruby"
CompilerSet makeprg=eruby
else
CompilerSet makeprg=erb
endif
CompilerSet errorformat=
\eruby:\ %f:%l:%m,
\%+E%f:%l:\ parse\ error,
\%W%f:%l:\ warning:\ %m,
\%E%f:%l:in\ %*[^:]:\ %m,
\%E%f:%l:\ %m,
\%-C%\t%\\d%#:%#\ %#from\ %f:%l:in\ %.%#,
\%-Z%\t%\\d%#:%#\ %#from\ %f:%l,
\%-Z%p^,
\%-G%.%#
let &cpo = s:cpo_save
unlet s:cpo_save
" vim: nowrap sw=2 sts=2 ts=8:

View file

@ -0,0 +1,39 @@
" Vim compiler file
" Language: Rake
" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
" URL: https://github.com/vim-ruby/vim-ruby
" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
if exists("current_compiler")
finish
endif
let current_compiler = "rake"
if exists(":CompilerSet") != 2 " older Vim always used :setlocal
command -nargs=* CompilerSet setlocal <args>
endif
let s:cpo_save = &cpo
set cpo-=C
CompilerSet makeprg=rake
CompilerSet errorformat=
\%D(in\ %f),
\%\\s%#%\\d%#:%#\ %#from\ %f:%l:%m,
\%\\s%#%\\d%#:%#\ %#from\ %f:%l:,
\%\\s%##\ %f:%l:%m%\\&%.%#%\\D:%\\d%\\+:%.%#,
\%\\s%##\ %f:%l%\\&%.%#%\\D:%\\d%\\+,
\%\\s%#[%f:%l:\ %#%m%\\&%.%#%\\D:%\\d%\\+:%.%#,
\%\\s%#%f:%l:\ %#%m%\\&%.%#%\\D:%\\d%\\+:%.%#,
\%\\s%#%f:%l:,
\%m\ [%f:%l]:,
\%+Erake\ aborted!,
\%+EDon't\ know\ how\ to\ build\ task\ %.%#,
\%+Einvalid\ option:%.%#,
\%+Irake\ %\\S%\\+%\\s%\\+#\ %.%#
let &cpo = s:cpo_save
unlet s:cpo_save
" vim: nowrap sw=2 sts=2 ts=8:

View file

@ -0,0 +1,35 @@
" Vim compiler file
" Language: RSpec
" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
" URL: https://github.com/vim-ruby/vim-ruby
" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
if exists("current_compiler")
finish
endif
let current_compiler = "rspec"
if exists(":CompilerSet") != 2 " older Vim always used :setlocal
command -nargs=* CompilerSet setlocal <args>
endif
let s:cpo_save = &cpo
set cpo-=C
CompilerSet makeprg=rspec
CompilerSet errorformat=
\%f:%l:\ %tarning:\ %m,
\%E%.%#:in\ `load':\ %f:%l:%m,
\%E%f:%l:in\ `%*[^']':\ %m,
\%-Z\ \ \ \ \ %\\+\#\ %f:%l:%.%#,
\%E\ \ \ \ \ Failure/Error:\ %m,
\%E\ \ \ \ \ Failure/Error:,
\%C\ \ \ \ \ %m,
\%C%\\s%#,
\%-G%.%#
let &cpo = s:cpo_save
unlet s:cpo_save
" vim: nowrap sw=2 sts=2 ts=8:

View file

@ -0,0 +1,44 @@
" Vim compiler file
" Language: Ruby
" Function: Syntax check and/or error reporting
" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
" URL: https://github.com/vim-ruby/vim-ruby
" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
if exists("current_compiler")
finish
endif
let current_compiler = "ruby"
if exists(":CompilerSet") != 2 " older Vim always used :setlocal
command -nargs=* CompilerSet setlocal <args>
endif
let s:cpo_save = &cpo
set cpo-=C
" default settings runs script normally
" add '-c' switch to run syntax check only:
"
" CompilerSet makeprg=ruby\ -c
"
" or add '-c' at :make command line:
"
" :make -c %<CR>
"
CompilerSet makeprg=ruby
CompilerSet errorformat=
\%+E%f:%l:\ parse\ error,
\%W%f:%l:\ warning:\ %m,
\%E%f:%l:in\ %*[^:]:\ %m,
\%E%f:%l:\ %m,
\%-C%\t%\\d%#:%#\ %#from\ %f:%l:in\ %.%#,
\%-Z%\t%\\d%#:%#\ %#from\ %f:%l,
\%-Z%p^,
\%-G%.%#
let &cpo = s:cpo_save
unlet s:cpo_save
" vim: nowrap sw=2 sts=2 ts=8:

View file

@ -0,0 +1,35 @@
" Vim compiler file
" Language: Test::Unit - Ruby Unit Testing Framework
" Maintainer: Doug Kearns <dougkearns@gmail.com>
" URL: https://github.com/vim-ruby/vim-ruby
" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
if exists("current_compiler")
finish
endif
let current_compiler = "rubyunit"
if exists(":CompilerSet") != 2 " older Vim always used :setlocal
command -nargs=* CompilerSet setlocal <args>
endif
let s:cpo_save = &cpo
set cpo-=C
CompilerSet makeprg=testrb
" CompilerSet makeprg=ruby\ -Itest
" CompilerSet makeprg=m
CompilerSet errorformat=\%W\ %\\+%\\d%\\+)\ Failure:,
\%C%m\ [%f:%l]:,
\%E\ %\\+%\\d%\\+)\ Error:,
\%C%m:,
\%C\ \ \ \ %f:%l:%.%#,
\%C%m,
\%Z\ %#,
\%-G%.%#
let &cpo = s:cpo_save
unlet s:cpo_save
" vim: nowrap sw=2 sts=2 ts=8:

View file

@ -0,0 +1,109 @@
RUBY *ft-ruby-indent*
Ruby: Access modifier indentation |ruby-access-modifier-indentation|
Ruby: Block style indentation |ruby-block-style-indentation|
Ruby: Assignment style indentation |ruby-assignment-style-indentation|
*ruby-access-modifier-indentation*
*g:ruby_indent_access_modifier_style*
Ruby: Access modifier indentation ~
Different access modifier indentation styles can be used by setting: >
:let g:ruby_indent_access_modifier_style = 'normal'
:let g:ruby_indent_access_modifier_style = 'indent'
:let g:ruby_indent_access_modifier_style = 'outdent'
<
By default, the "normal" access modifier style is used.
Access modifier style "normal":
>
class Indent
private :method
protected :method
private
def method; end
protected
def method; end
public
def method; end
end
<
Access modifier style "indent":
>
class Indent
private :method
protected :method
private
def method; end
protected
def method; end
public
def method; end
end
<
Access modifier style "outdent":
>
class Indent
private :method
protected :method
private
def method; end
protected
def method; end
public
def method; end
end
<
*ruby-block-style-indentation*
*g:ruby_indent_block_style*
Ruby: Block style indentation ~
Different block indentation styles can be used by setting: >
:let g:ruby_indent_block_style = 'expression'
:let g:ruby_indent_block_style = 'do'
<
By default, the "expression" block indent style is used.
Block indent style "expression":
>
first
.second do |x|
something
end
<
Block indent style "do":
>
first
.second do |x|
something
end
<
*ruby-assignment-style-indentation*
*g:ruby_indent_assignment_style*
Ruby: Assignment style indentation ~
Different styles of indenting assignment for multiline expressions:
>
:let g:ruby_indent_assignment_style = 'hanging'
:let g:ruby_indent_assignment_style = 'variable'
<
By default, the "hanging" style is used.
Assignment indent style "hanging":
>
x = if condition
something
end
<
Assignment indent style "variable":
>
x = if condition
something
end
<
vim:tw=78:sw=4:ts=8:ft=help:norl:

View file

@ -0,0 +1,51 @@
RUBY *ft-ruby-omni*
Completion of Ruby code requires that Vim be built with |+ruby|.
Ruby completion will parse your buffer on demand in order to provide a list of
completions. These completions will be drawn from modules loaded by "require"
and modules defined in the current buffer.
The completions provided by CTRL-X CTRL-O are sensitive to the context:
CONTEXT COMPLETIONS PROVIDED ~
1. Not inside a class definition Classes, constants and globals
2. Inside a class definition Methods or constants defined in the class
3. After '.', '::' or ':' Methods applicable to the object being
dereferenced
4. After ':' or ':foo' Symbol name (beginning with "foo")
Notes:
- Vim will load/evaluate code in order to provide completions. This may
cause some code execution, which may be a concern. This is no longer
enabled by default, to enable this feature add >
let g:rubycomplete_buffer_loading = 1
< - In context 1 above, Vim can parse the entire buffer to add a list of
classes to the completion results. This feature is turned off by default,
to enable it add >
let g:rubycomplete_classes_in_global = 1
< to your vimrc
- In context 2 above, anonymous classes are not supported.
- In context 3 above, Vim will attempt to determine the methods supported by
the object.
- Vim can detect and load the Rails environment for files within a rails
project. The feature is disabled by default, to enable it add >
let g:rubycomplete_rails = 1
< to your vimrc
- Vim can parse a Gemfile, in case gems are being implicitly required. To
activate the feature: >
let g:rubycomplete_load_gemfile = 1
< To specify an alternative path, use: >
let g:rubycomplete_gemfile_path = 'Gemfile.aux'
< To use Bundler.require instead of parsing the Gemfile, set: >
let g:rubycomplete_use_bundler = 1
< To use custom paths that should be added to $LOAD_PATH to correctly
resolve requires, set: >
let g:rubycomplete_load_paths = ["/path/to/code", "./lib/example"]
vim:tw=78:sw=4:ts=8:ft=help:norl:

View file

@ -0,0 +1,79 @@
RUBY *ft-ruby-plugin*
Ruby: Recommended settings |ruby-recommended|
Ruby: Motion commands |ruby-motion|
Ruby: Text objects |ruby-text-objects|
*ruby-recommended*
*g:ruby_recommended_style*
Ruby: Recommended settings ~
The `g:ruby_recommended_style` variable activates indentation settings
according to the most common ruby convention: two spaces for indentation. It's
turned on by default to ensure an unsurprising default experience for most
ruby developers.
If you'd like to enforce your own style, it's possible to apply your own
preferences in your own configuration in `after/ftplugin/ruby.vim`. You can
also disable the setting by setting the variable to 0:
>
let g:ruby_recommended_style = 0
<
*ruby-motion*
Ruby: Motion commands ~
Vim provides motions such as |[m| and |]m| for jumping to the start or end of
a method definition. Out of the box, these work for curly-bracket languages,
but not for Ruby. The vim-ruby plugin enhances these motions, by making them
also work on Ruby files.
*ruby-]m*
]m Go to start of next method definition.
*ruby-]M*
]M Go to end of next method definition.
*ruby-[m*
[m Go to start of previous method definition.
*ruby-[M*
[M Go to end of previous method definition.
*ruby-]]*
]] Go to start of next module or class definition.
*ruby-][*
][ Go to end of next module or class definition.
*ruby-[[*
[[ Go to start of previous module or class definition.
*ruby-[]*
[] Go to end of previous module or class definition.
*ruby-text-objects*
Ruby: Text objects ~
Vim's |text-objects| can be used to select or operate upon regions of text
that are defined by structure. The vim-ruby plugin adds text objects for
operating on methods and classes.
*ruby-v_am* *ruby-am*
am "a method", select from "def" until matching "end"
keyword.
*ruby-v_im* *ruby-im*
im "inner method", select contents of "def"/"end" block,
excluding the "def" and "end" themselves.
*ruby-v_aM* *ruby-aM*
aM "a class", select from "class" until matching "end"
keyword.
*ruby-v_iM* *ruby-iM*
iM "inner class", select contents of "class"/"end"
block, excluding the "class" and "end" themselves.
vim:tw=78:sw=4:ts=8:ft=help:norl:

View file

@ -0,0 +1,118 @@
RUBY *ruby.vim* *ft-ruby-syntax*
Ruby: Operator highlighting |ruby_operators|
Ruby: Whitespace errors |ruby_space_errors|
Ruby: Syntax errors |ruby_syntax_errors|
Ruby: Folding |ruby_fold| |ruby_foldable_groups|
Ruby: Reducing expensive operations |ruby_no_expensive| |ruby_minlines|
Ruby: Spellchecking strings |ruby_spellcheck_strings|
*ruby_operators*
Ruby: Operator highlighting ~
Operators, and pseudo operators, can be highlighted by defining: >
:let ruby_operators = 1
:let ruby_pseudo_operators = 1
<
The supported pseudo operators are ., &., ::, *, **, &, <, << and ->.
*ruby_space_errors*
Ruby: Whitespace errors ~
Whitespace errors can be highlighted by defining "ruby_space_errors": >
:let ruby_space_errors = 1
<
This will highlight trailing whitespace and tabs preceded by a space character
as errors. This can be refined by defining "ruby_no_trail_space_error" and
"ruby_no_tab_space_error" which will ignore trailing whitespace and tabs after
spaces respectively.
*ruby_syntax_errors*
Ruby: Syntax errors ~
Redundant line continuations and predefined global variable look-alikes (such
as $# and $-z) can be highlighted as errors by defining:
>
:let ruby_line_continuation_error = 1
:let ruby_global_variable_error = 1
<
*ruby_fold*
Ruby: Folding ~
Folding can be enabled by defining "ruby_fold": >
:let ruby_fold = 1
<
This will set the value of 'foldmethod' to "syntax" locally to the current
buffer or window, which will enable syntax-based folding when editing Ruby
filetypes.
*ruby_foldable_groups*
Default folding is rather detailed, i.e., small syntax units like "if", "do",
"%w[]" may create corresponding fold levels.
You can set "ruby_foldable_groups" to restrict which groups are foldable: >
:let ruby_foldable_groups = 'if case %'
<
The value is a space-separated list of keywords:
keyword meaning ~
-------- ------------------------------------- ~
ALL Most block syntax (default)
NONE Nothing
if "if" or "unless" block
def "def" block
class "class" block
module "module" block
do "do" block
begin "begin" block
case "case" block
for "for", "while", "until" loops
{ Curly bracket block or hash literal
[ Array literal
% Literal with "%" notation, e.g.: %w(STRING), %!STRING!
/ Regexp
string String and shell command output (surrounded by ', ", `)
: Symbol
# Multiline comment
<< Here documents
__END__ Source code after "__END__" directive
NONE and ALL have priority, in that order, over all other folding groups.
*ruby_no_expensive*
Ruby: Reducing expensive operations ~
By default, the "end" keyword is colorized according to the opening statement
of the block it closes. While useful, this feature can be expensive; if you
experience slow redrawing (or you are on a terminal with poor color support)
you may want to turn it off by defining the "ruby_no_expensive" variable: >
:let ruby_no_expensive = 1
<
In this case the same color will be used for all control keywords.
*ruby_minlines*
If you do want this feature enabled, but notice highlighting errors while
scrolling backwards, which are fixed when redrawing with CTRL-L, try setting
the "ruby_minlines" variable to a value larger than 50: >
:let ruby_minlines = 100
<
Ideally, this value should be a number of lines large enough to embrace your
largest class or module.
*ruby_spellcheck_strings*
Ruby: Spellchecking strings ~
Ruby syntax will perform spellchecking of strings if you define
"ruby_spellcheck_strings": >
:let ruby_spellcheck_strings = 1
<
vim:tw=78:sw=4:ts=8:ft=help:norl:

View file

@ -0,0 +1,621 @@
#!/usr/bin/env ruby
arg = ARGV.pop
# Usage example:
#
# ./etc/examples/generators/syntax.rb %Q > etc/examples/syntax/Q.rb
#
# then read the output file with 'foldlevel' 0
puts "# Generated by `" <<
"./etc/examples/generators/syntax.rb #{arg}" <<
" > etc/examples/syntax/#{arg.sub('%', '')}.rb" <<
"`\n\n"
# %Q {{{
# Generalized Double Quoted String and Array of Strings and Shell Command Output
if arg == '%Q'
# Note: %= is not matched here as the beginning of a double quoted string
%Q[~`!@\#$%^&*_-+|:;"',.?/].split(//).each do |s|
puts <<-END.gsub(/^\s{4}/, '')
%#{s}
foo
\\#{s}
\\\\\\#{s}
bar
#{s}
END
end
%w(Q W x).each do |leading|
%Q[~`!@\#$%^&*_-+=|:;"',.?/].split(//).each do |s|
puts <<-END.gsub(/^\s{6}/, '')
%#{leading}#{s}
foo
\\#{s}
\\\\\\#{s}
bar
#{s}
END
end
%w({} <> [] ()).each do |pair|
puts <<-END.gsub(/^\s{6}/, '')
%#{leading}#{pair[0]}
foo
\\#{pair[1]}
\\\\\\#{pair[1]}
bar
#{pair[1]}
END
end
puts " %#{leading} foo\\ \\\\\\ bar\nbaz \n\n" unless leading == 'W'
end
end
# }}}
# %q {{{
# Generalized Single Quoted String, Symbol and Array of Strings
if arg == '%q'
%w(q w s).each do |leading|
%Q[~`!@\#$%^&*_-+=|:;"',.?/].split(//).each do |s|
puts <<-END.gsub(/^\s{6}/, '')
%#{leading}#{s}
foo
\\#{s}
\\\\\\#{s}
bar
#{s}
END
end
%w({} <> [] ()).each do |pair|
puts <<-END.gsub(/^\s{6}/, '')
%#{leading}#{pair[0]}
foo
\\#{pair[1]}
\\\\\\#{pair[1]}
bar
#{pair[1]}
END
end
puts " %#{leading} foo\\ \\\\\\ bar\nbaz \n\n" unless leading == 'w'
end
end
# }}}
# %r {{{
# Generalized Regular Expression
if arg == '%r'
%Q[~`!@\#$%^&*_-+=|:;"',.?/].split(//).each do |s|
puts <<-END.gsub(/^\s{4}/, '')
%r#{s}
foo
\\#{s}
\\\\\\#{s}
bar
#{s}
END
end
puts " %r foo\\ \\\\\\ bar\nbaz \n\n"
%w({} <> [] ()).each do |pair|
puts <<-END.gsub(/^\s{4}/, '')
%r#{pair[0]}
foo
\\#{pair[1]}
\\\\\\#{pair[1]}
bar
#{pair[1]}
END
end
end
# }}}
# %i / %I {{{
# Array of Symbols
# Array of interpolated Symbols
if %w(%i %I).include?(arg)
%w(i I).each do |leading|
%Q[~`!@\#$%^&*_-+=|:;"',.?/].split(//).each do |s|
puts <<-END.gsub(/^\s{6}/, '')
%#{leading}#{s}
foo
\\#{s}
\\\\\\#{s}
bar
#{s}
END
end
%w({} <> [] ()).each do |pair|
puts <<-END.gsub(/^\s{6}/, '')
%#{leading}#{pair[0]}
foo
\\#{pair[1]}
\\\\\\#{pair[1]}
bar
#{pair[1]}
END
end
end
end
# }}}
# string {{{
# Normal String and Shell Command Output
if arg == 'string'
%w(' " `).each do |quote|
puts <<-END.gsub(/^\s{4}/, '')
#{quote}
foo
\\#{quote}
\\\\\\#{quote}
bar
#{quote}
END
end
end
# }}}
# regex (Normal Regular Expression) {{{
if arg == 'regexp'
'iomxneus'.split('').unshift('').each do |option|
puts "\n# Begin test for option '#{option}' {{{\n\n"
puts <<-END.gsub(/^\s{4}/, '')
/
foo
\\\/
\\\\\\\/
bar
/#{option}
END
%w(and or while until unless if elsif when not then else).each do |s|
puts <<-END.gsub(/^\s{6}/, '')
#{s}/
foo
\\\/
\\\\\\\/
bar
/#{option}
END
end
%w(; \ ~ = ! | \( & , { [ < > ? : * + -).each do |s|
puts <<-END.gsub(/^\s{6}/, '')
#{s}/
foo
\\\/
\\\\\\\/
bar
/#{option}
END
end
[' ', "\t", '=', 'OK'].each do |s|
puts <<-END.gsub(/^\s{6}/, '')
_foo /#{s}
foo
\\\/
\\\\\\\/
bar
/#{option}
END
end
puts "# }}} End test for option '#{option}'\n"
end
puts "\n# Test for ternary operation (8c1c484) {{{\n\n"
puts 'yo = 4 ? /quack#{3}/ : /quack/'
puts "\n# }}} End test for ternary operation\n"
end
# }}}
# symbol {{{
# Symbol region
if arg == 'symbol'
%w(' ").each do |quote|
%Q_]})\"':_.split(//).unshift('').each do |s|
puts <<-END.gsub(/^\s{6}/, '')
#{s}:#{quote}
foo
\\#{quote}
\\\\\\#{quote}
bar
#{quote}
#{" #{s} # close string to ensure next case clean" if %w(' ").include?(s) && s != quote }
END
end
end
end
# }}}
# heredoc {{{
# Here Documents
if arg == 'heredoc'
puts "\n# Begin of valid cases {{{\n\n"
%w(' " `).unshift('').each do |quote|
puts <<-END.gsub(/^\s{6}/, '')
<<#{quote}_LABEL#{quote}.?!, foo
bar baz
_LABEL
\n
<<-#{quote}_LABEL#{quote}.?!, foo
bar baz
_LABEL
\n
<<~#{quote}_LABEL#{quote}.?!, foo
bar baz
_LABEL
END
end
puts "# }}} End of valid cases'\n\n"
puts "\n# Begin of INVALID cases {{{\n\n"
# NOTE: for simplification, omit test for different quotes " ' `,
# they are all invalid anyway
%w(class ::).each do |s|
puts <<-END.gsub(/^\s{6}/, '')
#{s}\n <<LABEL
foo
LABEL
END
end
%Q_]})\"'._.split(//).each do |s|
puts <<-END.gsub(/^\s{4}/, '')
#{s} <<LABEL
foo
LABEL
#{" #{s} # close to ensure next case clean" if %w(' ").include?(s)}
END
end
%w(09 aZ _w).each do |s|
puts <<-END.gsub(/^\s{6}/, '')
#{s}<<LABEL
foo
LABEL
END
end
%w(' " `).unshift('').each do |quote|
puts <<-END.gsub(/^\s{6}/, '')
<<LABEL foo<<#{quote}_bar
baz
LABEL
#{" #{quote} # close to ensure next case clean" if %w(' ").include?(quote)}
\n
<<LABEL foo<<-#{quote}_bar
baz
LABEL
#{" #{quote} # close to ensure next case clean" if %w(' ").include?(quote)}
END
end
puts "# }}} End of INVALID cases'\n\n"
end
# }}}
# blocks {{{
# simple blocks (def, class, module, do, begin, case)
if arg == 'blocks'
puts <<-END.gsub(/^\s{4}/, '')
def
foo
def
bar
end
end
END
%w(class module do begin case).each do |s|
puts <<-END.gsub(/^\s{6}/, '')
#{s}
foo
end
END
end
end
# }}}
# brackets {{{
# curly bracket block and hash literal
if arg == 'brackets'
puts <<-END.gsub(/^\s{4}/, '')
{
foo
}
END
%w<_xxx ] } )>.unshift('').each do |s|
puts <<-END.gsub(/^\s{6}/, '')
#{s}[
foo
]
END
end
end
# }}}
# if {{{
# if/else blocks
if arg == 'if'
%w(if unless).each do |start|
puts <<-END.gsub(/^ {6}/, '')
#{start} 1
foo
else
bar
end
foo \\
#{start} 1
foo
else
bar
end
baz ...= #{start} 1
foo
else
bar
end
END
['?', '!'].each do |mark|
puts <<-END.gsub(/^ {8}/, '')
42foo#{mark} #{start} 1
bar
else
baz
end
END
end
'{:,;([<>~\*%&^|+=-'.split(//).each do |expr|
puts <<-END.gsub(/^ {8}/, '')
foo #{expr} #{start} 1
bar
else
baz
end
END
end
# c7cb532 match correct `end`
puts <<-END.gsub(/^ {6}/, '')
#{start} 1
(1..5).end
:: end
end
#{start} 1
..end
END
# INVALID cases
puts <<-END.gsub(/^ {6}/, '')
not_BOF #{start} 1
bar
else
baz
end
END
['?', '!'].each do |mark|
puts <<-END.gsub(/^ {8}/, '')
_foo#{mark} #{start} 1
bar
else
baz
end
END
end
end
end
# }}}
# for {{{
# rubyRepeatExpression (for, while, until)
if arg == 'for'
puts <<-END.gsub(/^ {4}/, '')
for 1
foo
end
END
%w(until while).each do |start|
puts <<-END.gsub(/^ {6}/, '')
#{start} 1
foo
end
baz ...= #{start} 1
foo
end
END
'{:,;([<>~\*/%&^|+-'.split(//).each do |expr|
puts <<-END.gsub(/^ {8}/, '')
foo #{expr} #{start} 1
bar
end
END
end
# INVALID cases
puts <<-END.gsub(/^ {6}/, '')
not_BOF #{start} 1
bar
end
END
['?', '!'].each do |mark|
puts <<-END.gsub(/^ {8}/, '')
_foo#{mark} #{start} 1
bar
end
END
end
end
end
# }}}
# comment {{{
if arg == 'comment'
puts <<-END.gsub(/^ {4}/, '')
# foo
# foo
# bar
baz
=begin foo bar
comment
=end baz
END
end
# }}}
# __END__ {{{
if arg == '__END__'
puts <<-EOF.gsub(/^ {4}/, '')
__END__
invalid
invalid
__END__
valid
valid
EOF
end
# }}}
puts "#\svim:foldmethod=syntax"
# vim:foldmethod=marker

View file

@ -0,0 +1,16 @@
[1, [2,
[3],
3],
4]
[1, [2,
3],
4]
[1, {2 =>
3},
4]
[1, f(2,
3),
4]

View file

@ -0,0 +1,42 @@
" Officially distributed filetypes
" Support functions {{{
function! s:setf(filetype) abort
if &filetype !~# '\<'.a:filetype.'\>'
let &filetype = a:filetype
endif
endfunction
func! s:StarSetf(ft)
if expand("<amatch>") !~ g:ft_ignore_pat
exe 'setf ' . a:ft
endif
endfunc
" }}}
" HTML with Ruby - eRuby
au BufNewFile,BufRead *.erb,*.rhtml call s:setf('eruby')
" Interactive Ruby shell
au BufNewFile,BufRead .irbrc,irbrc call s:setf('ruby')
" Ruby
au BufNewFile,BufRead *.rb,*.rbw,*.gemspec call s:setf('ruby')
" Rackup
au BufNewFile,BufRead *.ru call s:setf('ruby')
" Bundler
au BufNewFile,BufRead Gemfile call s:setf('ruby')
" Ruby on Rails
au BufNewFile,BufRead *.builder,*.rxml,*.rjs,*.ruby call s:setf('ruby')
" Rakefile
au BufNewFile,BufRead [rR]akefile,*.rake call s:setf('ruby')
au BufNewFile,BufRead [rR]akefile* call s:StarSetf('ruby')
" Rantfile
au BufNewFile,BufRead [rR]antfile,*.rant call s:setf('ruby')
" vim: nowrap sw=2 sts=2 ts=8 noet fdm=marker:

View file

@ -0,0 +1,66 @@
" All other filetypes
" Support functions {{{
function! s:setf(filetype) abort
if &filetype !=# a:filetype
let &filetype = a:filetype
endif
endfunction
" }}}
" Appraisal
au BufNewFile,BufRead Appraisals call s:setf('ruby')
" Autotest
au BufNewFile,BufRead .autotest call s:setf('ruby')
" Axlsx
au BufNewFile,BufRead *.axlsx call s:setf('ruby')
" Buildr Buildfile
au BufNewFile,BufRead [Bb]uildfile call s:setf('ruby')
" Capistrano
au BufNewFile,BufRead Capfile,*.cap call s:setf('ruby')
" Chef
au BufNewFile,BufRead Cheffile call s:setf('ruby')
au BufNewFile,BufRead Berksfile call s:setf('ruby')
" CocoaPods
au BufNewFile,BufRead Podfile,*.podspec call s:setf('ruby')
" Guard
au BufNewFile,BufRead Guardfile,.Guardfile call s:setf('ruby')
" Jbuilder
au BufNewFile,BufRead *.jbuilder call s:setf('ruby')
" Kitchen Sink
au BufNewFile,BufRead KitchenSink call s:setf('ruby')
" Opal
au BufNewFile,BufRead *.opal call s:setf('ruby')
" Pry config
au BufNewFile,BufRead .pryrc call s:setf('ruby')
" Puppet librarian
au BufNewFile,BufRead Puppetfile call s:setf('ruby')
" Rabl
au BufNewFile,BufRead *.rabl call s:setf('ruby')
" Routefile
au BufNewFile,BufRead [rR]outefile call s:setf('ruby')
" SimpleCov
au BufNewFile,BufRead .simplecov call s:setf('ruby')
" Thor
au BufNewFile,BufRead [tT]horfile,*.thor call s:setf('ruby')
" Vagrant
au BufNewFile,BufRead [vV]agrantfile call s:setf('ruby')
" vim: nowrap sw=2 sts=2 ts=8 noet fdm=marker:

View file

@ -0,0 +1,131 @@
" Vim filetype plugin
" Language: eRuby
" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
" URL: https://github.com/vim-ruby/vim-ruby
" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
" Only do this when not done yet for this buffer
if exists("b:did_ftplugin")
finish
endif
let s:save_cpo = &cpo
set cpo-=C
" Define some defaults in case the included ftplugins don't set them.
let s:undo_ftplugin = ""
let s:browsefilter = "All Files (*.*)\t*.*\n"
let s:match_words = ""
if !exists("g:eruby_default_subtype")
let g:eruby_default_subtype = "html"
endif
if &filetype =~ '^eruby\.'
let b:eruby_subtype = matchstr(&filetype,'^eruby\.\zs\w\+')
elseif !exists("b:eruby_subtype")
let s:lines = getline(1)."\n".getline(2)."\n".getline(3)."\n".getline(4)."\n".getline(5)."\n".getline("$")
let b:eruby_subtype = matchstr(s:lines,'eruby_subtype=\zs\w\+')
if b:eruby_subtype == ''
let b:eruby_subtype = matchstr(substitute(expand("%:t"),'\c\%(\.erb\|\.eruby\|\.erubis\|\.example\)\+$','',''),'\.\zs\w\+\%(\ze+\w\+\)\=$')
endif
if b:eruby_subtype == 'rhtml'
let b:eruby_subtype = 'html'
elseif b:eruby_subtype == 'rb'
let b:eruby_subtype = 'ruby'
elseif b:eruby_subtype == 'yml'
let b:eruby_subtype = 'yaml'
elseif b:eruby_subtype == 'js'
let b:eruby_subtype = 'javascript'
elseif b:eruby_subtype == 'txt'
" Conventional; not a real file type
let b:eruby_subtype = 'text'
elseif b:eruby_subtype == ''
let b:eruby_subtype = g:eruby_default_subtype
endif
endif
if exists("b:eruby_subtype") && b:eruby_subtype != '' && b:eruby_subtype !=? 'eruby'
exe "runtime! ftplugin/".b:eruby_subtype.".vim ftplugin/".b:eruby_subtype."_*.vim ftplugin/".b:eruby_subtype."/*.vim"
else
runtime! ftplugin/html.vim ftplugin/html_*.vim ftplugin/html/*.vim
endif
unlet! b:did_ftplugin
" Override our defaults if these were set by an included ftplugin.
if exists("b:undo_ftplugin")
let s:undo_ftplugin = b:undo_ftplugin
unlet b:undo_ftplugin
endif
if exists("b:browsefilter")
let s:browsefilter = b:browsefilter
unlet b:browsefilter
endif
if exists("b:match_words")
let s:match_words = b:match_words
unlet b:match_words
endif
let s:cfilemap = v:version >= 704 ? maparg('<Plug><cfile>', 'c', 0, 1) : {}
if !get(s:cfilemap, 'buffer') || !s:cfilemap.expr || s:cfilemap.rhs =~# 'ErubyAtCursor()'
let s:cfilemap = {}
endif
if !has_key(s:cfilemap, 'rhs')
let s:cfilemap.rhs = "substitute(&l:inex =~# '\\<v:fname\\>' && len(expand('<cfile>')) ? eval(substitute(&l:inex, '\\<v:fname\\>', '\\=string(expand(\"<cfile>\"))', 'g')) : '', '^$', \"\\022\\006\",'')"
endif
let s:ctagmap = v:version >= 704 ? maparg('<Plug><ctag>', 'c', 0, 1) : {}
if !get(s:ctagmap, 'buffer') || !s:ctagmap.expr || s:ctagmap.rhs =~# 'ErubyAtCursor()'
let s:ctagmap = {}
endif
let s:include = &l:include
let s:path = &l:path
let s:suffixesadd = &l:suffixesadd
runtime! ftplugin/ruby.vim ftplugin/ruby_*.vim ftplugin/ruby/*.vim
let b:did_ftplugin = 1
" Combine the new set of values with those previously included.
if exists("b:undo_ftplugin")
let s:undo_ftplugin = b:undo_ftplugin . " | " . s:undo_ftplugin
endif
if exists ("b:browsefilter")
let s:browsefilter = substitute(b:browsefilter,'\cAll Files (\*\.\*)\t\*\.\*\n','','') . s:browsefilter
endif
if exists("b:match_words")
let s:match_words = b:match_words . ',' . s:match_words
endif
if len(s:include)
let &l:include = s:include
endif
let &l:path = s:path . (s:path =~# ',$\|^$' ? '' : ',') . &l:path
let &l:suffixesadd = s:suffixesadd . (s:suffixesadd =~# ',$\|^$' ? '' : ',') . &l:suffixesadd
exe 'cmap <buffer><script><expr> <Plug><cfile> ErubyAtCursor() ? ' . maparg('<Plug><cfile>', 'c') . ' : ' . s:cfilemap.rhs
exe 'cmap <buffer><script><expr> <Plug><ctag> ErubyAtCursor() ? ' . maparg('<Plug><ctag>', 'c') . ' : ' . get(s:ctagmap, 'rhs', '"\022\027"')
unlet s:cfilemap s:ctagmap s:include s:path s:suffixesadd
" Change the browse dialog on Win32 to show mainly eRuby-related files
if has("gui_win32")
let b:browsefilter="eRuby Files (*.erb, *.rhtml)\t*.erb;*.rhtml\n" . s:browsefilter
endif
" Load the combined list of match_words for matchit.vim
if exists("loaded_matchit")
let b:match_words = s:match_words
endif
" TODO: comments=
setlocal commentstring=<%#%s%>
let b:undo_ftplugin = "setl cms< "
\ " | unlet! b:browsefilter b:match_words | " . s:undo_ftplugin
let &cpo = s:save_cpo
unlet s:save_cpo
function! ErubyAtCursor() abort
let groups = map(['erubyBlock', 'erubyComment', 'erubyExpression', 'erubyOneLiner'], 'hlID(v:val)')
return !empty(filter(synstack(line('.'), col('.')), 'index(groups, v:val) >= 0'))
endfunction
" vim: nowrap sw=2 sts=2 ts=8:

View file

@ -0,0 +1,436 @@
" Vim filetype plugin
" Language: Ruby
" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
" URL: https://github.com/vim-ruby/vim-ruby
" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
if (exists("b:did_ftplugin"))
finish
endif
let b:did_ftplugin = 1
let s:cpo_save = &cpo
set cpo&vim
if has("gui_running") && !has("gui_win32")
setlocal keywordprg=ri\ -T\ -f\ bs
else
setlocal keywordprg=ri
endif
" Matchit support
if exists("loaded_matchit") && !exists("b:match_words")
let b:match_ignorecase = 0
let b:match_words =
\ '{\|\<\%(if\|unless\|case\|while\|until\|for\|do\|class\|module\|def\|=\@<!begin\)\>=\@!' .
\ ':' .
\ '\<\%(else\|elsif\|ensure\|when\|rescue\|break\|redo\|next\|retry\)\>' .
\ ':' .
\ '}\|\%(^\|[^.\:@$=]\)\@<=\<end\:\@!\>' .
\ ',^=begin\>:^=end\>,' .
\ ',\[:\],(:)'
let b:match_skip =
\ "synIDattr(synID(line('.'),col('.'),0),'name') =~ '" .
\ "\\<ruby\\%(String\\|.\+Delimiter\\|Character\\|.\+Escape\\|" .
\ "Regexp\\|Interpolation\\|Comment\\|Documentation\\|" .
\ "ConditionalModifier\\|RepeatModifier\\|RescueModifier\\|OptionalDo\\|" .
\ "MethodName\\|BlockArgument\\|KeywordAsMethod\\|ClassVariable\\|" .
\ "InstanceVariable\\|GlobalVariable\\|Symbol\\)\\>'"
endif
setlocal formatoptions-=t formatoptions+=croql
setlocal include=^\\s*\\<\\(load\\>\\\|require\\>\\\|autoload\\s*:\\=[\"']\\=\\h\\w*[\"']\\=,\\)
setlocal suffixesadd=.rb
if exists("&ofu") && has("ruby")
setlocal omnifunc=rubycomplete#Complete
endif
" TODO:
"setlocal define=^\\s*def
setlocal comments=:#
setlocal commentstring=#\ %s
if !exists('g:ruby_version_paths')
let g:ruby_version_paths = {}
endif
function! s:query_path(root) abort
let code = "print $:.join %q{,}"
if &shell =~# 'sh' && empty(&shellxquote)
let prefix = 'env PATH='.shellescape($PATH).' '
else
let prefix = ''
endif
if &shellxquote == "'"
let path_check = prefix.'ruby --disable-gems -e "' . code . '"'
else
let path_check = prefix."ruby --disable-gems -e '" . code . "'"
endif
let cd = haslocaldir() ? 'lcd' : 'cd'
let cwd = fnameescape(getcwd())
try
exe cd fnameescape(a:root)
let path = split(system(path_check),',')
exe cd cwd
return path
finally
exe cd cwd
endtry
endfunction
function! s:build_path(path) abort
let path = join(map(copy(a:path), 'v:val ==# "." ? "" : v:val'), ',')
if &g:path !~# '\v^%(\.,)=%(/%(usr|emx)/include,)=,$'
let path = substitute(&g:path,',,$',',','') . ',' . path
endif
return path
endfunction
if !exists('b:ruby_version') && !exists('g:ruby_path') && isdirectory(expand('%:p:h'))
let s:version_file = findfile('.ruby-version', '.;')
if !empty(s:version_file) && filereadable(s:version_file)
let b:ruby_version = get(readfile(s:version_file, '', 1), '')
if !has_key(g:ruby_version_paths, b:ruby_version)
let g:ruby_version_paths[b:ruby_version] = s:query_path(fnamemodify(s:version_file, ':p:h'))
endif
endif
endif
if exists("g:ruby_path")
let s:ruby_path = type(g:ruby_path) == type([]) ? join(g:ruby_path, ',') : g:ruby_path
elseif has_key(g:ruby_version_paths, get(b:, 'ruby_version', ''))
let s:ruby_paths = g:ruby_version_paths[b:ruby_version]
let s:ruby_path = s:build_path(s:ruby_paths)
else
if !exists('g:ruby_default_path')
if has("ruby") && has("win32")
ruby ::VIM::command( 'let g:ruby_default_path = split("%s",",")' % $:.join(%q{,}) )
elseif executable('ruby')
let g:ruby_default_path = s:query_path($HOME)
else
let g:ruby_default_path = map(split($RUBYLIB,':'), 'v:val ==# "." ? "" : v:val')
endif
endif
let s:ruby_paths = g:ruby_default_path
let s:ruby_path = s:build_path(s:ruby_paths)
endif
if stridx(&l:path, s:ruby_path) == -1
let &l:path = s:ruby_path
endif
if exists('s:ruby_paths') && stridx(&l:tags, join(map(copy(s:ruby_paths),'v:val."/tags"'),',')) == -1
let &l:tags = &tags . ',' . join(map(copy(s:ruby_paths),'v:val."/tags"'),',')
endif
if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
let b:browsefilter = "Ruby Source Files (*.rb)\t*.rb\n" .
\ "All Files (*.*)\t*.*\n"
endif
let b:undo_ftplugin = "setl inc= sua= path= tags= fo< com< cms< kp="
\."| unlet! b:browsefilter b:match_ignorecase b:match_words b:match_skip"
\."| if exists('&ofu') && has('ruby') | setl ofu< | endif"
if get(g:, 'ruby_recommended_style', 1)
setlocal shiftwidth=2 softtabstop=2 expandtab
let b:undo_ftplugin .= ' | setl sw< sts< et<'
endif
" To activate, :set ballooneval
if exists('+balloonexpr') && get(g:, 'ruby_balloonexpr')
setlocal balloonexpr=RubyBalloonexpr()
let b:undo_ftplugin .= "| setl bexpr="
endif
function! s:map(mode, flags, map) abort
let from = matchstr(a:map, '\S\+')
if empty(mapcheck(from, a:mode))
exe a:mode.'map' '<buffer>' a:flags a:map
let b:undo_ftplugin .= '|sil! '.a:mode.'unmap <buffer> '.from
endif
endfunction
cmap <buffer><script><expr> <Plug><ctag> substitute(RubyCursorTag(),'^$',"\022\027",'')
cmap <buffer><script><expr> <Plug><cfile> substitute(RubyCursorFile(),'^$',"\022\006",'')
let b:undo_ftplugin .= "| sil! cunmap <buffer> <Plug><ctag>| sil! cunmap <buffer> <Plug><cfile>"
if !exists("g:no_plugin_maps") && !exists("g:no_ruby_maps")
nmap <buffer><script> <SID>: :<C-U>
nmap <buffer><script> <SID>c: :<C-U><C-R>=v:count ? v:count : ''<CR>
nnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','n')<CR>
nnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','n')<CR>
nnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','n')<CR>
nnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','n')<CR>
xnoremap <silent> <buffer> [m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'b','v')<CR>
xnoremap <silent> <buffer> ]m :<C-U>call <SID>searchsyn('\<def\>',['rubyDefine'],'','v')<CR>
xnoremap <silent> <buffer> [M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'b','v')<CR>
xnoremap <silent> <buffer> ]M :<C-U>call <SID>searchsyn('\<end\>',['rubyDefine'],'','v')<CR>
nnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','n')<CR>
nnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','n')<CR>
nnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','n')<CR>
nnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','n')<CR>
xnoremap <silent> <buffer> [[ :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'b','v')<CR>
xnoremap <silent> <buffer> ]] :<C-U>call <SID>searchsyn('\<\%(class\<Bar>module\)\>',['rubyModule','rubyClass'],'','v')<CR>
xnoremap <silent> <buffer> [] :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'b','v')<CR>
xnoremap <silent> <buffer> ][ :<C-U>call <SID>searchsyn('\<end\>',['rubyModule','rubyClass'],'','v')<CR>
let b:undo_ftplugin = b:undo_ftplugin
\."| sil! exe 'unmap <buffer> [[' | sil! exe 'unmap <buffer> ]]' | sil! exe 'unmap <buffer> []' | sil! exe 'unmap <buffer> ]['"
\."| sil! exe 'unmap <buffer> [m' | sil! exe 'unmap <buffer> ]m' | sil! exe 'unmap <buffer> [M' | sil! exe 'unmap <buffer> ]M'"
if maparg('im','x') == '' && maparg('im','o') == '' && maparg('am','x') == '' && maparg('am','o') == ''
onoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
onoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
xnoremap <silent> <buffer> im :<C-U>call <SID>wrap_i('[m',']M')<CR>
xnoremap <silent> <buffer> am :<C-U>call <SID>wrap_a('[m',']M')<CR>
let b:undo_ftplugin = b:undo_ftplugin
\."| sil! exe 'ounmap <buffer> im' | sil! exe 'ounmap <buffer> am'"
\."| sil! exe 'xunmap <buffer> im' | sil! exe 'xunmap <buffer> am'"
endif
if maparg('iM','x') == '' && maparg('iM','o') == '' && maparg('aM','x') == '' && maparg('aM','o') == ''
onoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
onoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
xnoremap <silent> <buffer> iM :<C-U>call <SID>wrap_i('[[','][')<CR>
xnoremap <silent> <buffer> aM :<C-U>call <SID>wrap_a('[[','][')<CR>
let b:undo_ftplugin = b:undo_ftplugin
\."| sil! exe 'ounmap <buffer> iM' | sil! exe 'ounmap <buffer> aM'"
\."| sil! exe 'xunmap <buffer> iM' | sil! exe 'xunmap <buffer> aM'"
endif
call s:map('c', '', '<C-R><C-F> <Plug><cfile>')
cmap <buffer><script><expr> <SID>tagzv &foldopen =~# 'tag' ? '<Bar>norm! zv' : ''
call s:map('n', '<silent>', '<C-]> <SID>:exe v:count1."tag <Plug><ctag>"<SID>tagzv<CR>')
call s:map('n', '<silent>', 'g<C-]> <SID>:exe "tjump <Plug><ctag>"<SID>tagzv<CR>')
call s:map('n', '<silent>', 'g] <SID>:exe "tselect <Plug><ctag>"<SID>tagzv<CR>')
call s:map('n', '<silent>', '<C-W>] <SID>:exe v:count1."stag <Plug><ctag>"<SID>tagzv<CR>')
call s:map('n', '<silent>', '<C-W><C-]> <SID>:exe v:count1."stag <Plug><ctag>"<SID>tagzv<CR>')
call s:map('n', '<silent>', '<C-W>g<C-]> <SID>:exe "stjump <Plug><ctag>"<SID>tagzv<CR>')
call s:map('n', '<silent>', '<C-W>g] <SID>:exe "stselect <Plug><ctag>"<SID>tagzv<CR>')
call s:map('n', '<silent>', '<C-W>} <SID>:exe v:count1."ptag <Plug><ctag>"<CR>')
call s:map('n', '<silent>', '<C-W>g} <SID>:exe "ptjump <Plug><ctag>"<CR>')
call s:map('n', '<silent>', 'gf <SID>c:find <Plug><cfile><CR>')
call s:map('n', '<silent>', '<C-W>f <SID>c:sfind <Plug><cfile><CR>')
call s:map('n', '<silent>', '<C-W><C-F> <SID>c:sfind <Plug><cfile><CR>')
call s:map('n', '<silent>', '<C-W>gf <SID>c:tabfind <Plug><cfile><CR>')
endif
let &cpo = s:cpo_save
unlet s:cpo_save
if exists("g:did_ruby_ftplugin_functions")
finish
endif
let g:did_ruby_ftplugin_functions = 1
function! RubyBalloonexpr() abort
if !exists('s:ri_found')
let s:ri_found = executable('ri')
endif
if s:ri_found
let line = getline(v:beval_lnum)
let b = matchstr(strpart(line,0,v:beval_col),'\%(\w\|[:.]\)*$')
let a = substitute(matchstr(strpart(line,v:beval_col),'^\w*\%([?!]\|\s*=\)\?'),'\s\+','','g')
let str = b.a
let before = strpart(line,0,v:beval_col-strlen(b))
let after = strpart(line,v:beval_col+strlen(a))
if str =~ '^\.'
let str = substitute(str,'^\.','#','g')
if before =~ '\]\s*$'
let str = 'Array'.str
elseif before =~ '}\s*$'
" False positives from blocks here
let str = 'Hash'.str
elseif before =~ "[\"'`]\\s*$" || before =~ '\$\d\+\s*$'
let str = 'String'.str
elseif before =~ '\$\d\+\.\d\+\s*$'
let str = 'Float'.str
elseif before =~ '\$\d\+\s*$'
let str = 'Integer'.str
elseif before =~ '/\s*$'
let str = 'Regexp'.str
else
let str = substitute(str,'^#','.','')
endif
endif
let str = substitute(str,'.*\.\s*to_f\s*\.\s*','Float#','')
let str = substitute(str,'.*\.\s*to_i\%(nt\)\=\s*\.\s*','Integer#','')
let str = substitute(str,'.*\.\s*to_s\%(tr\)\=\s*\.\s*','String#','')
let str = substitute(str,'.*\.\s*to_sym\s*\.\s*','Symbol#','')
let str = substitute(str,'.*\.\s*to_a\%(ry\)\=\s*\.\s*','Array#','')
let str = substitute(str,'.*\.\s*to_proc\s*\.\s*','Proc#','')
if str !~ '^\w'
return ''
endif
silent! let res = substitute(system("ri -f rdoc -T \"".str.'"'),'\n$','','')
if res =~ '^Nothing known about' || res =~ '^Bad argument:' || res =~ '^More than one method'
return ''
endif
return res
else
return ""
endif
endfunction
function! s:searchsyn(pattern, syn, flags, mode) abort
let cnt = v:count1
norm! m'
if a:mode ==# 'v'
norm! gv
endif
let i = 0
call map(a:syn, 'hlID(v:val)')
while i < cnt
let i = i + 1
let line = line('.')
let col = col('.')
let pos = search(a:pattern,'W'.a:flags)
while pos != 0 && index(a:syn, s:synid()) < 0
let pos = search(a:pattern,'W'.a:flags)
endwhile
if pos == 0
call cursor(line,col)
return
endif
endwhile
endfunction
function! s:synid() abort
return synID(line('.'),col('.'),0)
endfunction
function! s:wrap_i(back,forward) abort
execute 'norm! k'
execute 'norm '.a:forward
let line = line('.')
execute 'norm '.a:back
if line('.') == line - 1
return s:wrap_a(a:back,a:forward)
endif
execute 'norm! jV'
execute 'norm '.a:forward
execute 'norm! k'
endfunction
function! s:wrap_a(back,forward) abort
execute 'norm '.a:forward
if line('.') < line('$') && getline(line('.')+1) ==# ''
let after = 1
endif
execute 'norm '.a:back
while getline(line('.')-1) =~# '^\s*#' && line('.')
-
endwhile
if exists('after')
execute 'norm! V'
execute 'norm '.a:forward
execute 'norm! j'
elseif line('.') > 1 && getline(line('.')-1) =~# '^\s*$'
execute 'norm! kV'
execute 'norm '.a:forward
else
execute 'norm! V'
execute 'norm '.a:forward
endif
endfunction
function! RubyCursorIdentifier() abort
let asciicode = '\%(\w\|[]})\"'."'".']\)\@<!\%(?\%(\\M-\\C-\|\\C-\\M-\|\\M-\\c\|\\c\\M-\|\\c\|\\C-\|\\M-\)\=\%(\\\o\{1,3}\|\\x\x\{1,2}\|\\\=\S\)\)'
let number = '\%(\%(\w\|[]})\"'."'".']\s*\)\@<!-\)\=\%(\<[[:digit:]_]\+\%(\.[[:digit:]_]\+\)\=\%([Ee][[:digit:]_]\+\)\=\>\|\<0[xXbBoOdD][[:xdigit:]_]\+\>\)\|'.asciicode
let operator = '\%(\[\]\|<<\|<=>\|[!<>]=\=\|===\=\|[!=]\~\|>>\|\*\*\|\.\.\.\=\|=>\|[~^&|*/%+-]\)'
let method = '\%(\.[_a-zA-Z]\w*\s*=>\@!\|\<[_a-zA-Z]\w*\>[?!]\=\)'
let global = '$\%([!$&"'."'".'*+,./:;<=>?@\`~]\|-\=\w\+\>\)'
let symbolizable = '\%(\%(@@\=\)\w\+\>\|'.global.'\|'.method.'\|'.operator.'\)'
let pattern = '\C\s*\%('.number.'\|\%(:\@<!:\)\='.symbolizable.'\)'
let [lnum, col] = searchpos(pattern,'bcn',line('.'))
let raw = matchstr(getline('.')[col-1 : ],pattern)
let stripped = substitute(substitute(raw,'\s\+=$','=',''),'^\s*[:.]\=','','')
return stripped == '' ? expand("<cword>") : stripped
endfunction
function! RubyCursorTag() abort
return substitute(RubyCursorIdentifier(), '^[$@]*', '', '')
endfunction
function! RubyCursorFile() abort
let isfname = &isfname
try
set isfname+=:
let cfile = expand('<cfile>')
finally
let isfname = &isfname
endtry
let pre = matchstr(strpart(getline('.'), 0, col('.')-1), '.*\f\@<!')
let post = matchstr(strpart(getline('.'), col('.')), '\f\@!.*')
if s:synid() ==# hlID('rubyConstant')
let cfile = substitute(cfile,'\.\w\+[?!=]\=$','','')
let cfile = substitute(cfile,'^::','','')
let cfile = substitute(cfile,'::','/','g')
let cfile = substitute(cfile,'\(\u\+\)\(\u\l\)','\1_\2', 'g')
let cfile = substitute(cfile,'\(\l\|\d\)\(\u\)','\1_\2', 'g')
return tolower(cfile) . '.rb'
elseif getline('.') =~# '^\s*require_relative\s*\(["'']\).*\1\s*$'
let cfile = expand('%:p:h') . '/' . matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
elseif getline('.') =~# '^\s*\%(require[( ]\|load[( ]\|autoload[( ]:\w\+,\)\s*\%(::\)\=File\.expand_path(\(["'']\)\.\./.*\1,\s*__FILE__)\s*$'
let target = matchstr(getline('.'),'\(["'']\)\.\.\zs/.\{-\}\ze\1')
let cfile = expand('%:p:h') . target
let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
elseif getline('.') =~# '^\s*\%(require \|load \|autoload :\w\+,\)\s*\(["'']\).*\1\s*$'
let cfile = matchstr(getline('.'),'\(["'']\)\zs.\{-\}\ze\1')
let cfile .= cfile !~# '\.rb$' ? '.rb' : ''
elseif pre.post =~# '\<File.expand_path[( ].*[''"]\{2\}, *__FILE__\>' && cfile =~# '^\.\.'
let cfile = expand('%:p:h') . strpart(cfile, 2)
else
return substitute(cfile, '\C\v^(.*):(\d+)%(:in)=$', '+\2 \1', '')
endif
let cwdpat = '^\M' . substitute(getcwd(), '[\/]', '\\[\\/]', 'g').'\ze\[\/]'
let cfile = substitute(cfile, cwdpat, '.', '')
if fnameescape(cfile) !=# cfile
return '+ '.fnameescape(cfile)
else
return cfile
endif
endfunction
"
" Instructions for enabling "matchit" support:
"
" 1. Look for the latest "matchit" plugin at
"
" http://www.vim.org/scripts/script.php?script_id=39
"
" It is also packaged with Vim, in the $VIMRUNTIME/macros directory.
"
" 2. Copy "matchit.txt" into a "doc" directory (e.g. $HOME/.vim/doc).
"
" 3. Copy "matchit.vim" into a "plugin" directory (e.g. $HOME/.vim/plugin).
"
" 4. Ensure this file (ftplugin/ruby.vim) is installed.
"
" 5. Ensure you have this line in your $HOME/.vimrc:
" filetype plugin on
"
" 6. Restart Vim and create the matchit documentation:
"
" :helptags ~/.vim/doc
"
" Now you can do ":help matchit", and you should be able to use "%" on Ruby
" keywords. Try ":echo b:match_words" to be sure.
"
" Thanks to Mark J. Reed for the instructions. See ":help vimrc" for the
" locations of plugin directories, etc., as there are several options, and it
" differs on Windows. Email gsinclair@soyabean.com.au if you need help.
"
" vim: nowrap sw=2 sts=2 ts=8:

View file

@ -0,0 +1,110 @@
" Vim indent file
" Language: eRuby
" Maintainer: Tim Pope <vimNOSPAM@tpope.org>
" URL: https://github.com/vim-ruby/vim-ruby
" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
if exists("b:did_indent")
finish
endif
runtime! indent/ruby.vim
unlet! b:did_indent
setlocal indentexpr=
if exists("b:eruby_subtype") && b:eruby_subtype != '' && b:eruby_subtype !=# 'eruby'
exe "runtime! indent/".b:eruby_subtype.".vim"
else
runtime! indent/html.vim
endif
unlet! b:did_indent
" Force HTML indent to not keep state.
let b:html_indent_usestate = 0
if &l:indentexpr == ''
if &l:cindent
let &l:indentexpr = 'cindent(v:lnum)'
else
let &l:indentexpr = 'indent(prevnonblank(v:lnum-1))'
endif
endif
let b:eruby_subtype_indentexpr = &l:indentexpr
let b:did_indent = 1
setlocal indentexpr=GetErubyIndent()
setlocal indentkeys=o,O,*<Return>,<>>,{,},0),0],o,O,!^F,=end,=else,=elsif,=rescue,=ensure,=when
" Only define the function once.
if exists("*GetErubyIndent")
finish
endif
" this file uses line continuations
let s:cpo_sav = &cpo
set cpo&vim
function! GetErubyIndent(...)
" The value of a single shift-width
if exists('*shiftwidth')
let sw = shiftwidth()
else
let sw = &sw
endif
if a:0 && a:1 == '.'
let v:lnum = line('.')
elseif a:0 && a:1 =~ '^\d'
let v:lnum = a:1
endif
let vcol = col('.')
call cursor(v:lnum,1)
let inruby = searchpair('<%','','%>','W')
call cursor(v:lnum,vcol)
if inruby && getline(v:lnum) !~ '^<%\|^\s*[-=]\=%>'
let ind = GetRubyIndent(v:lnum)
else
exe "let ind = ".b:eruby_subtype_indentexpr
" Workaround for Andy Wokula's HTML indent. This should be removed after
" some time, since the newest version is fixed in a different way.
if b:eruby_subtype_indentexpr =~# '^HtmlIndent('
\ && exists('b:indent')
\ && type(b:indent) == type({})
\ && has_key(b:indent, 'lnum')
" Force HTML indent to not keep state
let b:indent.lnum = -1
endif
endif
let lnum = prevnonblank(v:lnum-1)
let line = getline(lnum)
let cline = getline(v:lnum)
if cline =~# '^\s*<%[-=]\=\s*\%(}\|end\|else\|\%(ensure\|rescue\|elsif\|when\).\{-\}\)\s*\%([-=]\=%>\|$\)'
let ind = ind - sw
endif
if line =~# '\S\s*<%[-=]\=\s*\%(}\|end\).\{-\}\s*\%([-=]\=%>\|$\)'
let ind = ind - sw
endif
if line =~# '\%({\|\<do\)\%(\s*|[^|]*|\)\=\s*[-=]\=%>'
let ind = ind + sw
elseif line =~# '<%[-=]\=\s*\%(module\|class\|def\|if\|for\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure\|rescue\)\>.*%>'
let ind = ind + sw
endif
if line =~# '^\s*<%[=#-]\=\s*$' && cline !~# '^\s*end\>'
let ind = ind + sw
endif
if line !~# '^\s*<%' && line =~# '%>\s*$' && line !~# '^\s*end\>'
\ && synID(v:lnum, match(cline, '\S') + 1, 1) != hlID('htmlEndTag')
let ind = ind - sw
endif
if cline =~# '^\s*[-=]\=%>\s*$'
let ind = ind - sw
endif
return ind
endfunction
let &cpo = s:cpo_sav
unlet! s:cpo_sav
" vim:set sw=2 sts=2 ts=8 noet:

View file

@ -0,0 +1,952 @@
" Vim indent file
" Language: Ruby
" Maintainer: Andrew Radev <andrey.radev@gmail.com>
" Previous Maintainer: Nikolai Weibull <now at bitwi.se>
" URL: https://github.com/vim-ruby/vim-ruby
" Release Coordinator: Doug Kearns <dougkearns@gmail.com>
" 0. Initialization {{{1
" =================
" Only load this indent file when no other was loaded.
if exists("b:did_indent")
finish
endif
let b:did_indent = 1
if !exists('g:ruby_indent_access_modifier_style')
" Possible values: "normal", "indent", "outdent"
let g:ruby_indent_access_modifier_style = 'normal'
endif
if !exists('g:ruby_indent_assignment_style')
" Possible values: "variable", "hanging"
let g:ruby_indent_assignment_style = 'hanging'
endif
if !exists('g:ruby_indent_block_style')
" Possible values: "expression", "do"
let g:ruby_indent_block_style = 'expression'
endif
setlocal nosmartindent
" Now, set up our indentation expression and keys that trigger it.
setlocal indentexpr=GetRubyIndent(v:lnum)
setlocal indentkeys=0{,0},0),0],!^F,o,O,e,:,.
setlocal indentkeys+==end,=else,=elsif,=when,=ensure,=rescue,==begin,==end
setlocal indentkeys+==private,=protected,=public
" Only define the function once.
if exists("*GetRubyIndent")
finish
endif
let s:cpo_save = &cpo
set cpo&vim
" 1. Variables {{{1
" ============
" Syntax group names that are strings.
let s:syng_string =
\ ['String', 'Interpolation', 'InterpolationDelimiter', 'StringEscape']
" Syntax group names that are strings or documentation.
let s:syng_stringdoc = s:syng_string + ['Documentation']
" Syntax group names that are or delimit strings/symbols/regexes or are comments.
let s:syng_strcom = s:syng_stringdoc + [
\ 'Character',
\ 'Comment',
\ 'HeredocDelimiter',
\ 'PercentRegexpDelimiter',
\ 'PercentStringDelimiter',
\ 'PercentSymbolDelimiter',
\ 'Regexp',
\ 'RegexpDelimiter',
\ 'RegexpEscape',
\ 'StringDelimiter',
\ 'Symbol',
\ 'SymbolDelimiter',
\ ]
" Expression used to check whether we should skip a match with searchpair().
let s:skip_expr =
\ 'index(map('.string(s:syng_strcom).',"hlID(''ruby''.v:val)"), synID(line("."),col("."),1)) >= 0'
" Regex used for words that, at the start of a line, add a level of indent.
let s:ruby_indent_keywords =
\ '^\s*\zs\<\%(module\|class\|if\|for' .
\ '\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure\|rescue' .
\ '\|\%(\K\k*[!?]\?\s\+\)\=def\):\@!\>' .
\ '\|\%([=,*/%+-]\|<<\|>>\|:\s\)\s*\zs' .
\ '\<\%(if\|for\|while\|until\|case\|unless\|begin\):\@!\>'
" Regex used for words that, at the start of a line, remove a level of indent.
let s:ruby_deindent_keywords =
\ '^\s*\zs\<\%(ensure\|else\|rescue\|elsif\|when\|end\):\@!\>'
" Regex that defines the start-match for the 'end' keyword.
"let s:end_start_regex = '\%(^\|[^.]\)\<\%(module\|class\|def\|if\|for\|while\|until\|case\|unless\|begin\|do\)\>'
" TODO: the do here should be restricted somewhat (only at end of line)?
let s:end_start_regex =
\ '\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' .
\ '\<\%(module\|class\|if\|for\|while\|until\|case\|unless\|begin' .
\ '\|\%(\K\k*[!?]\?\s\+\)\=def\):\@!\>' .
\ '\|\%(^\|[^.:@$]\)\@<=\<do:\@!\>'
" Regex that defines the middle-match for the 'end' keyword.
let s:end_middle_regex = '\<\%(ensure\|else\|\%(\%(^\|;\)\s*\)\@<=\<rescue:\@!\>\|when\|elsif\):\@!\>'
" Regex that defines the end-match for the 'end' keyword.
let s:end_end_regex = '\%(^\|[^.:@$]\)\@<=\<end:\@!\>'
" Expression used for searchpair() call for finding match for 'end' keyword.
let s:end_skip_expr = s:skip_expr .
\ ' || (expand("<cword>") == "do"' .
\ ' && getline(".") =~ "^\\s*\\<\\(while\\|until\\|for\\):\\@!\\>")'
" Regex that defines continuation lines, not including (, {, or [.
let s:non_bracket_continuation_regex =
\ '\%([\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|:\@<![^[:alnum:]:][|&?]\|||\|&&\)\s*\%(#.*\)\=$'
" Regex that defines continuation lines.
let s:continuation_regex =
\ '\%(%\@<![({[\\.,:*/%+]\|\<and\|\<or\|\%(<%\)\@<![=-]\|:\@<![^[:alnum:]:][|&?]\|||\|&&\)\s*\%(#.*\)\=$'
" Regex that defines continuable keywords
let s:continuable_regex =
\ '\C\%(^\s*\|[=,*/%+\-|;{]\|<<\|>>\|:\s\)\s*\zs' .
\ '\<\%(if\|for\|while\|until\|unless\):\@!\>'
" Regex that defines bracket continuations
let s:bracket_continuation_regex = '%\@<!\%([({[]\)\s*\%(#.*\)\=$'
" Regex that defines dot continuations
let s:dot_continuation_regex = '%\@<!\.\s*\%(#.*\)\=$'
" Regex that defines backslash continuations
let s:backslash_continuation_regex = '%\@<!\\\s*$'
" Regex that defines end of bracket continuation followed by another continuation
let s:bracket_switch_continuation_regex = '^\([^(]\+\zs).\+\)\+'.s:continuation_regex
" Regex that defines the first part of a splat pattern
let s:splat_regex = '[[,(]\s*\*\s*\%(#.*\)\=$'
" Regex that describes all indent access modifiers
let s:access_modifier_regex = '\C^\s*\%(public\|protected\|private\)\s*\%(#.*\)\=$'
" Regex that describes the indent access modifiers (excludes public)
let s:indent_access_modifier_regex = '\C^\s*\%(protected\|private\)\s*\%(#.*\)\=$'
" Regex that defines blocks.
"
" Note that there's a slight problem with this regex and s:continuation_regex.
" Code like this will be matched by both:
"
" method_call do |(a, b)|
"
" The reason is that the pipe matches a hanging "|" operator.
"
let s:block_regex =
\ '\%(\<do:\@!\>\|%\@<!{\)\s*\%(|[^|]*|\)\=\s*\%(#.*\)\=$'
let s:block_continuation_regex = '^\s*[^])}\t ].*'.s:block_regex
" Regex that describes a leading operator (only a method call's dot for now)
let s:leading_operator_regex = '^\s*\%(&\=\.\)'
" 2. GetRubyIndent Function {{{1
" =========================
function! GetRubyIndent(...) abort
" 2.1. Setup {{{2
" ----------
let indent_info = {}
" The value of a single shift-width
if exists('*shiftwidth')
let indent_info.sw = shiftwidth()
else
let indent_info.sw = &sw
endif
" For the current line, use the first argument if given, else v:lnum
let indent_info.clnum = a:0 ? a:1 : v:lnum
let indent_info.cline = getline(indent_info.clnum)
" Set up variables for restoring position in file. Could use clnum here.
let indent_info.col = col('.')
" 2.2. Work on the current line {{{2
" -----------------------------
let indent_callback_names = [
\ 's:AccessModifier',
\ 's:ClosingBracketOnEmptyLine',
\ 's:BlockComment',
\ 's:DeindentingKeyword',
\ 's:MultilineStringOrLineComment',
\ 's:ClosingHeredocDelimiter',
\ 's:LeadingOperator',
\ ]
for callback_name in indent_callback_names
" Decho "Running: ".callback_name
let indent = call(function(callback_name), [indent_info])
if indent >= 0
" Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info)
return indent
endif
endfor
" 2.3. Work on the previous line. {{{2
" -------------------------------
" Special case: we don't need the real s:PrevNonBlankNonString for an empty
" line inside a string. And that call can be quite expensive in that
" particular situation.
let indent_callback_names = [
\ 's:EmptyInsideString',
\ ]
for callback_name in indent_callback_names
" Decho "Running: ".callback_name
let indent = call(function(callback_name), [indent_info])
if indent >= 0
" Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info)
return indent
endif
endfor
" Previous line number
let indent_info.plnum = s:PrevNonBlankNonString(indent_info.clnum - 1)
let indent_info.pline = getline(indent_info.plnum)
let indent_callback_names = [
\ 's:StartOfFile',
\ 's:AfterAccessModifier',
\ 's:ContinuedLine',
\ 's:AfterBlockOpening',
\ 's:AfterHangingSplat',
\ 's:AfterUnbalancedBracket',
\ 's:AfterLeadingOperator',
\ 's:AfterEndKeyword',
\ 's:AfterIndentKeyword',
\ ]
for callback_name in indent_callback_names
" Decho "Running: ".callback_name
let indent = call(function(callback_name), [indent_info])
if indent >= 0
" Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info)
return indent
endif
endfor
" 2.4. Work on the MSL line. {{{2
" --------------------------
let indent_callback_names = [
\ 's:PreviousNotMSL',
\ 's:IndentingKeywordInMSL',
\ 's:ContinuedHangingOperator',
\ ]
" Most Significant line based on the previous one -- in case it's a
" contination of something above
let indent_info.plnum_msl = s:GetMSL(indent_info.plnum)
for callback_name in indent_callback_names
" Decho "Running: ".callback_name
let indent = call(function(callback_name), [indent_info])
if indent >= 0
" Decho "Match: ".callback_name." indent=".indent." info=".string(indent_info)
return indent
endif
endfor
" }}}2
" By default, just return the previous line's indent
" Decho "Default case matched"
return indent(indent_info.plnum)
endfunction
" 3. Indenting Logic Callbacks {{{1
" ============================
function! s:AccessModifier(cline_info) abort
let info = a:cline_info
" If this line is an access modifier keyword, align according to the closest
" class declaration.
if g:ruby_indent_access_modifier_style == 'indent'
if s:Match(info.clnum, s:access_modifier_regex)
let class_lnum = s:FindContainingClass()
if class_lnum > 0
return indent(class_lnum) + info.sw
endif
endif
elseif g:ruby_indent_access_modifier_style == 'outdent'
if s:Match(info.clnum, s:access_modifier_regex)
let class_lnum = s:FindContainingClass()
if class_lnum > 0
return indent(class_lnum)
endif
endif
endif
return -1
endfunction
function! s:ClosingBracketOnEmptyLine(cline_info) abort
let info = a:cline_info
" If we got a closing bracket on an empty line, find its match and indent
" according to it. For parentheses we indent to its column - 1, for the
" others we indent to the containing line's MSL's level. Return -1 if fail.
let col = matchend(info.cline, '^\s*[]})]')
if col > 0 && !s:IsInStringOrComment(info.clnum, col)
call cursor(info.clnum, col)
let closing_bracket = info.cline[col - 1]
let bracket_pair = strpart('(){}[]', stridx(')}]', closing_bracket) * 2, 2)
if searchpair(escape(bracket_pair[0], '\['), '', bracket_pair[1], 'bW', s:skip_expr) > 0
if closing_bracket == ')' && col('.') != col('$') - 1
let ind = virtcol('.') - 1
elseif g:ruby_indent_block_style == 'do'
let ind = indent(line('.'))
else " g:ruby_indent_block_style == 'expression'
let ind = indent(s:GetMSL(line('.')))
endif
endif
return ind
endif
return -1
endfunction
function! s:BlockComment(cline_info) abort
" If we have a =begin or =end set indent to first column.
if match(a:cline_info.cline, '^\s*\%(=begin\|=end\)$') != -1
return 0
endif
return -1
endfunction
function! s:DeindentingKeyword(cline_info) abort
let info = a:cline_info
" If we have a deindenting keyword, find its match and indent to its level.
" TODO: this is messy
if s:Match(info.clnum, s:ruby_deindent_keywords)
call cursor(info.clnum, 1)
if searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW',
\ s:end_skip_expr) > 0
let msl = s:GetMSL(line('.'))
let line = getline(line('.'))
if s:IsAssignment(line, col('.')) &&
\ strpart(line, col('.') - 1, 2) !~ 'do'
" assignment to case/begin/etc, on the same line
if g:ruby_indent_assignment_style == 'hanging'
" hanging indent
let ind = virtcol('.') - 1
else
" align with variable
let ind = indent(line('.'))
endif
elseif g:ruby_indent_block_style == 'do'
" align to line of the "do", not to the MSL
let ind = indent(line('.'))
elseif getline(msl) =~ '=\s*\(#.*\)\=$'
" in the case of assignment to the MSL, align to the starting line,
" not to the MSL
let ind = indent(line('.'))
else
" align to the MSL
let ind = indent(msl)
endif
endif
return ind
endif
return -1
endfunction
function! s:MultilineStringOrLineComment(cline_info) abort
let info = a:cline_info
" If we are in a multi-line string or line-comment, don't do anything to it.
if s:IsInStringOrDocumentation(info.clnum, matchend(info.cline, '^\s*') + 1)
return indent(info.clnum)
endif
return -1
endfunction
function! s:ClosingHeredocDelimiter(cline_info) abort
let info = a:cline_info
" If we are at the closing delimiter of a "<<" heredoc-style string, set the
" indent to 0.
if info.cline =~ '^\k\+\s*$'
\ && s:IsInStringDelimiter(info.clnum, 1)
\ && search('\V<<'.info.cline, 'nbW') > 0
return 0
endif
return -1
endfunction
function! s:LeadingOperator(cline_info) abort
" If the current line starts with a leading operator, add a level of indent.
if s:Match(a:cline_info.clnum, s:leading_operator_regex)
return indent(s:GetMSL(a:cline_info.clnum)) + a:cline_info.sw
endif
return -1
endfunction
function! s:EmptyInsideString(pline_info) abort
" If the line is empty and inside a string (the previous line is a string,
" too), use the previous line's indent
let info = a:pline_info
let plnum = prevnonblank(info.clnum - 1)
let pline = getline(plnum)
if info.cline =~ '^\s*$'
\ && s:IsInStringOrComment(plnum, 1)
\ && s:IsInStringOrComment(plnum, strlen(pline))
return indent(plnum)
endif
return -1
endfunction
function! s:StartOfFile(pline_info) abort
" At the start of the file use zero indent.
if a:pline_info.plnum == 0
return 0
endif
return -1
endfunction
function! s:AfterAccessModifier(pline_info) abort
let info = a:pline_info
if g:ruby_indent_access_modifier_style == 'indent'
" If the previous line was a private/protected keyword, add a
" level of indent.
if s:Match(info.plnum, s:indent_access_modifier_regex)
return indent(info.plnum) + info.sw
endif
elseif g:ruby_indent_access_modifier_style == 'outdent'
" If the previous line was a private/protected/public keyword, add
" a level of indent, since the keyword has been out-dented.
if s:Match(info.plnum, s:access_modifier_regex)
return indent(info.plnum) + info.sw
endif
endif
return -1
endfunction
" Example:
"
" if foo || bar ||
" baz || bing
" puts "foo"
" end
"
function! s:ContinuedLine(pline_info) abort
let info = a:pline_info
let col = s:Match(info.plnum, s:ruby_indent_keywords)
if s:Match(info.plnum, s:continuable_regex) &&
\ s:Match(info.plnum, s:continuation_regex)
if col > 0 && s:IsAssignment(info.pline, col)
if g:ruby_indent_assignment_style == 'hanging'
" hanging indent
let ind = col - 1
else
" align with variable
let ind = indent(info.plnum)
endif
else
let ind = indent(s:GetMSL(info.plnum))
endif
return ind + info.sw + info.sw
endif
return -1
endfunction
function! s:AfterBlockOpening(pline_info) abort
let info = a:pline_info
" If the previous line ended with a block opening, add a level of indent.
if s:Match(info.plnum, s:block_regex)
if g:ruby_indent_block_style == 'do'
" don't align to the msl, align to the "do"
let ind = indent(info.plnum) + info.sw
else
let plnum_msl = s:GetMSL(info.plnum)
if getline(plnum_msl) =~ '=\s*\(#.*\)\=$'
" in the case of assignment to the msl, align to the starting line,
" not to the msl
let ind = indent(info.plnum) + info.sw
else
let ind = indent(plnum_msl) + info.sw
endif
endif
return ind
endif
return -1
endfunction
function! s:AfterLeadingOperator(pline_info) abort
" If the previous line started with a leading operator, use its MSL's level
" of indent
if s:Match(a:pline_info.plnum, s:leading_operator_regex)
return indent(s:GetMSL(a:pline_info.plnum))
endif
return -1
endfunction
function! s:AfterHangingSplat(pline_info) abort
let info = a:pline_info
" If the previous line ended with the "*" of a splat, add a level of indent
if info.pline =~ s:splat_regex
return indent(info.plnum) + info.sw
endif
return -1
endfunction
function! s:AfterUnbalancedBracket(pline_info) abort
let info = a:pline_info
" If the previous line contained unclosed opening brackets and we are still
" in them, find the rightmost one and add indent depending on the bracket
" type.
"
" If it contained hanging closing brackets, find the rightmost one, find its
" match and indent according to that.
if info.pline =~ '[[({]' || info.pline =~ '[])}]\s*\%(#.*\)\=$'
let [opening, closing] = s:ExtraBrackets(info.plnum)
if opening.pos != -1
if opening.type == '(' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
if col('.') + 1 == col('$')
return indent(info.plnum) + info.sw
else
return virtcol('.')
endif
else
let nonspace = matchend(info.pline, '\S', opening.pos + 1) - 1
return nonspace > 0 ? nonspace : indent(info.plnum) + info.sw
endif
elseif closing.pos != -1
call cursor(info.plnum, closing.pos + 1)
normal! %
if s:Match(line('.'), s:ruby_indent_keywords)
return indent('.') + info.sw
else
return indent(s:GetMSL(line('.')))
endif
else
call cursor(info.clnum, info.col)
end
endif
return -1
endfunction
function! s:AfterEndKeyword(pline_info) abort
let info = a:pline_info
" If the previous line ended with an "end", match that "end"s beginning's
" indent.
let col = s:Match(info.plnum, '\%(^\|[^.:@$]\)\<end\>\s*\%(#.*\)\=$')
if col > 0
call cursor(info.plnum, col)
if searchpair(s:end_start_regex, '', s:end_end_regex, 'bW',
\ s:end_skip_expr) > 0
let n = line('.')
let ind = indent('.')
let msl = s:GetMSL(n)
if msl != n
let ind = indent(msl)
end
return ind
endif
end
return -1
endfunction
function! s:AfterIndentKeyword(pline_info) abort
let info = a:pline_info
let col = s:Match(info.plnum, s:ruby_indent_keywords)
if col > 0
call cursor(info.plnum, col)
let ind = virtcol('.') - 1 + info.sw
" TODO: make this better (we need to count them) (or, if a searchpair
" fails, we know that something is lacking an end and thus we indent a
" level
if s:Match(info.plnum, s:end_end_regex)
let ind = indent('.')
elseif s:IsAssignment(info.pline, col)
if g:ruby_indent_assignment_style == 'hanging'
" hanging indent
let ind = col + info.sw - 1
else
" align with variable
let ind = indent(info.plnum) + info.sw
endif
endif
return ind
endif
return -1
endfunction
function! s:PreviousNotMSL(msl_info) abort
let info = a:msl_info
" If the previous line wasn't a MSL
if info.plnum != info.plnum_msl
" If previous line ends bracket and begins non-bracket continuation decrease indent by 1.
if s:Match(info.plnum, s:bracket_switch_continuation_regex)
" TODO (2016-10-07) Wrong/unused? How could it be "1"?
return indent(info.plnum) - 1
" If previous line is a continuation return its indent.
" TODO: the || s:IsInString() thing worries me a bit.
elseif s:Match(info.plnum, s:non_bracket_continuation_regex) || s:IsInString(info.plnum, strlen(line))
return indent(info.plnum)
endif
endif
return -1
endfunction
function! s:IndentingKeywordInMSL(msl_info) abort
let info = a:msl_info
" If the MSL line had an indenting keyword in it, add a level of indent.
" TODO: this does not take into account contrived things such as
" module Foo; class Bar; end
let col = s:Match(info.plnum_msl, s:ruby_indent_keywords)
if col > 0
let ind = indent(info.plnum_msl) + info.sw
if s:Match(info.plnum_msl, s:end_end_regex)
let ind = ind - info.sw
elseif s:IsAssignment(getline(info.plnum_msl), col)
if g:ruby_indent_assignment_style == 'hanging'
" hanging indent
let ind = col + info.sw - 1
else
" align with variable
let ind = indent(info.plnum_msl) + info.sw
endif
endif
return ind
endif
return -1
endfunction
function! s:ContinuedHangingOperator(msl_info) abort
let info = a:msl_info
" If the previous line ended with [*+/.,-=], but wasn't a block ending or a
" closing bracket, indent one extra level.
if s:Match(info.plnum_msl, s:non_bracket_continuation_regex) && !s:Match(info.plnum_msl, '^\s*\([\])}]\|end\)')
if info.plnum_msl == info.plnum
let ind = indent(info.plnum_msl) + info.sw
else
let ind = indent(info.plnum_msl)
endif
return ind
endif
return -1
endfunction
" 4. Auxiliary Functions {{{1
" ======================
function! s:IsInRubyGroup(groups, lnum, col) abort
let ids = map(copy(a:groups), 'hlID("ruby".v:val)')
return index(ids, synID(a:lnum, a:col, 1)) >= 0
endfunction
" Check if the character at lnum:col is inside a string, comment, or is ascii.
function! s:IsInStringOrComment(lnum, col) abort
return s:IsInRubyGroup(s:syng_strcom, a:lnum, a:col)
endfunction
" Check if the character at lnum:col is inside a string.
function! s:IsInString(lnum, col) abort
return s:IsInRubyGroup(s:syng_string, a:lnum, a:col)
endfunction
" Check if the character at lnum:col is inside a string or documentation.
function! s:IsInStringOrDocumentation(lnum, col) abort
return s:IsInRubyGroup(s:syng_stringdoc, a:lnum, a:col)
endfunction
" Check if the character at lnum:col is inside a string delimiter
function! s:IsInStringDelimiter(lnum, col) abort
return s:IsInRubyGroup(
\ ['HeredocDelimiter', 'PercentStringDelimiter', 'StringDelimiter'],
\ a:lnum, a:col
\ )
endfunction
function! s:IsAssignment(str, pos) abort
return strpart(a:str, 0, a:pos - 1) =~ '=\s*$'
endfunction
" Find line above 'lnum' that isn't empty, in a comment, or in a string.
function! s:PrevNonBlankNonString(lnum) abort
let in_block = 0
let lnum = prevnonblank(a:lnum)
while lnum > 0
" Go in and out of blocks comments as necessary.
" If the line isn't empty (with opt. comment) or in a string, end search.
let line = getline(lnum)
if line =~ '^=begin'
if in_block
let in_block = 0
else
break
endif
elseif !in_block && line =~ '^=end'
let in_block = 1
elseif !in_block && line !~ '^\s*#.*$' && !(s:IsInStringOrComment(lnum, 1)
\ && s:IsInStringOrComment(lnum, strlen(line)))
break
endif
let lnum = prevnonblank(lnum - 1)
endwhile
return lnum
endfunction
" Find line above 'lnum' that started the continuation 'lnum' may be part of.
function! s:GetMSL(lnum) abort
" Start on the line we're at and use its indent.
let msl = a:lnum
let lnum = s:PrevNonBlankNonString(a:lnum - 1)
while lnum > 0
" If we have a continuation line, or we're in a string, use line as MSL.
" Otherwise, terminate search as we have found our MSL already.
let line = getline(lnum)
if !s:Match(msl, s:backslash_continuation_regex) &&
\ s:Match(lnum, s:backslash_continuation_regex)
" If the current line doesn't end in a backslash, but the previous one
" does, look for that line's msl
"
" Example:
" foo = "bar" \
" "baz"
"
let msl = lnum
elseif s:Match(msl, s:leading_operator_regex)
" If the current line starts with a leading operator, keep its indent
" and keep looking for an MSL.
let msl = lnum
elseif s:Match(lnum, s:splat_regex)
" If the above line looks like the "*" of a splat, use the current one's
" indentation.
"
" Example:
" Hash[*
" method_call do
" something
"
return msl
elseif s:Match(lnum, s:non_bracket_continuation_regex) &&
\ s:Match(msl, s:non_bracket_continuation_regex)
" If the current line is a non-bracket continuation and so is the
" previous one, keep its indent and continue looking for an MSL.
"
" Example:
" method_call one,
" two,
" three
"
let msl = lnum
elseif s:Match(lnum, s:dot_continuation_regex) &&
\ (s:Match(msl, s:bracket_continuation_regex) || s:Match(msl, s:block_continuation_regex))
" If the current line is a bracket continuation or a block-starter, but
" the previous is a dot, keep going to see if the previous line is the
" start of another continuation.
"
" Example:
" parent.
" method_call {
" three
"
let msl = lnum
elseif s:Match(lnum, s:non_bracket_continuation_regex) &&
\ (s:Match(msl, s:bracket_continuation_regex) || s:Match(msl, s:block_continuation_regex))
" If the current line is a bracket continuation or a block-starter, but
" the previous is a non-bracket one, respect the previous' indentation,
" and stop here.
"
" Example:
" method_call one,
" two {
" three
"
return lnum
elseif s:Match(lnum, s:bracket_continuation_regex) &&
\ (s:Match(msl, s:bracket_continuation_regex) || s:Match(msl, s:block_continuation_regex))
" If both lines are bracket continuations (the current may also be a
" block-starter), use the current one's and stop here
"
" Example:
" method_call(
" other_method_call(
" foo
return msl
elseif s:Match(lnum, s:block_regex) &&
\ !s:Match(msl, s:continuation_regex) &&
\ !s:Match(msl, s:block_continuation_regex)
" If the previous line is a block-starter and the current one is
" mostly ordinary, use the current one as the MSL.
"
" Example:
" method_call do
" something
" something_else
return msl
else
let col = match(line, s:continuation_regex) + 1
if (col > 0 && !s:IsInStringOrComment(lnum, col))
\ || s:IsInString(lnum, strlen(line))
let msl = lnum
else
break
endif
endif
let lnum = s:PrevNonBlankNonString(lnum - 1)
endwhile
return msl
endfunction
" Check if line 'lnum' has more opening brackets than closing ones.
function! s:ExtraBrackets(lnum) abort
let opening = {'parentheses': [], 'braces': [], 'brackets': []}
let closing = {'parentheses': [], 'braces': [], 'brackets': []}
let line = getline(a:lnum)
let pos = match(line, '[][(){}]', 0)
" Save any encountered opening brackets, and remove them once a matching
" closing one has been found. If a closing bracket shows up that doesn't
" close anything, save it for later.
while pos != -1
if !s:IsInStringOrComment(a:lnum, pos + 1)
if line[pos] == '('
call add(opening.parentheses, {'type': '(', 'pos': pos})
elseif line[pos] == ')'
if empty(opening.parentheses)
call add(closing.parentheses, {'type': ')', 'pos': pos})
else
let opening.parentheses = opening.parentheses[0:-2]
endif
elseif line[pos] == '{'
call add(opening.braces, {'type': '{', 'pos': pos})
elseif line[pos] == '}'
if empty(opening.braces)
call add(closing.braces, {'type': '}', 'pos': pos})
else
let opening.braces = opening.braces[0:-2]
endif
elseif line[pos] == '['
call add(opening.brackets, {'type': '[', 'pos': pos})
elseif line[pos] == ']'
if empty(opening.brackets)
call add(closing.brackets, {'type': ']', 'pos': pos})
else
let opening.brackets = opening.brackets[0:-2]
endif
endif
endif
let pos = match(line, '[][(){}]', pos + 1)
endwhile
" Find the rightmost brackets, since they're the ones that are important in
" both opening and closing cases
let rightmost_opening = {'type': '(', 'pos': -1}
let rightmost_closing = {'type': ')', 'pos': -1}
for opening in opening.parentheses + opening.braces + opening.brackets
if opening.pos > rightmost_opening.pos
let rightmost_opening = opening
endif
endfor
for closing in closing.parentheses + closing.braces + closing.brackets
if closing.pos > rightmost_closing.pos
let rightmost_closing = closing
endif
endfor
return [rightmost_opening, rightmost_closing]
endfunction
function! s:Match(lnum, regex) abort
let line = getline(a:lnum)
let offset = match(line, '\C'.a:regex)
let col = offset + 1
while offset > -1 && s:IsInStringOrComment(a:lnum, col)
let offset = match(line, '\C'.a:regex, offset + 1)
let col = offset + 1
endwhile
if offset > -1
return col
else
return 0
endif
endfunction
" Locates the containing class/module's definition line, ignoring nested classes
" along the way.
"
function! s:FindContainingClass() abort
let saved_position = getpos('.')
while searchpair(s:end_start_regex, s:end_middle_regex, s:end_end_regex, 'bW',
\ s:end_skip_expr) > 0
if expand('<cword>') =~# '\<class\|module\>'
let found_lnum = line('.')
call setpos('.', saved_position)
return found_lnum
endif
endwhile
call setpos('.', saved_position)
return 0
endfunction
" }}}1
let &cpo = s:cpo_save
unlet s:cpo_save
" vim:set sw=2 sts=2 ts=8 et:

View file

@ -0,0 +1,24 @@
require 'spec_helper'
describe "Indenting" do
specify "multi-line arguments" do
assert_correct_indenting <<~EOF
User.new(
:first_name => 'Some',
:second_name => 'Guy'
)
EOF
assert_correct_indenting <<~EOF
User.new(:first_name => 'Some',
:second_name => 'Guy')
EOF
assert_correct_indenting <<~EOF
User.new(
:first_name => 'Some',
:second_name => 'Guy'
)
EOF
end
end

View file

@ -0,0 +1,73 @@
require 'spec_helper'
describe "Indenting" do
specify "if-clauses" do
assert_correct_indenting <<~EOF
if foo
bar
end
EOF
assert_correct_indenting <<~EOF
if foo
bar
else
baz
end
EOF
assert_correct_indenting <<~EOF
bar if foo
something_else
EOF
end
specify "heredocs" do
assert_correct_indenting <<~EOF
def one
two = <<-THREE
four
THREE
end
EOF
assert_correct_indenting <<~EOF
def one
two = <<THREE
four
THREE
end
EOF
assert_correct_indenting <<~EOF
def one
two = <<~THREE
four
THREE
end
EOF
# See https://github.com/vim-ruby/vim-ruby/issues/318 for details
assert_correct_indenting <<~EOF
def foo
<<-EOS
one
\#{two} three
four
EOS
end
EOF
end
specify "comments" do
assert_correct_indenting <<~EOF
def one
example do |something|
=begin
something that is ignored
=end
end
end
EOF
end
end

View file

@ -0,0 +1,167 @@
require 'spec_helper'
describe "Indenting" do
after :each do
vim.command 'let g:ruby_indent_block_style = "expression"'
end
specify "indented blocks with expression style" do
vim.command 'let g:ruby_indent_block_style = "expression"'
assert_correct_indenting <<~EOF
a.
b.
c do |x|
something
end
next_line
EOF
assert_correct_indenting <<~EOF
a.
b.
c { |x|
something
}
next_line
EOF
end
specify "indented blocks with do style" do
vim.command 'let g:ruby_indent_block_style = "do"'
assert_correct_indenting <<~EOF
a.
b.
c do |x|
something
end
next_line
EOF
# Check that "do" style indentation does not mess up indentation
# following the bock.
assert_correct_indenting <<~EOF
a.
b.
c do |x|
something
end
next_line
EOF
# Check that "do" style indenting works properly for brace blocks.
assert_correct_indenting <<~EOF
a.
b.
c { |x|
something
}
next_line
EOF
end
specify "'do' indenting" do
assert_correct_indenting <<~EOF
do
something
end
EOF
assert_correct_indenting <<~EOF
def foo
a_hash = {:do => 'bar'}
end
EOF
assert_correct_indenting <<~EOF
def foo(job)
job.do!
end
EOF
end
specify "blocks with assignment on the previous line" do
assert_correct_indenting <<~EOF
foo =
something do
"other"
end
EOF
assert_correct_indenting <<~EOF
@foo ||=
something do
"other"
end
EOF
end
specify "blocks with multiline parameters" do
assert_correct_indenting <<~EOF
def foo
opts.on('--coordinator host=HOST[,port=PORT]',
'Specify the HOST and the PORT of the coordinator') do |str|
h = sub_opts_to_hash(str)
puts h
end
end
EOF
end
specify "case-insensitive matching" do
vim.set 'ignorecase'
assert_correct_indenting <<~EOF
module X
Class.new do
end
end
EOF
vim.set 'ignorecase&'
end
specify "blocks with tuple arguments" do
assert_correct_indenting <<~EOF
proc do |(a, b)|
puts a
puts b
end
EOF
assert_correct_indenting <<~EOF
proc do |foo, (a, b), bar|
puts a
puts b
end
EOF
assert_correct_indenting <<~EOF
proc do |(a, (b, c)), d|
puts a, b
puts c, d
end
EOF
end
specify "blocks with default arguments" do
assert_correct_indenting <<~EOF
proc do |a = 1|
puts a
end
EOF
# See https://github.com/vim-ruby/vim-ruby/issues/304
assert_correct_indenting <<~EOF
proc do |a: "asdf", b:|
proc do
puts a, b
end
end
EOF
end
end

View file

@ -0,0 +1,317 @@
require 'spec_helper'
describe "Indenting" do
specify "method chaining" do
assert_correct_indenting <<~EOF
some_object.
method_one.
method_two.
method_three
EOF
assert_correct_indenting <<~EOF
some_object
.method_one
.method_two
.method_three
EOF
assert_correct_indenting <<~EOF
some_object&.
method_one&.
method_two&.
method_three
EOF
assert_correct_indenting <<~EOF
some_object
&.method_one
&.method_two
&.method_three
EOF
end
specify "arrays" do
assert_correct_indenting <<~EOF
foo = [one,
two,
three]
EOF
end
specify "tricky string interpolation" do
# See https://github.com/vim-ruby/vim-ruby/issues/75 for details
assert_correct_indenting <<~EOF
puts %{\#{}}
puts "OK"
EOF
assert_correct_indenting <<~EOF
while true
begin
puts %{\#{x}}
rescue ArgumentError
end
end
EOF
end
specify "continuations after round braces" do
assert_correct_indenting <<~EOF
opts.on('--coordinator host=HOST[,port=PORT]',
'Specify the HOST and the PORT of the coordinator') do |str|
h = sub_opts_to_hash(str)
puts h
end
EOF
end
describe "assignments" do
after :each do
vim.command 'let g:ruby_indent_assignment_style = "hanging"'
end
specify "continuations after assignment" do
assert_correct_indenting <<~EOF
variable =
if condition?
1
else
2
end
EOF
assert_correct_indenting <<~EOF
variable = # evil comment
case something
when 'something'
something_else
else
other
end
EOF
assert_correct_indenting <<~EOF
variable = case something
when 'something'
something_else
else
other
end
EOF
assert_correct_indenting <<~EOF
variable = if something == something_else
something_else
elsif other == none
none
else
other
end
EOF
assert_correct_indenting <<~EOF
variable = while
break something
end
EOF
assert_correct_indenting <<~EOF
variable = if [].
map { |x| x * 2 }.
filter { |x| x % 3 == 0 }.
empty?
something
end
EOF
vim.command 'let g:ruby_indent_assignment_style = "variable"'
assert_correct_indenting <<~EOF
variable = case something # evil comment
when 'something'
something_else
else
other
end
EOF
assert_correct_indenting <<~EOF
variable = if something == something_else
something_else
elsif other == none
none
else
other
end
EOF
assert_correct_indenting <<~EOF
variable = while
break something
end
EOF
assert_correct_indenting <<~EOF
variable = if [].
map { |x| x * 2 }.
filter { |x| x % 3 == 0 }.
empty?
something
end
EOF
end
end
specify "continuations after hanging comma" do
assert_correct_indenting <<~EOF
array = [
:one,
].each do |x|
puts x.to_s
end
EOF
end
specify "string interpolation" do
# For details, see:
#
# https://github.com/vim-ruby/vim-ruby/issues/93
# https://github.com/vim-ruby/vim-ruby/issues/160
#
assert_correct_indenting <<~EOF
command = %|\#{file}|
settings.log.info("Returning: \#{command}")
EOF
assert_correct_indenting <<~EOF
{
thing: "[\#{}]",
thong: "b"
}
EOF
assert_correct_indenting <<~EOF
{
a: "(\#{a})",
b: "(\#{b})",
c: "(c)",
d: "(d)",
e: "(e)",
}
EOF
end
specify "closing bracket not on its own line" do
# See https://github.com/vim-ruby/vim-ruby/issues/81 for details
assert_correct_indenting <<~EOF
one { two >>
three }
four
EOF
end
specify "lonesome single parenthesis in a method definition" do
# See https://github.com/vim-ruby/vim-ruby/issues/130 for details
assert_correct_indenting <<~EOF
def bar(
baz
)
return baz+1
end
EOF
end
specify "brackets on their own line, followed by a comma" do
# See https://github.com/vim-ruby/vim-ruby/issues/124 for details
assert_correct_indenting <<~EOF
bla = {
:one => [
{:bla => :blub}
],
:two => (
{:blub => :abc}
),
:three => {
:blub => :abc
},
:four => 'five'
}
EOF
end
specify "string with an and#" do
# See https://github.com/vim-ruby/vim-ruby/issues/108 for details
assert_correct_indenting <<~EOF
outside_block "and#" do
inside_block do
end
end
EOF
end
specify "continuation with a symbol at the end" do
# See https://github.com/vim-ruby/vim-ruby/issues/132 for details
assert_correct_indenting <<~EOF
foo = :+
# Next indents correctly
EOF
end
specify "continuation with a hanging comma" do
# See https://github.com/vim-ruby/vim-ruby/issues/139 for details
assert_correct_indenting <<~EOF
thing :foo
thing 'a',
'b'
EOF
end
specify "continuations in an if-clause condition" do
# See https://github.com/vim-ruby/vim-ruby/issues/215 for details
assert_correct_indenting <<~EOF
if foo || bar ||
bong &&
baz || bing
puts "foo"
end
EOF
end
specify "continuations with round brackets" do
# See https://github.com/vim-ruby/vim-ruby/issues/17 for details
assert_correct_indenting <<~EOF
foo and
(bar and
baz) and
bing
EOF
end
specify "block within an argument list" do
# See https://github.com/vim-ruby/vim-ruby/issues/312 for details
assert_correct_indenting <<~EOF
foo(
x: 1,
y: [1, 2, 3].map { |i|
i + 1
}
)
EOF
end
specify "backslashes" do
# See https://github.com/vim-ruby/vim-ruby/issues/311 for details
assert_correct_indenting <<~EOF
def foo
x = 1
string = ". \#{x}" \\
"xyz"
puts string
puts string
end
EOF
end
end

View file

@ -0,0 +1,31 @@
require 'spec_helper'
describe "Indenting" do
specify "end constructs" do
assert_correct_indenting <<~EOF
f do
g { def h; end }
end
EOF
assert_correct_indenting <<~EOF
if foo
bar ; end
something_else
EOF
assert_correct_indenting <<~EOF
if bar ; end
something_else
EOF
assert_correct_indenting <<~EOF
foo do
foo = 3 . class
foo = lambda { class One; end }
foo = lambda { |args| class One; end }
foo = bar; class One; end
end
EOF
end
end

View file

@ -0,0 +1,18 @@
require 'spec_helper'
describe "Indenting" do
specify "closing html tag after multiline eruby tag" do
assert_correct_indenting 'erb', <<~EOF
<form>
<div>
<%= text_field_tag :email, nil,
placeholder: "email" %>
text
<%= text_field_tag :password, nil,
placeholder: "password" %>
</div>
</form>
EOF
end
end

View file

@ -0,0 +1,10 @@
require 'spec_helper'
describe "Indenting" do
specify "identifiers containing keyword substrings" do
assert_correct_indenting <<~EOF
foo_def
42
EOF
end
end

View file

@ -0,0 +1,137 @@
require 'spec_helper'
describe "Indenting" do
after :each do
vim.command 'let g:ruby_indent_access_modifier_style = "normal"'
end
specify "default indented access modifiers" do
assert_correct_indenting <<~EOF
class OuterClass
private :method
protected :method
def method; end
protected
def method; end
private
def method; end
public
def method; end
class InnerClass
private :method
protected :method
def method; end
protected
def method; end
private
def method; end
public
def method; end
end
private :method
protected :method
def method; end
protected
def method; end
private
def method; end
public
def method; end
end
EOF
end
specify "indented access modifiers" do
vim.command 'let g:ruby_indent_access_modifier_style = "indent"'
assert_correct_indenting <<~EOF
class OuterClass
private :method
protected :method
def method; end
protected
def method; end
private
def method; end
public
def method; end
class InnerClass
private :method
protected :method
def method; end
protected
def method; end
private
def method; end
public
def method; end
end
private :method
protected :method
def method; end
protected
def method; end
private
def method; end
public
def method; end
end
EOF
end
specify "outdented access modifiers" do
vim.command 'let g:ruby_indent_access_modifier_style = "outdent"'
assert_correct_indenting <<~EOF
class OuterClass
private :method
protected :method
def method; end
protected
def method; end
private
def method; end
public
def method; end
class InnerClass
private :method
protected :method
def method; end
protected
def method; end
private
def method; end
public
def method; end
end
private :method
protected :method
def method; end
protected
def method; end
private
def method; end
public
def method; end
end
EOF
end
end

View file

@ -0,0 +1,40 @@
require 'spec_helper'
describe "Indenting" do
specify "method definitions prefixed with access modifiers" do
assert_correct_indenting <<~EOF
class Foo
public def one(x)
end
private def two(y)
code
end
end
EOF
end
specify "method definitions prefixed with any method call" do
assert_correct_indenting <<~EOF
class Foo
foobar def one(x)
end
foobar? def one(x)
end
foobar! def one(x)
end
фубар def one(x)
end
foobar
def one(x)
end
FooBar1 def two(y)
code
end
end
EOF
end
end

View file

@ -0,0 +1,66 @@
require 'spec_helper'
describe "Indenting" do
specify "nested blocks" do
assert_correct_indenting <<~EOF
var.func1(:param => 'value') do
var.func2(:param => 'value') do
puts "test"
end
end
EOF
assert_correct_indenting <<~EOF
var.func1(:param => 'value') {
var.func2(:param => 'value') {
foo({ bar => baz })
puts "test one"
puts "test two"
}
}
EOF
assert_correct_indenting <<~EOF
var.
func1(:param => 'value') {
var.func2(:param => 'value') {
puts "test"
}
}
EOF
end
specify "nested hashes" do
assert_correct_indenting <<~EOF
foo, bar = {
:bar => {
:one => 'two',
:five => 'six'
}
}
EOF
assert_correct_indenting <<~EOF
foo,
bar = {
:bar => {
:foo => { 'bar' => 'baz' },
:one => 'two',
:three => 'four'
}
}
EOF
end
specify "nested blocks with a continuation and function call inbetween" do
assert_correct_indenting <<~EOF
var.
func1(:param => 'value') {
func1_5(:param => 'value')
var.func2(:param => 'value') {
puts "test"
}
}
EOF
end
end

View file

@ -0,0 +1,46 @@
require 'spec_helper'
describe "Indenting" do
specify "splats with blocks in square brackets" do
assert_correct_indenting <<~EOF
x = Foo[*
y do
z
end
]
EOF
assert_correct_indenting <<~EOF
x = Foo[* # with a comment
y do
z
end
]
EOF
end
specify "splats with blocks in assignment" do
assert_correct_indenting <<~EOF
x = *
array.map do
3
end
EOF
end
specify "splats with blocks in round brackets" do
assert_correct_indenting <<~EOF
x = Foo(*y do
z
end)
EOF
assert_correct_indenting <<~EOF
x = Foo(
*y do
z
end
)
EOF
end
end

View file

@ -0,0 +1,40 @@
require 'vimrunner'
require 'vimrunner/rspec'
Vimrunner::RSpec.configure do |config|
config.reuse_server = true
config.start_vim do
vim = Vimrunner.start_gvim
vim.prepend_runtimepath(File.expand_path('../..', __FILE__))
vim.add_plugin(File.expand_path('../vim', __FILE__), 'plugin/syntax_test.vim')
vim.set 'expandtab'
vim.set 'shiftwidth', 2
vim
end
def assert_correct_indenting(extension='rb', string)
filename = "test.#{extension}"
IO.write filename, string
vim.edit filename
vim.normal 'gg=G'
vim.write
expect(IO.read(filename)).to eq string
end
def assert_correct_highlighting(extension='rb', string, patterns, group)
filename = "test.#{extension}"
IO.write filename, string
vim.edit filename
Array(patterns).each do |pattern|
# TODO: add a custom matcher
expect(vim.echo("TestSyntax('#{pattern}', '#{group}')")).to eq '1'
end
end
end

View file

@ -0,0 +1,17 @@
require 'spec_helper'
describe "Syntax highlighting" do
specify "block parameters" do
assert_correct_highlighting <<~'EOF', 'bar', 'rubySymbol'
foo { |bar:| 42 }
EOF
assert_correct_highlighting <<~'EOF', %w[bar\ze: baz\ze:], 'rubySymbol'
foo { |bar: 'bar', baz: 'baz'| 42 }
EOF
end
specify "block parameters with default values including '|'" do
assert_correct_highlighting <<~'EOF', %w[|\zebar qux)\zs|], 'rubyBlockParameterList'
foo { |bar=(baz|qux)| 42 }
EOF
end
end

View file

@ -0,0 +1,54 @@
require 'spec_helper'
describe "Syntax highlighting" do
specify "single line comments" do
assert_correct_highlighting <<~'EOF', '#.*', 'rubyComment'
# comment line
EOF
end
specify "end of line comments" do
assert_correct_highlighting <<~'EOF', '#.*', 'rubyComment'
foo = 42 # comment
EOF
end
specify "multiline comments" do
assert_correct_highlighting <<~'EOF', ['#.*line 1', '#.*line 2'], 'rubyComment'
# comment line 1
# comment line 2
EOF
end
specify "embedded documentation" do
assert_correct_highlighting <<~'EOF', 'documentation.*', 'rubyDocumentation'
=begin
documentation line
=end
EOF
# See issue #3
assert_correct_highlighting <<~'EOF', 'documentation.*', 'rubyDocumentation'
=begin rdoc
documentation line
=end rdoc
EOF
end
specify "magic comments" do
assert_correct_highlighting <<~'EOF', 'frozen_string_literal', 'rubyMagicComment'
# frozen_string_literal: true
EOF
end
specify "TODO comments" do
assert_correct_highlighting <<~'EOF', 'TODO', 'rubyTodo'
# TODO: turn off the oven
EOF
end
specify "shebang comments" do
assert_correct_highlighting <<~'EOF', '#.*', 'rubySharpBang'
#!/bin/ruby
EOF
end
end

View file

@ -0,0 +1,23 @@
require 'spec_helper'
describe "Syntax highlighting" do
specify "useless line continuations" do
str = <<~'EOF'
foo = \
if true
42
end
EOF
assert_correct_highlighting str, '\\', 'rubyUselessLineContinuation'
assert_correct_highlighting str, 'if', 'rubyConditional'
end
specify "line continuations" do
str = <<~'EOF'
foo = 42 \
if true
EOF
assert_correct_highlighting str, '\\', 'rubyLineContinuation'
assert_correct_highlighting str, 'if', 'rubyConditionalModifier'
end
end

View file

@ -0,0 +1,36 @@
require 'spec_helper'
describe "Syntax highlighting" do
specify "method definitions" do
str = <<~'EOF'
def foo bar
end
EOF
assert_correct_highlighting str, %w[def end], 'rubyDefine'
assert_correct_highlighting str, 'foo', 'rubyMethodName'
end
specify "method definitions named 'end'" do
assert_correct_highlighting <<~'EOF', 'end', 'rubyMethodName'
def end end
EOF
assert_correct_highlighting <<~'EOF', 'end', 'rubyMethodName'
def
end
end
EOF
end
specify "method parameters with symbol default values" do
assert_correct_highlighting <<~'EOF', ':baz', 'rubySymbol'
def foo bar=:baz
end
EOF
end
specify "unparenthesised method parameters with a required trailing keyword then semicolon" do
assert_correct_highlighting <<~'EOF', 'bar', 'rubySymbol'
def foo bar:; end
EOF
end
end

View file

@ -0,0 +1,204 @@
require 'spec_helper'
describe "Syntax highlighting" do
before :each do
vim.command 'let g:ruby_operators = 1'
end
after :each do
vim.command 'unlet g:ruby_operators'
end
specify "defined? operator" do
assert_correct_highlighting 'defined? foo', 'defined?', 'rubyDefinedOperator'
end
specify "English boolean operators" do
assert_correct_highlighting <<~'EOF', %w[not and or], 'rubyEnglishBooleanOperator'
not true
true and false
true or false
EOF
end
specify "modulo-assignment operators" do
assert_correct_highlighting <<~'EOF', '%=', 'rubyAssignmentOperator'
foo %= bar
EOF
end
specify "ternary operators" do
assert_correct_highlighting <<~'EOF', %w[? :], 'rubyTernaryOperator'
foo = bar ? 4 : 2
EOF
end
context "bracket operators" do
specify "after a plain identifier" do
assert_correct_highlighting <<~'EOF', '\\[..]', 'rubyOperator'
foo[42]
EOF
end
specify "after a ?!-named bare method call" do
assert_correct_highlighting <<~'EOF', '\\[..]', 'rubyOperator'
foo?[42]
EOF
end
specify "after a closing parenthesis" do
assert_correct_highlighting <<~'EOF', '\\[..]', 'rubyOperator'
(foo)[42]
EOF
end
specify "after a literal hash" do
assert_correct_highlighting <<~'EOF', '\\[...]', 'rubyOperator'
{ foo: bar }[foo]
EOF
end
specify "after a block arg method call" do
assert_correct_highlighting <<~'EOF', '\\[..]', 'rubyOperator'
foo { bar }[42]
EOF
end
end
specify "exponentiation operators" do
[
'foo**bar',
'foo ** bar',
'foo** bar',
].each do |str|
assert_correct_highlighting str, '\*\*', 'rubyArithmeticOperator'
end
end
context "double splat operators" do
specify "in method definitions" do
assert_correct_highlighting <<~'EOF', '\*\*', 'rubyDoubleSplatOperator'
def foo(**bar)
end
EOF
end
specify "in multiline parameter list method definitions" do
assert_correct_highlighting <<~'EOF', '\*\*', 'rubyDoubleSplatOperator'
def foo(bar,
**baz)
end
EOF
end
specify "as an anonymous parameter in method definitions" do
assert_correct_highlighting <<~'EOF', '\*\*', 'rubyDoubleSplatOperator'
def foo(**)
end
EOF
end
specify "in unparenthesised method definitions" do
assert_correct_highlighting <<~'EOF', '\*\*', 'rubyDoubleSplatOperator'
def foo **bar
end
EOF
end
specify "in unparenthesised method calls" do
assert_correct_highlighting <<~'EOF', '\*\*', 'rubyDoubleSplatOperator'
foo **bar
EOF
end
specify "in block parameter lists" do
assert_correct_highlighting <<~'EOF', '\*\*', 'rubyDoubleSplatOperator'
foo { |**bar| 42 }
EOF
end
end
specify "multiplication operators" do
[
'foo*bar',
'foo * bar',
'foo* bar',
].each do |str|
assert_correct_highlighting str, '\*', 'rubyArithmeticOperator'
end
end
context "splat operators" do
specify "in method definitions" do
assert_correct_highlighting <<~'EOF', '\*', 'rubySplatOperator'
def foo(*bar)
end
EOF
end
specify "in multiline parameter list method definitions" do
assert_correct_highlighting <<~'EOF', '\*', 'rubySplatOperator'
def foo(bar,
*baz)
end
EOF
end
specify "as an anonymous parameter in method definitions" do
assert_correct_highlighting <<~'EOF', '\*', 'rubySplatOperator'
def foo(*)
end
EOF
end
specify "in unparenthesised method definitions" do
assert_correct_highlighting <<~'EOF', '\*', 'rubySplatOperator'
def foo *bar
end
EOF
end
specify "in unparenthesised method calls" do
assert_correct_highlighting <<~'EOF', '\*', 'rubySplatOperator'
foo *bar
EOF
end
specify "in block parameter lists" do
assert_correct_highlighting <<~'EOF', '\*', 'rubySplatOperator'
foo { |*bar| 42 }
EOF
end
end
context "proc operators" do
specify "in method definitions" do
assert_correct_highlighting <<~'EOF', '&', 'rubyProcOperator'
def foo(&bar)
end
EOF
end
specify "in multiline parameter list method definitions" do
assert_correct_highlighting <<~'EOF', '&', 'rubyProcOperator'
def foo(bar,
&baz)
end
EOF
end
specify "in unparenthesised method definitions" do
assert_correct_highlighting <<~'EOF', '&', 'rubyProcOperator'
def foo &bar
end
EOF
end
specify "in unparenthesised method calls" do
assert_correct_highlighting <<~'EOF', '&', 'rubyProcOperator'
foo &bar
EOF
end
specify "before literal lambdas" do
assert_correct_highlighting <<~'EOF', '&', 'rubyProcOperator'
foo &->{}
EOF
end
end
specify "eigenclass operators" do
assert_correct_highlighting <<~'EOF', '<<', 'rubyEigenClassOperator'
class << self
end
EOF
end
specify "superclass operators" do
assert_correct_highlighting <<~'EOF', '<', 'rubySuperClassOperator'
class Foo < Bar
end
EOF
end
end

View file

@ -0,0 +1,21 @@
require 'spec_helper'
describe "Syntax highlighting" do
# See issue #171
specify "ambiguous / at end of line is not a regexp" do
vim.command 'let g:ruby_operators = 1'
assert_correct_highlighting <<~'EOF', '/', 'rubyArithmeticOperator'
a = calculate(90).and_some_long_expression /
and_long_expression_here
puts a
EOF
vim.command 'unlet g:ruby_operators'
end
# See issue #63
specify "interpolated regexp in a host regexp" do
assert_correct_highlighting <<~'EOF', '/$', 'rubyRegexpDelimiter'
/#{foo.sub(/bar/, 'baz')}/
EOF
end
end

View file

@ -0,0 +1,31 @@
require 'spec_helper'
describe "Syntax highlighting" do
specify "only modifiers can appear after regexp literals" do
# See issue #254
assert_correct_highlighting <<~'EOF', 'if', 'rubyConditionalModifier'
def get_regex
/some regex/ if false
end
EOF
end
specify "only modifiers can appear after unparenthesised no-arg method calls" do
[
"foo if true",
"foo? if true",
"foo! if true",
"foo_ if true",
"foo_? if true",
"foo_! if true",
"foo42 if true",
"foo42? if true",
"foo42! if true",
"Foo if true",
"Foo? if true",
"Foo! if true"
].each do |str|
assert_correct_highlighting str, 'if', 'rubyConditionalModifier'
end
end
end

Some files were not shown because too many files have changed in this diff Show more