From 3d43c5460c8e6d9a81f30c298d64c02469c09bbf Mon Sep 17 00:00:00 2001 From: "Kenneth Benzie (Benie)" Date: Fri, 23 Dec 2016 22:39:31 +0000 Subject: [PATCH] Refactor and optimize C/C++ syntax highlights --- after/syntax/c.vim | 117 ++++++++++++++++++++++++++++--------------- after/syntax/cpp.vim | 37 -------------- syntax/cpp.vim | 116 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 192 insertions(+), 78 deletions(-) delete mode 100644 after/syntax/cpp.vim create mode 100644 syntax/cpp.vim diff --git a/after/syntax/c.vim b/after/syntax/c.vim index ad2239a..e755706 100644 --- a/after/syntax/c.vim +++ b/after/syntax/c.vim @@ -3,49 +3,84 @@ " Author: Kenneth Benzie (Benie) " Last Modified: September 04, 2016 -" Match delimiter expressions: (expr) {expr} : ; -" ^ ^ ^ ^ ^ ^ -syn match cDelimiter '[()\[\]:;]' -" Match single character operators, ordering within the [] is important -syn match cOperator '[\*=\.\^\~+\-,&|!%?]' -" Match single character operators if they are surrounded by white space: / < > -" ^ ^ ^ -syn match cOperator '\(\s\zs\/\s\ze\|\s\zs<\s\ze\|\s\zs>\s\ze\)' -" Match multi character operators -syn match cOperator '\(+=\|-=\|\*=\|/=\|%=\|\ˆ=\|&=\||=\|<<\|>>\|>>=\)' -syn match cOperator '\(<<=\|==\|!=\|<=\|>=\|&&\|||\|++\|--\|->\*\|->\)' -" Match : in ternary operator as cOperator -syn region cOperatorTernary matchgroup=cOperator start='?' end=':' transparent -" TODO: Match operator/ operator< operator> -" Match function expressions: expr() -" ^^^^ -syn match cFunction '\w[A-Za-z0-9_]*\ze\s*(' -" Match label expressions: expr: -" ^^^^ -syn match cLabel '^\s*\w[A-Za-z0-9_]\+\ze::\@!' contains=cppAccess +if !exists('c_no_function') + " Match function expressions: expr() + " ^^^^ + syn match cFunction '\a\w*\ze\s*(' display -" Match Doxygen comments -let g:c_doxygen = get(g:, 'c_doxygen', 1) -if exists('g:c_doxygen') && g:c_doxygen - syn region cDoxygenMarkdownMono oneline start='`' end='`' - syn match cDoxygenUrl /https\?:\/\/\(\w\+\(:\w\+\)\?@\)\?\([A-Za-z][-_0-9A-Za-z]*\.\)\{1,}\(\w\{2,}\.\?\)\{1,}\(:[0-9]\{1,5}\)\?\S*/ - syn region cDoxygenComment oneline start='\/\/\/' end='$' contains=cDoxygenMarkdownMono,cDoxygenUrl,@Spell - syn region cDoxygen oneline matchgroup=cComment start='\/\/\/\s*[\\@]brief\s\+' end='$' contains=cDoxygenMarkdownMono,@Spell - syn region cDoxygen oneline matchgroup=cComment start='\/\/\/\s*[\\@]tparam\s\+' end='\(\s.*$\|$\)' - syn region cDoxygen oneline matchgroup=cComment start='\/\/\/\s*[\\@]param\(\[\(\|in\|out\|in,out\)\]\)\=\s\+' end='\(\s.*$\|$\)' - syn match cDoxygenTodo 'todo' contained - syn region cComment oneline start='\/\/\/\s*[\\@]\zetodo\s\+' end='$' contains=cDoxygenTodo,@Spell - hi default link cDoxygenMarkdownMono SpecialComment - hi default link cDoxygenUrl Underlined - hi default link cDoxygenComment cComment - hi default link cDoxygen SpecialComment - hi default link cDoxygenTodo Todo + hi default link cFunction Function endif +if !exists('c_no_delimiters') + " Match delimiter expressions: (expr) {expr} ; + " ^ ^ ^ ^ ^ + syn match cDelimiter '[()\[\];:]' display -" Match curly braces with cDelimiter highlight group -syn region cBlock matchgroup=cDelimiter start="{" end="}" transparent fold + " Match curly braces with cDelimiter highlight group + syn region cBlock matchgroup=cDelimiter start="{" end="}" transparent fold -" Define additional highlight groups -hi default link cDelimiter Delimiter -hi default link cFunction Function + hi link cUserCont Delimiter + hi default link cDelimiter cUserCont +endif + +if !exists('c_no_operators') + " Match: * - . ^ ~ + - , & | ! % ? < > order is important + " ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ + syn match cOperator '[\*=\.\^\~+\-,&|!%?><]' display + + " Match: / don't override // or /* or */ + " ^ + syn match cOperator '[\*\/]\@, ex::pr -" ^ ^ ^^ -syn match cppDelimiter '\(<\|>\|::\)' -" Match keyword logic operators -syn keyword cppOperator and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq -" Match keyword operators -syn keyword cppOperator new delete alignas alignof decltype -" Match single character operators if they are surrounded by white space: / < > -" ^ ^ ^ -syn match cppOperator '\(\s\zs\/\s\ze\|\s\zs<\s\ze\|\s\zs>\s\ze\)' -" Match nested namespace expressions: expr:: -" ^^^^ -syn match cppNestedNameSpecifier '\w[A-Za-z0-9_]*\ze::' -" Match multi character operators, override cppDelimiter matches for < > -" ^ ^ -syn match cppOperator '\(<<\ze\s\|\s\zs>>\|>>=\|<<=\|<=\|>=\|->\*\|->\)' -" Match = delete; as a statement -" ^^^^^^ -" TODO: For some reason \zs stop this from matching, so the cppEquals hack was -" added to work around the failure to highlight in that case. This is terrible -" and a fix needs to be found. -syn match cppEquals '=' -syn match cppStatement '=\s\+delete\ze\s*;' contains=cppEquals -hi default link cppEquals cppOperator - -" Define additional highlight groups -hi default link cppDelimiter Delimiter - -" Override default highlight groups -hi link cppCast Operator -hi link cppModifier Statement -hi link cppNestedNameSpecifier Include diff --git a/syntax/cpp.vim b/syntax/cpp.vim new file mode 100644 index 0000000..7da5fb0 --- /dev/null +++ b/syntax/cpp.vim @@ -0,0 +1,116 @@ +" Language: C++ +" Description: +" Current Maintainer: Kenneth Benzie (Benie) +" Created: December 23, 2016 + +" Quit when a syntax file was already loaded +if exists('b:current_syntax') + finish +endif + +" Disable C syntax options to improve performance +let b:c_no_curly_error = 1 +let b:c_no_bracket_error = 1 +let b:c_no_cformat = 1 +let b:c_no_c11 = 1 +let b:c_no_ansi = 1 +let b:c_no_bsd = 1 +let b:c_gnu = 0 +let b:c_no_operators = 1 + +" Read the C syntax to start with +runtime! syntax/c.vim +unlet b:current_syntax + +" C++98 +syn keyword cppStatement this friend using +syn keyword cppAccess public protected private +syn keyword cppModifier inline virtual explicit export +syn keyword cppType bool wchar_t +syn keyword cppExceptions throw try catch +syn keyword cppOperator new delete typeid and bitor or xor compl bitand and_eq + \ or_eq xor_eq not not_eq +if exists('c_no_operators') + syn keyword cppOperator operator +endif +syn keyword cppCast const_cast static_cast dynamic_cast reinterpret_cast +syn keyword cppStorageClass mutable +syn keyword cppStructure class typename template namespace +syn keyword cppBoolean true false +syn keyword cppConstant __cplusplus + +" C++11 +if !exists('cpp_no_cpp11') + syn keyword cppModifier override final + syn keyword cppType char16_t char32_t nullptr_t + syn keyword cppExceptions noexcept + syn keyword cppStorageClass constexpr decltype thread_local + syn keyword cppConstant nullptr + syn keyword cppOperator alignas alignof decltype static_assert + syn region cppRawString matchgroup=cppRawStringDelimiter start=+\%(u8\|[uLU]\)\=R"\z([[:alnum:]_{}[\]#<>%:;.?*\+\-/\^&|~!=,"']\{,16}\)(+ end=+)\z1"+ contains=@Spell display + + " Match: = delete; and = default; + " ^^^^^^ ^^^^^^^ + " Both `delete` and `default` are keywords which normally take precidence, + " however if the match starts before the beginning of the keyword it will be + " chosen instead so we can specialze for this case. + syn match cppStatement '=\s\+\(delete\|default\)\ze\s*;\?' contains=cOperator display +endif + +" C++14 +if !exists('cpp_no_cpp14') + syn match cppNumber display '\<0b[01]\+\(u\=l\{0,2}\|ll\=u\)\>' +endif + +if !exists('cpp_no_delimiters') + " Match: delimiter expressions: , ex::pr + " ^ ^ ^^ + syn match cppDelimiter '\(<\|>\|::\)' display + + " Match: nested namespace expressions: expr:: + " ^^^^ + syn match cppNestedName '\a\w*::' display contains=cppDelimiter + + " Match: label: as a Label and public: protected: private: as a Statement + " ^^^^^ ^^^^^^ ^^^^^^^^^ ^^^^^^^ + syn match cUserCont "^\s*\I\i*\s*:$" contains=cppAccess display + + hi default link cppDelimiter cDelimiter + hi default link cppNestedName cInclude +endif + +if !exists('cpp_no_operators') && !exists('cpp_no_delimiters') + " Match: single character operators if they are surrounded by white space: / < > + " ^ ^ ^ + syn match cppOperator '\(\s\zs\/\s\ze\|\s\zs<\s\ze\|\s\zs>\s\ze\)' display + + " Match: <= >= -> << >> <<= >>= ->* + " ^^ ^^ ^^ ^^ ^^ ^^^ ^^^ ^^^ + syn match cppOperator '\(<=\|>=\|->\|<<\ze\s\|\s\zs>>\|>>=\|<<=\|->\*\)' display + + " Match: operator<< operator>> operator< operator> operator[] + " ^^^^^^^^^^ ^^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^ ^^^^^^^^^^ + " operator new operator delete operator new[] operator delete[] + " ^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^ + syn match cppOperator 'operator\s*\(\/\|<<\|>>\|<\|>\|\(new\|delete\)\?\s*\[\s*\]\)\?' display +endif + +" Default highlighting +hi def link cppAccess cppStatement +hi def link cppCast cppOperator +hi def link cppExceptions Exception +hi def link cppOperator Operator +hi def link cppStatement Statement +hi def link cppModifier cppStatement +hi def link cppType Type +hi def link cppStorageClass StorageClass +hi def link cppStructure Structure +hi def link cppBoolean Boolean +hi def link cppConstant Constant +hi def link cppRawStringDelimiter Delimiter +hi def link cppRawString String +hi def link cppNumber Number + +let b:current_syntax = 'cpp' + +" vim: ts=8