2014-07-07 17:48:09 +04:00
|
|
|
# http://python.org
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
# Detection
|
|
|
|
# ‾‾‾‾‾‾‾‾‾
|
|
|
|
|
|
|
|
hook global BufCreate .*[.](py) %{
|
2017-11-03 10:34:41 +03:00
|
|
|
set-option buffer filetype python
|
2014-07-07 17:48:09 +04:00
|
|
|
}
|
|
|
|
|
2019-03-13 08:24:33 +03:00
|
|
|
hook -once global BufSetOption filetype=python %{
|
|
|
|
require-module python
|
|
|
|
}
|
|
|
|
|
|
|
|
provide-module python %{
|
|
|
|
|
2016-03-14 15:08:51 +03:00
|
|
|
# Highlighters & Completion
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
2014-07-07 17:48:09 +04:00
|
|
|
|
2018-06-28 14:08:58 +03:00
|
|
|
add-highlighter shared/python regions
|
|
|
|
add-highlighter shared/python/code default-region group
|
2018-07-02 13:59:12 +03:00
|
|
|
add-highlighter shared/python/docstring region -match-capture ("""|''') ("""|''') regions
|
|
|
|
add-highlighter shared/python/double_string region '"' (?<!\\)(\\\\)*" fill string
|
|
|
|
add-highlighter shared/python/single_string region "'" (?<!\\)(\\\\)*' fill string
|
|
|
|
add-highlighter shared/python/comment region '#' '$' fill comment
|
2014-07-07 17:48:09 +04:00
|
|
|
|
2017-11-14 12:42:57 +03:00
|
|
|
# Integer formats
|
2018-06-28 14:08:58 +03:00
|
|
|
add-highlighter shared/python/code/ regex '(?i)\b0b[01]+l?\b' 0:value
|
|
|
|
add-highlighter shared/python/code/ regex '(?i)\b0x[\da-f]+l?\b' 0:value
|
|
|
|
add-highlighter shared/python/code/ regex '(?i)\b0o?[0-7]+l?\b' 0:value
|
|
|
|
add-highlighter shared/python/code/ regex '(?i)\b([1-9]\d*|0)l?\b' 0:value
|
2017-11-14 12:42:57 +03:00
|
|
|
# Float formats
|
2018-06-28 14:08:58 +03:00
|
|
|
add-highlighter shared/python/code/ regex '\b\d+[eE][+-]?\d+\b' 0:value
|
|
|
|
add-highlighter shared/python/code/ regex '(\b\d+)?\.\d+\b' 0:value
|
|
|
|
add-highlighter shared/python/code/ regex '\b\d+\.' 0:value
|
2017-11-14 12:42:57 +03:00
|
|
|
# Imaginary formats
|
2018-06-28 14:08:58 +03:00
|
|
|
add-highlighter shared/python/code/ regex '\b\d+\+\d+[jJ]\b' 0:value
|
2017-11-14 12:42:57 +03:00
|
|
|
|
2018-06-28 14:08:58 +03:00
|
|
|
add-highlighter shared/python/docstring/ default-region fill string
|
2018-07-02 13:59:12 +03:00
|
|
|
add-highlighter shared/python/docstring/ region '>>> \K' '\z' ref python
|
|
|
|
add-highlighter shared/python/docstring/ region '\.\.\. \K' '\z' ref python
|
2014-07-07 17:48:09 +04:00
|
|
|
|
2018-05-07 00:29:52 +03:00
|
|
|
evaluate-commands %sh{
|
2016-03-14 15:08:51 +03:00
|
|
|
# Grammar
|
2017-11-14 12:42:57 +03:00
|
|
|
values="True|False|None|self|inf"
|
2016-03-14 19:29:11 +03:00
|
|
|
meta="import|from"
|
2017-11-18 11:56:47 +03:00
|
|
|
|
2017-11-14 12:42:57 +03:00
|
|
|
# attributes and methods list based on https://docs.python.org/3/reference/datamodel.html
|
|
|
|
attributes="__annotations__|__closure__|__code__|__defaults__|__dict__|__doc__"
|
|
|
|
attributes="${attributes}|__globals__|__kwdefaults__|__module__|__name__|__qualname__"
|
|
|
|
methods="__abs__|__add__|__aenter__|__aexit__|__aiter__|__and__|__anext__"
|
|
|
|
methods="${methods}|__await__|__bool__|__bytes__|__call__|__complex__|__contains__"
|
|
|
|
methods="${methods}|__del__|__delattr__|__delete__|__delitem__|__dir__|__divmod__"
|
|
|
|
methods="${methods}|__enter__|__eq__|__exit__|__float__|__floordiv__|__format__"
|
|
|
|
methods="${methods}|__ge__|__get__|__getattr__|__getattribute__|__getitem__"
|
|
|
|
methods="${methods}|__gt__|__hash__|__iadd__|__iand__|__ifloordiv__|__ilshift__"
|
|
|
|
methods="${methods}|__imatmul__|__imod__|__imul__|__index__|__init__"
|
|
|
|
methods="${methods}|__init_subclass__|__int__|__invert__|__ior__|__ipow__"
|
|
|
|
methods="${methods}|__irshift__|__isub__|__iter__|__itruediv__|__ixor__|__le__"
|
|
|
|
methods="${methods}|__len__|__length_hint__|__lshift__|__lt__|__matmul__"
|
|
|
|
methods="${methods}|__missing__|__mod__|__mul__|__ne__|__neg__|__new__|__or__"
|
|
|
|
methods="${methods}|__pos__|__pow__|__radd__|__rand__|__rdivmod__|__repr__"
|
|
|
|
methods="${methods}|__reversed__|__rfloordiv__|__rlshift__|__rmatmul__|__rmod__"
|
|
|
|
methods="${methods}|__rmul__|__ror__|__round__|__rpow__|__rrshift__|__rshift__"
|
|
|
|
methods="${methods}|__rsub__|__rtruediv__|__rxor__|__set__|__setattr__"
|
|
|
|
methods="${methods}|__setitem__|__set_name__|__slots__|__str__|__sub__"
|
|
|
|
methods="${methods}|__truediv__|__xor__"
|
2017-11-18 11:56:47 +03:00
|
|
|
|
|
|
|
# built-in exceptions https://docs.python.org/3/library/exceptions.html
|
|
|
|
exceptions="ArithmeticError|AssertionError|AttributeError|BaseException|BlockingIOError"
|
|
|
|
exceptions="${exceptions}|BrokenPipeError|BufferError|BytesWarning|ChildProcessError"
|
|
|
|
exceptions="${exceptions}|ConnectionAbortedError|ConnectionError|ConnectionRefusedError"
|
|
|
|
exceptions="${exceptions}|ConnectionResetError|DeprecationWarning|EOFError|Exception"
|
|
|
|
exceptions="${exceptions}|FileExistsError|FileNotFoundError|FloatingPointError|FutureWarning"
|
|
|
|
exceptions="${exceptions}|GeneratorExit|ImportError|ImportWarning|IndentationError"
|
|
|
|
exceptions="${exceptions}|IndexError|InterruptedError|IsADirectoryError|KeyboardInterrupt"
|
|
|
|
exceptions="${exceptions}|KeyError|LookupError|MemoryError|ModuleNotFoundError|NameError"
|
|
|
|
exceptions="${exceptions}|NotADirectoryError|NotImplementedError|OSError|OverflowError"
|
|
|
|
exceptions="${exceptions}|PendingDeprecationWarning|PermissionError|ProcessLookupError"
|
|
|
|
exceptions="${exceptions}|RecursionError|ReferenceError|ResourceWarning|RuntimeError"
|
|
|
|
exceptions="${exceptions}|RuntimeWarning|StopAsyncIteration|StopIteration|SyntaxError"
|
|
|
|
exceptions="${exceptions}|SyntaxWarning|SystemError|SystemExit|TabError|TimeoutError|TypeError"
|
|
|
|
exceptions="${exceptions}|UnboundLocalError|UnicodeDecodeError|UnicodeEncodeError|UnicodeError"
|
|
|
|
exceptions="${exceptions}|UnicodeTranslateError|UnicodeWarning|UserWarning|ValueError|Warning"
|
|
|
|
exceptions="${exceptions}|ZeroDivisionError"
|
|
|
|
|
2016-03-14 15:08:51 +03:00
|
|
|
# Keyword list is collected using `keyword.kwlist` from `keyword`
|
2018-11-12 15:20:18 +03:00
|
|
|
keywords="and|as|assert|async|await|break|class|continue|def|del|elif|else|except|exec"
|
2018-07-03 00:28:25 +03:00
|
|
|
keywords="${keywords}|finally|for|global|if|in|is|lambda|nonlocal|not|or|pass|print"
|
2016-11-11 16:07:45 +03:00
|
|
|
keywords="${keywords}|raise|return|try|while|with|yield"
|
2017-11-18 11:56:47 +03:00
|
|
|
|
2016-11-11 16:07:45 +03:00
|
|
|
types="bool|buffer|bytearray|bytes|complex|dict|file|float|frozenset|int"
|
|
|
|
types="${types}|list|long|memoryview|object|set|str|tuple|unicode|xrange"
|
2017-11-18 11:56:47 +03:00
|
|
|
|
2019-04-04 05:46:20 +03:00
|
|
|
functions="abs|all|any|ascii|bin|breakpoint|callable|chr|classmethod|compile|complex"
|
2016-11-11 16:07:45 +03:00
|
|
|
functions="${functions}|delattr|dict|dir|divmod|enumerate|eval|exec|filter"
|
|
|
|
functions="${functions}|format|frozenset|getattr|globals|hasattr|hash|help"
|
|
|
|
functions="${functions}|hex|id|__import__|input|isinstance|issubclass|iter"
|
|
|
|
functions="${functions}|len|locals|map|max|memoryview|min|next|oct|open|ord"
|
|
|
|
functions="${functions}|pow|print|property|range|repr|reversed|round"
|
|
|
|
functions="${functions}|setattr|slice|sorted|staticmethod|sum|super|type|vars|zip"
|
2014-07-07 17:48:09 +04:00
|
|
|
|
2016-03-14 15:08:51 +03:00
|
|
|
# Add the language's grammar to the static completion list
|
2016-04-23 08:47:01 +03:00
|
|
|
printf %s\\n "hook global WinSetOption filetype=python %{
|
2018-06-17 06:30:40 +03:00
|
|
|
set-option window static_words ${values} ${meta} ${attributes} ${methods} ${exceptions} ${keywords} ${types} ${functions}
|
|
|
|
}" | tr '|' ' '
|
2014-07-07 17:48:09 +04:00
|
|
|
|
2016-03-14 15:08:51 +03:00
|
|
|
# Highlight keywords
|
2016-03-17 16:30:14 +03:00
|
|
|
printf %s "
|
2018-06-28 14:08:58 +03:00
|
|
|
add-highlighter shared/python/code/ regex '\b(${values})\b' 0:value
|
|
|
|
add-highlighter shared/python/code/ regex '\b(${meta})\b' 0:meta
|
|
|
|
add-highlighter shared/python/code/ regex '\b(${attribute})\b' 0:attribute
|
|
|
|
add-highlighter shared/python/code/ regex '\bdef\s+(${methods})\b' 1:function
|
|
|
|
add-highlighter shared/python/code/ regex '\b(${exceptions})\b' 0:function
|
|
|
|
add-highlighter shared/python/code/ regex '\b(${keywords})\b' 0:keyword
|
|
|
|
add-highlighter shared/python/code/ regex '\b(${functions})\b\(' 1:builtin
|
|
|
|
add-highlighter shared/python/code/ regex '\b(${types})\b' 0:type
|
|
|
|
add-highlighter shared/python/code/ regex '@[\w_]+\b' 0:attribute
|
2016-11-11 16:07:45 +03:00
|
|
|
"
|
2014-07-07 17:48:09 +04:00
|
|
|
}
|
|
|
|
|
2018-06-28 14:08:58 +03:00
|
|
|
add-highlighter shared/python/code/ regex (?<=[\w\s\d'"_])(<=|<<|>>|>=|<>|<|>|!=|==|\||\^|&|\+|-|\*\*|\*|//|/|%|~) 0:operator
|
|
|
|
add-highlighter shared/python/code/ regex (?<=[\w\s\d'"_])((?<![=<>!])=(?![=])|[+*-]=) 0:builtin
|
2018-01-08 01:35:54 +03:00
|
|
|
|
2016-03-14 15:08:51 +03:00
|
|
|
# Commands
|
|
|
|
# ‾‾‾‾‾‾‾‾
|
|
|
|
|
2017-11-03 10:34:41 +03:00
|
|
|
define-command -hidden python-indent-on-new-line %{
|
2017-11-03 11:09:45 +03:00
|
|
|
evaluate-commands -draft -itersel %{
|
2017-01-11 16:56:48 +03:00
|
|
|
# copy '#' comment prefix and following white spaces
|
2017-11-03 11:09:45 +03:00
|
|
|
try %{ execute-keys -draft k <a-x> s ^\h*#\h* <ret> y jgh P }
|
2014-07-07 17:48:09 +04:00
|
|
|
# preserve previous line indent
|
2017-11-03 11:09:45 +03:00
|
|
|
try %{ execute-keys -draft \; K <a-&> }
|
2016-05-05 15:59:06 +03:00
|
|
|
# cleanup trailing whitespaces from previous line
|
2017-11-03 11:09:45 +03:00
|
|
|
try %{ execute-keys -draft k <a-x> s \h+$ <ret> d }
|
2016-10-13 22:10:41 +03:00
|
|
|
# indent after line ending with :
|
2018-07-08 19:21:01 +03:00
|
|
|
try %{ execute-keys -draft <space> k <a-x> <a-k> :$ <ret> j <a-gt> }
|
2014-07-07 17:48:09 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Initialization
|
|
|
|
# ‾‾‾‾‾‾‾‾‾‾‾‾‾‾
|
|
|
|
|
2018-11-28 12:39:07 +03:00
|
|
|
hook -group python-highlight global WinSetOption filetype=python %{
|
|
|
|
add-highlighter window/python ref python
|
2018-12-11 02:11:35 +03:00
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-highlighter window/python }
|
2018-11-28 12:39:07 +03:00
|
|
|
}
|
2016-09-25 16:15:07 +03:00
|
|
|
|
2014-07-07 17:48:09 +04:00
|
|
|
hook global WinSetOption filetype=python %{
|
2017-01-02 07:20:05 +03:00
|
|
|
hook window InsertChar \n -group python-indent python-indent-on-new-line
|
2016-05-05 15:59:06 +03:00
|
|
|
# cleanup trailing whitespaces on current line insert end
|
2018-12-19 12:10:26 +03:00
|
|
|
hook window ModeChange insert:.* -group python-trim-indent %{ try %{ execute-keys -draft \; <a-x> s ^\h+$ <ret> d } }
|
2018-12-11 02:11:35 +03:00
|
|
|
hook -once -always window WinSetOption filetype=.* %{ remove-hooks window python-.+ }
|
2014-07-07 17:48:09 +04:00
|
|
|
}
|
2019-03-13 08:24:33 +03:00
|
|
|
|
|
|
|
}
|