on master help sign in
minor: Simplify some code.
87a6d4eBlaž Hrastnik
helix-core/src/indent.rs+1−1
@@ -293,7 +293,7 @@ where
293 let language_config = crate::syntax::LOADER
293 let language_config = crate::syntax::LOADER
294 .language_config_for_scope("source.rust")
294 .language_config_for_scope("source.rust")
295 .unwrap();
295 .unwrap();
296− let highlight_config = language_config.highlight_config(&[]).unwrap().unwrap();
296+ let highlight_config = language_config.highlight_config(&[]).unwrap();
297 let syntax = Syntax::new(&state.doc, highlight_config.clone());
297 let syntax = Syntax::new(&state.doc, highlight_config.clone());
298 let text = state.doc.slice(..);
298 let text = state.doc.slice(..);
299
299
helix-core/src/syntax.rs+5−8
@@ -27,12 +27,9 @@ pub struct LanguageConfiguration {
27}
27}
28
28
29impl LanguageConfiguration {
29impl 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>> {
34 self.highlight_config
31 self.highlight_config
35− .get_or_try_init(|| {
32+ .get_or_init(|| {
36 // let name = get_language_name(&self.language_id);
33 // let name = get_language_name(&self.language_id);
37
34
38 let highlights_query =
35 let highlights_query =
@@ -46,7 +43,7 @@ impl LanguageConfiguration {
46 let locals_query = "";
43 let locals_query = "";
47
44
48 if highlights_query.is_empty() {
45 if highlights_query.is_empty() {
49− Ok(None)
46+ None
50 } else {
47 } else {
51 let language = get_language(self.language_id);
48 let language = get_language(self.language_id);
52 let mut config = HighlightConfiguration::new(
49 let mut config = HighlightConfiguration::new(
@@ -57,10 +54,10 @@ impl LanguageConfiguration {
57 )
54 )
58 .unwrap(); // TODO: no unwrap
55 .unwrap(); // TODO: no unwrap
59 config.configure(&scopes);
56 config.configure(&scopes);
60− Ok(Some(Arc::new(config)))
57+ Some(Arc::new(config))
61 }
58 }
62 })
59 })
63− .map(Option::as_ref)
60+ .clone()
64 }
61 }
65
62
66 pub fn scope(&self) -> &str {
63 pub fn scope(&self) -> &str {
helix-lsp/src/client.rs+16−10
@@ -255,11 +255,6 @@ impl Client {
255 .await
255 .await
256 }
256 }
257
257
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.
263 pub fn changeset_to_changes(
258 pub fn changeset_to_changes(
264 old_text: &Rope,
259 old_text: &Rope,
265 new_text: &Rope,
260 new_text: &Rope,
@@ -274,6 +269,12 @@ impl Client {
274 use crate::util::pos_to_lsp_pos;
269 use crate::util::pos_to_lsp_pos;
275 use helix_core::Operation::*;
270 use helix_core::Operation::*;
276
271
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+
277 // TODO: stolen from syntax.rs, share
278 // TODO: stolen from syntax.rs, share
278 use helix_core::RopeSlice;
279 use helix_core::RopeSlice;
279 fn traverse(pos: lsp::Position, text: RopeSlice) -> lsp::Position {
280 fn traverse(pos: lsp::Position, text: RopeSlice) -> lsp::Position {
@@ -397,8 +398,6 @@ impl Client {
397 .await
398 .await
398 }
399 }
399
400
400− // TODO: impl into() TextDocumentIdentifier / VersionedTextDocumentIdentifier for Document.
401−
402 pub async fn text_document_did_close(
401 pub async fn text_document_did_close(
403 &self,
402 &self,
404 text_document: lsp::TextDocumentIdentifier,
403 text_document: lsp::TextDocumentIdentifier,
@@ -411,15 +410,22 @@ impl Client {
411
410
412 // will_save / will_save_wait_until
411 // will_save / will_save_wait_until
413
412
414− pub async fn text_document_did_save(&self) -> anyhow::Result<()> {
413+ pub async fn text_document_did_save(
415− unimplemented!()
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
416 }
422 }
417
423
418 pub async fn completion(
424 pub async fn completion(
419 &self,
425 &self,
420 text_document: lsp::TextDocumentIdentifier,
426 text_document: lsp::TextDocumentIdentifier,
421 position: lsp::Position,
427 position: lsp::Position,
422− ) -> anyhow::Result<Vec<lsp::CompletionItem>> {
428+ ) -> Result<Vec<lsp::CompletionItem>> {
423 // TODO: figure out what should happen when you complete with multiple cursors
429 // TODO: figure out what should happen when you complete with multiple cursors
424
430
425 let params = lsp::CompletionParams {
431 let params = lsp::CompletionParams {
helix-lsp/src/transport.rs+4−3
@@ -1,4 +1,5 @@
1use std::collections::HashMap;
1use std::collections::HashMap;
2+use std::io;
2
3
3use log::{error, info};
4use log::{error, info};
4
5
@@ -128,7 +129,7 @@ impl Transport {
128 Ok(())
129 Ok(())
129 }
130 }
130
131
131− pub async fn send_payload(&mut self, payload: Payload) -> anyhow::Result<()> {
132+ pub async fn send_payload(&mut self, payload: Payload) -> io::Result<()> {
132 match payload {
133 match payload {
133 Payload::Request { chan, value } => {
134 Payload::Request { chan, value } => {
134 self.pending_requests.insert(value.id.clone(), chan);
135 self.pending_requests.insert(value.id.clone(), chan);
@@ -147,7 +148,7 @@ impl Transport {
147 }
148 }
148 }
149 }
149
150
150− pub async fn send(&mut self, request: String) -> anyhow::Result<()> {
151+ pub async fn send(&mut self, request: String) -> io::Result<()> {
151 info!("-> {}", request);
152 info!("-> {}", request);
152
153
153 // send the headers
154 // send the headers
@@ -174,7 +175,7 @@ impl Transport {
174 Ok(())
175 Ok(())
175 }
176 }
176
177
177− async fn recv_response(&mut self, output: jsonrpc::Output) -> anyhow::Result<()> {
178+ async fn recv_response(&mut self, output: jsonrpc::Output) -> io::Result<()> {
178 let (id, result) = match output {
179 let (id, result) = match output {
179 jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => {
180 jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => {
180 info!("<- {}", result);
181 info!("<- {}", result);
helix-term/src/commands.rs+3−15
@@ -201,18 +201,6 @@ pub fn extend_next_word_end(cx: &mut Context) {
201 doc.set_selection(selection);
201 doc.set_selection(selection);
202}
202}
203
203
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−
216pub fn page_up(cx: &mut Context) {
204pub fn page_up(cx: &mut Context) {
217 let view = cx.view();
205 let view = cx.view();
218 if view.first_line < PADDING {
206 if view.first_line < PADDING {
@@ -221,7 +209,7 @@ pub fn page_up(cx: &mut Context) {
221
209
222 view.first_line = view.first_line.saturating_sub(view.area.height as usize);
210 view.first_line = view.first_line.saturating_sub(view.area.height as usize);
223
211
224− if !check_cursor_in_view(view) {
212+ if !view.check_cursor_in_view() {
225 let text = view.doc.text();
213 let text = view.doc.text();
226 let pos = text.line_to_char(view.last_line().saturating_sub(PADDING));
214 let pos = text.line_to_char(view.last_line().saturating_sub(PADDING));
227 view.doc.set_selection(Selection::point(pos));
215 view.doc.set_selection(Selection::point(pos));
@@ -249,7 +237,7 @@ pub fn half_page_up(cx: &mut Context) {
249 .first_line
237 .first_line
250 .saturating_sub(view.area.height as usize / 2);
238 .saturating_sub(view.area.height as usize / 2);
251
239
252− if !check_cursor_in_view(view) {
240+ if !view.check_cursor_in_view() {
253 let text = &view.doc.text();
241 let text = &view.doc.text();
254 let pos = text.line_to_char(view.last_line() - PADDING);
242 let pos = text.line_to_char(view.last_line() - PADDING);
255 view.doc.set_selection(Selection::point(pos));
243 view.doc.set_selection(Selection::point(pos));
@@ -262,7 +250,7 @@ pub fn half_page_down(cx: &mut Context) {
262 if view.first_line < lines.saturating_sub(view.area.height as usize) {
250 if view.first_line < lines.saturating_sub(view.area.height as usize) {
263 view.first_line += view.area.height as usize / 2;
251 view.first_line += view.area.height as usize / 2;
264 }
252 }
265− if !check_cursor_in_view(view) {
253+ if !view.check_cursor_in_view() {
266 let text = view.doc.text();
254 let text = view.doc.text();
267 let pos = text.line_to_char(view.first_line as usize);
255 let pos = text.line_to_char(view.first_line as usize);
268 view.doc.set_selection(Selection::point(pos));
256 view.doc.set_selection(Selection::point(pos));
helix-view/src/document.rs+4−2
@@ -140,10 +140,12 @@ impl Document {
140 // TODO: this ties lsp support to tree-sitter enabled languages for now. Language
140 // TODO: this ties lsp support to tree-sitter enabled languages for now. Language
141 // config should use Option<HighlightConfig> to let us have non-tree-sitter configs.
141 // config should use Option<HighlightConfig> to let us have non-tree-sitter configs.
142
142
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!");
144 // TODO: config.configure(scopes) is now delayed, is that ok?
146 // TODO: config.configure(scopes) is now delayed, is that ok?
145
147
146− let syntax = Syntax::new(&self.state.doc, highlight_config.clone());
148+ let syntax = Syntax::new(&self.state.doc, highlight_config);
147
149
148 self.syntax = Some(syntax);
150 self.syntax = Some(syntax);
149 } else {
151 } else {
helix-view/src/view.rs+11
@@ -35,6 +35,17 @@ impl View {
35 Ok(view)
35 Ok(view)
36 }
36 }
37
37
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+
38 pub fn ensure_cursor_in_view(&mut self) {
49 pub fn ensure_cursor_in_view(&mut self) {
39 let cursor = self.doc.state.selection().cursor();
50 let cursor = self.doc.state.selection().cursor();
40 let line = self.doc.text().char_to_line(cursor);
51 let line = self.doc.text().char_to_line(cursor);