minor: Simplify some code.

This commit is contained in:
Blaž Hrastnik 2021-02-24 16:07:39 +09:00
parent c6456d04b9
commit 87a6d4e736
7 changed files with 44 additions and 39 deletions

View file

@ -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(..);

View file

@ -27,12 +27,9 @@ pub struct LanguageConfiguration {
}
impl LanguageConfiguration {
pub fn highlight_config(
&self,
scopes: &[String],
) -> Result<Option<&Arc<HighlightConfiguration>>, anyhow::Error> {
pub fn highlight_config(&self, scopes: &[String]) -> Option<Arc<HighlightConfiguration>> {
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 {

View file

@ -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::notification::DidSaveTextDocument>(lsp::DidSaveTextDocumentParams {
text_document,
text: None, // TODO:
})
.await
}
pub async fn completion(
&self,
text_document: lsp::TextDocumentIdentifier,
position: lsp::Position,
) -> anyhow::Result<Vec<lsp::CompletionItem>> {
) -> Result<Vec<lsp::CompletionItem>> {
// TODO: figure out what should happen when you complete with multiple cursors
let params = lsp::CompletionParams {

View file

@ -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);

View file

@ -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));

View file

@ -140,10 +140,12 @@ impl Document {
// 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.
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 {

View file

@ -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);