Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(text-area): new languages #4160

Merged
merged 3 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/textual/document/_languages.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
BUILTIN_LANGUAGES = sorted(
Copy link
Contributor Author

@juftin juftin Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've found the TheRenegadeCoder/sample-programs repo useful for checking how these languages render.

App for Testing

"""
TextArea Example
"""

import os
import pathlib
from traceback import format_exc
from typing import Any, ClassVar

import click

from textual import on
from textual.app import ComposeResult, App
from textual.binding import BindingType, Binding
from textual.containers import Horizontal
from textual.widgets import TextArea, DirectoryTree, Header, Footer

KNOWN_EXTENSIONS = {
    "bash": [".sh", ".bash"],
    "c": [".c"],
    "c_sharp": [".cs"],
    "cpp": [".cpp", ".hpp", ".h"],
    "css": [".css"],
    "dockerfile": ["Dockerfile"],
    "dot": [".dot"],
    "elisp": [".el"],
    "elixir": [".ex", ".exs"],
    "elm": [".elm"],
    "embedded_template": [".et"],
    "erlang": [".erl", ".hrl"],
    "fortran": [".f", ".f90", ".f95", ".f03", ".f08", ".f18", ".for", ".f77"],
    "go": [".go"],
    "gomod": ["go.mod", "go.sum"],
    "hack": [".hh"],
    "haskell": [".hs"],
    "html": [".html", ".htm"],
    "java": [".java"],
    "javascript": [".js", ".jsx"],
    "jsdoc": [".jsdoc"],
    "json": [".json"],
    "kotlin": [".kt"],
    "make": ["Makefile", "makefile", "GNUmakefile"],
    "markdown": [".md", ".markdown", ".mdown", ".mkdn"],
    "objc": [".m", ".mm"],
    "ocaml": [".ml", ".mli"],
    "php": [".php", ".php5", ".php7"],
    "python": [".py"],
    "r": [".r"],
    "regex": [".regex"],
    "ruby": [".rb", ".erb"],
    "rust": [".rs"],
    "scala": [".scala"],
    "sql": [".sql"],
    "sqlite": [".sqlite"],
    "toml": [".toml"],
    "typescript": [".ts", ".tsx"],
    "yaml": [".yaml", ".yml"],
}

EXTENSION_TO_LANGUAGE_MAPPING = {
    extension: language
    for language, extensions in KNOWN_EXTENSIONS.items()
    for extension in extensions
}


class TextAreaApp(App):
    CSS: ClassVar[
        str
    ] = """
    DirectoryTree {
        width: auto;
    }
    """

    BINDINGS: ClassVar[list[BindingType]] = [
        Binding("q", "quit", "Quit", show=True, priority=True),
        Binding("t", "theme", "Change Theme", show=True, priority=True),
    ]

    def __init__(self, path: os.PathLike, *args: Any, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)
        self.path = pathlib.Path(path)
        self.directory_tree = DirectoryTree(path=self.path)
        self.text_area = TextArea(
            theme="monokai",
            soft_wrap=False,
            show_line_numbers=True,
        )

    def compose(self) -> ComposeResult:
        yield Header()
        yield Horizontal(self.directory_tree, self.text_area)
        yield Footer()

    @on(DirectoryTree.FileSelected)
    def handle_file_selected(self, event: DirectoryTree.FileSelected) -> None:
        self.path = pathlib.Path(event.path)
        try:
            new_text = event.path.read_text()
            extension_mapping = EXTENSION_TO_LANGUAGE_MAPPING.get(
                self.path.suffix.lower()
            )
            filename_mapping = EXTENSION_TO_LANGUAGE_MAPPING.get(self.path.name)
            language = extension_mapping or filename_mapping or "regex"
            self.text_area.text = new_text
            self.text_area.language = language
        except:  # noqa
            self.text_area.language = "regex"
            self.text_area.text = f"Error: {format_exc()}"
        finally:
            self.sub_title = f"[{self.text_area.language}] [{self.text_area.theme}]"

    def action_theme(self) -> None:
        all_themes = list(self.text_area.available_themes)
        current_index = all_themes.index(self.text_area.theme)
        next_index = (current_index + 1) % len(all_themes)
        self.text_area.theme = all_themes[next_index]
        self.sub_title = f"[{self.text_area.language}] [{self.text_area.theme}]"


@click.command()
@click.argument(
    "path", type=click.Path(exists=True, dir_okay=True, file_okay=False), default="."
)
def cli(path: os.PathLike) -> None:
    """
    TextArea CLI
    """
    app = TextAreaApp(path=path)
    app.run()


if __name__ == "__main__":
    cli()

[
"markdown",
"yaml",
"sql",
"bash",
"c",
"c_sharp",
"cpp",
"css",
"dockerfile",
"dot",
"elisp",
"elixir",
"elm",
"embedded_template",
"erlang",
"fortran",
"go",
"gomod",
"hack",
"haskell",
"html",
"java",
"javascript",
"jsdoc",
"json",
"kotlin",
"make",
"markdown",
"objc",
"ocaml",
"php",
"python",
"r",
"regex",
"ruby",
"rust",
"scala",
juftin marked this conversation as resolved.
Show resolved Hide resolved
"sql",
"sqlite",
"toml",
"typescript",
Copy link
Contributor Author

@juftin juftin Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript has issues

NameError: Invalid node type satisfies

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I've found that this means the parser and highlights file is out of sync: the highlight query file has been updated to refer to a node type that the parser is unaware of. Usually moving to an older version of the highlights file (the version that "aligns" with the parser) fixes the problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, you're right. Once I pin on tree-sitter-languages==1.10.2, which is the version I used to map to the .scm files, everything works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typescript highlighting is non existent though after the new version pinning
image

Copy link
Contributor

@TomJGooding TomJGooding Feb 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDIT: See #4160 (comment)

"yaml",
]
)
199 changes: 55 additions & 144 deletions src/textual/tree-sitter/highlights/bash.scm
Original file line number Diff line number Diff line change
@@ -1,145 +1,56 @@
(simple_expansion) @none
(expansion
"${" @punctuation.special
"}" @punctuation.special) @none
[
"("
")"
"(("
"))"
"{"
"}"
"["
"]"
"[["
"]]"
] @punctuation.bracket

