From 51c16139c15183aea30ccf0b7f3482a2b83e895e Mon Sep 17 00:00:00 2001 From: Amadeus Demarzi Date: Tue, 14 Jun 2016 11:13:18 -0700 Subject: [PATCH 1/4] Adds support for string func names in objs classes --- syntax/javascript.vim | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/syntax/javascript.vim b/syntax/javascript.vim index c82f950a..04179088 100644 --- a/syntax/javascript.vim +++ b/syntax/javascript.vim @@ -83,6 +83,8 @@ syntax region jsObjectValue contained start=/:/ end=/\%(,\|}\)\@=/ contai syntax match jsObjectFuncName contained /\<[a-zA-Z_$][0-9a-zA-Z_$]*\>[\r\n\t ]*(\@=/ skipwhite skipempty nextgroup=jsFuncArgs syntax match jsFunctionKey contained /\<[a-zA-Z_$][0-9a-zA-Z_$]*\>\(\s*:\s*function\s*\)\@=/ syntax match jsObjectGetSet contained /\%(get\|set\|static\)\%( \k\+\)\@=/ skipwhite skipempty nextgroup=jsObjectFuncName +syntax region jsFunctionStringKey contained start=+"+ skip=+\\\("\|$\)+ end=+"\|$+ contains=jsSpecial,@Spell extend skipwhite skipempty nextgroup=jsFuncArgs +syntax region jsFunctionStringKey contained start=+'+ skip=+\\\('\|$\)+ end=+'\|$+ contains=jsSpecial,@Spell extend skipwhite skipempty nextgroup=jsFuncArgs exe 'syntax keyword jsNull null '.(exists('g:javascript_conceal_null') ? 'conceal cchar='.g:javascript_conceal_null : '') exe 'syntax keyword jsReturn return contained '.(exists('g:javascript_conceal_return') ? 'conceal cchar='.g:javascript_conceal_return : '') @@ -132,11 +134,11 @@ syntax region jsParenIfElse contained matchgroup=jsParens start=/(/ syntax region jsParenRepeat contained matchgroup=jsParens start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsBlock extend fold syntax region jsParenSwitch contained matchgroup=jsParens start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsSwitchBlock extend fold syntax region jsParenCatch contained matchgroup=jsParens start=/(/ end=/)/ skipwhite skipempty nextgroup=jsBlock extend fold -syntax region jsClassBlock contained matchgroup=jsClassBraces start=/{/ end=/}/ contains=jsClassFuncName,jsClassMethodDefinitions,jsOperator,jsArrowFunction,jsArrowFuncArgs,jsComment,jsGenerator,jsDecorator,jsClassProperty,jsClassPropertyComputed,jsNoise extend fold +syntax region jsClassBlock contained matchgroup=jsClassBraces start=/{/ end=/}/ contains=jsClassFuncName,jsClassMethodDefinitions,jsOperator,jsArrowFunction,jsArrowFuncArgs,jsComment,jsGenerator,jsDecorator,jsClassProperty,jsClassPropertyComputed,jsFunctionStringKey,jsNoise extend fold syntax region jsFuncBlock contained matchgroup=jsFuncBraces start=/{/ end=/}/ contains=@jsAll extend fold syntax region jsBlock contained matchgroup=jsBraces start=/{/ end=/}/ contains=@jsAll extend fold syntax region jsSwitchBlock contained matchgroup=jsBraces start=/{/ end=/}/ contains=@jsAll,jsLabel extend fold -syntax region jsObject matchgroup=jsObjectBraces start=/{/ end=/}/ contains=jsObjectKey,jsObjectKeyString,jsObjectKeyComputed,jsObjectSeparator,jsObjectFuncName,jsObjectGetSet,jsGenerator,jsComment,jsSpreadOperator extend fold +syntax region jsObject matchgroup=jsObjectBraces start=/{/ end=/}/ contains=jsObjectKey,jsObjectKeyString,jsObjectKeyComputed,jsObjectSeparator,jsObjectFuncName,jsObjectGetSet,jsGenerator,jsComment,jsSpreadOperator,jsFunctionStringKey extend fold syntax region jsTernaryIf matchgroup=jsTernaryIfOperator start=/?/ end=/\%(:\|[\}]\@=\)/ contains=@jsExpression syntax match jsGenerator contained /\*/ skipwhite skipempty nextgroup=jsFuncName,jsFuncArgs @@ -229,6 +231,7 @@ if version >= 508 || !exists("did_javascript_syn_inits") HiLink jsString String HiLink jsObjectKeyString String HiLink jsTemplateString String + HiLink jsFunctionStringKey String HiLink jsTaggedTemplate StorageClass HiLink jsTernaryIfOperator Operator HiLink jsRegexpString String From 233b6c32c941559377134a04db9c0a178708bf83 Mon Sep 17 00:00:00 2001 From: bounceme Date: Wed, 15 Jun 2016 09:02:26 -0700 Subject: [PATCH 2/4] paren matching (#474) https://github.com/pangloss/vim-javascript/issues/469#issuecomment-225765624 --- indent/javascript.vim | 56 +++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/indent/javascript.vim b/indent/javascript.vim index 84d69690..806f079b 100644 --- a/indent/javascript.vim +++ b/indent/javascript.vim @@ -45,7 +45,7 @@ let s:line_pre = '^\s*\%(\/\*.*\*\/\s*\)*' let s:js_keywords = s:line_pre . '\%(break\|import\|export\|catch\|const\|continue\|debugger\|delete\|do\|else\|finally\|for\|function\|if\|in\|instanceof\|let\|new\|return\|switch\|this\|throw\|try\|typeof\|var\|void\|while\|with\)\>\C' let s:expr_case = s:line_pre . '\%(case\s\+[^\:]*\|default\)\s*:\s*\C' " Regex of syntax group names that are or delimit string or are comments. -let s:syng_strcom = '\%(string\|regex\|special\|comment\|template\)\c' +let s:syng_strcom = '\%(string\|regex\|special\|doc\|comment\|template\)\c' " Regex of syntax group names that are strings. let s:syng_string = 'regex\c' @@ -347,22 +347,29 @@ function GetJavascriptIndent() endif " 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:line_pre . '[]})]') - if col > 0 && !s:IsInStringOrComment(v:lnum, col) - call cursor(v:lnum, col) - let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) - if parlnum > 0 - let ind = s:InMultiVarStatement(parlnum, 0, 0) ? indent(parlnum) : indent(s:GetMSL(parlnum, 0)) - endif + " according to it. + let col = s:Match(v:lnum, s:line_pre . '[]})]') + if col > 0 + let parlnum = v:lnum + while col + call cursor(parlnum, 1) + let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) + let col = s:Match(parlnum, s:line_pre . '[]})]') + if col + continue + end + if parlnum > 0 + let ind = s:InMultiVarStatement(parlnum, 0, 0)|| s:LineHasOpeningBrackets(parlnum) !~ '2' + \ ? indent(parlnum) : indent(s:GetMSL(parlnum, 0)) + endif + endwhile return ind endif " If line starts with an operator... if (line =~ s:operator_first) - if (s:Match(lnum, s:operator_first)) + if (s:Match(lnum, s:operator_first) || s:Match(lnum, s:line_pre . '[])}]')) " and so does previous line, don't indent return indent(lnum) end @@ -421,10 +428,20 @@ function GetJavascriptIndent() return 0 endif +" foo('foo', +" bar('bar', function() { +" hi(); +" }) +" ); +" function (a, b, c, d, +" e, f, g) { +" console.log('inner'); +" } " If the previous line ended with a block opening, add a level of indent. if s:Match(lnum, s:block_regex) - return s:InMultiVarStatement(lnum, 0, 0) ? indent(lnum) + s:sw() : indent(s:GetMSL(lnum, 0)) + s:sw() + return s:InMultiVarStatement(lnum, 0, 0) || s:LineHasOpeningBrackets(lnum) !~ '2' ? + \ indent(lnum) + s:sw() : indent(s:GetMSL(lnum, 0)) + s:sw() endif " Set up variables for current line. @@ -432,19 +449,18 @@ function GetJavascriptIndent() let ind = indent(lnum) " If the previous line contained an opening bracket, and we are still in it, " add indent depending on the bracket type. - if s:Match(lnum, '[[({})\]]') + if s:Match(lnum, '\%([[({]\)\|\%([^\t \])}][})\]]\)') let counts = s:LineHasOpeningBrackets(lnum) if counts =~ '2' - call cursor(lnum, 1) - " Search for the opening tag - let parlnum = s:lookForParens('(\|{\|\[', ')\|}\|\]', 'nbW', 0) - if parlnum > 0 && !s:InMultiVarStatement(parlnum,0,0) - return indent(s:GetMSL(parlnum, 0)) + call cursor(lnum,matchend(s:RemoveTrailingComments(line), '.\+\zs[])}]')) + while s:lookForParens('(\|{\|\[', ')\|}\|\]', 'bW', 0) == lnum + call cursor(lnum, matchend(s:RemoveTrailingComments(strpart(line,0,col('.'))), '.*\zs[])}]')) + endwhile + if line('.') < lnum && !s:InMultiVarStatement(line('.'),0,0) + return indent(s:GetMSL(line('.'), 0)) end elseif counts =~ '1' || s:Onescope(lnum) return ind + s:sw() - else - call cursor(v:lnum, vcol) end end From ecae87560ccf3001cca2b3f0ec48c4b18abee101 Mon Sep 17 00:00:00 2001 From: Amadeus Demarzi Date: Wed, 15 Jun 2016 19:04:22 -0700 Subject: [PATCH 3/4] Fixing object string keys --- syntax/javascript.vim | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/syntax/javascript.vim b/syntax/javascript.vim index 04179088..3086c39d 100644 --- a/syntax/javascript.vim +++ b/syntax/javascript.vim @@ -83,8 +83,8 @@ syntax region jsObjectValue contained start=/:/ end=/\%(,\|}\)\@=/ contai syntax match jsObjectFuncName contained /\<[a-zA-Z_$][0-9a-zA-Z_$]*\>[\r\n\t ]*(\@=/ skipwhite skipempty nextgroup=jsFuncArgs syntax match jsFunctionKey contained /\<[a-zA-Z_$][0-9a-zA-Z_$]*\>\(\s*:\s*function\s*\)\@=/ syntax match jsObjectGetSet contained /\%(get\|set\|static\)\%( \k\+\)\@=/ skipwhite skipempty nextgroup=jsObjectFuncName -syntax region jsFunctionStringKey contained start=+"+ skip=+\\\("\|$\)+ end=+"\|$+ contains=jsSpecial,@Spell extend skipwhite skipempty nextgroup=jsFuncArgs -syntax region jsFunctionStringKey contained start=+'+ skip=+\\\('\|$\)+ end=+'\|$+ contains=jsSpecial,@Spell extend skipwhite skipempty nextgroup=jsFuncArgs +syntax region jsObjectStringKey contained start=+"+ skip=+\\\("\|$\)+ end=+"\|$+ contains=jsSpecial,@Spell extend skipwhite skipempty nextgroup=jsFuncArgs,jsObjectValue +syntax region jsObjectStringKey contained start=+'+ skip=+\\\('\|$\)+ end=+'\|$+ contains=jsSpecial,@Spell extend skipwhite skipempty nextgroup=jsFuncArgs,jsObjectValue exe 'syntax keyword jsNull null '.(exists('g:javascript_conceal_null') ? 'conceal cchar='.g:javascript_conceal_null : '') exe 'syntax keyword jsReturn return contained '.(exists('g:javascript_conceal_return') ? 'conceal cchar='.g:javascript_conceal_return : '') @@ -134,11 +134,11 @@ syntax region jsParenIfElse contained matchgroup=jsParens start=/(/ syntax region jsParenRepeat contained matchgroup=jsParens start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsBlock extend fold syntax region jsParenSwitch contained matchgroup=jsParens start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsSwitchBlock extend fold syntax region jsParenCatch contained matchgroup=jsParens start=/(/ end=/)/ skipwhite skipempty nextgroup=jsBlock extend fold -syntax region jsClassBlock contained matchgroup=jsClassBraces start=/{/ end=/}/ contains=jsClassFuncName,jsClassMethodDefinitions,jsOperator,jsArrowFunction,jsArrowFuncArgs,jsComment,jsGenerator,jsDecorator,jsClassProperty,jsClassPropertyComputed,jsFunctionStringKey,jsNoise extend fold +syntax region jsClassBlock contained matchgroup=jsClassBraces start=/{/ end=/}/ contains=jsClassFuncName,jsClassMethodDefinitions,jsOperator,jsArrowFunction,jsArrowFuncArgs,jsComment,jsGenerator,jsDecorator,jsClassProperty,jsClassPropertyComputed,jsClassStringKey,jsNoise extend fold syntax region jsFuncBlock contained matchgroup=jsFuncBraces start=/{/ end=/}/ contains=@jsAll extend fold syntax region jsBlock contained matchgroup=jsBraces start=/{/ end=/}/ contains=@jsAll extend fold syntax region jsSwitchBlock contained matchgroup=jsBraces start=/{/ end=/}/ contains=@jsAll,jsLabel extend fold -syntax region jsObject matchgroup=jsObjectBraces start=/{/ end=/}/ contains=jsObjectKey,jsObjectKeyString,jsObjectKeyComputed,jsObjectSeparator,jsObjectFuncName,jsObjectGetSet,jsGenerator,jsComment,jsSpreadOperator,jsFunctionStringKey extend fold +syntax region jsObject matchgroup=jsObjectBraces start=/{/ end=/}/ contains=jsObjectKey,jsObjectKeyString,jsObjectKeyComputed,jsObjectSeparator,jsObjectFuncName,jsObjectGetSet,jsGenerator,jsComment,jsSpreadOperator,jsObjectStringKey extend fold syntax region jsTernaryIf matchgroup=jsTernaryIfOperator start=/?/ end=/\%(:\|[\}]\@=\)/ contains=@jsExpression syntax match jsGenerator contained /\*/ skipwhite skipempty nextgroup=jsFuncName,jsFuncArgs @@ -168,6 +168,8 @@ syntax match jsClassProperty contained /\<[0-9a-zA-Z_$]*\>\(\s*=\)\@= syntax region jsClassValue contained start=/=/ end=/\%(;\|}\|\n\)\@=/ contains=@jsExpression syntax region jsClassPropertyComputed contained matchgroup=jsBrackets start=/\[/ end=/]/ contains=@jsExpression skipwhite skipempty nextgroup=jsFuncArgs,jsClassValue extend syntax match jsClassFuncName contained /\<[a-zA-Z_$][0-9a-zA-Z_$]*\>\%(\s*(\)\@=/ skipwhite skipempty nextgroup=jsFuncArgs +syntax region jsClassStringKey contained start=+"+ skip=+\\\("\|$\)+ end=+"\|$+ contains=jsSpecial,@Spell extend skipwhite skipempty nextgroup=jsFuncArgs +syntax region jsClassStringKey contained start=+'+ skip=+\\\('\|$\)+ end=+'\|$+ contains=jsSpecial,@Spell extend skipwhite skipempty nextgroup=jsFuncArgs " Comments syntax keyword jsCommentTodo contained TODO FIXME XXX TBD @@ -231,7 +233,8 @@ if version >= 508 || !exists("did_javascript_syn_inits") HiLink jsString String HiLink jsObjectKeyString String HiLink jsTemplateString String - HiLink jsFunctionStringKey String + HiLink jsObjectStringKey String + HiLink jsClassStringKey String HiLink jsTaggedTemplate StorageClass HiLink jsTernaryIfOperator Operator HiLink jsRegexpString String From d3d09b9c584f2e59334fe2fd74f00cbfce0ad3c8 Mon Sep 17 00:00:00 2001 From: Amadeus Demarzi Date: Thu, 16 Jun 2016 11:08:42 -0700 Subject: [PATCH 4/4] First pass at fixing Promise catch methods. Made the scoping of catch statements dependent on try. --- syntax/javascript.vim | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/syntax/javascript.vim b/syntax/javascript.vim index 3086c39d..5e17a502 100644 --- a/syntax/javascript.vim +++ b/syntax/javascript.vim @@ -101,8 +101,9 @@ syntax keyword jsConditional switch skipwhite skipempty nextgro syntax keyword jsRepeat while for skipwhite skipempty nextgroup=jsParenRepeat syntax keyword jsRepeat do skipwhite skipempty nextgroup=jsBlock syntax keyword jsLabel contained case default -syntax keyword jsException try finally skipwhite skipempty nextgroup=jsBlock -syntax keyword jsException catch skipwhite skipempty nextgroup=jsParenCatch +syntax keyword jsTry try skipwhite skipempty nextgroup=jsTryCatchBlock +syntax keyword jsFinally contained finally skipwhite skipempty nextgroup=jsBlock +syntax keyword jsCatch contained catch skipwhite skipempty nextgroup=jsParenCatch syntax keyword jsException throw syntax keyword jsAsyncKeyword async await @@ -128,18 +129,19 @@ syntax keyword jsDomNodeConsts ELEMENT_NODE ATTRIBUTE_NODE TEXT_NODE CDATA_SECT syntax keyword jsHtmlEvents onblur onclick oncontextmenu ondblclick onfocus onkeydown onkeypress onkeyup onmousedown onmousemove onmouseout onmouseover onmouseup onresize "" Code blocks -syntax region jsBracket matchgroup=jsBrackets start="\[" end="\]" contains=@jsExpression extend fold -syntax region jsParen matchgroup=jsParens start=/(/ end=/)/ contains=@jsAll extend fold -syntax region jsParenIfElse contained matchgroup=jsParens start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsBlock extend fold -syntax region jsParenRepeat contained matchgroup=jsParens start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsBlock extend fold -syntax region jsParenSwitch contained matchgroup=jsParens start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsSwitchBlock extend fold -syntax region jsParenCatch contained matchgroup=jsParens start=/(/ end=/)/ skipwhite skipempty nextgroup=jsBlock extend fold -syntax region jsClassBlock contained matchgroup=jsClassBraces start=/{/ end=/}/ contains=jsClassFuncName,jsClassMethodDefinitions,jsOperator,jsArrowFunction,jsArrowFuncArgs,jsComment,jsGenerator,jsDecorator,jsClassProperty,jsClassPropertyComputed,jsClassStringKey,jsNoise extend fold -syntax region jsFuncBlock contained matchgroup=jsFuncBraces start=/{/ end=/}/ contains=@jsAll extend fold -syntax region jsBlock contained matchgroup=jsBraces start=/{/ end=/}/ contains=@jsAll extend fold -syntax region jsSwitchBlock contained matchgroup=jsBraces start=/{/ end=/}/ contains=@jsAll,jsLabel extend fold -syntax region jsObject matchgroup=jsObjectBraces start=/{/ end=/}/ contains=jsObjectKey,jsObjectKeyString,jsObjectKeyComputed,jsObjectSeparator,jsObjectFuncName,jsObjectGetSet,jsGenerator,jsComment,jsSpreadOperator,jsObjectStringKey extend fold -syntax region jsTernaryIf matchgroup=jsTernaryIfOperator start=/?/ end=/\%(:\|[\}]\@=\)/ contains=@jsExpression +syntax region jsBracket matchgroup=jsBrackets start="\[" end="\]" contains=@jsExpression extend fold +syntax region jsParen matchgroup=jsParens start=/(/ end=/)/ contains=@jsAll extend fold +syntax region jsParenIfElse contained matchgroup=jsParens start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsBlock extend fold +syntax region jsParenRepeat contained matchgroup=jsParens start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsBlock extend fold +syntax region jsParenSwitch contained matchgroup=jsParens start=/(/ end=/)/ contains=@jsAll skipwhite skipempty nextgroup=jsSwitchBlock extend fold +syntax region jsParenCatch contained matchgroup=jsParens start=/(/ end=/)/ skipwhite skipempty nextgroup=jsTryCatchBlock extend fold +syntax region jsClassBlock contained matchgroup=jsClassBraces start=/{/ end=/}/ contains=jsClassFuncName,jsClassMethodDefinitions,jsOperator,jsArrowFunction,jsArrowFuncArgs,jsComment,jsGenerator,jsDecorator,jsClassProperty,jsClassPropertyComputed,jsClassStringKey,jsNoise extend fold +syntax region jsFuncBlock contained matchgroup=jsFuncBraces start=/{/ end=/}/ contains=@jsAll extend fold +syntax region jsBlock contained matchgroup=jsBraces start=/{/ end=/}/ contains=@jsAll extend fold +syntax region jsTryCatchBlock contained matchgroup=jsBraces start=/{/ end=/}/ contains=@jsAll skipwhite skipempty nextgroup=jsCatch,jsFinally extend fold +syntax region jsSwitchBlock contained matchgroup=jsBraces start=/{/ end=/}/ contains=@jsAll,jsLabel extend fold +syntax region jsObject matchgroup=jsObjectBraces start=/{/ end=/}/ contains=jsObjectKey,jsObjectKeyString,jsObjectKeyComputed,jsObjectSeparator,jsObjectFuncName,jsObjectGetSet,jsGenerator,jsComment,jsSpreadOperator,jsObjectStringKey extend fold +syntax region jsTernaryIf matchgroup=jsTernaryIfOperator start=/?/ end=/\%(:\|[\}]\@=\)/ contains=@jsExpression syntax match jsGenerator contained /\*/ skipwhite skipempty nextgroup=jsFuncName,jsFuncArgs syntax match jsFuncName contained /\<[a-zA-Z_$][0-9a-zA-Z_$]*\>/ skipwhite skipempty nextgroup=jsFuncArgs @@ -206,7 +208,7 @@ if !exists("javascript_ignore_javaScriptdoc") endif "" JSDoc end syntax cluster jsExpression contains=jsBracket,jsParen,jsObject,jsBlock,jsTernaryIf,jsTaggedTemplate,jsTemplateString,jsString,jsRegexpString,jsNumber,jsFloat,jsOperator,jsBooleanTrue,jsBooleanFalse,jsNull,jsFunction,jsArrowFunction,jsGlobalObjects,jsExceptions,jsFutureKeys,jsDomErrNo,jsDomNodeConsts,jsHtmlEvents,jsFuncCall,jsUndefined,jsNan,jsPrototype,jsBuiltins,jsNoise,jsClassDefinition,jsArrowFunction,jsArrowFuncArgs,jsParensError,jsComment,jsArguments,jsThis,jsSuper,jsSpreadOperator -syntax cluster jsAll contains=@jsExpression,jsExportContainer,jsImportContainer,jsStorageClass,jsConditional,jsRepeat,jsReturn,jsStatement,jsException,jsAsyncKeyword +syntax cluster jsAll contains=@jsExpression,jsExportContainer,jsImportContainer,jsStorageClass,jsConditional,jsRepeat,jsReturn,jsStatement,jsException,jsTry,jsAsyncKeyword " Define the default highlighting. " For version 5.7 and earlier: only when not done already @@ -254,6 +256,9 @@ if version >= 508 || !exists("did_javascript_syn_inits") HiLink jsRepeat Repeat HiLink jsStatement Statement HiLink jsException Exception + HiLink jsTry Exception + HiLink jsFinally Exception + HiLink jsCatch Exception HiLink jsAsyncKeyword Keyword HiLink jsArrowFunction Type HiLink jsFunction Type