From d9ee362537a34473b32ad3b4a1d7feb56a6b6811 Mon Sep 17 00:00:00 2001 From: Adam Stankiewicz Date: Sun, 6 Sep 2020 15:08:20 +0200 Subject: Fix weird indentation issue of yaml --- README.md | 5 +- after/ftplugin/yaml.vim | 2 +- after/syntax/yaml.vim | 2 +- ftdetect/polyglot.vim | 3 + ftplugin/yaml.vim | 29 ++++++ indent/yaml.vim | 159 +++++++++++++++++++++++++++++++ packages.yaml | 9 +- scripts/build | 9 +- syntax/yaml.vim | 247 ++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 455 insertions(+), 10 deletions(-) create mode 100644 ftplugin/yaml.vim create mode 100644 indent/yaml.vim create mode 100644 syntax/yaml.vim diff --git a/README.md b/README.md index 6789eb72..deaa8666 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,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. - It **won't affect your startup time**, as scripts are loaded only on demand\*. -- It **installs and updates 120+ times faster** than the 191 packages it consists of. +- It **installs and updates 120+ times faster** than the 192 packages it consists of. - It is more secure because scripts loaded for all extensions are generated by vim-polyglot (ftdetect). - Solid syntax and indentation support (other features skipped). Only the best language packs. - All unnecessary files are ignored (like enormous documentation from php support). @@ -229,7 +229,8 @@ If you need full functionality of any plugin, please use it directly with your p - [xdc](https://github.com/amal-khailtash/vim-xdc-syntax) - [xml](https://github.com/amadeus/vim-xml) - [xsl](https://github.com/vim-scripts/XSLT-syntax) -- [yaml](https://github.com/stephpy/vim-yaml) +- [yaml-extras](https://github.com/stephpy/vim-yaml) +- [yaml](https://github.com/vim/vim/tree/df44a27b53586fccfc6a3aedc89061fdd9a515ff/runtime) - [yard](https://github.com/sheerun/vim-yardoc) - [zephir](https://github.com/xwsoul/vim-zephir) - [zig](https://github.com/ziglang/zig.vim) diff --git a/after/ftplugin/yaml.vim b/after/ftplugin/yaml.vim index db608fa4..69785fc2 100644 --- a/after/ftplugin/yaml.vim +++ b/after/ftplugin/yaml.vim @@ -1,4 +1,4 @@ -if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'yaml') == -1 +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'yaml-extras') == -1 " Vim indent file " Language: Yaml diff --git a/after/syntax/yaml.vim b/after/syntax/yaml.vim index 097d1535..3a1e89a2 100644 --- a/after/syntax/yaml.vim +++ b/after/syntax/yaml.vim @@ -1,4 +1,4 @@ -if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'yaml') == -1 +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'yaml-extras') == -1 " To make this file do stuff, add something like the following (without the " leading ") to your ~/.vimrc: diff --git a/ftdetect/polyglot.vim b/ftdetect/polyglot.vim index 186ba9da..792c2432 100644 --- a/ftdetect/polyglot.vim +++ b/ftdetect/polyglot.vim @@ -1677,6 +1677,9 @@ if !has_key(s:disabled_packages, 'xsl') au BufNewFile,BufRead *.xslt setf xsl endif +if !has_key(s:disabled_packages, 'yaml-extras') +endif + if !has_key(s:disabled_packages, 'ansible') au BufNewFile,BufRead group_vars/* setf yaml.ansible au BufNewFile,BufRead handlers.*.y{a,}ml setf yaml.ansible diff --git a/ftplugin/yaml.vim b/ftplugin/yaml.vim new file mode 100644 index 00000000..2e9728bd --- /dev/null +++ b/ftplugin/yaml.vim @@ -0,0 +1,29 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'yaml') == -1 + +" Vim filetype plugin file +" Language: YAML (YAML Ain't Markup Language) +" Previous Maintainer: Nikolai Weibull (inactive) +" Last Change: 2020 Mar 02 + +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=:# commentstring=#\ %s expandtab +setlocal formatoptions-=t formatoptions+=croql + +if !exists("g:yaml_recommended_style") || g:yaml_recommended_style != 0 + let b:undo_ftplugin ..= " sw< sts<" + setlocal shiftwidth=2 softtabstop=2 +endif + +let &cpo = s:cpo_save +unlet s:cpo_save + +endif diff --git a/indent/yaml.vim b/indent/yaml.vim new file mode 100644 index 00000000..c7fdebcf --- /dev/null +++ b/indent/yaml.vim @@ -0,0 +1,159 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'yaml') == -1 + +" Vim indent file +" Language: YAML +" Maintainer: Nikolai Pavlov +" Last Change: 2019 Sep 28 + +" Only load this indent file when no other was loaded. +if exists('b:did_indent') + finish +endif + +let s:save_cpo = &cpo +set cpo&vim + +let b:did_indent = 1 + +setlocal indentexpr=GetYAMLIndent(v:lnum) +setlocal indentkeys=!^F,o,O,0#,0},0],<:>,0- +setlocal nosmartindent + +let b:undo_indent = 'setlocal indentexpr< indentkeys< smartindent<' + +" Only define the function once. +if exists('*GetYAMLIndent') + finish +endif + +function s:FindPrevLessIndentedLine(lnum, ...) + let prevlnum = prevnonblank(a:lnum-1) + let curindent = a:0 ? a:1 : indent(a:lnum) + while prevlnum + \&& indent(prevlnum) >= curindent + \&& getline(prevlnum) !~# '^\s*#' + let prevlnum = prevnonblank(prevlnum-1) + endwhile + return prevlnum +endfunction + +function s:FindPrevLEIndentedLineMatchingRegex(lnum, regex) + let plilnum = s:FindPrevLessIndentedLine(a:lnum, indent(a:lnum)+1) + while plilnum && getline(plilnum) !~# a:regex + let plilnum = s:FindPrevLessIndentedLine(plilnum) + endwhile + return plilnum +endfunction + +let s:mapkeyregex='\v^\s*\#@!\S@=%(\''%([^'']|\''\'')*\'''. + \ '|\"%([^"\\]|\\.)*\"'. + \ '|%(%(\:\ )@!.)*)\:%(\ |$)' +let s:liststartregex='\v^\s*%(\-%(\ |$))' + +let s:c_ns_anchor_char = '\v%([\n\r\uFEFF \t,[\]{}]@!\p)' +let s:c_ns_anchor_name = s:c_ns_anchor_char.'+' +let s:c_ns_anchor_property = '\v\&'.s:c_ns_anchor_name + +let s:ns_word_char = '\v[[:alnum:]_\-]' +let s:ns_tag_char = '\v%(%\x\x|'.s:ns_word_char.'|[#/;?:@&=+$.~*''()])' +let s:c_named_tag_handle = '\v\!'.s:ns_word_char.'+\!' +let s:c_secondary_tag_handle = '\v\!\!' +let s:c_primary_tag_handle = '\v\!' +let s:c_tag_handle = '\v%('.s:c_named_tag_handle. + \ '|'.s:c_secondary_tag_handle. + \ '|'.s:c_primary_tag_handle.')' +let s:c_ns_shorthand_tag = '\v'.s:c_tag_handle . s:ns_tag_char.'+' +let s:c_non_specific_tag = '\v\!' +let s:ns_uri_char = '\v%(%\x\x|'.s:ns_word_char.'\v|[#/;?:@&=+$,.!~*''()[\]])' +let s:c_verbatim_tag = '\v\!\<'.s:ns_uri_char.'+\>' +let s:c_ns_tag_property = '\v'.s:c_verbatim_tag. + \ '\v|'.s:c_ns_shorthand_tag. + \ '\v|'.s:c_non_specific_tag + +let s:block_scalar_header = '\v[|>]%([+-]?[1-9]|[1-9]?[+-])?' + +function GetYAMLIndent(lnum) + if a:lnum == 1 || !prevnonblank(a:lnum-1) + return 0 + endif + + let prevlnum = prevnonblank(a:lnum-1) + let previndent = indent(prevlnum) + + let line = getline(a:lnum) + if line =~# '^\s*#' && getline(a:lnum-1) =~# '^\s*#' + " Comment blocks should have identical indent + return previndent + elseif line =~# '^\s*[\]}]' + " Lines containing only closing braces should have previous indent + return indent(s:FindPrevLessIndentedLine(a:lnum)) + endif + + " Ignore comment lines when calculating indent + while getline(prevlnum) =~# '^\s*#' + let prevlnum = prevnonblank(prevlnum-1) + if !prevlnum + return previndent + endif + endwhile + + let prevline = getline(prevlnum) + let previndent = indent(prevlnum) + + " Any examples below assume that shiftwidth=2 + if prevline =~# '\v[{[:]$|[:-]\ [|>][+\-]?%(\s+\#.*|\s*)$' + " Mapping key: + " nested mapping: ... + " + " - { + " key: [ + " list value + " ] + " } + " + " - |- + " Block scalar without indentation indicator + return previndent+shiftwidth() + elseif prevline =~# '\v[:-]\ [|>]%(\d+[+\-]?|[+\-]?\d+)%(\#.*|\s*)$' + " - |+2 + " block scalar with indentation indicator + "#^^ indent+2, not indent+shiftwidth + return previndent + str2nr(matchstr(prevline, + \'\v([:-]\ [|>])@<=[+\-]?\d+%([+\-]?%(\s+\#.*|\s*)$)@=')) + elseif prevline =~# '\v\"%([^"\\]|\\.)*\\$' + " "Multiline string \ + " with escaped end" + let qidx = match(prevline, '\v\"%([^"\\]|\\.)*\\') + return virtcol([prevlnum, qidx+1]) + elseif line =~# s:liststartregex + " List line should have indent equal to previous list line unless it was + " caught by one of the previous rules + return indent(s:FindPrevLEIndentedLineMatchingRegex(a:lnum, + \ s:liststartregex)) + elseif line =~# s:mapkeyregex + " Same for line containing mapping key + let prevmapline = s:FindPrevLEIndentedLineMatchingRegex(a:lnum, + \ s:mapkeyregex) + if getline(prevmapline) =~# '^\s*- ' + return indent(prevmapline) + 2 + else + return indent(prevmapline) + endif + elseif prevline =~# '^\s*- ' + " - List with + " multiline scalar + return previndent+2 + elseif prevline =~# s:mapkeyregex . '\v\s*%(%('.s:c_ns_tag_property. + \ '\v|'.s:c_ns_anchor_property. + \ '\v|'.s:block_scalar_header. + \ '\v)%(\s+|\s*%(\#.*)?$))*' + " Mapping with: value + " that is multiline scalar + return previndent+shiftwidth() + endif + return previndent +endfunction + +let &cpo = s:save_cpo + +endif diff --git a/packages.yaml b/packages.yaml index f757cf99..65df8369 100644 --- a/packages.yaml +++ b/packages.yaml @@ -1650,7 +1650,9 @@ filetypes: linguist: XSLT --- name: yaml -remote: stephpy/vim-yaml +# Fixes indentation issue: https://github.com/vim/vim/issues/6417 +remote: vim/vim@df44a27b53586fccfc6a3aedc89061fdd9a515ff:runtime +glob: '**/yaml.vim' filetypes: - name: yaml linguist: YAML @@ -1659,6 +1661,11 @@ filetypes: - fish_read_history ignored_filenames: - '~/.config/fish/fish_{read_,}history' +--- +name: yaml-extras +remote: stephpy/vim-yaml +after: yaml +filetypes: [] # Ansible needs to be after YAML --- name: ansible diff --git a/scripts/build b/scripts/build index 79a1cfe7..c9a12743 100755 --- a/scripts/build +++ b/scripts/build @@ -155,7 +155,8 @@ end def parse_remote(remote) match = remote.match(/(?[^@:]+)(?:@(?[^:]+))?(?::(?.*))?/) - [match[:repo], match[:branch] || "master", match[:path]] + dir = "tmp/" + match[:repo] + (match[:branch] ? "-#{match[:branch]}" : "") + [match[:repo], match[:branch] || "master", match[:path], dir] end def copy_file(package, src, dest) @@ -179,8 +180,7 @@ def download(packages) packages.map { |p| p["remote"] or raise "No remote for: " + p["name"] }.uniq.each_slice(20) do |remotes| remotes.map do |remote| Thread.new do - repo, branch, path = parse_remote(remote) - dir = "tmp/" + repo + repo, branch, path, dir = parse_remote(remote) unless File.exist?(dir) FileUtils.mkdir_p(dir) url = "https://codeload.github.com/#{repo}/tar.gz/#{branch}" @@ -321,8 +321,7 @@ def extract(packages) output = [] packages.map do |package| - repo, branch, path = parse_remote(package["remote"]) - dir = "tmp/" + repo + repo, branch, path, dir = parse_remote(package["remote"]) dirs = package.fetch("dirs", default_dirs) ignored_dirs = package.fetch("ignored_dirs", []) if ignored_dirs.size > 0 diff --git a/syntax/yaml.vim b/syntax/yaml.vim new file mode 100644 index 00000000..53e60684 --- /dev/null +++ b/syntax/yaml.vim @@ -0,0 +1,247 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'yaml') == -1 + +" Vim syntax file +" Language: YAML (YAML Ain't Markup Language) 1.2 +" Maintainer: Nikolai Pavlov +" First author: Nikolai Weibull +" Latest Revision: 2015-03-28 +" removed duplicate yamlKeyValueDelimiter (pull #4799) + +if exists('b:current_syntax') + finish +endif + +let s:cpo_save = &cpo +set cpo&vim + +" Choose the schema to use +" TODO: Validate schema +if !exists('b:yaml_schema') + if exists('g:yaml_schema') + let b:yaml_schema = g:yaml_schema + else + let b:yaml_schema = 'core' + endif +endif + +let s:ns_char = '\%([\n\r\uFEFF \t]\@!\p\)' +let s:ns_word_char = '[[:alnum:]_\-]' +let s:ns_uri_char = '\%(%\x\x\|'.s:ns_word_char.'\|[#/;?:@&=+$,.!~*''()[\]]\)' +let s:ns_tag_char = '\%(%\x\x\|'.s:ns_word_char.'\|[#/;?:@&=+$.~*''()]\)' +let s:c_ns_anchor_char = '\%([\n\r\uFEFF \t,[\]{}]\@!\p\)' +let s:c_indicator = '[\-?:,[\]{}#&*!|>''"%@`]' +let s:c_flow_indicator = '[,[\]{}]' + +let s:ns_char_without_c_indicator = substitute(s:ns_char, '\v\C[\zs', '\=s:c_indicator[1:-2]', '') + +let s:_collection = '[^\@!\(\%(\\\.\|\[^\\\]]\)\+\)]' +let s:_neg_collection = '[^\(\%(\\\.\|\[^\\\]]\)\+\)]' +function s:SimplifyToAssumeAllPrintable(p) + return substitute(a:p, '\V\C\\%('.s:_collection.'\\@!\\p\\)', '[^\1]', '') +endfunction +let s:ns_char = s:SimplifyToAssumeAllPrintable(s:ns_char) +let s:ns_char_without_c_indicator = s:SimplifyToAssumeAllPrintable(s:ns_char_without_c_indicator) +let s:c_ns_anchor_char = s:SimplifyToAssumeAllPrintable(s:c_ns_anchor_char) + +function s:SimplifyAdjacentCollections(p) + return substitute(a:p, '\V\C'.s:_collection.'\\|'.s:_collection, '[\1\2]', 'g') +endfunction +let s:ns_uri_char = s:SimplifyAdjacentCollections(s:ns_uri_char) +let s:ns_tag_char = s:SimplifyAdjacentCollections(s:ns_tag_char) + +let s:c_verbatim_tag = '!<'.s:ns_uri_char.'\+>' +let s:c_named_tag_handle = '!'.s:ns_word_char.'\+!' +let s:c_secondary_tag_handle = '!!' +let s:c_primary_tag_handle = '!' +let s:c_tag_handle = '\%('.s:c_named_tag_handle. + \ '\|'.s:c_secondary_tag_handle. + \ '\|'.s:c_primary_tag_handle.'\)' +let s:c_ns_shorthand_tag = s:c_tag_handle . s:ns_tag_char.'\+' +let s:c_non_specific_tag = '!' +let s:c_ns_tag_property = s:c_verbatim_tag. + \ '\|'.s:c_ns_shorthand_tag. + \ '\|'.s:c_non_specific_tag + +let s:c_ns_anchor_name = s:c_ns_anchor_char.'\+' +let s:c_ns_anchor_property = '&'.s:c_ns_anchor_name +let s:c_ns_alias_node = '\*'.s:c_ns_anchor_name + +let s:ns_directive_name = s:ns_char.'\+' + +let s:ns_local_tag_prefix = '!'.s:ns_uri_char.'*' +let s:ns_global_tag_prefix = s:ns_tag_char.s:ns_uri_char.'*' +let s:ns_tag_prefix = s:ns_local_tag_prefix. + \ '\|'.s:ns_global_tag_prefix + +let s:ns_plain_safe_out = s:ns_char +let s:ns_plain_safe_in = '\%('.s:c_flow_indicator.'\@!'.s:ns_char.'\)' + +let s:ns_plain_safe_in = substitute(s:ns_plain_safe_in, '\V\C\\%('.s:_collection.'\\@!'.s:_neg_collection.'\\)', '[^\1\2]', '') +let s:ns_plain_safe_in_without_colhash = substitute(s:ns_plain_safe_in, '\V\C'.s:_neg_collection, '[^\1:#]', '') +let s:ns_plain_safe_out_without_colhash = substitute(s:ns_plain_safe_out, '\V\C'.s:_neg_collection, '[^\1:#]', '') + +let s:ns_plain_first_in = '\%('.s:ns_char_without_c_indicator.'\|[?:\-]\%('.s:ns_plain_safe_in.'\)\@=\)' +let s:ns_plain_first_out = '\%('.s:ns_char_without_c_indicator.'\|[?:\-]\%('.s:ns_plain_safe_out.'\)\@=\)' + +let s:ns_plain_char_in = '\%('.s:ns_char.'#\|:'.s:ns_plain_safe_in.'\|'.s:ns_plain_safe_in_without_colhash.'\)' +let s:ns_plain_char_out = '\%('.s:ns_char.'#\|:'.s:ns_plain_safe_out.'\|'.s:ns_plain_safe_out_without_colhash.'\)' + +let s:ns_plain_out = s:ns_plain_first_out . s:ns_plain_char_out.'*' +let s:ns_plain_in = s:ns_plain_first_in . s:ns_plain_char_in.'*' + + +syn keyword yamlTodo contained TODO FIXME XXX NOTE + +syn region yamlComment display oneline start='\%\(^\|\s\)#' end='$' + \ contains=yamlTodo + +execute 'syn region yamlDirective oneline start='.string('^\ze%'.s:ns_directive_name.'\s\+').' '. + \ 'end="$" '. + \ 'contains=yamlTAGDirective,'. + \ 'yamlYAMLDirective,'. + \ 'yamlReservedDirective '. + \ 'keepend' + +syn match yamlTAGDirective '%TAG\s\+' contained nextgroup=yamlTagHandle +execute 'syn match yamlTagHandle contained nextgroup=yamlTagPrefix '.string(s:c_tag_handle.'\s\+') +execute 'syn match yamlTagPrefix contained nextgroup=yamlComment ' . string(s:ns_tag_prefix) + +syn match yamlYAMLDirective '%YAML\s\+' contained nextgroup=yamlYAMLVersion +syn match yamlYAMLVersion '\d\+\.\d\+' contained nextgroup=yamlComment + +execute 'syn match yamlReservedDirective contained nextgroup=yamlComment '. + \string('%\%(\%(TAG\|YAML\)\s\)\@!'.s:ns_directive_name) + +syn region yamlFlowString matchgroup=yamlFlowStringDelimiter start='"' skip='\\"' end='"' + \ contains=yamlEscape + \ nextgroup=yamlKeyValueDelimiter +syn region yamlFlowString matchgroup=yamlFlowStringDelimiter start="'" skip="''" end="'" + \ contains=yamlSingleEscape + \ nextgroup=yamlKeyValueDelimiter +syn match yamlEscape contained '\\\%([\\"abefnrtv\^0_ NLP\n]\|x\x\x\|u\x\{4}\|U\x\{8}\)' +syn match yamlSingleEscape contained "''" + +syn match yamlBlockScalarHeader contained '\s\+\zs[|>]\%([+-]\=[1-9]\|[1-9]\=[+-]\)\=' + +syn cluster yamlConstant contains=yamlBool,yamlNull + +syn cluster yamlFlow contains=yamlFlowString,yamlFlowMapping,yamlFlowCollection +syn cluster yamlFlow add=yamlFlowMappingKey,yamlFlowMappingMerge +syn cluster yamlFlow add=@yamlConstant,yamlPlainScalar,yamlFloat +syn cluster yamlFlow add=yamlTimestamp,yamlInteger,yamlMappingKeyStart +syn cluster yamlFlow add=yamlComment +syn region yamlFlowMapping matchgroup=yamlFlowIndicator start='{' end='}' contains=@yamlFlow +syn region yamlFlowCollection matchgroup=yamlFlowIndicator start='\[' end='\]' contains=@yamlFlow + +execute 'syn match yamlPlainScalar /'.s:ns_plain_out.'/' +execute 'syn match yamlPlainScalar contained /'.s:ns_plain_in.'/' + +syn match yamlMappingKeyStart '?\ze\s' +syn match yamlMappingKeyStart '?' contained + +execute 'syn match yamlFlowMappingKey /\%#=1'.s:ns_plain_in.'\%(\s\+'.s:ns_plain_in.'\)*\ze\s*:/ contained '. + \'nextgroup=yamlKeyValueDelimiter' +syn match yamlFlowMappingMerge /<<\ze\s*:/ contained nextgroup=yamlKeyValueDelimiter + +syn match yamlBlockCollectionItemStart '^\s*\zs-\%(\s\+-\)*\s' nextgroup=yamlBlockMappingKey,yamlBlockMappingMerge +" Use the old regexp engine, the NFA engine doesn't like all the \@ items. +execute 'syn match yamlBlockMappingKey /\%#=1^\s*\zs'.s:ns_plain_out.'\%(\s\+'.s:ns_plain_out.'\)*\ze\s*:\%(\s\|$\)/ '. + \'nextgroup=yamlKeyValueDelimiter' +execute 'syn match yamlBlockMappingKey /\%#=1\s*\zs'.s:ns_plain_out.'\%(\s\+'.s:ns_plain_out.'\)*\ze\s*:\%(\s\|$\)/ contained '. + \'nextgroup=yamlKeyValueDelimiter' +syn match yamlBlockMappingMerge /^\s*\zs<<\ze:\%(\s\|$\)/ nextgroup=yamlKeyValueDelimiter +syn match yamlBlockMappingMerge /<<\ze\s*:\%(\s\|$\)/ nextgroup=yamlKeyValueDelimiter contained + +syn match yamlKeyValueDelimiter /\s*:/ contained + +syn cluster yamlScalarWithSpecials contains=yamlPlainScalar,yamlBlockMappingKey,yamlFlowMappingKey + +let s:_bounder = s:SimplifyToAssumeAllPrintable('\%([[\]{}, \t]\@!\p\)') +if b:yaml_schema is# 'json' + syn keyword yamlNull null contained containedin=@yamlScalarWithSpecials + syn keyword yamlBool true false + exe 'syn match yamlInteger /'.s:_bounder.'\@1