From 34351c0a51cad8f354f628d23a0dc04e4112d04b Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 21 Jun 2022 17:04:54 -0700 Subject: [PATCH] Start work on Python support --- Cargo.lock | 11 ++ crates/zed/Cargo.toml | 1 + crates/zed/src/languages.rs | 5 + crates/zed/src/languages/python/config.toml | 11 ++ .../zed/src/languages/python/highlights.scm | 125 ++++++++++++++++++ crates/zed/src/languages/python/indents.scm | 6 + styles/src/themes/common/base16.ts | 4 + 7 files changed, 163 insertions(+) create mode 100644 crates/zed/src/languages/python/config.toml create mode 100644 crates/zed/src/languages/python/highlights.scm create mode 100644 crates/zed/src/languages/python/indents.scm diff --git a/Cargo.lock b/Cargo.lock index b96bcce58167661d73830530f0cdcb414dc8b6e5..427db02e74e911bc34ea21691b4be53e61ae0e30 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5277,6 +5277,16 @@ dependencies = [ "tree-sitter", ] +[[package]] +name = "tree-sitter-python" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0d315475c65416274539dd1db9fa2de94918a6ef399dc1287be7cb7bc83dd6d" +dependencies = [ + "cc", + "tree-sitter", +] + [[package]] name = "tree-sitter-rust" version = "0.20.1" @@ -6030,6 +6040,7 @@ dependencies = [ "tree-sitter-go", "tree-sitter-json 0.20.0", "tree-sitter-markdown", + "tree-sitter-python", "tree-sitter-rust", "tree-sitter-toml", "tree-sitter-typescript", diff --git a/crates/zed/Cargo.toml b/crates/zed/Cargo.toml index a866e2b92e85a7afb28556ca86e244d5ec24ed32..3e43f45c7af67b5c831e52e936fcd2a0ae6f761b 100644 --- a/crates/zed/Cargo.toml +++ b/crates/zed/Cargo.toml @@ -94,6 +94,7 @@ tree-sitter-go = { git = "https://github.com/tree-sitter/tree-sitter-go", rev = tree-sitter-json = { git = "https://github.com/tree-sitter/tree-sitter-json", rev = "137e1ce6a02698fc246cdb9c6b886ed1de9a1ed8" } tree-sitter-rust = "0.20.1" tree-sitter-markdown = { git = "https://github.com/MDeiml/tree-sitter-markdown", rev = "330ecab87a3e3a7211ac69bbadc19eabecdb1cca" } +tree-sitter-python = "0.20.0" tree-sitter-toml = { git = "https://github.com/tree-sitter/tree-sitter-toml", rev = "342d9be207c2dba869b9967124c679b5e6fd0ebe" } tree-sitter-typescript = "0.20.1" url = "2.2" diff --git a/crates/zed/src/languages.rs b/crates/zed/src/languages.rs index 825925c07f41a9bcb5d80547b97fb689ac1a58e8..f3c1a83f25a4ea9d141507b42cd6d3ced3aa56fd 100644 --- a/crates/zed/src/languages.rs +++ b/crates/zed/src/languages.rs @@ -43,6 +43,11 @@ pub fn build_language_registry(login_shell_env_loaded: Task<()>) -> LanguageRegi tree_sitter_markdown::language(), None, // ), + ( + "python", + tree_sitter_python::language(), + None, // + ), ( "rust", tree_sitter_rust::language(), diff --git a/crates/zed/src/languages/python/config.toml b/crates/zed/src/languages/python/config.toml new file mode 100644 index 0000000000000000000000000000000000000000..beddf578f2ba325291c738f02aea54fccf78706b --- /dev/null +++ b/crates/zed/src/languages/python/config.toml @@ -0,0 +1,11 @@ +name = "Python" +path_suffixes = ["py"] +line_comment = "# " +autoclose_before = ";:.,=}])>" +brackets = [ + { start = "{", end = "}", close = true, newline = true }, + { start = "[", end = "]", close = true, newline = true }, + { start = "(", end = ")", close = true, newline = true }, + { start = "\"", end = "\"", close = true, newline = false }, + { start = "'", end = "'", close = false, newline = false }, +] diff --git a/crates/zed/src/languages/python/highlights.scm b/crates/zed/src/languages/python/highlights.scm new file mode 100644 index 0000000000000000000000000000000000000000..c752e586c7efa1d9510a2076dec6abdf5c15b06a --- /dev/null +++ b/crates/zed/src/languages/python/highlights.scm @@ -0,0 +1,125 @@ +(attribute attribute: (identifier) @property) +(type (identifier) @type) + +; Function calls + +(decorator) @function + +(call + function: (attribute attribute: (identifier) @function.method)) +(call + function: (identifier) @function) + +; Function definitions + +(function_definition + name: (identifier) @function) + +; Identifier naming conventions + +((identifier) @constructor + (#match? @constructor "^[A-Z]")) + +((identifier) @constant + (#match? @constant "^[A-Z][A-Z_]*$")) + +; Builtin functions + +((call + function: (identifier) @function.builtin) + (#match? + @function.builtin + "^(abs|all|any|ascii|bin|bool|breakpoint|bytearray|bytes|callable|chr|classmethod|compile|complex|delattr|dict|dir|divmod|enumerate|eval|exec|filter|float|format|frozenset|getattr|globals|hasattr|hash|help|hex|id|input|int|isinstance|issubclass|iter|len|list|locals|map|max|memoryview|min|next|object|oct|open|ord|pow|print|property|range|repr|reversed|round|set|setattr|slice|sorted|staticmethod|str|sum|super|tuple|type|vars|zip|__import__)$")) + +; Literals + +[ + (none) + (true) + (false) +] @constant.builtin + +[ + (integer) + (float) +] @number + +(comment) @comment +(string) @string +(escape_sequence) @escape + +(interpolation + "{" @punctuation.special + "}" @punctuation.special) @embedded + +[ + "-" + "-=" + "!=" + "*" + "**" + "**=" + "*=" + "/" + "//" + "//=" + "/=" + "&" + "%" + "%=" + "^" + "+" + "->" + "+=" + "<" + "<<" + "<=" + "<>" + "=" + ":=" + "==" + ">" + ">=" + ">>" + "|" + "~" + "and" + "in" + "is" + "not" + "or" +] @operator + +[ + "as" + "assert" + "async" + "await" + "break" + "class" + "continue" + "def" + "del" + "elif" + "else" + "except" + "exec" + "finally" + "for" + "from" + "global" + "if" + "import" + "lambda" + "nonlocal" + "pass" + "print" + "raise" + "return" + "try" + "while" + "with" + "yield" + "match" + "case" +] @keyword \ No newline at end of file diff --git a/crates/zed/src/languages/python/indents.scm b/crates/zed/src/languages/python/indents.scm new file mode 100644 index 0000000000000000000000000000000000000000..efc93466a7e39e2f3a0051376648ace62a33fbb0 --- /dev/null +++ b/crates/zed/src/languages/python/indents.scm @@ -0,0 +1,6 @@ +(class_definition body: (block)) @indent +(function_definition body: (block)) @indent + +(_ "[" "]" @end) @indent +(_ "{" "}" @end) @indent +(_ "(" ")" @end) @indent diff --git a/styles/src/themes/common/base16.ts b/styles/src/themes/common/base16.ts index 0bdf455be3a79b435d1dc2b5c2a9d5edcfbfefe2..52715bd5445306a0bf51b2f6b11a61e5f938a0e3 100644 --- a/styles/src/themes/common/base16.ts +++ b/styles/src/themes/common/base16.ts @@ -171,6 +171,10 @@ export function createTheme( color: sample(ramps.cyan, 0.5), weight: fontWeights.normal, }, + constructor: { + color: sample(ramps.blue, 0.5), + weight: fontWeights.normal, + }, variant: { color: sample(ramps.blue, 0.5), weight: fontWeights.normal,