From a477733bcb7ab666c368c6cbd2e52195de1f057a Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Mon, 27 Jun 2022 15:48:15 -0700 Subject: [PATCH 1/2] Address hover panic and add hint to install go when language server failed to install --- crates/gpui/src/elements/text.rs | 11 ++++++++++- crates/project/src/lsp_command.rs | 11 +++++++---- crates/zed/src/languages/go.rs | 2 +- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/gpui/src/elements/text.rs b/crates/gpui/src/elements/text.rs index 94be18da4e..fb1e2ea331 100644 --- a/crates/gpui/src/elements/text.rs +++ b/crates/gpui/src/elements/text.rs @@ -10,6 +10,7 @@ use crate::{ DebugContext, Element, Event, EventContext, FontCache, LayoutContext, PaintContext, SizeConstraint, TextLayoutCache, }; +use log::warn; use serde_json::json; use std::{borrow::Cow, ops::Range, sync::Arc}; @@ -62,6 +63,7 @@ impl Element for Text { cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { // Convert the string and highlight ranges into an iterator of highlighted chunks. + let mut offset = 0; let mut highlight_ranges = self.highlights.iter().peekable(); let chunks = std::iter::from_fn(|| { @@ -70,10 +72,17 @@ impl Element for Text { if offset < range.start { result = Some((&self.text[offset..range.start], None)); offset = range.start; - } else { + } else if range.end <= self.text.len() { result = Some((&self.text[range.clone()], Some(*highlight_style))); highlight_ranges.next(); offset = range.end; + } else { + warn!( + "Highlight out of text range. Text len: {}, Highlight range: {}..{}", + self.text.len(), + range.start, + range.end); + result = None; } } else if offset < self.text.len() { result = Some((&self.text[offset..], None)); diff --git a/crates/project/src/lsp_command.rs b/crates/project/src/lsp_command.rs index 3af86db0fd..ee2bf37aa1 100644 --- a/crates/project/src/lsp_command.rs +++ b/crates/project/src/lsp_command.rs @@ -932,7 +932,9 @@ impl LspCommand for GetHover { } Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(new_language))) => { if !current_text.is_empty() { - let text = std::mem::replace(&mut current_text, String::new()); + let text = std::mem::replace(&mut current_text, String::new()) + .trim() + .to_string(); contents.push(HoverBlock { text, language }); } @@ -948,9 +950,10 @@ impl LspCommand for GetHover { | Event::End(Tag::BlockQuote) | Event::HardBreak => { if !current_text.is_empty() { - let text = std::mem::replace(&mut current_text, String::new()); + let text = std::mem::replace(&mut current_text, String::new()) + .trim() + .to_string(); contents.push(HoverBlock { text, language }); - current_text.clear(); } language = None; } @@ -958,7 +961,7 @@ impl LspCommand for GetHover { } } - if !current_text.is_empty() { + if !current_text.trim().is_empty() { contents.push(HoverBlock { text: current_text, language, diff --git a/crates/zed/src/languages/go.rs b/crates/zed/src/languages/go.rs index 9a8895266c..978af215c1 100644 --- a/crates/zed/src/languages/go.rs +++ b/crates/zed/src/languages/go.rs @@ -92,7 +92,7 @@ impl super::LspAdapter for GoLspAdapter { .output() .await?; if !install_output.status.success() { - Err(anyhow!("failed to install gopls"))?; + Err(anyhow!("failed to install gopls. Is go installed?"))?; } let installed_binary_path = gobin_dir.join("gopls"); From 1eb835144086f49c5010d43867d8026dbeda7113 Mon Sep 17 00:00:00 2001 From: Keith Simmons Date: Mon, 27 Jun 2022 16:21:22 -0700 Subject: [PATCH 2/2] add hover popover setting --- crates/editor/src/hover_popover.rs | 11 +++++++---- crates/settings/src/settings.rs | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/editor/src/hover_popover.rs b/crates/editor/src/hover_popover.rs index 31323d01d2..7ae9e01b09 100644 --- a/crates/editor/src/hover_popover.rs +++ b/crates/editor/src/hover_popover.rs @@ -7,6 +7,7 @@ use gpui::{ }; use language::Bias; use project::{HoverBlock, Project}; +use settings::Settings; use std::{ops::Range, time::Duration}; use util::TryFutureExt; @@ -40,10 +41,12 @@ pub fn hover(editor: &mut Editor, _: &Hover, cx: &mut ViewContext) { /// The internal hover action dispatches between `show_hover` or `hide_hover` /// depending on whether a point to hover over is provided. pub fn hover_at(editor: &mut Editor, action: &HoverAt, cx: &mut ViewContext) { - if let Some(point) = action.point { - show_hover(editor, point, false, cx); - } else { - hide_hover(editor, cx); + if cx.global::().hover_popover_enabled { + if let Some(point) = action.point { + show_hover(editor, point, false, cx); + } else { + hide_hover(editor, cx); + } } } diff --git a/crates/settings/src/settings.rs b/crates/settings/src/settings.rs index 75961bf2a1..1e84e5bec6 100644 --- a/crates/settings/src/settings.rs +++ b/crates/settings/src/settings.rs @@ -23,6 +23,7 @@ pub struct Settings { pub buffer_font_family: FamilyId, pub buffer_font_size: f32, pub default_buffer_font_size: f32, + pub hover_popover_enabled: bool, pub vim_mode: bool, pub language_settings: LanguageSettings, pub language_defaults: HashMap, LanguageSettings>, @@ -57,6 +58,8 @@ pub struct SettingsFileContent { #[serde(default)] pub buffer_font_size: Option, #[serde(default)] + pub hover_popover_enabled: Option, + #[serde(default)] pub vim_mode: Option, #[serde(default)] pub format_on_save: Option, @@ -80,6 +83,7 @@ impl Settings { buffer_font_family: font_cache.load_family(&[buffer_font_family])?, buffer_font_size: 15., default_buffer_font_size: 15., + hover_popover_enabled: true, vim_mode: false, language_settings: Default::default(), language_defaults: Default::default(), @@ -151,6 +155,7 @@ impl Settings { buffer_font_family: cx.font_cache().load_family(&["Monaco"]).unwrap(), buffer_font_size: 14., default_buffer_font_size: 14., + hover_popover_enabled: true, vim_mode: false, language_settings: Default::default(), language_defaults: Default::default(), @@ -191,6 +196,7 @@ impl Settings { ); merge(&mut self.buffer_font_size, data.buffer_font_size); merge(&mut self.default_buffer_font_size, data.buffer_font_size); + merge(&mut self.hover_popover_enabled, data.hover_popover_enabled); merge(&mut self.vim_mode, data.vim_mode); merge_option( &mut self.language_settings.format_on_save,