From 87a6d4e73615e17559b99afa065799a38fe6c39e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Wed, 24 Feb 2021 16:07:39 +0900 Subject: [PATCH] minor: Simplify some code. --- helix-core/src/indent.rs | 2 +- helix-core/src/syntax.rs | 13 +++++-------- helix-lsp/src/client.rs | 26 ++++++++++++++++---------- helix-lsp/src/transport.rs | 7 ++++--- helix-term/src/commands.rs | 18 +++--------------- helix-view/src/document.rs | 6 ++++-- helix-view/src/view.rs | 11 +++++++++++ 7 files changed, 44 insertions(+), 39 deletions(-) diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index 68d5d7b0c..ec0460d54 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -293,7 +293,7 @@ where let language_config = crate::syntax::LOADER .language_config_for_scope("source.rust") .unwrap(); - let highlight_config = language_config.highlight_config(&[]).unwrap().unwrap(); + let highlight_config = language_config.highlight_config(&[]).unwrap(); let syntax = Syntax::new(&state.doc, highlight_config.clone()); let text = state.doc.slice(..); diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 32974e11f..0f6b7319e 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -27,12 +27,9 @@ pub struct LanguageConfiguration { } impl LanguageConfiguration { - pub fn highlight_config( - &self, - scopes: &[String], - ) -> Result>, anyhow::Error> { + pub fn highlight_config(&self, scopes: &[String]) -> Option> { self.highlight_config - .get_or_try_init(|| { + .get_or_init(|| { // let name = get_language_name(&self.language_id); let highlights_query = @@ -46,7 +43,7 @@ impl LanguageConfiguration { let locals_query = ""; if highlights_query.is_empty() { - Ok(None) + None } else { let language = get_language(self.language_id); let mut config = HighlightConfiguration::new( @@ -57,10 +54,10 @@ impl LanguageConfiguration { ) .unwrap(); // TODO: no unwrap config.configure(&scopes); - Ok(Some(Arc::new(config))) + Some(Arc::new(config)) } }) - .map(Option::as_ref) + .clone() } pub fn scope(&self) -> &str { diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index b8b2971c8..b229cd1a2 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -255,11 +255,6 @@ impl Client { .await } - // TODO: this is dumb. TextEdit describes changes to the initial doc (concurrent), but - // TextDocumentContentChangeEvent describes a series of changes (sequential). - // So S -> S1 -> S2, meaning positioning depends on the previous edits. - // - // Calculation is therefore a bunch trickier. pub fn changeset_to_changes( old_text: &Rope, new_text: &Rope, @@ -274,6 +269,12 @@ impl Client { use crate::util::pos_to_lsp_pos; use helix_core::Operation::*; + // this is dumb. TextEdit describes changes to the initial doc (concurrent), but + // TextDocumentContentChangeEvent describes a series of changes (sequential). + // So S -> S1 -> S2, meaning positioning depends on the previous edits. + // + // Calculation is therefore a bunch trickier. + // TODO: stolen from syntax.rs, share use helix_core::RopeSlice; fn traverse(pos: lsp::Position, text: RopeSlice) -> lsp::Position { @@ -397,8 +398,6 @@ impl Client { .await } - // TODO: impl into() TextDocumentIdentifier / VersionedTextDocumentIdentifier for Document. - pub async fn text_document_did_close( &self, text_document: lsp::TextDocumentIdentifier, @@ -411,15 +410,22 @@ impl Client { // will_save / will_save_wait_until - pub async fn text_document_did_save(&self) -> anyhow::Result<()> { - unimplemented!() + pub async fn text_document_did_save( + &self, + text_document: lsp::TextDocumentIdentifier, + ) -> Result<()> { + self.notify::(lsp::DidSaveTextDocumentParams { + text_document, + text: None, // TODO: + }) + .await } pub async fn completion( &self, text_document: lsp::TextDocumentIdentifier, position: lsp::Position, - ) -> anyhow::Result> { + ) -> Result> { // TODO: figure out what should happen when you complete with multiple cursors let params = lsp::CompletionParams { diff --git a/helix-lsp/src/transport.rs b/helix-lsp/src/transport.rs index 74ecde5e1..ff1eedafe 100644 --- a/helix-lsp/src/transport.rs +++ b/helix-lsp/src/transport.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::io; use log::{error, info}; @@ -128,7 +129,7 @@ impl Transport { Ok(()) } - pub async fn send_payload(&mut self, payload: Payload) -> anyhow::Result<()> { + pub async fn send_payload(&mut self, payload: Payload) -> io::Result<()> { match payload { Payload::Request { chan, value } => { self.pending_requests.insert(value.id.clone(), chan); @@ -147,7 +148,7 @@ impl Transport { } } - pub async fn send(&mut self, request: String) -> anyhow::Result<()> { + pub async fn send(&mut self, request: String) -> io::Result<()> { info!("-> {}", request); // send the headers @@ -174,7 +175,7 @@ impl Transport { Ok(()) } - async fn recv_response(&mut self, output: jsonrpc::Output) -> anyhow::Result<()> { + async fn recv_response(&mut self, output: jsonrpc::Output) -> io::Result<()> { let (id, result) = match output { jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => { info!("<- {}", result); diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index c28650072..c3ec49df4 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -201,18 +201,6 @@ pub fn extend_next_word_end(cx: &mut Context) { doc.set_selection(selection); } -pub fn check_cursor_in_view(view: &View) -> bool { - let doc = &view.doc; - let cursor = doc.selection().cursor(); - let line = doc.text().char_to_line(cursor); - let document_end = view.first_line + view.area.height.saturating_sub(1) as usize; - - if (line > document_end.saturating_sub(PADDING)) || (line < view.first_line + PADDING) { - return false; - } - true -} - pub fn page_up(cx: &mut Context) { let view = cx.view(); if view.first_line < PADDING { @@ -221,7 +209,7 @@ pub fn page_up(cx: &mut Context) { view.first_line = view.first_line.saturating_sub(view.area.height as usize); - if !check_cursor_in_view(view) { + if !view.check_cursor_in_view() { let text = view.doc.text(); let pos = text.line_to_char(view.last_line().saturating_sub(PADDING)); view.doc.set_selection(Selection::point(pos)); @@ -249,7 +237,7 @@ pub fn half_page_up(cx: &mut Context) { .first_line .saturating_sub(view.area.height as usize / 2); - if !check_cursor_in_view(view) { + if !view.check_cursor_in_view() { let text = &view.doc.text(); let pos = text.line_to_char(view.last_line() - PADDING); view.doc.set_selection(Selection::point(pos)); @@ -262,7 +250,7 @@ pub fn half_page_down(cx: &mut Context) { if view.first_line < lines.saturating_sub(view.area.height as usize) { view.first_line += view.area.height as usize / 2; } - if !check_cursor_in_view(view) { + if !view.check_cursor_in_view() { let text = view.doc.text(); let pos = text.line_to_char(view.first_line as usize); view.doc.set_selection(Selection::point(pos)); diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs index 569e72eee..3f3f620ff 100644 --- a/helix-view/src/document.rs +++ b/helix-view/src/document.rs @@ -140,10 +140,12 @@ impl Document { // TODO: this ties lsp support to tree-sitter enabled languages for now. Language // config should use Option to let us have non-tree-sitter configs. - let highlight_config = language_config.highlight_config(scopes).unwrap().unwrap(); + let highlight_config = language_config + .highlight_config(scopes) + .expect("No highlight_config found!"); // TODO: config.configure(scopes) is now delayed, is that ok? - let syntax = Syntax::new(&self.state.doc, highlight_config.clone()); + let syntax = Syntax::new(&self.state.doc, highlight_config); self.syntax = Some(syntax); } else { diff --git a/helix-view/src/view.rs b/helix-view/src/view.rs index e02436b3f..f3d92bfda 100644 --- a/helix-view/src/view.rs +++ b/helix-view/src/view.rs @@ -35,6 +35,17 @@ impl View { Ok(view) } + pub fn check_cursor_in_view(&self) -> bool { + let cursor = self.doc.selection().cursor(); + let line = self.doc.text().char_to_line(cursor); + let document_end = self.first_line + self.area.height.saturating_sub(1) as usize; + + if (line > document_end.saturating_sub(PADDING)) || (line < self.first_line + PADDING) { + return false; + } + true + } + pub fn ensure_cursor_in_view(&mut self) { let cursor = self.doc.state.selection().cursor(); let line = self.doc.text().char_to_line(cursor);