Loading ...
Sorry, an error occurred while loading the content.

YAML indentation file

Expand Messages
  • ZyX
    # HG changeset patch # User ZyX # Date 1354682690 -14400 # Node ID baf3d5da0a1c9f2334a472f7690abfbc235fbf74 # Parent
    Message 1 of 5 , Dec 4, 2012
    • 0 Attachment
      # HG changeset patch
      # User ZyX <kp-pav@...>
      # Date 1354682690 -14400
      # Node ID baf3d5da0a1c9f2334a472f7690abfbc235fbf74
      # Parent 538ff809ae53b35c18a2a3b3763a397f3f4a785d
      Added YAML indentation file

      diff -r 538ff809ae53 -r baf3d5da0a1c runtime/indent/yaml.vim
      --- /dev/null Thu Jan 01 00:00:00 1970 +0000
      +++ b/runtime/indent/yaml.vim Wed Dec 05 08:44:50 2012 +0400
      @@ -0,0 +1,125 @@
      +" Vim indent file
      +" Language: YAML
      +" Maintainer: Nikolai Pavlov <zyx.vim@...>
      +
      +" Only load this indent file when no other was loaded.
      +if exists("b:did_indent")
      + finish
      +endif
      +
      +let b:did_indent = 1
      +
      +setlocal indentexpr=GetYAMLIndent(v:lnum)
      +setlocal indentkeys=!^F,o,O,0#,0},0],<:>,-
      +setlocal nosmartindent
      +
      +" Only define the function once.
      +if exists("*GetYAMLIndent")
      + finish
      +endif
      +
      +if exists('*shiftwidth')
      + let s:shiftwidth = function('shiftwidth')
      +else
      + function s:shiftwidth()
      + return &shiftwidth
      + endfunction
      +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*%(\''%([^'']|'''')*\'''.
      + \ '|\"%([^"\\]|\\.)*\"'.
      + \ '|%(%(\:\ )@!.)*)\:%(\ |$)'
      +let s:liststartregex='\v^\s*%(\-%(\ |$))'
      +
      +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+s: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
      + return indent(s:FindPrevLEIndentedLineMatchingRegex(a:lnum,
      + \ s:mapkeyregex))
      + elseif prevline =~# '^\s*- '
      + " - List with
      + " multiline scalar
      + return previndent+2
      + elseif prevline =~# s:mapkeyregex
      + " Mapping with: value
      + " that is multiline scalar
      + return previndent+s:shiftwidth()
      + endif
      + return previndent
      +endfunction

      --
      You received this message from the "vim_dev" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php
    • Bram Moolenaar
      ... Thanks. Saving and restoring cpo is missing though. -- hundred-and-one symptoms of being an internet addict: 94. Now admit it... How many of you have
      Message 2 of 5 , Dec 5, 2012
      • 0 Attachment
        ZyX wrote:

        > Added YAML indentation file

        Thanks. Saving and restoring 'cpo' is missing though.


        --
        hundred-and-one symptoms of being an internet addict:
        94. Now admit it... How many of you have made "modem noises" into
        the phone just to see if it was possible? :-)

        /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
        /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
        \\\ an exciting new programming language -- http://www.Zimbu.org ///
        \\\ help me help AIDS victims -- http://ICCF-Holland.org ///

        --
        You received this message from the "vim_dev" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php
      • ZyX
        # HG changeset patch # User ZyX # Date 1354721282 -14400 # Node ID fdde3db8b31e1b37a0e29441471d19be9adf35c2 # Parent
        Message 3 of 5 , Dec 5, 2012
        • 0 Attachment
          # HG changeset patch
          # User ZyX <kp-pav@...>
          # Date 1354721282 -14400
          # Node ID fdde3db8b31e1b37a0e29441471d19be9adf35c2
          # Parent baf3d5da0a1c9f2334a472f7690abfbc235fbf74
          Added undo_indent and cpo saving/setting

          diff -r baf3d5da0a1c -r fdde3db8b31e runtime/indent/yaml.vim
          --- a/runtime/indent/yaml.vim Wed Dec 05 08:44:50 2012 +0400
          +++ b/runtime/indent/yaml.vim Wed Dec 05 19:28:02 2012 +0400
          @@ -3,18 +3,23 @@
          " Maintainer: Nikolai Pavlov <zyx.vim@...>

          " Only load this indent file when no other was loaded.
          -if exists("b:did_indent")
          +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],<:>,-
          setlocal nosmartindent

          +let b:undo_indent = 'setlocal indentexpr< indentkeys< smartindent<'
          +
          " Only define the function once.
          -if exists("*GetYAMLIndent")
          +if exists('*GetYAMLIndent')
          finish
          endif

          @@ -123,3 +128,5 @@
          endif
          return previndent
          endfunction
          +
          +let &cpo = s:save_cpo

          --
          You received this message from the "vim_dev" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php
        • Bram Moolenaar
          ... Thanks. For me it s a bit easier if you send the whole file instead of a patch. -- hundred-and-one symptoms of being an internet addict: 105. When someone
          Message 4 of 5 , Dec 5, 2012
          • 0 Attachment
            ZyX wrote:

            > Added undo_indent and cpo saving/setting

            Thanks. For me it's a bit easier if you send the whole file instead of
            a patch.

            --
            hundred-and-one symptoms of being an internet addict:
            105. When someone asks you for your address, you tell them your URL.

            /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
            /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
            \\\ an exciting new programming language -- http://www.Zimbu.org ///
            \\\ help me help AIDS victims -- http://ICCF-Holland.org ///

            --
            You received this message from the "vim_dev" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php
          • ZyX
            ... a patch. https://bitbucket.org/ZyX_I/vim/raw/262f483812f8f7c5a0e6c9a43cd1967782b7b9ed/runtime/indent/yaml.vim Google groups interface for some reason does
            Message 5 of 5 , Dec 5, 2012
            • 0 Attachment
              > Thanks. For me it's a bit easier if you send the whole file instead of
              a patch.

              https://bitbucket.org/ZyX_I/vim/raw/262f483812f8f7c5a0e6c9a43cd1967782b7b9ed/runtime/indent/yaml.vim

              Google groups interface for some reason does not like “.vim” extension.

              --
              You received this message from the "vim_dev" maillist.
              Do not top-post! Type your reply below the text you are replying to.
              For more information, visit http://www.vim.org/maillist.php
            Your message has been successfully submitted and would be delivered to recipients shortly.