on master help sign in
minor: Simplify some code.
commit87a6d4e73615e17559b99afa065799a38fe6c39e authorBlaž Hrastnik <blaz@mxxn.io> dateWed 24 Feb 2021, 16:07 (6 years ago) parentc6456d0
7 files changed+44−39
helix-core/src/indent.rs+1−1 helix-core/src/syntax.rs+5−8 helix-lsp/src/client.rs+16−10 helix-lsp/src/transport.rs+4−3 helix-term/src/commands.rs+3−15 helix-view/src/document.rs+4−2 helix-view/src/view.rs+11
helix-core/src/indent.rs+1−1
@@ -293,7 +293,7 @@ where
293293 let language_config = crate::syntax::LOADER
294294 .language_config_for_scope("source.rust")
295295 .unwrap();
296− let highlight_config = language_config.highlight_config(&[]).unwrap().unwrap();
296+ let highlight_config = language_config.highlight_config(&[]).unwrap();
297297 let syntax = Syntax::new(&state.doc, highlight_config.clone());
298298 let text = state.doc.slice(..);
299299
helix-core/src/syntax.rs+5−8
@@ -27,12 +27,9 @@ pub struct LanguageConfiguration {
2727}
2828
2929impl LanguageConfiguration {
30− pub fn highlight_config(
31− &self,
32− scopes: &[String],
33− ) -> Result<Option<&Arc<HighlightConfiguration>>, anyhow::Error> {
30+ pub fn highlight_config(&self, scopes: &[String]) -> Option<Arc<HighlightConfiguration>> {
3431 self.highlight_config
35− .get_or_try_init(|| {
32+ .get_or_init(|| {
3633 // let name = get_language_name(&self.language_id);
3734
3835 let highlights_query =
@@ -46,7 +43,7 @@ impl LanguageConfiguration {
4643 let locals_query = "";
4744
4845 if highlights_query.is_empty() {
49− Ok(None)
46+ None
5047 } else {
5148 let language = get_language(self.language_id);
5249 let mut config = HighlightConfiguration::new(
@@ -57,10 +54,10 @@ impl LanguageConfiguration {
5754 )
5855 .unwrap(); // TODO: no unwrap
5956 config.configure(&scopes);
60− Ok(Some(Arc::new(config)))
57+ Some(Arc::new(config))
6158 }
6259 })
63− .map(Option::as_ref)
60+ .clone()
6461 }
6562
6663 pub fn scope(&self) -> &str {
helix-lsp/src/client.rs+16−10
@@ -255,11 +255,6 @@ impl Client {
255255 .await
256256 }
257257
258− // TODO: this is dumb. TextEdit describes changes to the initial doc (concurrent), but
259− // TextDocumentContentChangeEvent describes a series of changes (sequential).
260− // So S -> S1 -> S2, meaning positioning depends on the previous edits.
261− //
262− // Calculation is therefore a bunch trickier.
263258 pub fn changeset_to_changes(
264259 old_text: &Rope,
265260 new_text: &Rope,
@@ -274,6 +269,12 @@ impl Client {
274269 use crate::util::pos_to_lsp_pos;
275270 use helix_core::Operation::*;
276271
272+ // this is dumb. TextEdit describes changes to the initial doc (concurrent), but
273+ // TextDocumentContentChangeEvent describes a series of changes (sequential).
274+ // So S -> S1 -> S2, meaning positioning depends on the previous edits.
275+ //
276+ // Calculation is therefore a bunch trickier.
277+
277278 // TODO: stolen from syntax.rs, share
278279 use helix_core::RopeSlice;
279280 fn traverse(pos: lsp::Position, text: RopeSlice) -> lsp::Position {
@@ -397,8 +398,6 @@ impl Client {
397398 .await
398399 }
399400
400− // TODO: impl into() TextDocumentIdentifier / VersionedTextDocumentIdentifier for Document.
401−
402401 pub async fn text_document_did_close(
403402 &self,
404403 text_document: lsp::TextDocumentIdentifier,
@@ -411,15 +410,22 @@ impl Client {
411410
412411 // will_save / will_save_wait_until
413412
414− pub async fn text_document_did_save(&self) -> anyhow::Result<()> {
415− unimplemented!()
413+ pub async fn text_document_did_save(
414+ &self,
415+ text_document: lsp::TextDocumentIdentifier,
416+ ) -> Result<()> {
417+ self.notify::<lsp::notification::DidSaveTextDocument>(lsp::DidSaveTextDocumentParams {
418+ text_document,
419+ text: None, // TODO:
420+ })
421+ .await
416422 }
417423
418424 pub async fn completion(
419425 &self,
420426 text_document: lsp::TextDocumentIdentifier,
421427 position: lsp::Position,
422− ) -> anyhow::Result<Vec<lsp::CompletionItem>> {
428+ ) -> Result<Vec<lsp::CompletionItem>> {
423429 // TODO: figure out what should happen when you complete with multiple cursors
424430
425431 let params = lsp::CompletionParams {
helix-lsp/src/transport.rs+4−3
@@ -1,4 +1,5 @@
11use std::collections::HashMap;
2+use std::io;
23
34use log::{error, info};
45
@@ -128,7 +129,7 @@ impl Transport {
128129 Ok(())
129130 }
130131
131− pub async fn send_payload(&mut self, payload: Payload) -> anyhow::Result<()> {
132+ pub async fn send_payload(&mut self, payload: Payload) -> io::Result<()> {
132133 match payload {
133134 Payload::Request { chan, value } => {
134135 self.pending_requests.insert(value.id.clone(), chan);
@@ -147,7 +148,7 @@ impl Transport {
147148 }
148149 }
149150
150− pub async fn send(&mut self, request: String) -> anyhow::Result<()> {
151+ pub async fn send(&mut self, request: String) -> io::Result<()> {
151152 info!("-> {}", request);
152153
153154 // send the headers
@@ -174,7 +175,7 @@ impl Transport {
174175 Ok(())
175176 }
176177
177− async fn recv_response(&mut self, output: jsonrpc::Output) -> anyhow::Result<()> {
178+ async fn recv_response(&mut self, output: jsonrpc::Output) -> io::Result<()> {
178179 let (id, result) = match output {
179180 jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => {
180181 info!("<- {}", result);
helix-term/src/commands.rs+3−15
@@ -201,18 +201,6 @@ pub fn extend_next_word_end(cx: &mut Context) {
201201 doc.set_selection(selection);
202202}
203203
204−pub fn check_cursor_in_view(view: &View) -> bool {
205− let doc = &view.doc;
206− let cursor = doc.selection().cursor();
207− let line = doc.text().char_to_line(cursor);
208− let document_end = view.first_line + view.area.height.saturating_sub(1) as usize;
209−
210− if (line > document_end.saturating_sub(PADDING)) || (line < view.first_line + PADDING) {
211− return false;
212− }
213− true
214−}
215−
216204pub fn page_up(cx: &mut Context) {
217205 let view = cx.view();
218206 if view.first_line < PADDING {
@@ -221,7 +209,7 @@ pub fn page_up(cx: &mut Context) {
221209
222210 view.first_line = view.first_line.saturating_sub(view.area.height as usize);
223211
224− if !check_cursor_in_view(view) {
212+ if !view.check_cursor_in_view() {
225213 let text = view.doc.text();
226214 let pos = text.line_to_char(view.last_line().saturating_sub(PADDING));
227215 view.doc.set_selection(Selection::point(pos));
@@ -249,7 +237,7 @@ pub fn half_page_up(cx: &mut Context) {
249237 .first_line
250238 .saturating_sub(view.area.height as usize / 2);
251239
252− if !check_cursor_in_view(view) {
240+ if !view.check_cursor_in_view() {
253241 let text = &view.doc.text();
254242 let pos = text.line_to_char(view.last_line() - PADDING);
255243 view.doc.set_selection(Selection::point(pos));
@@ -262,7 +250,7 @@ pub fn half_page_down(cx: &mut Context) {
262250 if view.first_line < lines.saturating_sub(view.area.height as usize) {
263251 view.first_line += view.area.height as usize / 2;
264252 }
265− if !check_cursor_in_view(view) {
253+ if !view.check_cursor_in_view() {
266254 let text = view.doc.text();
267255 let pos = text.line_to_char(view.first_line as usize);
268256 view.doc.set_selection(Selection::point(pos));
helix-view/src/document.rs+4−2
@@ -140,10 +140,12 @@ impl Document {
140140 // TODO: this ties lsp support to tree-sitter enabled languages for now. Language
141141 // config should use Option<HighlightConfig> to let us have non-tree-sitter configs.
142142
143− let highlight_config = language_config.highlight_config(scopes).unwrap().unwrap();
143+ let highlight_config = language_config
144+ .highlight_config(scopes)
145+ .expect("No highlight_config found!");
144146 // TODO: config.configure(scopes) is now delayed, is that ok?
145147
146− let syntax = Syntax::new(&self.state.doc, highlight_config.clone());
148+ let syntax = Syntax::new(&self.state.doc, highlight_config);
147149
148150 self.syntax = Some(syntax);
149151 } else {
helix-view/src/view.rs+11
@@ -35,6 +35,17 @@ impl View {
3535 Ok(view)
3636 }
3737
38+ pub fn check_cursor_in_view(&self) -> bool {
39+ let cursor = self.doc.selection().cursor();
40+ let line = self.doc.text().char_to_line(cursor);
41+ let document_end = self.first_line + self.area.height.saturating_sub(1) as usize;
42+
43+ if (line > document_end.saturating_sub(PADDING)) || (line < self.first_line + PADDING) {
44+ return false;
45+ }
46+ true
47+ }
48+
3849 pub fn ensure_cursor_in_view(&mut self) {
3950 let cursor = self.doc.state.selection().cursor();
4051 let line = self.doc.text().char_to_line(cursor);