├── LICENSE ├── README.md ├── ftdetect └── mojo.vim ├── syntax-example.png └── syntax └── mojo.vim /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Carlos Oviedo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Mojo :fire: Language Syntax Highlighting for Vim 2 | 3 | ![alt text](syntax-example.png "Highlight Example") 4 | 5 | How to install 6 | -------------- 7 | Write this line in your vimrc: 8 | ``` 9 | Plug 'ovikrai/mojo-syntax' 10 | ``` 11 | -------------------------------------------------------------------------------- /ftdetect/mojo.vim: -------------------------------------------------------------------------------- 1 | autocmd BufRead,BufNewFile *.mojo,*.🔥 call s:set_mojo_filetype() 2 | 3 | function! s:set_mojo_filetype() abort 4 | if &filetype !=# 'mojo' 5 | set filetype=mojo 6 | endif 7 | endfunction 8 | -------------------------------------------------------------------------------- /syntax-example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ovikrai/mojo-syntax/a4245fb73e95dc3b7c2ad00893a5c199e4c4c25c/syntax-example.png -------------------------------------------------------------------------------- /syntax/mojo.vim: -------------------------------------------------------------------------------- 1 | " Mojo syntax file for Vim 2 | " Based on: https://raw.githubusercontent.com/vim/vim/master/runtime/syntax/mojo.vim 3 | 4 | " Quit when a syntax file was already loaded. 5 | if exists("b:current_syntax") 6 | finish 7 | endif 8 | 9 | " We need nocompatible mode in order to continue lines with backslashes. 10 | " Original setting will be restored. 11 | let s:cpo_save = &cpo 12 | set cpo&vim 13 | 14 | " These keywords are based on Python syntax highlight, and adds to it struct, 15 | " fn, alias, var, let 16 | " 17 | syn keyword mojoStatement False None True 18 | syn keyword mojoStatement as assert break continue del global 19 | syn keyword mojoStatement lambda nonlocal pass return with yield 20 | syn keyword mojoStatement class def nextgroup=mojoFunction skipwhite 21 | syn keyword mojoStatement struct fn trait nextgroup=mojoFunction skipwhite 22 | syn keyword mojoStatement alias var let 23 | syn keyword mojoStatement inout owned borrowed 24 | syn keyword mojoConditional elif else if 25 | syn keyword mojoRepeat for while 26 | syn keyword mojoOperator and in is not or 27 | syn keyword mojoException except finally raise try raises 28 | syn keyword mojoInclude from import self 29 | syn keyword mojoAsync async await 30 | 31 | " Soft keywords 32 | " These keywords do not mean anything unless used in the right context. 33 | " See https://docs.python.org/3/reference/lexical_analysis.html#soft-keywords 34 | " for more on this. 35 | syn match mojoConditional "^\s*\zscase\%(\s\+.*:.*$\)\@=" 36 | syn match mojoConditional "^\s*\zsmatch\%(\s\+.*:\s*\%(#.*\)\=$\)\@=" 37 | 38 | " Decorators 39 | " A dot must be allowed because of @MyClass.myfunc decorators. 40 | syn match mojoDecorator "@" display contained 41 | syn match mojoDecoratorName "@\s*\h\%(\w\|\.\)*" display contains=pythonDecorator 42 | 43 | " Python 3.5 introduced the use of the same symbol for matrix multiplication: 44 | " https://www.python.org/dev/peps/pep-0465/. We now have to exclude the 45 | " symbol from highlighting when used in that context. 46 | " Single line multiplication. 47 | syn match mojoMatrixMultiply 48 | \ "\%(\w\|[])]\)\s*@" 49 | \ contains=ALLBUT,mojoDecoratorName,mojoDecorator,mojoFunction,mojoDoctestValue 50 | \ transparent 51 | " Multiplication continued on the next line after backslash. 52 | syn match mojoMatrixMultiply 53 | \ "[^\\]\\\s*\n\%(\s*\.\.\.\s\)\=\s\+@" 54 | \ contains=ALLBUT,mojoDecoratorName,mojoDecorator,mojoFunction,mojoDoctestValue 55 | \ transparent 56 | " Multiplication in a parenthesized expression over multiple lines with @ at 57 | " the start of each continued line; very similar to decorators and complex. 58 | syn match mojoMatrixMultiply 59 | \ "^\s*\%(\%(>>>\|\.\.\.\)\s\+\)\=\zs\%(\h\|\%(\h\|[[(]\).\{-}\%(\w\|[])]\)\)\s*\n\%(\s*\.\.\.\s\)\=\s\+@\%(.\{-}\n\%(\s*\.\.\.\s\)\=\s\+@\)*" 60 | \ contains=ALLBUT,mojoDecoratorName,mojoDecorator,mojoFunction,mojoDoctestValue 61 | \ transparent 62 | 63 | syn match mojoFunction "\h\w*" display contained 64 | 65 | syn match mojoComment "#.*$" contains=mojoTodo,@Spell 66 | syn keyword mojoTodo FIXME NOTE NOTES TODO contained 67 | 68 | " Triple-quoted strings can contain doctests. 69 | syn region mojoString matchgroup=mojoQuotes 70 | \ start=+[uU]\=\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1" 71 | \ contains=mojoEscape,@Spell 72 | syn region mojoString matchgroup=mojoTripleQuotes 73 | \ start=+[uU]\=\z('''\|"""\)+ end="\z1" keepend 74 | \ contains=mojoEscape,mojoSpaceError,mojoDoctest,@Spell 75 | syn region mojoRawString matchgroup=mojoQuotes 76 | \ start=+[uU]\=[rR]\z(['"]\)+ end="\z1" skip="\\\\\|\\\z1" 77 | \ contains=@Spell 78 | syn region mojoRawString matchgroup=pythonTripleQuotes 79 | \ start=+[uU]\=[rR]\z('''\|"""\)+ end="\z1" keepend 80 | \ contains=pythonSpaceError,mojoDoctest,@Spell 81 | 82 | syn match mojoEscape +\\[abfnrtv'"\\]+ contained 83 | syn match mojoEscape "\\\o\{1,3}" contained 84 | syn match mojoEscape "\\x\x\{2}" contained 85 | syn match mojoEscape "\%(\\u\x\{4}\|\\U\x\{8}\)" contained 86 | 87 | " Python allows case-insensitive Unicode IDs: http://www.unicode.org/charts/ 88 | syn match mojoEscape "\\N{\a\+\%(\s\a\+\)*}" contained 89 | syn match mojoEscape "\\$" 90 | 91 | " It is very important to understand all details before changing the 92 | " regular expressions below or their order. 93 | " The word boundaries are *not* the floating-point number boundaries 94 | " because of a possible leading or trailing decimal point. 95 | " The expressions below ensure that all valid number literals are 96 | " highlighted, and invalid number literals are not. For example, 97 | " 98 | " - a decimal point in '4.' at the end of a line is highlighted, 99 | " - a second dot in 1.0.0 is not highlighted, 100 | " - 08 is not highlighted, 101 | " - 08e0 or 08j are highlighted, 102 | " 103 | " and so on, as specified in the 'Python Language Reference'. 104 | " https://docs.python.org/reference/lexical_analysis.html#numeric-literals 105 | " numbers (including complex) 106 | syn match mojoNumber "\<0[oO]\%(_\=\o\)\+\>" 107 | syn match mojoNumber "\<0[xX]\%(_\=\x\)\+\>" 108 | syn match mojoNumber "\<0[bB]\%(_\=[01]\)\+\>" 109 | syn match mojoNumber "\<\%([1-9]\%(_\=\d\)*\|0\+\%(_\=0\)*\)\>" 110 | syn match mojoNumber "\<\d\%(_\=\d\)*[jJ]\>" 111 | syn match mojoNumber "\<\d\%(_\=\d\)*[eE][+-]\=\d\%(_\=\d\)*[jJ]\=\>" 112 | syn match mojoNumber 113 | \ "\<\d\%(_\=\d\)*\.\%([eE][+-]\=\d\%(_\=\d\)*\)\=[jJ]\=\%(\W\|$\)\@=" 114 | syn match mojoNumber 115 | \ "\%(^\|\W\)\zs\%(\d\%(_\=\d\)*\)\=\.\d\%(_\=\d\)*\%([eE][+-]\=\d\%(_\=\d\)*\)\=[jJ]\=\>" 116 | 117 | " The built-ins are added in the same order of appearance in Mojo stdlib docs 118 | " https://docs.modular.com/mojo/lib.html 119 | " 120 | " Built-in functions 121 | syn keyword mojoBuiltin slice constrained debug_assert put_new_line print 122 | syn keyword mojoBuiltin print_no_newline len range rebind element_type 123 | syn keyword mojoBuiltin ord chr atol isdigit index address string 124 | 125 | " Built-in types 126 | syn keyword mojoType Byte ListLiteral CoroutineContext Coroutine DType 127 | syn keyword mojoType dtype type invalid bool int8 si8 unit8 ui8 int16 128 | syn keyword mojoType si16 unit16 ui16 int32 si32 uint32 ui32 int64 129 | syn keyword mojoType si64 uint64 ui64 bfloat16 bf16 float16 f16 float32 130 | syn keyword mojoType f32 float64 f64 Error FloatLiteral Int Attr SIMD 131 | syn keyword mojoType Int8 UInt8 Int16 UInt16 Int32 UInt32 Int64 UInt64 132 | syn keyword mojoType Float16 Float32 Float64 element_type _65x13_type 133 | syn keyword mojoType String StringLiteral StringRef Tuple AnyType 134 | syn keyword mojoType NoneType None Lifetime 135 | 136 | " avoid highlighting attributes as builtins 137 | syn match mojoAttribute /\.\h\w*/hs=s+1 138 | \ contains=ALLBUT,mojoBuiltin,mojoFunction,mojoAsync 139 | \ transparent 140 | 141 | " From the 'Python Library Reference' class hierarchy at the bottom. 142 | " http://docs.python.org/library/exceptions.html 143 | " builtin base exceptions (used mostly as base classes for other exceptions) 144 | syn keyword mojoExceptions BaseException Exception 145 | syn keyword mojoExceptions ArithmeticError BufferError LookupError 146 | 147 | " builtin exceptions (actually raised) 148 | syn keyword mojoExceptions AssertionError AttributeError EOFError 149 | syn keyword mojoExceptions FloatingPointError GeneratorExit ImportError 150 | syn keyword mojoExceptions IndentationError IndexError KeyError 151 | syn keyword mojoExceptions KeyboardInterrupt MemoryError 152 | syn keyword mojoExceptions ModuleNotFoundError NameError 153 | syn keyword mojoExceptions NotImplementedError OSError OverflowError 154 | syn keyword mojoExceptions RecursionError ReferenceError RuntimeError 155 | syn keyword mojoExceptions StopAsyncIteration StopIteration SyntaxError 156 | syn keyword mojoExceptions SystemError SystemExit TabError TypeError 157 | syn keyword mojoExceptions UnboundLocalError UnicodeDecodeError 158 | syn keyword mojoExceptions UnicodeEncodeError UnicodeError 159 | syn keyword mojoExceptions UnicodeTranslateError ValueError 160 | syn keyword mojoExceptions ZeroDivisionError 161 | 162 | " builtin exception aliases for OSError 163 | syn keyword mojoExceptions EnvironmentError IOError WindowsError 164 | 165 | " builtin OS exceptions in Python 3 166 | syn keyword mojoExceptions BlockingIOError BrokenPipeError 167 | syn keyword mojoExceptions ChildProcessError ConnectionAbortedError 168 | syn keyword mojoExceptions ConnectionError ConnectionRefusedError 169 | syn keyword mojoExceptions ConnectionResetError FileExistsError 170 | syn keyword mojoExceptions FileNotFoundError InterruptedError 171 | syn keyword mojoExceptions IsADirectoryError NotADirectoryError 172 | syn keyword mojoExceptions PermissionError ProcessLookupError TimeoutError 173 | 174 | " builtin warnings 175 | syn keyword mojoExceptions BytesWarning DeprecationWarning FutureWarning 176 | syn keyword mojoExceptions ImportWarning PendingDeprecationWarning 177 | syn keyword mojoExceptions ResourceWarning RuntimeWarning 178 | syn keyword mojoExceptions SyntaxWarning UnicodeWarning 179 | syn keyword mojoExceptions UserWarning Warning 180 | 181 | " trailing whitespace 182 | syn match mojoSpaceError display excludenl "\s\+$" 183 | " mixed tabs and spaces 184 | syn match mojoSpaceError display " \+\t" 185 | syn match mojoSpaceError display "\t\+ " 186 | 187 | " Do not spell doctests inside strings. 188 | " Notice that the end of a string, will end the contained 189 | " doctest too. Thus, we do *not* need to have it as an end pattern. 190 | syn region mojoDoctest 191 | \ start="^\s*>>>\s" end="^\s*$" 192 | \ contained contains=ALLBUT,mojoDoctest,mojoFunction,@Spell 193 | syn region mojoDoctestValue 194 | \ start=+^\s*\%(>>>\s\|\.\.\.\s\|"""\|'''\)\@!\S\++ end="$" 195 | \ contained 196 | syn region mojoDoctest 197 | \ start="^\s*>>>" end="^\s*$" 198 | \ contained contains=@NoSpell 199 | 200 | " Sync at the beginning of class, function, or method definition. 201 | syn sync match mojoSync grouphere NONE "^\%(def\|class\)\s\+\h\w*\s*[(:]" 202 | 203 | " The default highlight links. Can be overridden later. 204 | hi def link mojoStatement Statement 205 | hi def link mojoConditional Conditional 206 | hi def link mojoRepeat Repeat 207 | hi def link mojoOperator Operator 208 | hi def link mojoException Exception 209 | hi def link mojoInclude Include 210 | hi def link mojoAsync Statement 211 | hi def link mojoDecorator Define 212 | hi def link mojoDecoratorName Function 213 | hi def link mojoFunction Function 214 | hi def link mojoComment Comment 215 | hi def link mojoTodo Todo 216 | hi def link mojoString String 217 | hi def link mojoRawString String 218 | hi def link mojoQuotes String 219 | hi def link mojoTripleQuotes mojoQuotes 220 | hi def link mojoEscape Special 221 | hi def link mojoNumber Number 222 | hi def link mojoBuiltin Function 223 | hi def link mojoType Type 224 | hi def link mojoExceptions Structure 225 | hi def link mojoSpaceError Error 226 | hi def link mojoDoctest Special 227 | hi def link mojoDoctestValue Define 228 | 229 | let b:current_syntax = "mojo" 230 | 231 | let &cpo = s:cpo_save 232 | unlet s:cpo_save 233 | 234 | --------------------------------------------------------------------------------