minor: Simplify some code.

pull/8/head
Blaž Hrastnik 4 years ago
parent c6456d04b9
commit 87a6d4e736

@ -293,7 +293,7 @@ where
let language_config = crate::syntax::LOADER let language_config = crate::syntax::LOADER
.language_config_for_scope("source.rust") .language_config_for_scope("source.rust")
.unwrap(); .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 syntax = Syntax::new(&state.doc, highlight_config.clone());
let text = state.doc.slice(..); let text = state.doc.slice(..);

@ -27,12 +27,9 @@ pub struct LanguageConfiguration {
} }
impl LanguageConfiguration { impl LanguageConfiguration {
pub fn highlight_config( pub fn highlight_config(&self, scopes: &[String]) -> Option<Arc<HighlightConfiguration>> {
&self,
scopes: &[String],
) -> Result<Option<&Arc<HighlightConfiguration>>, anyhow::Error> {
self.highlight_config self.highlight_config
.get_or_try_init(|| { .get_or_init(|| {
// let name = get_language_name(&self.language_id); // let name = get_language_name(&self.language_id);
let highlights_query = let highlights_query =
@ -46,7 +43,7 @@ impl LanguageConfiguration {
let locals_query = ""; let locals_query = "";
if highlights_query.is_empty() { if highlights_query.is_empty() {
Ok(None) None
} else { } else {
let language = get_language(self.language_id); let language = get_language(self.language_id);
let mut config = HighlightConfiguration::new( let mut config = HighlightConfiguration::new(
@ -57,10 +54,10 @@ impl LanguageConfiguration {
) )
.unwrap(); // TODO: no unwrap .unwrap(); // TODO: no unwrap
config.configure(&scopes); config.configure(&scopes);
Ok(Some(Arc::new(config))) Some(Arc::new(config))
} }
}) })
.map(Option::as_ref) .clone()
} }
pub fn scope(&self) -> &str { pub fn scope(&self) -> &str {

@ -255,11 +255,6 @@ impl Client {
.await .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( pub fn changeset_to_changes(
old_text: &Rope, old_text: &Rope,
new_text: &Rope, new_text: &Rope,
@ -274,6 +269,12 @@ impl Client {
use crate::util::pos_to_lsp_pos; use crate::util::pos_to_lsp_pos;
use helix_core::Operation::*; 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 // TODO: stolen from syntax.rs, share
use helix_core::RopeSlice; use helix_core::RopeSlice;
fn traverse(pos: lsp::Position, text: RopeSlice) -> lsp::Position { fn traverse(pos: lsp::Position, text: RopeSlice) -> lsp::Position {
@ -397,8 +398,6 @@ impl Client {
.await .await
} }
// TODO: impl into() TextDocumentIdentifier / VersionedTextDocumentIdentifier for Document.
pub async fn text_document_did_close( pub async fn text_document_did_close(
&self, &self,
text_document: lsp::TextDocumentIdentifier, text_document: lsp::TextDocumentIdentifier,
@ -411,15 +410,22 @@ impl Client {
// will_save / will_save_wait_until // will_save / will_save_wait_until
pub async fn text_document_did_save(&self) -> anyhow::Result<()> { pub async fn text_document_did_save(
unimplemented!() &self,
text_document: lsp::TextDocumentIdentifier,
) -> Result<()> {
self.notify::<lsp::notification::DidSaveTextDocument>(lsp::DidSaveTextDocumentParams {
text_document,
text: None, // TODO:
})
.await
} }
pub async fn completion( pub async fn completion(
&self, &self,
text_document: lsp::TextDocumentIdentifier, text_document: lsp::TextDocumentIdentifier,
position: lsp::Position, position: lsp::Position,
) -> anyhow::Result<Vec<lsp::CompletionItem>> { ) -> Result<Vec<lsp::CompletionItem>> {
// TODO: figure out what should happen when you complete with multiple cursors // TODO: figure out what should happen when you complete with multiple cursors
let params = lsp::CompletionParams { let params = lsp::CompletionParams {

@ -1,4 +1,5 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::io;
use log::{error, info}; use log::{error, info};
@ -128,7 +129,7 @@ impl Transport {
Ok(()) 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 { match payload {
Payload::Request { chan, value } => { Payload::Request { chan, value } => {
self.pending_requests.insert(value.id.clone(), chan); 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); info!("-> {}", request);
// send the headers // send the headers
@ -174,7 +175,7 @@ impl Transport {
Ok(()) 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 { let (id, result) = match output {
jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => { jsonrpc::Output::Success(jsonrpc::Success { id, result, .. }) => {
info!("<- {}", result); info!("<- {}", result);

@ -201,18 +201,6 @@ pub fn extend_next_word_end(cx: &mut Context) {
doc.set_selection(selection); 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) { pub fn page_up(cx: &mut Context) {
let view = cx.view(); let view = cx.view();
if view.first_line < PADDING { 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); 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 text = view.doc.text();
let pos = text.line_to_char(view.last_line().saturating_sub(PADDING)); let pos = text.line_to_char(view.last_line().saturating_sub(PADDING));
view.doc.set_selection(Selection::point(pos)); view.doc.set_selection(Selection::point(pos));
@ -249,7 +237,7 @@ pub fn half_page_up(cx: &mut Context) {
.first_line .first_line
.saturating_sub(view.area.height as usize / 2); .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 text = &view.doc.text();
let pos = text.line_to_char(view.last_line() - PADDING); let pos = text.line_to_char(view.last_line() - PADDING);
view.doc.set_selection(Selection::point(pos)); 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) { if view.first_line < lines.saturating_sub(view.area.height as usize) {
view.first_line += view.area.height as usize / 2; 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 text = view.doc.text();
let pos = text.line_to_char(view.first_line as usize); let pos = text.line_to_char(view.first_line as usize);
view.doc.set_selection(Selection::point(pos)); view.doc.set_selection(Selection::point(pos));

@ -140,10 +140,12 @@ impl Document {
// TODO: this ties lsp support to tree-sitter enabled languages for now. Language // TODO: this ties lsp support to tree-sitter enabled languages for now. Language
// config should use Option<HighlightConfig> to let us have non-tree-sitter configs. // config should use Option<HighlightConfig> 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? // 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); self.syntax = Some(syntax);
} else { } else {

@ -35,6 +35,17 @@ impl View {
Ok(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) { pub fn ensure_cursor_in_view(&mut self) {
let cursor = self.doc.state.selection().cursor(); let cursor = self.doc.state.selection().cursor();
let line = self.doc.text().char_to_line(cursor); let line = self.doc.text().char_to_line(cursor);

Loading…
Cancel
Save