Add rst support

This commit is contained in:
Adam Stankiewicz 2018-12-26 11:22:36 +01:00
parent 678fc65514
commit cad135aa01
5 changed files with 385 additions and 1 deletions

View File

@ -8,7 +8,7 @@ A collection of language packs for Vim.
> One to rule them all, one to find them, one to bring them all and in the darkness bind them. > One to rule them all, one to find them, one to bring them all and in the darkness bind them.
- It **won't affect your startup time**, as scripts are loaded only on demand\*. - It **won't affect your startup time**, as scripts are loaded only on demand\*.
- It **installs and updates 100+ times faster** than the <!--Package Count-->114<!--/Package Count--> packages it consists of. - It **installs and updates 100+ times faster** than the <!--Package Count-->115<!--/Package Count--> packages it consists of.
- Solid syntax and indentation support (other features skipped). Only the best language packs. - Solid syntax and indentation support (other features skipped). Only the best language packs.
- All unnecessary files are ignored (like enormous documentation from php support). - All unnecessary files are ignored (like enormous documentation from php support).
- No support for esoteric languages, only most popular ones (modern too, like `slim`). - No support for esoteric languages, only most popular ones (modern too, like `slim`).
@ -128,6 +128,7 @@ If you need full functionality of any plugin, please use it directly with your p
- [ragel](https://github.com/jneen/ragel.vim) (syntax) - [ragel](https://github.com/jneen/ragel.vim) (syntax)
- [raml](https://github.com/IN3D/vim-raml) (syntax, ftplugin) - [raml](https://github.com/IN3D/vim-raml) (syntax, ftplugin)
- [rspec](https://github.com/sheerun/rspec.vim) (syntax) - [rspec](https://github.com/sheerun/rspec.vim) (syntax)
- [rst](https://github.com/marshallward/vim-restructuredtext) (syntax, autoload, ftplugin)
- [ruby](https://github.com/vim-ruby/vim-ruby) (syntax, indent, compiler, autoload, ftplugin) - [ruby](https://github.com/vim-ruby/vim-ruby) (syntax, indent, compiler, autoload, ftplugin)
- [rust](https://github.com/rust-lang/rust.vim) (syntax, indent, compiler, autoload, ftplugin) - [rust](https://github.com/rust-lang/rust.vim) (syntax, indent, compiler, autoload, ftplugin)
- [sbt](https://github.com/derekwyatt/vim-sbt) (syntax) - [sbt](https://github.com/derekwyatt/vim-sbt) (syntax)

54
autoload/RstFold.vim Normal file
View File

@ -0,0 +1,54 @@
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'rst') == -1
" Author: Antony Lee <anntzer.lee@gmail.com>
" Description: Helper functions for reStructuredText syntax folding
" Last Modified: 2018-01-07
function s:CacheRstFold()
let closure = {'header_types': {}, 'max_level': 0, 'levels': {}}
function closure.Process(match) dict
let curline = getcurpos()[1]
if has_key(self.levels, curline - 1)
" For over+under-lined headers, the regex will match both at the
" overline and at the title itself; in that case, skip the second match.
return
endif
let lines = split(a:match, '\n')
let key = repeat(lines[-1][0], len(lines))
if !has_key(self.header_types, key)
let self.max_level += 1
let self.header_types[key] = self.max_level
endif
let self.levels[curline] = self.header_types[key]
endfunction
let save_cursor = getcurpos()
let save_mark = getpos("'[")
silent keeppatterns %s/\v^%(%(([=`:.'"~^_*+#-])\1+\n)?.{1,2}\n([=`:.'"~^_*+#-])\2+)|%(%(([=`:.''"~^_*+#-])\3{2,}\n)?.{3,}\n([=`:.''"~^_*+#-])\4{2,})$/\=closure.Process(submatch(0))/gn
call setpos('.', save_cursor)
call setpos("'[", save_mark)
let b:RstFoldCache = closure.levels
endfunction
function RstFold#GetRstFold()
if !has_key(b:, 'RstFoldCache')
call s:CacheRstFold()
endif
if has_key(b:RstFoldCache, v:lnum)
return '>' . b:RstFoldCache[v:lnum]
else
return '='
endif
endfunction
function RstFold#GetRstFoldText()
if !has_key(b:, 'RstFoldCache')
call s:CacheRstFold()
endif
let indent = repeat(' ', b:RstFoldCache[v:foldstart] - 1)
let thisline = getline(v:foldstart)
" For over+under-lined headers, skip the overline.
let text = thisline =~ '^\([=`:.''"~^_*+#-]\)\1\+$' ? getline(v:foldstart + 1) : thisline
return indent . text
endfunction
endif

1
build
View File

@ -239,6 +239,7 @@ PACKS="
rspec:sheerun/rspec.vim rspec:sheerun/rspec.vim
ruby:vim-ruby/vim-ruby ruby:vim-ruby/vim-ruby
rust:rust-lang/rust.vim rust:rust-lang/rust.vim
rst:marshallward/vim-restructuredtext
sbt:derekwyatt/vim-sbt sbt:derekwyatt/vim-sbt
scala:derekwyatt/vim-scala scala:derekwyatt/vim-scala
scss:cakebaker/scss-syntax.vim scss:cakebaker/scss-syntax.vim

46
ftplugin/rst.vim Normal file
View File

@ -0,0 +1,46 @@
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'rst') == -1
" reStructuredText filetype plugin file
" Language: reStructuredText documentation format
" Maintainer: Marshall Ward <marshall.ward@gmail.com>
" Original Maintainer: Nikolai Weibull <now@bitwi.se>
" Website: https://github.com/marshallward/vim-restructuredtext
" Latest Revision: 2018-01-07
if exists("b:did_ftplugin")
finish
endif
let b:did_ftplugin = 1
let s:cpo_save = &cpo
set cpo&vim
let b:undo_ftplugin = "setl com< cms< et< fo<"
setlocal comments=fb:.. commentstring=..\ %s expandtab
setlocal formatoptions+=tcroql
" reStructuredText standard recommends that tabs be expanded to 8 spaces
" The choice of 3-space indentation is to provide slightly better support for
" directives (..) and ordered lists (1.), although it can cause problems for
" many other cases.
"
" More sophisticated indentation rules should be revisted in the future.
if !exists("g:rst_style") || g:rst_style != 0
setlocal expandtab shiftwidth=3 softtabstop=3 tabstop=8
endif
if has('patch-7.3.867') " Introduced the TextChanged event.
setlocal foldmethod=expr
setlocal foldexpr=RstFold#GetRstFold()
setlocal foldtext=RstFold#GetRstFoldText()
augroup RstFold
autocmd TextChanged,InsertLeave <buffer> unlet! b:RstFoldCache
augroup END
endif
let &cpo = s:cpo_save
unlet s:cpo_save
endif

282
syntax/rst.vim Normal file
View File

@ -0,0 +1,282 @@
if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'rst') == -1
" Vim syntax file
" Language: reStructuredText documentation format
" Maintainer: Marshall Ward <marshall.ward@gmail.com>
" Previous Maintainer: Nikolai Weibull <now@bitwi.se>
" Website: https://github.com/marshallward/vim-restructuredtext
" Latest Revision: 2018-07-23
if exists("b:current_syntax")
finish
endif
let s:cpo_save = &cpo
set cpo&vim
syn case ignore
syn match rstTransition /^[=`:.'"~^_*+#-]\{4,}\s*$/
syn cluster rstCruft contains=rstEmphasis,rstStrongEmphasis,
\ rstInterpretedText,rstInlineLiteral,rstSubstitutionReference,
\ rstInlineInternalTargets,rstFootnoteReference,rstHyperlinkReference
syn region rstLiteralBlock matchgroup=rstDelimiter
\ start='::\_s*\n\ze\z(\s\+\)' skip='^$' end='^\z1\@!'
\ contains=@NoSpell
syn region rstQuotedLiteralBlock matchgroup=rstDelimiter
\ start="::\_s*\n\ze\z([!\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]\)"
\ end='^\z1\@!' contains=@NoSpell
syn region rstDoctestBlock oneline display matchgroup=rstDelimiter
\ start='^>>>\s' end='^$'
syn region rstTable transparent start='^\n\s*+[-=+]\+' end='^$'
\ contains=rstTableLines,@rstCruft
syn match rstTableLines contained display '|\|+\%(=\+\|-\+\)\='
syn region rstSimpleTable transparent
\ start='^\n\%(\s*\)\@>\%(\%(=\+\)\@>\%(\s\+\)\@>\)\%(\%(\%(=\+\)\@>\%(\s*\)\@>\)\+\)\@>$'
\ end='^$'
\ contains=rstSimpleTableLines,@rstCruft
syn match rstSimpleTableLines contained display
\ '^\%(\s*\)\@>\%(\%(=\+\)\@>\%(\s\+\)\@>\)\%(\%(\%(=\+\)\@>\%(\s*\)\@>\)\+\)\@>$'
syn match rstSimpleTableLines contained display
\ '^\%(\s*\)\@>\%(\%(-\+\)\@>\%(\s\+\)\@>\)\%(\%(\%(-\+\)\@>\%(\s*\)\@>\)\+\)\@>$'
syn cluster rstDirectives contains=rstFootnote,rstCitation,
\ rstHyperlinkTarget,rstExDirective
syn match rstExplicitMarkup '^\s*\.\.\_s'
\ nextgroup=@rstDirectives,rstComment,rstSubstitutionDefinition
" "Simple reference names are single words consisting of alphanumerics plus
" isolated (no two adjacent) internal hyphens, underscores, periods, colons
" and plus signs."
let s:ReferenceName = '[[:alnum:]]\%([-_.:+]\?[[:alnum:]]\+\)*'
syn keyword rstTodo contained FIXME TODO XXX NOTE
execute 'syn region rstComment contained' .
\ ' start=/.*/'
\ ' skip=+^$+' .
\ ' end=/^\s\@!/ contains=rstTodo'
execute 'syn region rstFootnote contained matchgroup=rstDirective' .
\ ' start=+\[\%(\d\+\|#\%(' . s:ReferenceName . '\)\=\|\*\)\]\_s+' .
\ ' skip=+^$+' .
\ ' end=+^\s\@!+ contains=@rstCruft,@NoSpell'
execute 'syn region rstCitation contained matchgroup=rstDirective' .
\ ' start=+\[' . s:ReferenceName . '\]\_s+' .
\ ' skip=+^$+' .
\ ' end=+^\s\@!+ contains=@rstCruft,@NoSpell'
syn region rstHyperlinkTarget contained matchgroup=rstDirective
\ start='_\%(_\|[^:\\]*\%(\\.[^:\\]*\)*\):\_s' skip=+^$+ end=+^\s\@!+
syn region rstHyperlinkTarget contained matchgroup=rstDirective
\ start='_`[^`\\]*\%(\\.[^`\\]*\)*`:\_s' skip=+^$+ end=+^\s\@!+
syn region rstHyperlinkTarget matchgroup=rstDirective
\ start=+^__\_s+ skip=+^$+ end=+^\s\@!+
execute 'syn region rstExDirective contained matchgroup=rstDirective' .
\ ' start=+' . s:ReferenceName . '::\_s+' .
\ ' skip=+^$+' .
\ ' end=+^\s\@!+ contains=@rstCruft,rstLiteralBlock'
execute 'syn match rstSubstitutionDefinition contained' .
\ ' /|.*|\_s\+/ nextgroup=@rstDirectives'
function! s:DefineOneInlineMarkup(name, start, middle, end, char_left, char_right)
execute 'syn region rst' . a:name .
\ ' start=+' . a:char_left . '\zs' . a:start .
\ '\ze[^[:space:]' . a:char_right . a:start[strlen(a:start) - 1] . ']+' .
\ a:middle .
\ ' end=+\S' . a:end . '\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)+'
endfunction
function! s:DefineInlineMarkup(name, start, middle, end)
let middle = a:middle != "" ?
\ (' skip=+\\\\\|\\' . a:middle . '+') :
\ ""
call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, "'", "'")
call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '"', '"')
call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '(', ')')
call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '\[', '\]')
call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '{', '}')
call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '<', '>')
call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '', '')
" TODO: Additional Unicode Pd, Po, Pi, Pf, Ps characters
call s:DefineOneInlineMarkup(a:name, a:start, middle, a:end, '\%(^\|\s\|\%ua0\|[/:]\)', '')
execute 'syn match rst' . a:name .
\ ' +\%(^\|\s\|\%ua0\|[''"([{</:]\)\zs' . a:start .
\ '[^[:space:]' . a:start[strlen(a:start) - 1] . ']'
\ a:end . '\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)+'
execute 'hi def link rst' . a:name . 'Delimiter' . ' rst' . a:name
endfunction
call s:DefineInlineMarkup('Emphasis', '\*', '\*', '\*')
call s:DefineInlineMarkup('StrongEmphasis', '\*\*', '\*', '\*\*')
call s:DefineInlineMarkup('InterpretedTextOrHyperlinkReference', '`', '`', '`_\{0,2}')
call s:DefineInlineMarkup('InlineLiteral', '``', "", '``')
call s:DefineInlineMarkup('SubstitutionReference', '|', '|', '|_\{0,2}')
call s:DefineInlineMarkup('InlineInternalTargets', '_`', '`', '`')
" Sections are identified through their titles, which are marked up with
" adornment: "underlines" below the title text, or underlines and matching
" "overlines" above the title. An underline/overline is a single repeated
" punctuation character that begins in column 1 and forms a line extending at
" least as far as the right edge of the title text.
"
" It is difficult to count characters in a regex, but we at least special-case
" the case where the title has at least three characters to require the
" adornment to have at least three characters as well, in order to handle
" properly the case of a literal block:
"
" this is the end of a paragraph
" ::
" this is a literal block
syn match rstSections "\v^%(([=`:.'"~^_*+#-])\1+\n)?.{1,2}\n([=`:.'"~^_*+#-])\2+$"
\ contains=@Spell
syn match rstSections "\v^%(([=`:.'"~^_*+#-])\1{2,}\n)?.{3,}\n([=`:.'"~^_*+#-])\2{2,}$"
\ contains=@Spell
" TODO: Cant remember why these two cant be defined like the ones above.
execute 'syn match rstFootnoteReference contains=@NoSpell' .
\ ' +\%(\s\|^\)\[\%(\d\+\|#\%(' . s:ReferenceName . '\)\=\|\*\)\]_+'
execute 'syn match rstCitationReference contains=@NoSpell' .
\ ' +\%(\s\|^\)\[' . s:ReferenceName . '\]_\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)+'
execute 'syn match rstHyperlinkReference' .
\ ' /\<' . s:ReferenceName . '__\=\ze\%($\|\s\|[''")\]}>/:.,;!?\\-]\)/'
syn match rstStandaloneHyperlink contains=@NoSpell
\ "\<\%(\%(\%(https\=\|file\|ftp\|gopher\)://\|\%(mailto\|news\):\)[^[:space:]'\"<>]\+\|www[[:alnum:]_-]*\.[[:alnum:]_-]\+\.[^[:space:]'\"<>]\+\)[[:alnum:]/]"
syn region rstCodeBlock contained matchgroup=rstDirective
\ start=+\%(sourcecode\|code\%(-block\)\=\)::\s\+.*\_s*\n\ze\z(\s\+\)+
\ skip=+^$+
\ end=+^\z1\@!+
\ contains=@NoSpell
syn cluster rstDirectives add=rstCodeBlock
if !exists('g:rst_syntax_code_list')
" A mapping from a Vim filetype to a list of alias patterns (pattern
" branches to be specific, see ':help /pattern'). E.g. given:
"
" let g:rst_syntax_code_list = {
" \ 'cpp': ['cpp', 'c++'],
" \ }
"
" then the respective contents of the following two rST directives:
"
" .. code:: cpp
"
" auto i = 42;
"
" .. code:: C++
"
" auto i = 42;
"
" will both be highlighted as C++ code. As shown by the latter block
" pattern matching will be case-insensitive.
let g:rst_syntax_code_list = {
\ 'vim': ['vim'],
\ 'java': ['java'],
\ 'cpp': ['cpp', 'c++'],
\ 'lisp': ['lisp'],
\ 'php': ['php'],
\ 'python': ['python'],
\ 'perl': ['perl'],
\ 'sh': ['sh'],
\ }
elseif type(g:rst_syntax_code_list) == type([])
" backward compatibility with former list format
let s:old_spec = g:rst_syntax_code_list
let g:rst_syntax_code_list = {}
for s:elem in s:old_spec
let g:rst_syntax_code_list[s:elem] = [s:elem]
endfor
endif
for s:filetype in keys(g:rst_syntax_code_list)
unlet! b:current_syntax
" guard against setting 'isk' option which might cause problems (issue #108)
let prior_isk = &l:iskeyword
let s:alias_pattern = ''
\.'\%('
\.join(g:rst_syntax_code_list[s:filetype], '\|')
\.'\)'
exe 'syn include @rst'.s:filetype.' syntax/'.s:filetype.'.vim'
exe 'syn region rstDirective'.s:filetype
\.' matchgroup=rstDirective fold'
\.' start="\c\%(sourcecode\|code\%(-block\)\=\)::\s\+'.s:alias_pattern.'\_s*\n\ze\z(\s\+\)"'
\.' skip=#^$#'
\.' end=#^\z1\@!#'
\.' contains=@NoSpell,@rst'.s:filetype
exe 'syn cluster rstDirectives add=rstDirective'.s:filetype
" reset 'isk' setting, if it has been changed
if &l:iskeyword !=# prior_isk
let &l:iskeyword = prior_isk
endif
unlet! prior_isk
endfor
" Enable top level spell checking
syntax spell toplevel
" TODO: Use better syncing.
syn sync minlines=50 linebreaks=2
hi def link rstTodo Todo
hi def link rstComment Comment
hi def link rstSections Title
hi def link rstTransition rstSections
hi def link rstLiteralBlock String
hi def link rstQuotedLiteralBlock String
hi def link rstDoctestBlock PreProc
hi def link rstTableLines rstDelimiter
hi def link rstSimpleTableLines rstTableLines
hi def link rstExplicitMarkup rstDirective
hi def link rstDirective Keyword
hi def link rstFootnote String
hi def link rstCitation String
hi def link rstHyperlinkTarget String
hi def link rstExDirective String
hi def link rstSubstitutionDefinition rstDirective
hi def link rstDelimiter Delimiter
hi def link rstInterpretedTextOrHyperlinkReference Identifier
hi def link rstInlineLiteral String
hi def link rstSubstitutionReference PreProc
hi def link rstInlineInternalTargets Identifier
hi def link rstFootnoteReference Identifier
hi def link rstCitationReference Identifier
hi def link rstHyperLinkReference Identifier
hi def link rstStandaloneHyperlink Identifier
hi def link rstCodeBlock String
if exists('g:rst_use_emphasis_colors')
" TODO: Less arbitrary color selection
hi def rstEmphasis ctermfg=13 term=italic cterm=italic gui=italic
hi def rstStrongEmphasis ctermfg=1 term=bold cterm=bold gui=bold
else
hi def rstEmphasis term=italic cterm=italic gui=italic
hi def rstStrongEmphasis term=bold cterm=bold gui=bold
endif
let b:current_syntax = "rst"
let &cpo = s:cpo_save
unlet s:cpo_save
endif