From 4e95df7c7e12cb76e781f2dacf1c07f8984cce58 Mon Sep 17 00:00:00 2001 From: Adam Stankiewicz Date: Mon, 4 Nov 2019 09:04:28 +0100 Subject: Update --- indent/meson.vim | 1 + indent/octave.vim | 6 +-- indent/rst.vim | 63 +++++++++++++++++++++++++ indent/tsx.vim | 114 --------------------------------------------- indent/typescriptreact.vim | 114 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 181 insertions(+), 117 deletions(-) create mode 100644 indent/rst.vim delete mode 100644 indent/tsx.vim create mode 100644 indent/typescriptreact.vim (limited to 'indent') diff --git a/indent/meson.vim b/indent/meson.vim index b9b72ec6..db5610ac 100644 --- a/indent/meson.vim +++ b/indent/meson.vim @@ -2,6 +2,7 @@ if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'meson') == -1 " Vim indent file " Language: Meson +" License: VIM License " Maintainer: Nirbheek Chauhan " Original Authors: David Bustos " Bram Moolenaar diff --git a/indent/octave.vim b/indent/octave.vim index 3a6a2e78..e7bb4aca 100644 --- a/indent/octave.vim +++ b/indent/octave.vim @@ -1,11 +1,11 @@ if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'octave') == -1 " Vim indent file -" Language: Octave -" Maintainer: Nguyễn Gia Phong +" Language: Octave +" Maintainer: Nguyễn Gia Phong " Original Maintainer: Marcus Aurelius Farias " First Author: Max Ischenko -" Last Change: 2019-10-11 +" Last Change: 2019-10-16 " Only load this indent file when no other was loaded. if exists("b:did_indent") diff --git a/indent/rst.vim b/indent/rst.vim new file mode 100644 index 00000000..df7287ea --- /dev/null +++ b/indent/rst.vim @@ -0,0 +1,63 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'rst') == -1 + +" Vim indent file +" Language: reStructuredText Documentation Format +" Previous Maintainer: Nikolai Weibull +" Latest Revision: 2011-08-03 + +if exists("b:did_indent") + finish +endif +let b:did_indent = 1 + +setlocal indentexpr=GetRSTIndent() +setlocal indentkeys=!^F,o,O +setlocal nosmartindent + +if exists("*GetRSTIndent") + finish +endif + +let s:itemization_pattern = '^\s*[-*+]\s' +let s:enumeration_pattern = '^\s*\%(\d\+\|#\)\.\s\+' + +function GetRSTIndent() + let lnum = prevnonblank(v:lnum - 1) + if lnum == 0 + return 0 + endif + + let ind = indent(lnum) + let line = getline(lnum) + + if line =~ s:itemization_pattern + let ind += 2 + elseif line =~ s:enumeration_pattern + let ind += matchend(line, s:enumeration_pattern) + endif + + let line = getline(v:lnum - 1) + + " Indent :FIELD: lines. Don’t match if there is no text after the field or + " if the text ends with a sent-ender. + if line =~ '^:.\+:\s\{-1,\}\S.\+[^.!?:]$' + return matchend(line, '^:.\{-1,}:\s\+') + endif + + if line =~ '^\s*$' + execute lnum + call search('^\s*\%([-*+]\s\|\%(\d\+\|#\)\.\s\|\.\.\|$\)', 'bW') + let line = getline('.') + if line =~ s:itemization_pattern + let ind -= 2 + elseif line =~ s:enumeration_pattern + let ind -= matchend(line, s:enumeration_pattern) + elseif line =~ '^\s*\.\.' + let ind -= 3 + endif + endif + + return ind +endfunction + +endif diff --git a/indent/tsx.vim b/indent/tsx.vim deleted file mode 100644 index 3c59cc3f..00000000 --- a/indent/tsx.vim +++ /dev/null @@ -1,114 +0,0 @@ -if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'typescript') == -1 - -" Save the current JavaScript indentexpr. -let b:tsx_ts_indentexpr = &indentexpr - -" Prologue; load in XML indentation. -if exists('b:did_indent') - let s:did_indent=b:did_indent - unlet b:did_indent -endif -exe 'runtime! indent/xml.vim' -if exists('s:did_indent') - let b:did_indent=s:did_indent -endif - -setlocal indentexpr=GetTsxIndent() - -" JS indentkeys -setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e -" XML indentkeys -setlocal indentkeys+=*,<>>,<<>,/ - -" Multiline end tag regex (line beginning with '>' or '/>') -let s:endtag = '^\s*\/\?>\s*;\=' -let s:startexp = '[\{\(]\s*$' - -" Get all syntax types at the beginning of a given line. -fu! SynSOL(lnum) - return map(synstack(a:lnum, 1), 'synIDattr(v:val, "name")') -endfu - -" Get all syntax types at the end of a given line. -fu! SynEOL(lnum) - let lnum = prevnonblank(a:lnum) - let col = strlen(getline(lnum)) - return map(synstack(lnum, col), 'synIDattr(v:val, "name")') -endfu - -" Check if a syntax attribute is XMLish. -fu! SynAttrXMLish(synattr) - return a:synattr =~ "^xml" || a:synattr =~ "^tsx" -endfu - -" Check if a synstack is XMLish (i.e., has an XMLish last attribute). -fu! SynXMLish(syns) - return SynAttrXMLish(get(a:syns, -1)) -endfu - -" Check if a synstack denotes the end of a TSX block. -fu! SynTSXBlockEnd(syns) - return get(a:syns, -1) =~ '\%(ts\|typescript\)Braces' && - \ SynAttrXMLish(get(a:syns, -2)) -endfu - -" Determine how many tsxRegions deep a synstack is. -fu! SynTSXDepth(syns) - return len(filter(copy(a:syns), 'v:val ==# "tsxRegion"')) -endfu - -" Check whether `cursyn' continues the same tsxRegion as `prevsyn'. -fu! SynTSXContinues(cursyn, prevsyn) - let curdepth = SynTSXDepth(a:cursyn) - let prevdepth = SynTSXDepth(a:prevsyn) - - " In most places, we expect the nesting depths to be the same between any - " two consecutive positions within a tsxRegion (e.g., between a parent and - " child node, between two TSX attributes, etc.). The exception is between - " sibling nodes, where after a completed element (with depth N), we return - " to the parent's nesting (depth N - 1). This case is easily detected, - " since it is the only time when the top syntax element in the synstack is - " tsxRegion---specifically, the tsxRegion corresponding to the parent. - return prevdepth == curdepth || - \ (prevdepth == curdepth + 1 && get(a:cursyn, -1) ==# 'tsxRegion') -endfu - -" Cleverly mix JS and XML indentation. -fu! GetTsxIndent() - let cursyn = SynSOL(v:lnum) - let prevsyn = SynEOL(v:lnum - 1) - - " Use XML indenting iff: - " - the syntax at the end of the previous line was either TSX or was the - " closing brace of a jsBlock whose parent syntax was TSX; and - " - the current line continues the same tsxRegion as the previous line. - if (SynXMLish(prevsyn) || SynTSXBlockEnd(prevsyn)) && - \ SynTSXContinues(cursyn, prevsyn) - let ind = XmlIndentGet(v:lnum, 0) - let l:line = getline(v:lnum) - let l:pline = getline(v:lnum - 1) - - " Align '/>' and '>' with '<' for multiline tags. - " Align end of expression ')' or '}'. - if l:line =~? s:endtag - let ind = ind - shiftwidth() - endif - - " Then correct the indentation of any TSX following '/>' or '>'. - " Align start of expression '(' or '{' - if l:pline =~? s:endtag || l:pline =~? s:startexp - let ind = ind + shiftwidth() - endif - else - if len(b:tsx_ts_indentexpr) - " Invoke the base TS package's custom indenter - let ind = eval(b:tsx_ts_indentexpr) - else - let ind = cindent(v:lnum) - endif - endif - - return ind -endfu - -endif diff --git a/indent/typescriptreact.vim b/indent/typescriptreact.vim new file mode 100644 index 00000000..65db50dc --- /dev/null +++ b/indent/typescriptreact.vim @@ -0,0 +1,114 @@ +if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'typescript') == -1 + +exe 'runtime! indent/typescript.vim' +" Save the current JavaScript indentexpr. +let b:tsx_ts_indentexpr = &indentexpr + +" Prologue; load in XML indentation. +if exists('b:did_indent') + let s:did_indent=b:did_indent + unlet b:did_indent +endif +exe 'runtime! indent/xml.vim' +if exists('s:did_indent') + let b:did_indent=s:did_indent +endif + +setlocal indentexpr=GetTsxIndent() + +" JS indentkeys +setlocal indentkeys=0{,0},0),0],0\,,!^F,o,O,e +" XML indentkeys +setlocal indentkeys+=*,<>>,<<>,/ + +" Multiline end tag regex (line beginning with '>' or '/>') +let s:endtag = '^\s*\/\?>\s*;\=' +let s:startexp = '[\{\(]\s*$' + +" Get all syntax types at the beginning of a given line. +fu! SynSOL(lnum) + return map(synstack(a:lnum, 1), 'synIDattr(v:val, "name")') +endfu + +" Get all syntax types at the end of a given line. +fu! SynEOL(lnum) + let lnum = prevnonblank(a:lnum) + let col = strlen(getline(lnum)) + return map(synstack(lnum, col), 'synIDattr(v:val, "name")') +endfu + +" Check if a syntax attribute is XMLish. +fu! SynAttrXMLish(synattr) + return a:synattr =~ "^xml" || a:synattr =~ "^tsx" +endfu + +" Check if a synstack is XMLish (i.e., has an XMLish last attribute). +fu! SynXMLish(syns) + return SynAttrXMLish(get(a:syns, -1)) +endfu + +" Check if a synstack denotes the end of a TSX block. +fu! SynTSXBlockEnd(syns) + return get(a:syns, -1) =~ '\%(ts\|typescript\)Braces' && + \ SynAttrXMLish(get(a:syns, -2)) +endfu + +" Determine how many tsxRegions deep a synstack is. +fu! SynTSXDepth(syns) + return len(filter(copy(a:syns), 'v:val ==# "tsxRegion"')) +endfu + +" Check whether `cursyn' continues the same tsxRegion as `prevsyn'. +fu! SynTSXContinues(cursyn, prevsyn) + let curdepth = SynTSXDepth(a:cursyn) + let prevdepth = SynTSXDepth(a:prevsyn) + + " In most places, we expect the nesting depths to be the same between any + " two consecutive positions within a tsxRegion (e.g., between a parent and + " child node, between two TSX attributes, etc.). The exception is between + " sibling nodes, where after a completed element (with depth N), we return + " to the parent's nesting (depth N - 1). This case is easily detected, + " since it is the only time when the top syntax element in the synstack is + " tsxRegion---specifically, the tsxRegion corresponding to the parent. + return prevdepth == curdepth || + \ (prevdepth == curdepth + 1 && get(a:cursyn, -1) ==# 'tsxRegion') +endfu + +" Cleverly mix JS and XML indentation. +fu! GetTsxIndent() + let cursyn = SynSOL(v:lnum) + let prevsyn = SynEOL(v:lnum - 1) + + " Use XML indenting iff: + " - the syntax at the end of the previous line was either TSX or was the + " closing brace of a jsBlock whose parent syntax was TSX; and + " - the current line continues the same tsxRegion as the previous line. + if (SynXMLish(prevsyn) || SynTSXBlockEnd(prevsyn)) && + \ SynTSXContinues(cursyn, prevsyn) + let ind = XmlIndentGet(v:lnum, 0) + let l:line = getline(v:lnum) + let l:pline = getline(v:lnum - 1) + + " Align '/>' and '>' with '<' for multiline tags. + " Align end of expression ')' or '}'. + if l:line =~? s:endtag + let ind = ind - shiftwidth() + endif + + " Then correct the indentation of any TSX following '/>' or '>'. + " Align start of expression '(' or '{' + if l:pline =~? s:endtag || l:pline =~? s:startexp + let ind = ind + shiftwidth() + endif + else + if len(b:tsx_ts_indentexpr) + " Invoke the base TS package's custom indenter + let ind = eval(b:tsx_ts_indentexpr) + else + let ind = cindent(v:lnum) + endif + endif + + return ind +endfu +endif -- cgit v1.2.3