From 0aa43902cab5dbcddb72ddf5d3b825ef874dc620 Mon Sep 17 00:00:00 2001 From: Cor Peters Date: Sun, 18 Jul 2021 09:56:25 +0200 Subject: [PATCH 1/4] Added option to provide a custom config file to the lsp. (#460) * Added option to provide a custom config file to the lsp. * Simplified lsp loading routine with anyhow * Moved config to language.toml * Fixed test case * Cargo fmt * Revert now-useless changes * Renamed custom_config to config Co-authored-by: Cor --- helix-core/src/indent.rs | 1 + helix-core/src/syntax.rs | 1 + helix-lsp/src/client.rs | 9 ++++++++- helix-lsp/src/lib.rs | 7 ++++++- languages.toml | 10 ++++++++++ 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index 1b36db7b1..292ade4b5 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -262,6 +262,7 @@ where file_types: vec!["rs".to_string()], language_id: "Rust".to_string(), highlight_config: OnceCell::new(), + config: None, // roots: vec![], auto_format: false, diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index f249f5fe7..621cd0469 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -35,6 +35,7 @@ pub struct LanguageConfiguration { pub scope: String, // source.rust pub file_types: Vec, // filename ends_with? pub roots: Vec, // these indicate project roots <.git, Cargo.toml> + pub config: Option, #[serde(default)] pub auto_format: bool, diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index 7f136fe84..1c2a49b52 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -24,12 +24,14 @@ pub struct Client { request_counter: AtomicU64, capabilities: Option, offset_encoding: OffsetEncoding, + config: Option, } impl Client { pub fn start( cmd: &str, args: &[String], + config: Option, id: usize, ) -> Result<(Self, UnboundedReceiver<(usize, Call)>)> { let process = Command::new(cmd) @@ -57,6 +59,7 @@ impl Client { request_counter: AtomicU64::new(0), capabilities: None, offset_encoding: OffsetEncoding::Utf8, + config, }; // TODO: async client.initialize() @@ -214,13 +217,17 @@ impl Client { // TODO: delay any requests that are triggered prior to initialize let root = find_root(None).and_then(|root| lsp::Url::from_file_path(root).ok()); + if self.config.is_some() { + log::info!("Using custom LSP config: {}", self.config.as_ref().unwrap()); + } + #[allow(deprecated)] let params = lsp::InitializeParams { process_id: Some(std::process::id()), // root_path is obsolete, use root_uri root_path: None, root_uri: root, - initialization_options: None, + initialization_options: self.config.clone(), capabilities: lsp::ClientCapabilities { text_document: Some(lsp::TextDocumentClientCapabilities { completion: Some(lsp::CompletionClientCapabilities { diff --git a/helix-lsp/src/lib.rs b/helix-lsp/src/lib.rs index 96a45bb90..72606b709 100644 --- a/helix-lsp/src/lib.rs +++ b/helix-lsp/src/lib.rs @@ -312,7 +312,12 @@ impl Registry { Entry::Vacant(entry) => { // initialize a new client let id = self.counter.fetch_add(1, Ordering::Relaxed); - let (mut client, incoming) = Client::start(&config.command, &config.args, id)?; + let (mut client, incoming) = Client::start( + &config.command, + &config.args, + serde_json::from_str(language_config.config.as_deref().unwrap_or("")).ok(), + id, + )?; // TODO: run this async without blocking futures_executor::block_on(client.initialize())?; s_incoming.push(UnboundedReceiverStream::new(incoming)); diff --git a/languages.toml b/languages.toml index 204a59878..ee495635f 100644 --- a/languages.toml +++ b/languages.toml @@ -5,6 +5,16 @@ injection-regex = "rust" file-types = ["rs"] roots = [] auto-format = true +config = """ +{ + "cargo": { + "loadOutDirsFromCheck": true + }, + "procMacro": { + "enable": false + } +} +""" language-server = { command = "rust-analyzer" } indent = { tab-width = 4, unit = " " } From cd65a48635d49ccc2aed55a315c3a33875e13f88 Mon Sep 17 00:00:00 2001 From: Cor Peters Date: Sun, 18 Jul 2021 18:33:38 +0200 Subject: [PATCH 2/4] Made toggle_comments language dependent (#463) * Made toggle_comments language dependent * Fixed Test Cases * Added clippy suggestion * Small Fixes * Clippy Suggestion Co-authored-by: Cor --- helix-core/src/comment.rs | 10 +++++----- helix-core/src/indent.rs | 1 + helix-core/src/syntax.rs | 1 + helix-term/src/commands.rs | 6 +++++- languages.toml | 12 ++++++++++++ 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs index 07f685d83..5d564055d 100644 --- a/helix-core/src/comment.rs +++ b/helix-core/src/comment.rs @@ -38,18 +38,18 @@ fn find_line_comment( } #[must_use] -pub fn toggle_line_comments(doc: &Rope, selection: &Selection) -> Transaction { +pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&str>) -> Transaction { let text = doc.slice(..); let mut changes: Vec = Vec::new(); - let token = "//"; + let token = token.unwrap_or("//"); let comment = Tendril::from(format!("{} ", token)); for selection in selection { let start = text.char_to_line(selection.from()); let end = text.char_to_line(selection.to()); let lines = start..end + 1; - let (commented, skipped, min) = find_line_comment(token, text, lines.clone()); + let (commented, skipped, min) = find_line_comment(&token, text, lines.clone()); changes.reserve((end - start).saturating_sub(skipped.len())); @@ -95,14 +95,14 @@ mod test { assert_eq!(res, (false, vec![1], 2)); // comment - let transaction = toggle_line_comments(&state.doc, &state.selection); + let transaction = toggle_line_comments(&state.doc, &state.selection, None); transaction.apply(&mut state.doc); state.selection = state.selection.clone().map(transaction.changes()); assert_eq!(state.doc, " // 1\n\n // 2\n // 3"); // uncomment - let transaction = toggle_line_comments(&state.doc, &state.selection); + let transaction = toggle_line_comments(&state.doc, &state.selection, None); transaction.apply(&mut state.doc); state.selection = state.selection.clone().map(transaction.changes()); assert_eq!(state.doc, " 1\n\n 2\n 3"); diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index 292ade4b5..0ca05fb3c 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -265,6 +265,7 @@ where config: None, // roots: vec![], + comment_token: None, auto_format: false, language_server: None, indent: Some(IndentationConfiguration { diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 621cd0469..9acf3d879 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -35,6 +35,7 @@ pub struct LanguageConfiguration { pub scope: String, // source.rust pub file_types: Vec, // filename ends_with? pub roots: Vec, // these indicate project roots <.git, Cargo.toml> + pub comment_token: Option, pub config: Option, #[serde(default)] diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 7e769f4e1..5fc96cd92 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -3399,7 +3399,11 @@ fn hover(cx: &mut Context) { // comments fn toggle_comments(cx: &mut Context) { let (view, doc) = current!(cx.editor); - let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id)); + let token = doc + .language_config() + .and_then(|lc| lc.comment_token.as_ref()) + .map(|tc| tc.as_ref()); + let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id), token); doc.apply(&transaction, view.id); doc.append_changes_to_history(view.id); diff --git a/languages.toml b/languages.toml index ee495635f..05268ddd0 100644 --- a/languages.toml +++ b/languages.toml @@ -5,6 +5,7 @@ injection-regex = "rust" file-types = ["rs"] roots = [] auto-format = true +comment_token = "//" config = """ { "cargo": { @@ -25,6 +26,7 @@ scope = "source.toml" injection-regex = "toml" file-types = ["toml"] roots = [] +comment_token = "#" indent = { tab-width = 2, unit = " " } @@ -52,6 +54,7 @@ scope = "source.c" injection-regex = "c" file-types = ["c"] # TODO: ["h"] roots = [] +comment_token = "//" language-server = { command = "clangd" } indent = { tab-width = 2, unit = " " } @@ -62,6 +65,7 @@ scope = "source.cpp" injection-regex = "cpp" file-types = ["cc", "cpp", "hpp", "h"] roots = [] +comment_token = "//" language-server = { command = "clangd" } indent = { tab-width = 2, unit = " " } @@ -73,6 +77,7 @@ injection-regex = "go" file-types = ["go"] roots = ["Gopkg.toml", "go.mod"] auto-format = true +comment_token = "//" language-server = { command = "gopls" } # TODO: gopls needs utf-8 offsets? @@ -84,6 +89,7 @@ scope = "source.js" injection-regex = "^(js|javascript)$" file-types = ["js"] roots = [] +comment_token = "//" # TODO: highlights-jsx, highlights-params indent = { tab-width = 2, unit = " " } @@ -123,6 +129,7 @@ scope = "source.python" injection-regex = "python" file-types = ["py"] roots = [] +comment_token = "#" language-server = { command = "pyls" } # TODO: pyls needs utf-8 offsets @@ -143,6 +150,7 @@ scope = "source.ruby" injection-regex = "ruby" file-types = ["rb"] roots = [] +comment_token = "#" language-server = { command = "solargraph", args = ["stdio"] } indent = { tab-width = 2, unit = " " } @@ -153,6 +161,7 @@ scope = "source.bash" injection-regex = "bash" file-types = ["sh", "bash"] roots = [] +comment_token = "#" language-server = { command = "bash-language-server", args = ["start"] } indent = { tab-width = 2, unit = " " } @@ -172,6 +181,7 @@ scope = "source.tex" injection-regex = "tex" file-types = ["tex"] roots = [] +comment_token = "%" indent = { tab-width = 4, unit = "\t" } @@ -181,6 +191,7 @@ scope = "source.julia" injection-regex = "julia" file-types = ["jl"] roots = [] +comment_token = "#" language-server = { command = "julia", args = [ "--startup-file=no", "--history-file=no", "-e", "using LanguageServer;using Pkg;import StaticLint;import SymbolServer;env_path = dirname(Pkg.Types.Context().env.project_file);server = LanguageServer.LanguageServerInstance(stdin, stdout, env_path, \"\");server.runlinter = true;run(server);" ] } indent = { tab-width = 2, unit = " " } @@ -190,5 +201,6 @@ indent = { tab-width = 2, unit = " " } # injection-regex = "haskell" # file-types = ["hs"] # roots = [] +# comment_token = "--" # # indent = { tab-width = 2, unit = " " } From bf43fabf65fe4062f582e59fe21e92a0b0f48cb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 19 Jul 2021 11:19:05 +0900 Subject: [PATCH 3/4] Remove ExactSizeIterator requirement on Transaction::change Size hint is enough. --- helix-core/src/diff.rs | 56 +++++++++++++++++------------------ helix-core/src/transaction.rs | 6 ++-- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/helix-core/src/diff.rs b/helix-core/src/diff.rs index 9c1fc999b..a83db3338 100644 --- a/helix-core/src/diff.rs +++ b/helix-core/src/diff.rs @@ -1,6 +1,4 @@ -use ropey::Rope; - -use crate::{Change, Transaction}; +use crate::{Rope, Transaction}; /// Compares `old` and `new` to generate a [`Transaction`] describing /// the steps required to get from `old` to `new`. @@ -25,34 +23,34 @@ pub fn compare_ropes(old: &Rope, new: &Rope) -> Transaction { // The current position of the change needs to be tracked to // construct the `Change`s. let mut pos = 0; - let changes: Vec = diff - .ops() - .iter() - .map(|op| op.as_tag_tuple()) - .filter_map(|(tag, old_range, new_range)| { - // `old_pos..pos` is equivalent to `start..end` for where - // the change should be applied. - let old_pos = pos; - pos += old_range.end - old_range.start; + Transaction::change( + old, + diff.ops() + .iter() + .map(|op| op.as_tag_tuple()) + .filter_map(|(tag, old_range, new_range)| { + // `old_pos..pos` is equivalent to `start..end` for where + // the change should be applied. + let old_pos = pos; + pos += old_range.end - old_range.start; - match tag { - // Semantically, inserts and replacements are the same thing. - similar::DiffTag::Insert | similar::DiffTag::Replace => { - // This is the text from the `new` rope that should be - // inserted into `old`. - let text: &str = { - let start = new.char_to_byte(new_range.start); - let end = new.char_to_byte(new_range.end); - &new_converted[start..end] - }; - Some((old_pos, pos, Some(text.into()))) + match tag { + // Semantically, inserts and replacements are the same thing. + similar::DiffTag::Insert | similar::DiffTag::Replace => { + // This is the text from the `new` rope that should be + // inserted into `old`. + let text: &str = { + let start = new.char_to_byte(new_range.start); + let end = new.char_to_byte(new_range.end); + &new_converted[start..end] + }; + Some((old_pos, pos, Some(text.into()))) + } + similar::DiffTag::Delete => Some((old_pos, pos, None)), + similar::DiffTag::Equal => None, } - similar::DiffTag::Delete => Some((old_pos, pos, None)), - similar::DiffTag::Equal => None, - } - }) - .collect(); - Transaction::change(old, changes.into_iter()) + }), + ) } #[cfg(test)] diff --git a/helix-core/src/transaction.rs b/helix-core/src/transaction.rs index 048839b3d..e20e550fa 100644 --- a/helix-core/src/transaction.rs +++ b/helix-core/src/transaction.rs @@ -473,11 +473,13 @@ impl Transaction { /// Generate a transaction from a set of changes. pub fn change(doc: &Rope, changes: I) -> Self where - I: IntoIterator + ExactSizeIterator, + I: IntoIterator + Iterator, { let len = doc.len_chars(); - let mut changeset = ChangeSet::with_capacity(2 * changes.len() + 1); // rough estimate + let (lower, upper) = changes.size_hint(); + let size = upper.unwrap_or(lower); + let mut changeset = ChangeSet::with_capacity(2 * size + 1); // rough estimate // TODO: verify ranges are ordered and not overlapping or change will panic. From 5292fe0f7df9f1a420744007aa9dd67e7a5a6610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Mon, 19 Jul 2021 11:28:29 +0900 Subject: [PATCH 4/4] Calculate completion popup sizing Fixes #220 --- helix-term/src/ui/menu.rs | 34 +++++++++++++++++++++++++++++++--- helix-tui/src/widgets/table.rs | 4 ++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs index 226c1135d..8681e5b1c 100644 --- a/helix-term/src/ui/menu.rs +++ b/helix-term/src/ui/menu.rs @@ -8,6 +8,7 @@ use fuzzy_matcher::skim::SkimMatcherV2 as Matcher; use fuzzy_matcher::FuzzyMatcher; use helix_view::{graphics::Rect, Editor}; +use tui::layout::Constraint; pub trait Item { // TODO: sort_text @@ -26,6 +27,8 @@ pub struct Menu { /// (index, score) matches: Vec<(usize, i64)>, + widths: Vec, + callback_fn: Box, MenuEvent)>, scroll: usize, @@ -44,6 +47,7 @@ impl Menu { matcher: Box::new(Matcher::default()), matches: Vec::new(), cursor: None, + widths: Vec::new(), callback_fn: Box::new(callback_fn), scroll: 0, size: (0, 0), @@ -218,8 +222,33 @@ impl Component for Menu { EventResult::Ignored } + // TODO: completion sorting + fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { - let width = std::cmp::min(30, viewport.0); + let n = self + .options + .first() + .map(|option| option.row().cells.len()) + .unwrap_or_default(); + let max_lens = self.options.iter().fold(vec![0; n], |mut acc, option| { + let row = option.row(); + // maintain max for each column + for (i, cell) in row.cells.iter().enumerate() { + let width = cell.content.width(); + if width > acc[i] { + acc[i] = width; + } + } + + acc + }); + let len = (max_lens.iter().sum::()) + n + 1; // +1: reserve some space for scrollbar + let width = len.min(viewport.0 as usize); + + self.widths = max_lens + .into_iter() + .map(|len| Constraint::Length(len as u16)) + .collect(); const MAX: usize = 10; let height = std::cmp::min(self.options.len(), MAX); @@ -263,13 +292,12 @@ impl Component for Menu { let scroll_line = (win_height - scroll_height) * scroll / std::cmp::max(1, len.saturating_sub(win_height)); - use tui::layout::Constraint; let rows = options.iter().map(|option| option.row()); let table = Table::new(rows) .style(style) .highlight_style(selected) .column_spacing(1) - .widths(&[Constraint::Percentage(50), Constraint::Percentage(50)]); + .widths(&self.widths); use tui::widgets::TableState; diff --git a/helix-tui/src/widgets/table.rs b/helix-tui/src/widgets/table.rs index 1ee4286a8..d7caa0b0a 100644 --- a/helix-tui/src/widgets/table.rs +++ b/helix-tui/src/widgets/table.rs @@ -36,7 +36,7 @@ use std::collections::HashMap; /// capabilities of [`Text`]. #[derive(Debug, Clone, PartialEq, Default)] pub struct Cell<'a> { - content: Text<'a>, + pub content: Text<'a>, style: Style, } @@ -81,7 +81,7 @@ where /// By default, a row has a height of 1 but you can change this using [`Row::height`]. #[derive(Debug, Clone, PartialEq, Default)] pub struct Row<'a> { - cells: Vec>, + pub cells: Vec>, height: u16, style: Style, bottom_margin: u16,