Permalink
Browse files

vim-javascript up

  • Loading branch information...
1 parent caa6235 commit 96d9faa10e75a82d2275f3162852c9c26947e4a0 @narqo committed Mar 17, 2012
Showing with 33 additions and 34 deletions.
  1. +16 −14 vim/indent/javascript.vim
  2. +17 −20 vim/syntax/javascript.vim
View
30 vim/indent/javascript.vim
@@ -39,7 +39,7 @@ let s:syng_strcom = 'javaScript\%(String\|RegexpString\|CommentTodo\|LineComment
let s:syng_string = 'javaScript\%(RegexpString\)'
" Regex of syntax group names that are strings or documentation.
-let s:syng_stringdoc = 'javaScriptDocComment'
+let s:syng_multiline = 'javaScriptDocComment\|javaScriptComment'
" 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."'"
@@ -71,9 +71,9 @@ function s:IsInString(lnum, col)
return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_string
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
+" Check if the character at lnum:col is inside a multi-line comment.
+function s:IsInMultilineComment(lnum, col)
+ return synIDattr(synID(a:lnum, a:col, 1), 'name') =~ s:syng_multiline
endfunction
" Find line above 'lnum' that isn't empty, in a comment, or in a string.
@@ -226,7 +226,6 @@ function GetJavascriptIndent()
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.
@@ -244,24 +243,27 @@ function GetJavascriptIndent()
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('.')
+ " If we are in a multi-line comment, cindent does the right thing.
+ if s:IsInMultilineComment(v:lnum, 1)
+ return cindent(v:lnum)
endif
" 3.3. Work on the previous line. {{{2
" -------------------------------
+ " If the line is empty and the previous nonblank line was a multi-line
+ " comment, use that comment's indent. Deduct one char to account for the
+ " space in ' */'.
+ let nonblank_lnum = prevnonblank(v:lnum - 1)
+ if line =~ '^\s*$' && s:IsInMultilineComment(nonblank_lnum, 1)
+ return indent(nonblank_lnum) - 1
+ endif
+
" 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)
+ if line =~ '^\s*$' && lnum != nonblank_lnum
return indent(prevnonblank(v:lnum))
endif
View
37 vim/syntax/javascript.vim
@@ -18,13 +18,13 @@ if !exists("main_syntax")
let main_syntax = 'javascript'
endif
-"" Drop fold if it set but VIM doesn't support it.
+"" Drop fold if it is set but VIM doesn't support it.
let b:javascript_fold='true'
if version < 600 " Don't support the old version
unlet! b:javascript_fold
endif
-"" dollar sigh is permittd anywhere in an identifier
+"" dollar sign is permittd anywhere in an identifier
setlocal iskeyword+=$
syntax sync fromstart
@@ -58,8 +58,10 @@ if !exists("javascript_ignore_javaScriptdoc")
" other tags (no extra syntax)
syntax match javaScriptDocTags contained "@\(access\|addon\|alias\|author\|beta\|constant\|constructor\|copyright\|deprecated\|description\|event\|example\|exec\|field\|fileOverview\|fileoverview\|function\|global\|ignore\|inner\|license\|overview\|private\|protected\|project\|public\|readonly\|since\|static\)\>"
- syntax match javaScriptDocType contained "\%(#\|\"\|{\|}\|\w\|\.\|:\|\/\)\+" nextgroup=javaScriptDocParam skipwhite
- syntax match javaScriptDocTypeNoParam contained "\%(#\|\"\|{\|}\|\w\|\.\|:\|\/\)\+"
+ syntax region javaScriptDocType start="{" end="}" oneline contained nextgroup=javaScriptDocParam skipwhite
+ syntax match javaScriptDocType contained "\%(#\|\"\|\w\|\.\|:\|\/\)\+" nextgroup=javaScriptDocParam skipwhite
+ syntax region javaScriptDocTypeNoParam start="{" end="}" oneline contained
+ syntax match javaScriptDocTypeNoParam contained "\%(#\|\"\|\w\|\.\|:\|\/\)\+"
syntax match javaScriptDocParam contained "\%(#\|\"\|{\|}\|\w\|\.\|:\|\/\)\+"
syntax region javaScriptDocSeeTag contained matchgroup=javaScriptDocSeeTag start="{" end="}" contains=javaScriptDocTags
@@ -72,16 +74,16 @@ syntax case match
syntax match javaScriptSpecial "\\\d\d\d\|\\x\x\{2\}\|\\u\x\{4\}\|\\."
syntax region javaScriptStringD start=+"+ skip=+\\\\\|\\$"+ end=+"+ contains=javaScriptSpecial,@htmlPreproc
syntax region javaScriptStringS start=+'+ skip=+\\\\\|\\$'+ end=+'+ contains=javaScriptSpecial,@htmlPreproc
-syntax region javaScriptRegexpCharClass start=+\[\]\|\[^\]\|\[+ end=+\]+ contained
-syntax region javaScriptRegexpString start=+\(\(\(return\|case\)\s\+\)\@<=\|\(\([)\]"']\|\d\|\w\)\s*\)\@<!\)/\(\*\|/\)\@!+ skip=+\\\\\|\\/+ end=+/[gim]\{,3}+ contains=javaScriptSpecial,javaScriptRegexpCharClass,@htmlPreproc oneline
+syntax region javaScriptRegexpCharClass start=+\[+ end=+\]+ contained
+syntax region javaScriptRegexpString start=+\(\(\(return\|case\)\s\+\)\@<=\|\(\([)\]"']\|\d\|\w\)\s*\)\@<!\)/\(\*\|/\)\@!+ skip=+\\\\\|\\/+ end=+/[gimy]\{,4}+ contains=javaScriptSpecial,javaScriptRegexpCharClass,@htmlPreproc oneline
syntax match javaScriptNumber /\<-\=\d\+L\=\>\|\<0[xX]\x\+\>/
syntax match javaScriptFloat /\<-\=\%(\d\+\.\d\+\|\d\+\.\|\.\d\+\)\%([eE][+-]\=\d\+\)\=\>/
-syntax match javaScriptLabel /\(?\s*\)\@<!\<\w\+\(\s*:\)\@=/
+syntax match javaScriptLabel /\<\w\+\(\s*:\)\@=/
"" JavaScript Prototype
syntax keyword javaScriptPrototype prototype
-"" Programm Keywords
+"" Program Keywords
syntax keyword javaScriptSource import export
syntax keyword javaScriptType const undefined var void yield
syntax keyword javaScriptOperator delete new in instanceof let typeof
@@ -151,10 +153,13 @@ endif "DOM/HTML/CSS
"" Code blocks
-syntax cluster javaScriptAll contains=javaScriptComment,javaScriptLineComment,javaScriptDocComment,javaScriptStringD,javaScriptStringS,javaScriptRegexpString,javaScriptNumber,javaScriptFloat,javaScriptLabel,javaScriptSource,javaScriptThis,javaScriptType,javaScriptOperator,javaScriptBoolean,javaScriptNull,javaScriptFunction,javaScriptConditional,javaScriptRepeat,javaScriptBranch,javaScriptStatement,javaScriptGlobalObjects,javaScriptExceptions,javaScriptFutureKeys,javaScriptDomErrNo,javaScriptDomNodeConsts,javaScriptHtmlEvents,javaScriptDotNotation
+" there is a name collision with javaScriptExpression in html.vim, hence the use of the '2' here
+syntax cluster javaScriptExpression2 contains=javaScriptComment,javaScriptLineComment,javaScriptDocComment,javaScriptStringD,javaScriptStringS,javaScriptRegexpString,javaScriptNumber,javaScriptFloat,javaScriptSource,javaScriptThis,javaScriptType,javaScriptOperator,javaScriptBoolean,javaScriptNull,javaScriptFunction,javaScriptGlobalObjects,javaScriptExceptions,javaScriptFutureKeys,javaScriptDomErrNo,javaScriptDomNodeConsts,javaScriptHtmlEvents,javaScriptDotNotation,javaScriptBracket,javaScriptParen,javaScriptBlock,javaScriptParenError
+syntax cluster javaScriptAll contains=@javaScriptExpression2,javaScriptLabel,javaScriptConditional,javaScriptRepeat,javaScriptBranch,javaScriptStatement,javaScriptTernaryIf
syntax region javaScriptBracket matchgroup=javaScriptBracket transparent start="\[" end="\]" contains=@javaScriptAll,javaScriptParensErrB,javaScriptParensErrC,javaScriptBracket,javaScriptParen,javaScriptBlock,@htmlPreproc
syntax region javaScriptParen matchgroup=javaScriptParen transparent start="(" end=")" contains=@javaScriptAll,javaScriptParensErrA,javaScriptParensErrC,javaScriptParen,javaScriptBracket,javaScriptBlock,@htmlPreproc
syntax region javaScriptBlock matchgroup=javaScriptBlock transparent start="{" end="}" contains=@javaScriptAll,javaScriptParensErrA,javaScriptParensErrB,javaScriptParen,javaScriptBracket,javaScriptBlock,@htmlPreproc
+syntax region javaScriptTernaryIf matchgroup=javaScriptTernaryIfOperator start=+?+ end=+:+ contains=@javaScriptExpression2
"" catch errors caused by wrong parenthesis
syntax match javaScriptParensError ")\|}\|\]"
@@ -174,19 +179,8 @@ if exists("b:javascript_fold")
syntax match javaScriptOpAssign /=\@<!=/ nextgroup=javaScriptFuncBlock skipwhite skipempty
syntax region javaScriptFuncName contained matchgroup=javaScriptFuncName start=/\%(\$\|\w\)*\s*(/ end=/)/ contains=javaScriptLineComment,javaScriptComment nextgroup=javaScriptFuncBlock skipwhite skipempty
syntax region javaScriptFuncBlock contained matchgroup=javaScriptFuncBlock start="{" end="}" contains=@javaScriptAll,javaScriptParensErrA,javaScriptParensErrB,javaScriptParen,javaScriptBracket,javaScriptBlock fold
-
- if &l:filetype=='javascript' && !&diff
- " Fold setting
- " Redefine the foldtext (to show a JS function outline) and foldlevel
- " only if the entire buffer is JavaScript, but not if JavaScript syntax
- " is embedded in another syntax (e.g. HTML).
- setlocal foldmethod=syntax
- setlocal foldlevel=4
- endif
else
syntax keyword javaScriptFunction function
- setlocal foldmethod<
- setlocal foldlevel<
endif
" Define the default highlighting.
@@ -212,6 +206,7 @@ if version >= 508 || !exists("did_javascript_syn_inits")
HiLink javaScriptDocParam Label
HiLink javaScriptStringS String
HiLink javaScriptStringD String
+ HiLink javaScriptTernaryIfOperator Conditional
HiLink javaScriptRegexpString String
HiLink javaScriptRegexpCharClass Character
HiLink javaScriptCharacter Character
@@ -258,6 +253,8 @@ endif
"syntax clear javaScriptExpression
syntax cluster htmlJavaScript contains=@javaScriptAll,javaScriptBracket,javaScriptParen,javaScriptBlock,javaScriptParenError
syntax cluster javaScriptExpression contains=@javaScriptAll,javaScriptBracket,javaScriptParen,javaScriptBlock,javaScriptParenError,@htmlPreproc
+" Vim's default html.vim highlights all javascript as 'Special'
+hi! def link javaScript NONE
let b:current_syntax = "javascript"
if main_syntax == 'javascript'

0 comments on commit 96d9faa

Please sign in to comment.