# pyright: basic
"""Kakoune lexer for Pygments."""

from pygments.lexers.shell import BashLexer
from pygments.lexer import inherit, words, bygroups
from pygments.token import Keyword, Literal, Name, Number, String, Text


class KakouneLexer(BashLexer):
    name = "Kakoune"
    aliases = ["kak", "kakoune", "kakrc", "kakscript"]
    filenames = ["*.kak", "kakrc"]
    _kak_keywords = [
        "add-highlighter",
        "alias",
        "arrange-buffers",
        "buffer",
        "buffer-next",
        "buffer-previous",
        "catch",
        "change-directory",
        "colorscheme",
        "debug",
        "declare-option",
        "declare-user-mode",
        "define-command",
        "complete-command",
        "delete-buffer",
        "delete-buffer!",
        "echo",
        "edit",
        "edit!",
        "enter-user-mode",
        "evaluate-commands",
        "execute-keys",
        "fail",
        "hook",
        "info",
        "kill",
        "kill!",
        "map",
        "nop",
        "on-key",
        "prompt",
        "provide-module",
        "quit",
        "quit!",
        "remove-highlighter",
        "remove-hooks",
        "rename-buffer",
        "rename-client",
        "rename-session",
        "require-module",
        "select",
        "set-face",
        "set-option",
        "set-register",
        "source",
        "trigger-user-hook",
        "try",
        "unalias",
        "unmap",
        "unset-face",
        "unset-option",
        "update-option",
        "write",
        "write!",
        "write-all",
        "write-all-quit",
        "write-quit",
        "write-quit!",
    ]
    _kak_attributes = [
        "global",
        "buffer",
        "window",
        "current",
        "normal",
        "insert",
        "prompt",
        "goto",
        "view",
        "user",
        "object",
        "number-lines",
        "show-matching",
        "show-whitespaces",
        "fill",
        "regex",
        "dynregex",
        "group",
        "flag-lines",
        "ranges",
        "line",
        "column",
        "wrap",
        "ref",
        "regions",
        "region",
        "default-region",
        "replace-ranges",
    ]
    _kak_types = [
        "int",
        "bool",
        "str",
        "regex",
        "int-list",
        "str-list",
        "completions",
        "line-specs",
        "range-specs",
        "str-to-str-map",
    ]
    _kak_values = [
        "default",
        "black",
        "red",
        "green",
        "yellow",
        "blue",
        "magenta",
        "cyan",
        "white",
        "yes",
        "no",
        "false",
        "true",
    ]

    tokens = {
        "root": [
            (words(_kak_keywords, prefix=r"\b", suffix=r"\b"), Keyword),
            (
                words(_kak_attributes, prefix=r"\b", suffix=r"\b"),
                Name.Attribute,
            ),
            (words(_kak_types, prefix=r"\b", suffix=r"\b"), Name.Class),
            (words(_kak_values, prefix=r"\b", suffix=r"\b"), Literal),
            (r"\brgba?:[0-9a-fA-F]{6,8}\b", Literal.Number.Hex),
            (
                r"(-params )(\b\d+)(\.\.)(\d+\b)",
                bygroups(Text, Number, Text, Number),
            ),
            (r'"(""|[^"])*"', String),
            (r"'(''|[^'])*'", String),
            inherit,
        ]
    }
