Skip to content
Browse files

New indent plugins for html/js.

  • Loading branch information...
1 parent 8e51b99 commit cdc527fd6ef898781ac94ba907cc1057238780e4 @palbo committed Jan 24, 2012
Showing with 614 additions and 283 deletions.
  1. +254 −0 .vim/indent/html.vim
  2. +360 −283 .vim/indent/javascript.vim
View
254 .vim/indent/html.vim
@@ -0,0 +1,254 @@
+
+" Description: html indenter
+" Author: Johannes Zellner <johannes@zellner.org>
+" Last Change: Mo, 05 Jun 2006 22:32:41 CEST
+" Restoring 'cpo' and 'ic' added by Bram 2006 May 5
+" Globals: g:html_indent_tags -- indenting tags
+" g:html_indent_strict -- inhibit 'O O' elements
+" g:html_indent_strict_table -- inhibit 'O -' elements
+
+" 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:js_indent")
+ so g:js_indent
+else
+ ru! indent/javascript.vim
+endif
+
+echo "Sourcing html indent"
+
+
+" [-- local settings (must come before aborting the script) --]
+setlocal indentexpr=HtmlIndentGetter(v:lnum)
+setlocal indentkeys=o,O,*<Return>,<>>,{,}
+
+
+if exists('g:html_indent_tags')
+ unlet g:html_indent_tags
+endif
+
+" [-- helper function to assemble tag list --]
+fun! <SID>HtmlIndentPush(tag)
+ if exists('g:html_indent_tags')
+ let g:html_indent_tags = g:html_indent_tags.'\|'.a:tag
+ else
+ let g:html_indent_tags = a:tag
+ endif
+endfun
+
+
+" [-- <ELEMENT ? - - ...> --]
+call <SID>HtmlIndentPush('a')
+call <SID>HtmlIndentPush('abbr')
+call <SID>HtmlIndentPush('acronym')
+call <SID>HtmlIndentPush('address')
+call <SID>HtmlIndentPush('b')
+call <SID>HtmlIndentPush('bdo')
+call <SID>HtmlIndentPush('big')
+call <SID>HtmlIndentPush('blockquote')
+call <SID>HtmlIndentPush('button')
+call <SID>HtmlIndentPush('caption')
+call <SID>HtmlIndentPush('center')
+call <SID>HtmlIndentPush('cite')
+call <SID>HtmlIndentPush('code')
+call <SID>HtmlIndentPush('colgroup')
+call <SID>HtmlIndentPush('del')
+call <SID>HtmlIndentPush('dfn')
+call <SID>HtmlIndentPush('dir')
+call <SID>HtmlIndentPush('div')
+call <SID>HtmlIndentPush('dl')
+call <SID>HtmlIndentPush('em')
+call <SID>HtmlIndentPush('fieldset')
+call <SID>HtmlIndentPush('font')
+call <SID>HtmlIndentPush('form')
+call <SID>HtmlIndentPush('frameset')
+call <SID>HtmlIndentPush('h1')
+call <SID>HtmlIndentPush('h2')
+call <SID>HtmlIndentPush('h3')
+call <SID>HtmlIndentPush('h4')
+call <SID>HtmlIndentPush('h5')
+call <SID>HtmlIndentPush('h6')
+call <SID>HtmlIndentPush('i')
+call <SID>HtmlIndentPush('iframe')
+call <SID>HtmlIndentPush('ins')
+call <SID>HtmlIndentPush('kbd')
+call <SID>HtmlIndentPush('label')
+call <SID>HtmlIndentPush('legend')
+call <SID>HtmlIndentPush('map')
+call <SID>HtmlIndentPush('menu')
+call <SID>HtmlIndentPush('noframes')
+call <SID>HtmlIndentPush('noscript')
+call <SID>HtmlIndentPush('object')
+call <SID>HtmlIndentPush('ol')
+call <SID>HtmlIndentPush('optgroup')
+" call <SID>HtmlIndentPush('pre')
+call <SID>HtmlIndentPush('q')
+call <SID>HtmlIndentPush('s')
+call <SID>HtmlIndentPush('samp')
+call <SID>HtmlIndentPush('script')
+call <SID>HtmlIndentPush('select')
+call <SID>HtmlIndentPush('small')
+call <SID>HtmlIndentPush('span')
+call <SID>HtmlIndentPush('strong')
+call <SID>HtmlIndentPush('style')
+call <SID>HtmlIndentPush('sub')
+call <SID>HtmlIndentPush('sup')
+call <SID>HtmlIndentPush('table')
+call <SID>HtmlIndentPush('textarea')
+call <SID>HtmlIndentPush('title')
+call <SID>HtmlIndentPush('tt')
+call <SID>HtmlIndentPush('u')
+call <SID>HtmlIndentPush('ul')
+call <SID>HtmlIndentPush('var')
+
+
+" [-- <ELEMENT ? O O ...> --]
+if !exists('g:html_indent_strict')
+ call <SID>HtmlIndentPush('body')
+ call <SID>HtmlIndentPush('head')
+ call <SID>HtmlIndentPush('html')
+ call <SID>HtmlIndentPush('tbody')
+endif
+
+
+" [-- <ELEMENT ? O - ...> --]
+if !exists('g:html_indent_strict_table')
+ call <SID>HtmlIndentPush('th')
+ call <SID>HtmlIndentPush('td')
+ call <SID>HtmlIndentPush('tr')
+ call <SID>HtmlIndentPush('tfoot')
+ call <SID>HtmlIndentPush('thead')
+endif
+
+delfun <SID>HtmlIndentPush
+
+let s:cpo_save = &cpo
+set cpo-=C
+
+" [-- count indent-increasing tags of line a:lnum --]
+fun! <SID>HtmlIndentOpen(lnum, pattern)
+ let s = substitute('x'.getline(a:lnum),
+ \ '.\{-}\(\(<\)\('.a:pattern.'\)\>\)', "\1", 'g')
+ let s = substitute(s, "[^\1].*$", '', '')
+ return strlen(s)
+endfun
+
+" [-- count indent-decreasing tags of line a:lnum --]
+fun! <SID>HtmlIndentClose(lnum, pattern)
+ let s = substitute('x'.getline(a:lnum),
+ \ '.\{-}\(\(<\)/\('.a:pattern.'\)\>>\)', "\1", 'g')
+ let s = substitute(s, "[^\1].*$", '', '')
+ return strlen(s)
+endfun
+
+" [-- count indent-increasing '{' of (java|css) line a:lnum --]
+fun! <SID>HtmlIndentOpenAlt(lnum)
+ return strlen(substitute(getline(a:lnum), '[^{]\+', '', 'g'))
+endfun
+
+" [-- count indent-decreasing '}' of (java|css) line a:lnum --]
+fun! <SID>HtmlIndentCloseAlt(lnum)
+ return strlen(substitute(getline(a:lnum), '[^}]\+', '', 'g'))
+endfun
+
+" [-- return the sum of indents respecting the syntax of a:lnum --]
+fun! <SID>HtmlIndentSum(lnum, style)
+ if a:style == match(getline(a:lnum), '^\s*</')
+ if a:style == match(getline(a:lnum), '^\s*</\<\('.g:html_indent_tags.'\)\>')
+ let open = <SID>HtmlIndentOpen(a:lnum, g:html_indent_tags)
+ let close = <SID>HtmlIndentClose(a:lnum, g:html_indent_tags)
+ if 0 != open || 0 != close
+ return open - close
+ endif
+ endif
+ endif
+ if '' != &syntax &&
+ \ synIDattr(synID(a:lnum, 1, 1), 'name') =~ '\(css\|java\).*' &&
+ \ synIDattr(synID(a:lnum, strlen(getline(a:lnum)), 1), 'name')
+ \ =~ '\(css\|java\).*'
+ if a:style == match(getline(a:lnum), '^\s*}')
+ return <SID>HtmlIndentOpenAlt(a:lnum) - <SID>HtmlIndentCloseAlt(a:lnum)
+ endif
+ endif
+ return 0
+endfun
+
+fun! HtmlIndentGetter(lnum)
+
+ echo "Grabbing html indent for line: " . a:lnum
+ " Find a non-empty line above the current line.
+ let lnum = prevnonblank(a:lnum - 1)
+
+ " Hit the start of the file, use zero indent.
+ if lnum == 0
+ return 0
+ endif
+
+ let restore_ic = &ic
+ setlocal ic " ignore case
+
+ " [-- special handling for <pre>: no indenting --]
+ if getline(a:lnum) =~ '\c</pre>'
+ \ || 0 < searchpair('\c<pre>', '', '\c</pre>', 'nWb')
+ \ || 0 < searchpair('\c<pre>', '', '\c</pre>', 'nW')
+ " we're in a line with </pre> or inside <pre> ... </pre>
+ if restore_ic == 0
+ setlocal noic
+ endif
+ return -1
+ endif
+
+ " [-- special handling for <javascript>: use cindent --]
+ let js = '<script.*type\s*=.*javascript'
+
+ """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
+ " by Tye Zdrojewski <zdro@yahoo.com>, 05 Jun 2006
+ " ZDR: This needs to be an AND (we are 'after the start of the pair' AND
+ " we are 'before the end of the pair'). Otherwise, indentation
+ " before the start of the script block will be affected; the end of
+ " the pair will still match if we are before the beginning of the
+ " pair.
+ "
+ if 0 < searchpair(js, '', '</script>', 'nWb')
+ \ && 0 < searchpair(js, '', '</script>', 'nW')
+ " we're inside javascript
+
+ if getline(lnum) !~ js && getline(a:lnum) !~ '</script>'
+ if restore_ic == 0
+ setlocal noic
+ endif
+ return GetJsIndent(a:lnum)
+ endif
+ endif
+
+ if getline(lnum) =~ '\c</pre>'
+ " line before the current line a:lnum contains
+ " a closing </pre>. --> search for line before
+ " starting <pre> to restore the indent.
+ let preline = prevnonblank(search('\c<pre>', 'bW') - 1)
+ if preline > 0
+ if restore_ic == 0
+ setlocal noic
+ endif
+ return indent(preline)
+ endif
+ endif
+
+ let ind = <SID>HtmlIndentSum(lnum, -1)
+ let ind = ind + <SID>HtmlIndentSum(a:lnum, 0)
+
+ if restore_ic == 0
+ setlocal noic
+ endif
+
+ return indent(lnum) + (&sw * ind)
+endfun
+
+let &cpo = s:cpo_save
+unlet s:cpo_save
+
+" [-- EOF <runtime>/indent/html.vim --]
View
643 .vim/indent/javascript.vim
@@ -1,330 +1,407 @@
-" Vim indent file
-" Language: Javascript
-" Maintainer: Darrick Wiebe <darrick at innatesoftware.com>
-" URL: http://github.com/pangloss/vim-javascript
-" Version: 1.0.0
-" Last Change: August 31, 2009
-" Acknowledgement: Based off of vim-ruby maintained by Nikolai Weibull http://vim-ruby.rubyforge.org
-
-" 0. Initialization {{{1
+" Vim indent file Language: JavaScript
+" Author: Preston Koprivica (pkopriv2@gmail.com)
+" URL:
+" Last Change: April 30, 2010
+
+" 0. Standard Stuff
" =================
-" Only load this indent file when no other was loaded.
-if exists("b:did_indent")
+" Only load one indent script per buffer
+if exists('b:did_indent')
finish
endif
+
let b:did_indent = 1
-setlocal nosmartindent
+" Set the global log variable 1 = logging enabled, 0 = logging disabled
+if !exists("g:js_indent_log")
+ let g:js_indent_log = 0
+endif
-" Now, set up our indentation expression and keys that trigger it.
-setlocal indentexpr=GetJavascriptIndent()
-setlocal indentkeys=0{,0},0),0],!^F,o,O,e
+setlocal indentexpr=GetJsIndent(v:lnum)
+setlocal indentkeys=
-" Only define the function once.
-if exists("*GetJavascriptIndent")
- finish
-endif
-let s:cpo_save = &cpo
-set cpo&vim
+setlocal cindent
+setlocal autoindent
-" 1. Variables {{{1
+
+" 1. Variables
" ============
-" Regex of syntax group names that are or delimit string or are comments.
-let s:syng_strcom = '\<javaScript\%(RegexpString\|CommentTodo\|LineComment\|Comment\|DocComment\)\>'
+" Inline comments (for anchoring other statements)
+let s:js_mid_line_comment = '\s*\(\/\*.*\*\/\)*\s*'
+let s:js_end_line_comment = s:js_mid_line_comment . '\s*\(//.*\)*'
+let s:js_line_comment = s:js_end_line_comment
+
+" Comment/String Syntax Key
+let s:syn_comment = '\(Comment\|String\|Regexp\)'
+
+
+" 2. Aux. Functions
+" =================
+
+" = Method: IsInComment
+"
+" Determines whether the specified position is contained in a comment. "Note:
+" This depends on a
+function! s:IsInComment(lnum, cnum)
+ return synIDattr(synID(a:lnum, a:cnum, 1), 'name') =~? s:syn_comment
+endfunction
+
+
+" = Method: IsComment
+"
+" Determines whether a line is a comment or not.
+function! s:IsComment(lnum)
+ let line = getline(a:lnum)
+
+ return s:IsInComment(a:lnum, 1) && s:IsInComment(a:lnum, strlen(line)) "Doesn't absolutely work. Only Probably!
+endfunction
+
+
+" = Method: GetNonCommentLine
+"
+" Grabs the nearest non-commented line
+function! s:GetNonCommentLine(lnum)
+ let lnum = prevnonblank(a:lnum)
+
+ while lnum > 0
+ if s:IsComment(lnum)
+ let lnum = prevnonblank(lnum - 1)
+ else
+ return lnum
+ endif
+ endwhile
+
+ return lnum
+endfunction
+
+" = Method: SearchForPair
+"
+" Returns the beginning tag of a given pair starting from the given line.
+function! s:SearchForPair(lnum, beg, end)
+ " Save the cursor position.
+ let curpos = getpos(".")
+
+ " Set the cursor position to the beginning of the line (default
+ " behavior when using ==)
+ call cursor(a:lnum, 0)
+
+ " Search for the opening tag
+ let mnum = searchpair(a:beg, '', a:end, 'bW',
+ \ 'synIDattr(synID(line("."), col("."), 0), "name") =~? s:syn_comment' )
+
+ "Restore the cursor position
+ call cursor(curpos)
+
+ " Finally, return the matched line number
+ return mnum
+endfunction
+
+
+" Object Helpers
+" ==============
+let s:object_beg = '{[^}]*' . s:js_end_line_comment . '$'
+let s:object_end = '^' . s:js_mid_line_comment . '}[;,]\='
-" Regex of syntax group names that are strings.
-let s:syng_string =
- \ '\<javaScript\%(RegexpString\)\>'
-" Regex of syntax group names that are strings or documentation.
-let s:syng_stringdoc =
- \'\<javaScriptDocComment\>'
+function! s:IsObjectBeg(line)
+ return a:line =~ s:object_beg
+endfunction
-" Expression used to check whether we should skip a match with searchpair().
-let s:skip_expr = "synIDattr(synID(line('.'),col('.'),1),'name') =~ '".s:syng_strcom."'"
+function! s:IsObjectEnd(line)
+ return a:line =~ s:object_end
+endfunction
-let s:line_term = '\s*\%(\%(\/\/\).*\)\=$'
+function! s:GetObjectBeg(lnum)
+ return s:SearchForPair(a:lnum, '{', '}')
+endfunction
-" Regex that defines continuation lines, not including (, {, or [.
-let s:continuation_regex = '\%([\\*+/.:]\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)' . s:line_term
-" Regex that defines continuation lines.
-" TODO: this needs to deal with if ...: and so on
-let s:msl_regex = '\%([\\*+/.:([]\|\%(<%\)\@<![=-]\|\W[|&?]\|||\|&&\)' . s:line_term
+" Array Helpers
+" ==============
+let s:array_beg = '\[[^\]]*' . s:js_end_line_comment . '$'
+let s:array_end = '^' . s:js_mid_line_comment . '[^\[]*\][;,]*' . s:js_end_line_comment . '$'
-let s:one_line_scope_regex = '\<\%(if\|else\|for\|while\)\>[^{;]*' . s:line_term
-" Regex that defines blocks.
-let s:block_regex = '\%({\)\s*\%(|\%([*@]\=\h\w*,\=\s*\)\%(,\s*[*@]\=\h\w*\)*|\)\=' . s:line_term
+function! s:IsArrayBeg(line)
+ return a:line =~ s:array_beg
+endfunction
-" 2. Auxiliary Functions {{{1
-" ======================
+function! s:IsArrayEnd(line)
+ return a:line =~ s:array_end
+endfunction
-" Check if the character at lnum:col is inside a string, comment, or is ascii.
-function s:IsInStringOrComment(lnum, col)
- return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_strcom
+function! s:GetArrayBeg(lnum)
+ return s:SearchForPair(a:lnum, '\[', '\]')
endfunction
-" Check if the character at lnum:col is inside a string.
-function s:IsInString(lnum, col)
- return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_string
+
+" MultiLine Declaration/Invocation Helpers
+" ========================================
+let s:paren_beg = '([^)]*' . s:js_end_line_comment . '$'
+let s:paren_end = '^' . s:js_mid_line_comment . '[^(]*)[;,]*'
+
+function! s:IsParenBeg(line)
+ return a:line =~ s:paren_beg
endfunction
-" Check if the character at lnum:col is inside a string or documentation.
-function s:IsInStringOrDocumentation(lnum, col)
- return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_stringdoc
+function! s:IsParenEnd(line)
+ return a:line =~ s:paren_end
+endfunction
+
+function! s:GetParenBeg(lnum)
+ return s:SearchForPair(a:lnum, '(', ')')
endfunction
-" Find line above 'lnum' that isn't empty, in a comment, or in a string.
-function s:PrevNonBlankNonString(lnum)
- 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 =~ '/\*'
- if in_block
- let in_block = 0
- else
- break
- endif
- elseif !in_block && line =~ '\*/'
- 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
+
+
+" Continuation Helpers
+" ====================
+let s:continuation = '\(+\|\\\)\{1}' . s:js_line_comment . '$'
+
+function! s:IsContinuationLine(line)
+ return a:line =~ s:continuation
endfunction
-" Find line above 'lnum' that started the continuation 'lnum' may be part of.
-function s:GetMSL(lnum, in_one_line_scope)
- " 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)
- let col = match(line, s:msl_regex) + 1
- if (col > 0 && !s:IsInStringOrComment(lnum, col)) || s:IsInString(lnum, strlen(line))
- let msl = lnum
- else
- " Don't use lines that are part of a one line scope as msl unless the
- " flag in_one_line_scope is set to 1
- "
- if a:in_one_line_scope
- break
- end
- let msl_one_line = s:Match(lnum, s:one_line_scope_regex)
- if msl_one_line == 0
- break
- endif
- endif
- let lnum = s:PrevNonBlankNonString(lnum - 1)
- endwhile
- return msl
+function! s:GetContinuationBegin(lnum)
+ let cur = a:lnum
+
+ while s:IsContinuationLine(getline(cur))
+ let cur -= 1
+ endwhile
+
+ return cur + 1
+endfunction
+
+
+" Switch Helpers
+" ==============
+let s:switch_beg_next_line = 'switch\s*(.*)\s*' . s:js_mid_line_comment . s:js_end_line_comment . '$'
+let s:switch_beg_same_line = 'switch\s*(.*)\s*' . s:js_mid_line_comment . '{\s*' . s:js_line_comment . '$'
+let s:switch_mid = '^.*\(case.*\|default\)\s*:\s*'
+
+function! s:IsSwitchBeginNextLine(line)
+ return a:line =~ s:switch_beg_next_line
endfunction
-" Check if line 'lnum' has more opening brackets than closing ones.
-function s:LineHasOpeningBrackets(lnum)
- let open_0 = 0
- let open_2 = 0
- let open_4 = 0
- let line = getline(a:lnum)
- let pos = match(line, '[][(){}]', 0)
- while pos != -1
- if !s:IsInStringOrComment(a:lnum, pos + 1)
- let idx = stridx('(){}[]', line[pos])
- if idx % 2 == 0
- let open_{idx} = open_{idx} + 1
- else
- let open_{idx - 1} = open_{idx - 1} - 1
- endif
- endif
- let pos = match(line, '[][(){}]', pos + 1)
- endwhile
- return (open_0 > 0) . (open_2 > 0) . (open_4 > 0)
+function! s:IsSwitchBeginSameLine(line)
+ return a:line =~ s:switch_beg_same_line
endfunction
-function s:Match(lnum, regex)
- let col = match(getline(a:lnum), a:regex) + 1
- return col > 0 && !s:IsInStringOrComment(a:lnum, col) ? col : 0
+function! s:IsSwitchMid(line)
+ return a:line =~ s:switch_mid
+endfunction
+
+
+" Control Helpers
+" ===============
+let s:cntrl_beg_keys = '\(\(\(if\|for\|with\|while\)\s*(.*)\)\|\(try\|do\)\)\s*'
+let s:cntrl_mid_keys = '\(\(\(else\s*if\|catch\)\s*(.*)\)\|\(finally\|else\)\)\s*'
+
+let s:cntrl_beg = s:cntrl_beg_keys . s:js_end_line_comment . '$'
+let s:cntrl_mid = s:cntrl_mid_keys . s:js_end_line_comment . '$'
+
+let s:cntrl_end = '\(while\s*(.*)\)\s*;\=\s*' . s:js_end_line_comment . '$'
+
+function! s:IsControlBeg(line)
+ return a:line =~ s:cntrl_beg
endfunction
-function s:IndentWithContinuation(lnum, ind, width)
- " Set up variables to use and search for MSL to the previous line.
- let p_lnum = a:lnum
- let lnum = s:GetMSL(a:lnum, 1)
- let line = getline(line)
-
- " If the previous line wasn't a MSL and is continuation return its indent.
- " TODO: the || s:IsInString() thing worries me a bit.
- if p_lnum != lnum
- if s:Match(p_lnum,s:continuation_regex)||s:IsInString(p_lnum,strlen(line))
- return a:ind + a:width
- endif
- endif
-
- " Set up more variables now that we know we aren't continuation bound.
- let msl_ind = indent(lnum)
-
- " If the previous line ended with [*+/.-=], start a continuation that
- " indents an extra level.
- if s:Match(lnum, s:continuation_regex)
- if lnum == p_lnum
- return msl_ind + a:width
- else
- return msl_ind
- endif
- endif
-
- return a:ind
+function! s:IsControlMid(line)
+ return a:line =~ s:cntrl_mid
endfunction
-function s:InOneLineScope(lnum)
- let msl = s:GetMSL(a:lnum, 1)
- if msl > 0 && s:Match(msl, s:one_line_scope_regex)
- return msl
- endif
- return 0
+function! s:IsControlMidStrict(line)
+ return a:line =~ s:cntrl_mid
endfunction
-function s:ExitingOneLineScope(lnum)
- let msl = s:GetMSL(a:lnum, 1)
- if msl > 0
- " if the current line is in a one line scope ..
- if s:Match(msl, s:one_line_scope_regex)
- return 0
- else
- let prev_msl = s:GetMSL(msl - 1, 1)
- if s:Match(prev_msl, s:one_line_scope_regex)
- return prev_msl
- endif
- endif
- endif
- return 0
+function! s:IsControlEnd(line)
+ return a:line =~ s:cntrl_end
endfunction
-" 3. GetJavascriptIndent Function {{{1
-" =========================
-
-function GetJavascriptIndent()
- " 3.1. Setup {{{2
- " ----------
-
- " Set up variables for restoring position in file. Could use v:lnum here.
- let vcol = col('.')
-
- " 3.2. Work on the current line {{{2
- " -----------------------------
-
- " Get the current line.
- let line = getline(v:lnum)
- let ind = -1
-
-
- " 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(line, '^\s*[]})]')
- if col > 0 && !s:IsInStringOrComment(v:lnum, col)
- call cursor(v:lnum, col)
- let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
- if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0
- if line[col-1]==')' && col('.') != col('$') - 1
- let ind = virtcol('.')-1
- else
- let ind = indent(s:GetMSL(line('.'), 0))
- endif
- endif
- return ind
- endif
-
- " If we have a /* or */ set indent to first column.
- if match(line, '^\s*\%(/\*\|\*/\)$') != -1
- return 0
- endif
-
- " If we are in a multi-line string or line-comment, don't do anything to it.
- if s:IsInStringOrDocumentation(v:lnum, matchend(line, '^\s*') + 1)
- return indent('.')
- endif
-
- " 3.3. Work on the previous line. {{{2
- " -------------------------------
-
- " Find a non-blank, non-multi-line string line above the current line.
- let lnum = s:PrevNonBlankNonString(v:lnum - 1)
-
- " If the line is empty and inside a string, use the previous line.
- if line =~ '^\s*$' && lnum != prevnonblank(v:lnum - 1)
- return indent(prevnonblank(v:lnum))
- endif
-
- " At the start of the file use zero indent.
- if lnum == 0
- return 0
- endif
-
- " Set up variables for current line.
- let line = getline(lnum)
- let ind = indent(lnum)
-
- " If the previous line ended with a block opening, add a level of indent.
- if s:Match(lnum, s:block_regex)
- return indent(s:GetMSL(lnum, 0)) + &sw
- endif
-
- " If the previous line contained an opening bracket, and we are still in it,
- " add indent depending on the bracket type.
- if line =~ '[[({]'
- let counts = s:LineHasOpeningBrackets(lnum)
- if counts[0] == '1' && searchpair('(', '', ')', 'bW', s:skip_expr) > 0
- if col('.') + 1 == col('$')
- return ind + &sw
- else
- return virtcol('.')
- endif
- elseif counts[1] == '1' || counts[2] == '1'
- return ind + &sw
- else
- call cursor(v:lnum, vcol)
- end
- endif
-
- " 3.4. Work on the MSL line. {{{2
- " --------------------------
-
- let ind_con = ind
- let ind = s:IndentWithContinuation(lnum, ind_con, &sw)
-
- " }}}2
- "
- "
- let ols = s:InOneLineScope(lnum)
- if ols > 0
- let ind = ind + &sw
- else
- let ols = s:ExitingOneLineScope(lnum)
- while ols > 0 && ind > 0
- let ind = ind - &sw
- let ols = s:InOneLineScope(ols - 1)
- endwhile
- endif
-
- return ind
+" = Method: Log
+"
+" Logs a message to the stdout.
+function! s:Log(msg)
+ if g:js_indent_log
+ echo "LOG: " . a:msg
+ endif
endfunction
-" }}}1
-let &cpo = s:cpo_save
-unlet s:cpo_save
+" 3. Indenter
+" ===========
+function! GetJsIndent(lnum)
+ " Grab the first non-comment line prior to this line
+ let pnum = s:GetNonCommentLine(a:lnum-1)
-" vim:set sw=2 sts=2 ts=8 noet:
+ " First line, start at indent = 0
+ if pnum == 0
+ call s:Log("No, noncomment lines prior to the current line.")
+ return 0
+ endif
+ " Grab the second non-comment line prior to this line
+ let ppnum = s:GetNonCommentLine(pnum-1)
+
+ call s:Log("Line: " . a:lnum)
+ call s:Log("PLine: " . pnum)
+ call s:Log("PPLine: " . ppnum)
+
+ " Grab the lines themselves.
+ let line = getline(a:lnum)
+ let pline = getline(pnum)
+ let ppline = getline(ppnum)
+
+ " Determine the current level of indentation
+ let ind = indent(pnum)
+
+
+ " Handle: Object Closers (ie })
+ " =============================
+ if s:IsObjectEnd(line) && !s:IsComment(a:lnum)
+ call s:Log("Line matched object end")
+
+ let obeg = s:GetObjectBeg(a:lnum)
+ let oind = indent(obeg)
+ let oline = getline(obeg)
+
+ call s:Log("The object beg was found at: " . obeg)
+ return oind
+ endif
+
+ if s:IsObjectBeg(pline)
+ call s:Log("Pline matched object beg")
+ return ind + &sw
+ endif
+
+
+ " Handle: Array Closer (ie ])
+ " ============================
+ if s:IsArrayEnd(line) && !s:IsComment(a:lnum)
+ call s:Log("Line matched array end")
+
+ let abeg = s:GetArrayBeg(a:lnum)
+ let aind = indent(abeg)
+
+ call s:Log("The array beg was found at: " . abeg)
+ return aind
+ endif
+
+ if s:IsArrayBeg(pline)
+ call s:Log("Pline matched array beg")
+ return ind + &sw
+ endif
+
+ " Handle: Parens
+ " ==============
+ if s:IsParenEnd(line) && !s:IsComment(a:lnum)
+ call s:Log("Line matched paren end")
+
+ let abeg = s:GetParenBeg(a:lnum)
+ let aind = indent(abeg)
+
+ call s:Log("The paren beg was found at: " . abeg)
+ return aind
+ endif
+
+ if s:IsParenBeg(pline)
+ call s:Log("Pline matched paren beg")
+ return ind + &sw
+ endif
+
+
+ " Handle: Continuation Lines.
+ " ========================================================
+ if s:IsContinuationLine(pline)
+ call s:Log('Pline is a continuation line.')
+
+ let cbeg = s:GetContinuationBegin(pnum)
+ let cind = indent(cbeg)
+
+ call s:Log('The continuation block begin found at: ' . cbeg)
+ return cind + &sw
+ endif
+
+ if s:IsContinuationLine(ppline)
+ call s:Log('PPline was a continuation line but pline wasnt.')
+ return ind - &sw
+ endif
+
+ " Handle: Switch Control Blocks
+ " =============================
+ if s:IsSwitchMid(pline)
+ call s:Log("PLine matched switch cntrl mid")
+ if s:IsSwitchMid(line) || s:IsObjectEnd(line)
+ call s:Log("Line matched a cntrl mid")
+ return ind
+ else
+ call s:Log("Line didnt match a cntrl mid")
+ return ind + &sw
+ endif
+ endif
+
+ if s:IsSwitchMid(line)
+ call s:Log("Line matched switch cntrl mid")
+ return ind - &sw
+ endif
+
+
+ " Handle: Single Line Control Blocks
+ " ==================================
+ if s:IsControlBeg(pline)
+ call s:Log("Pline matched control beginning")
+
+ if s:IsControlMid(line)
+ call s:Log("Line matched a control mid")
+ return ind
+ elseif line =~ '^\s*{\s*$'
+ call s:Log("Line matched an object beg")
+ return ind
+ else
+ return ind + &sw
+ endif
+
+ endif
+
+ if s:IsControlMid(pline)
+ call s:Log("Pline matched a control mid")
+
+ if s:IsControlMid(line)
+ call s:Log("Line matched a control mid")
+ return ind
+ elseif s:IsObjectBeg(line)
+ call s:Log("Line matched an object beg")
+ return ind
+ else
+ call s:Log("Line didn't match a control mid or object beg."
+ return ind + &sw
+ endif
+ endif
+
+ if s:IsControlMid(line)
+ call s:Log("Line matched a control mid.")
+
+ if s:IsControlEnd(pline) || s:IsObjectEnd(pline)
+ call s:Log("PLine matched control end")
+ return ind
+ else
+ call s:Log("Pline didn't match object end")
+ return ind - &sw
+ endif
+ endif
+
+
+ if ( s:IsControlBeg(ppline) || s:IsControlMid(ppline) ) &&
+ \ !s:IsObjectBeg(pline) && !s:IsObjectEnd(pline)
+ call s:Log("PPLine matched single line control beg or mid")
+ return ind - &sw
+ endif
+
+ " Handle: No matches
+ " ==================
+ "call s:Log("Line didn't match anything. Retaining indent")
+ return ind
+endfunction

0 comments on commit cdc527f

Please sign in to comment.
Something went wrong with that request. Please try again.