[
";"
";;"
(heredoc_start)
] @punctuation.delimiter

[
"$"
] @punctuation.special

[
">"
">>"
"<"
"<<"
"&"
"&&"
"|"
"||"
"="
"=~"
"=="
"!="
] @operator

[
(string)
(raw_string)
(ansi_c_string)
(heredoc_body)
] @string @spell

(variable_assignment (word) @string)

[
"if"
"then"
"else"
"elif"
"fi"
"case"
"in"
"esac"
] @conditional

[
"for"
"do"
"done"
"select"
"until"
"while"
] @repeat

[
"declare"
"export"
"local"
"readonly"
"unset"
] @keyword

"function" @keyword.function

(special_variable_name) @constant

; trap -l
((word) @constant.builtin
(#match? @constant.builtin "^SIG(HUP|INT|QUIT|ILL|TRAP|ABRT|BUS|FPE|KILL|USR[12]|SEGV|PIPE|ALRM|TERM|STKFLT|CHLD|CONT|STOP|TSTP|TT(IN|OU)|URG|XCPU|XFSZ|VTALRM|PROF|WINCH|IO|PWR|SYS|RTMIN([+]([1-9]|1[0-5]))?|RTMAX(-([1-9]|1[0-4]))?)$"))

((word) @boolean
(#any-of? @boolean "true" "false"))

(comment) @comment @spell
(test_operator) @string

(command_substitution
[ "$(" ")" ] @punctuation.bracket)

(process_substitution
[ "<(" ")" ] @punctuation.bracket)


(function_definition
name: (word) @function)

(command_name (word) @function.call)

((command_name (word) @function.builtin)
(#any-of? @function.builtin
"alias" "bg" "bind" "break" "builtin" "caller" "cd"
"command" "compgen" "complete" "compopt" "continue"
"coproc" "dirs" "disown" "echo" "enable" "eval"
"exec" "exit" "fc" "fg" "getopts" "hash" "help"
"history" "jobs" "kill" "let" "logout" "mapfile"
"popd" "printf" "pushd" "pwd" "read" "readarray"
"return" "set" "shift" "shopt" "source" "suspend"
"test" "time" "times" "trap" "type" "typeset"
"ulimit" "umask" "unalias" "wait"))

(command
argument: [
(word) @parameter
(concatenation (word) @parameter)
])

((word) @number
(#lua-match? @number "^[0-9]+$"))

(file_redirect
descriptor: (file_descriptor) @operator
destination: (word) @parameter)

(expansion
[ "${" "}" ] @punctuation.bracket)

(variable_name) @variable

((variable_name) @constant
(#lua-match? @constant "^[A-Z][A-Z_0-9]*$"))

(case_item
value: (word) @parameter)

(regex) @string.regex

((program . (comment) @preproc)
(#lua-match? @preproc "^#!/"))
(string)
(raw_string)
(heredoc_body)
(heredoc_start)
] @string

(command_name) @function

(variable_name) @property

[
"case"
"do"
"done"
"elif"
"else"
"esac"
"export"
"fi"
"for"
"function"
"if"
"in"
"select"
"then"
"unset"
"until"
"while"
] @keyword

(comment) @comment

(function_definition name: (word) @function)

(file_descriptor) @number

[
(command_substitution)
(process_substitution)
(expansion)
]@embedded

[
"$"
"&&"
">"
">>"
"<"
"|"
] @operator

(
(command (_) @constant)
(#match? @constant "^-")
)
81 changes: 81 additions & 0 deletions src/textual/tree-sitter/highlights/c.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"break" @keyword
"case" @keyword
"const" @keyword
"continue" @keyword
"default" @keyword
"do" @keyword
"else" @keyword
"enum" @keyword
"extern" @keyword
"for" @keyword
"if" @keyword
"inline" @keyword
"return" @keyword
"sizeof" @keyword
"static" @keyword
"struct" @keyword
"switch" @keyword
"typedef" @keyword
"union" @keyword
"volatile" @keyword
"while" @keyword

"#define" @keyword
"#elif" @keyword
"#else" @keyword
"#endif" @keyword
"#if" @keyword
"#ifdef" @keyword
"#ifndef" @keyword
"#include" @keyword
(preproc_directive) @keyword

"--" @operator
"-" @operator
"-=" @operator
"->" @operator
"=" @operator
"!=" @operator
"*" @operator
"&" @operator
"&&" @operator
"+" @operator
"++" @operator
"+=" @operator
"<" @operator
"==" @operator
">" @operator
"||" @operator

"." @delimiter
";" @delimiter

(string_literal) @string
(system_lib_string) @string

(null) @constant
(number_literal) @number
(char_literal) @number

(call_expression
function: (identifier) @function)
(call_expression
function: (field_expression
field: (field_identifier) @function))
(function_declarator
declarator: (identifier) @function)
(preproc_function_def
name: (identifier) @function.special)

(field_identifier) @property
(statement_identifier) @label
(type_identifier) @type
(primitive_type) @type
(sized_type_specifier) @type

((identifier) @constant
(#match? @constant "^[A-Z][A-Z\\d_]*$"))

(identifier) @variable

(comment) @comment
Loading