Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
keymap_ui: Create language for Zed keybind context (#34558)
Ben Kunkle58807f0dd2e5
diff --git a/crates/languages/src/lib.rs b/crates/languages/src/lib.rsindex 3db015a2418..431c0510818 100644--- a/crates/languages/src/lib.rs+++ b/crates/languages/src/lib.rs@@ -212,6 +212,10 @@ pub fn init(languages: Arc<LanguageRegistry>, node: NodeRuntime, cx: &mut App) {name: "gitcommit",..Default::default()},+ LanguageInfo {+ name: "zed-keybind-context",+ ..Default::default()+ },];for registration in built_in_languages {diff --git a/crates/languages/src/zed-keybind-context/brackets.scm b/crates/languages/src/zed-keybind-context/brackets.scmnew file mode 100644index 00000000000..d086b2e98df--- /dev/null+++ b/crates/languages/src/zed-keybind-context/brackets.scm@@ -0,0 +1 @@+("(" @open ")" @close)diff --git a/crates/languages/src/zed-keybind-context/config.toml b/crates/languages/src/zed-keybind-context/config.tomlnew file mode 100644index 00000000000..a999c70f667--- /dev/null+++ b/crates/languages/src/zed-keybind-context/config.toml@@ -0,0 +1,6 @@+name = "Zed Keybind Context"+grammar = "rust"+autoclose_before = ")"+brackets = [+ { start = "(", end = ")", close = true, newline = false },+]diff --git a/crates/languages/src/zed-keybind-context/highlights.scm b/crates/languages/src/zed-keybind-context/highlights.scmnew file mode 100644index 00000000000..9c5ec58eaeb--- /dev/null+++ b/crates/languages/src/zed-keybind-context/highlights.scm@@ -0,0 +1,23 @@+(identifier) @variable++[+ "("+ ")"+] @punctuation.bracket++[+ (integer_literal)+ (float_literal)+] @number++(boolean_literal) @boolean++[+ "!="+ "=="+ "=>"+ ">"+ "&&"+ "||"+ "!"+] @operatordiff --git a/crates/settings_ui/src/keybindings.rs b/crates/settings_ui/src/keybindings.rsindex c83a4c2423a..2bfa6f820e9 100644--- a/crates/settings_ui/src/keybindings.rs+++ b/crates/settings_ui/src/keybindings.rs@@ -505,7 +505,7 @@ impl KeymapEditor {fn process_bindings(json_language: Arc<Language>,- rust_language: Arc<Language>,+ zed_keybind_context_language: Arc<Language>,cx: &mut App,) -> (Vec<ProcessedKeybinding>, Vec<StringMatchCandidate>) {let key_bindings_ptr = cx.key_bindings();@@ -536,7 +536,10 @@ impl KeymapEditor {let context = key_binding.predicate().map(|predicate| {- KeybindContextString::Local(predicate.to_string().into(), rust_language.clone())+ KeybindContextString::Local(+ predicate.to_string().into(),+ zed_keybind_context_language.clone(),+ )}).unwrap_or(KeybindContextString::Global);@@ -588,11 +591,12 @@ impl KeymapEditor {let workspace = self.workspace.clone();cx.spawn(async move |this, cx| {let json_language = load_json_language(workspace.clone(), cx).await;- let rust_language = load_rust_language(workspace.clone(), cx).await;+ let zed_keybind_context_language =+ load_keybind_context_language(workspace.clone(), cx).await;let (action_query, keystroke_query) = this.update(cx, |this, cx| {let (key_bindings, string_match_candidates) =- Self::process_bindings(json_language, rust_language, cx);+ Self::process_bindings(json_language, zed_keybind_context_language, cx);this.keybinding_conflict_state = ConflictState::new(&key_bindings);@@ -1590,13 +1594,20 @@ impl KeybindingEditorModal {}let editor_entity = input.editor().clone();+ let workspace = workspace.clone();cx.spawn(async move |_input_handle, cx| {let contexts = cx.background_spawn(async { collect_contexts_from_assets() }).await;+ let language = load_keybind_context_language(workspace, cx).await;editor_entity- .update(cx, |editor, _cx| {+ .update(cx, |editor, cx| {+ if let Some(buffer) = editor.buffer().read(cx).as_singleton() {+ buffer.update(cx, |buffer, cx| {+ buffer.set_language(Some(language), cx);+ });+ }editor.set_completion_provider(Some(std::rc::Rc::new(KeyContextCompletionProvider { contexts },)));@@ -2131,25 +2142,31 @@ async fn load_json_language(workspace: WeakEntity<Workspace>, cx: &mut AsyncApp)});}-async fn load_rust_language(workspace: WeakEntity<Workspace>, cx: &mut AsyncApp) -> Arc<Language> {- let rust_language_task = workspace+async fn load_keybind_context_language(+ workspace: WeakEntity<Workspace>,+ cx: &mut AsyncApp,+) -> Arc<Language> {+ let language_task = workspace.read_with(cx, |workspace, cx| {workspace.project().read(cx).languages()- .language_for_name("Rust")+ .language_for_name("Zed Keybind Context")})- .context("Failed to load Rust language")+ .context("Failed to load Zed Keybind Context language").log_err();- let rust_language = match rust_language_task {- Some(task) => task.await.context("Failed to load Rust language").log_err(),+ let language = match language_task {+ Some(task) => task+ .await+ .context("Failed to load Zed Keybind Context language")+ .log_err(),None => None,};- return rust_language.unwrap_or_else(|| {+ return language.unwrap_or_else(|| {Arc::new(Language::new(LanguageConfig {- name: "Rust".into(),+ name: "Zed Keybind Context".into(),..Default::default()},Some(tree_sitter_rust::LANGUAGE.into()),diff --git a/crates/ui_input/src/ui_input.rs b/crates/ui_input/src/ui_input.rsindex ca2dea36df0..18aa732e815 100644--- a/crates/ui_input/src/ui_input.rs+++ b/crates/ui_input/src/ui_input.rs@@ -135,6 +135,7 @@ impl Render for SingleLineInput {let editor_style = EditorStyle {background: theme_color.ghost_element_background,local_player: cx.theme().players().local(),+ syntax: cx.theme().syntax().clone(),text: text_style,..Default::default()};