Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

languages: Improve semantic token highlighting for parameters and Python (#52130)

Xin Zhao23e1bcf59ddd
diff --git a/assets/settings/default_semantic_token_rules.json b/assets/settings/default_semantic_token_rules.json
index 65b20a7423a..c070a253d30 100644
--- a/assets/settings/default_semantic_token_rules.json
+++ b/assets/settings/default_semantic_token_rules.json
@@ -119,6 +119,16 @@
"style": ["type"],
},
// References
+ {
+ "token_type": "parameter",
+ "token_modifiers": ["declaration"],
+ "style": ["variable.parameter"]
+ },
+ {
+ "token_type": "parameter",
+ "token_modifiers": ["definition"],
+ "style": ["variable.parameter"]
+ },
{
"token_type": "parameter",
"token_modifiers": [],
@@ -201,6 +211,11 @@
"token_modifiers": [],
"style": ["comment"],
},
+ {
+ "token_type": "string",
+ "token_modifiers": ["documentation"],
+ "style": ["string.doc"],
+ },
{
"token_type": "string",
"token_modifiers": [],
diff --git a/crates/languages/src/lib.rs b/crates/languages/src/lib.rs
index 240935d2f81..53aa24e9e87 100644
--- a/crates/languages/src/lib.rs
+++ b/crates/languages/src/lib.rs
@@ -191,7 +191,7 @@ pub fn init(languages: Arc<LanguageRegistry>, fs: Arc<dyn Fs>, node: NodeRuntime
context: Some(python_context_provider),
toolchain: Some(python_toolchain_provider),
manifest_name: Some(SharedString::new_static("pyproject.toml").into()),
- ..Default::default()
+ semantic_token_rules: Some(python::semantic_token_rules()),
},
LanguageInfo {
name: "rust",
diff --git a/crates/languages/src/python.rs b/crates/languages/src/python.rs
index 99d9788ddb5..ef750f5ce6e 100644
--- a/crates/languages/src/python.rs
+++ b/crates/languages/src/python.rs
@@ -24,7 +24,7 @@ use project::lsp_store::language_server_settings;
use semver::Version;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
-use settings::Settings;
+use settings::{SemanticTokenRules, Settings};
use terminal::terminal_settings::TerminalSettings;
use smol::lock::OnceCell;
@@ -37,6 +37,7 @@ use util::fs::{make_file_executable, remove_matching};
use util::paths::PathStyle;
use util::rel_path::RelPath;
+use crate::LanguageDir;
use http_client::github_download::{GithubBinaryMetadata, download_server_binary};
use parking_lot::Mutex;
use std::str::FromStr;
@@ -49,6 +50,14 @@ use std::{
use task::{ShellKind, TaskTemplate, TaskTemplates, VariableName};
use util::{ResultExt, maybe};
+pub(crate) fn semantic_token_rules() -> SemanticTokenRules {
+ let content = LanguageDir::get("python/semantic_token_rules.json")
+ .expect("missing python/semantic_token_rules.json");
+ let json = std::str::from_utf8(&content.data).expect("invalid utf-8 in semantic_token_rules");
+ settings::parse_json_with_comments::<SemanticTokenRules>(json)
+ .expect("failed to parse python semantic_token_rules.json")
+}
+
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct PythonToolchainData {
#[serde(flatten)]
diff --git a/crates/languages/src/python/semantic_token_rules.json b/crates/languages/src/python/semantic_token_rules.json
new file mode 100644
index 00000000000..b73bae962fe
--- /dev/null
+++ b/crates/languages/src/python/semantic_token_rules.json
@@ -0,0 +1,15 @@
+[
+ {
+ "token_type": "selfParameter",
+ "style": ["variable.special"]
+ },
+ {
+ "token_type": "clsParameter",
+ "style": ["variable.special"]
+ },
+ // ty specific
+ {
+ "token_type": "builtinConstant",
+ "style": ["constant.builtin"]
+ }
+]