on master help sign in
state.version tracking
commitef5e5f9296d27d11ddfddf6d1c7daf93f9464ddb authorBlaž Hrastnik <blaz@mxxn.io> dateWed 21 Oct 2020, 13:47 (6 years ago) parent49254d7
4 files changed+36−38
helix-core/src/state.rs+2 helix-lsp/src/lib.rs+21−11 helix-term/src/application.rs+7−26 helix-view/src/commands.rs+6−1
helix-core/src/state.rs+2
@@ -29,6 +29,7 @@ pub struct State {
2929 pub changes: ChangeSet,
3030 pub old_state: Option<(Rope, Selection)>,
3131
32+ pub version: i64,
3233 pub diagnostics: Vec<Diagnostic>,
3334}
3435
@@ -61,6 +62,7 @@ impl State {
6162 changes,
6263 old_state,
6364 diagnostics: Vec::new(),
65+ version: 0,
6466 }
6567 }
6668
helix-lsp/src/lib.rs+21−11
@@ -2,6 +2,8 @@ mod transport;
22
33use transport::{Payload, Transport};
44
5+use helix_core::{State, Transaction};
6+
57// use std::collections::HashMap;
68
79use jsonrpc_core as jsonrpc;
@@ -13,14 +15,24 @@ use serde::{Deserialize, Serialize};
1315pub use lsp::Position;
1416pub use lsp::Url;
1517
16−use smol::prelude::*;
1718use smol::{
1819 channel::{Receiver, Sender},
1920 io::{BufReader, BufWriter},
21+ // prelude::*,
2022 process::{Child, ChildStderr, Command, Stdio},
2123 Executor,
2224};
2325
26+pub mod util {
27+ use super::*;
28+
29+ pub fn lsp_pos_to_pos(doc: &helix_core::RopeSlice, pos: lsp::Position) -> usize {
30+ let line = doc.line_to_char(pos.line as usize);
31+ let line_start = doc.char_to_utf16_cu(line);
32+ doc.utf16_cu_to_char(pos.character as usize + line_start)
33+ }
34+}
35+
2436/// A type representing all possible values sent from the server to the client.
2537#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
2638#[serde(deny_unknown_fields)]
@@ -58,7 +70,7 @@ impl Notification {
5870}
5971
6072pub struct Client {
61− process: Child,
73+ _process: Child,
6274 stderr: BufReader<ChildStderr>,
6375
6476 outgoing: Sender<Payload>,
@@ -90,7 +102,7 @@ impl Client {
90102 let (incoming, outgoing) = Transport::start(ex, reader, writer);
91103
92104 Client {
93− process,
105+ _process: process,
94106 stderr,
95107
96108 outgoing,
@@ -224,15 +236,12 @@ impl Client {
224236 // Text document
225237 // -------------------------------------------------------------------------------------------
226238
227− pub async fn text_document_did_open(
228− &mut self,
229− state: &helix_core::State,
230− ) -> anyhow::Result<()> {
239+ pub async fn text_document_did_open(&mut self, state: &State) -> anyhow::Result<()> {
231240 self.notify::<lsp::notification::DidOpenTextDocument>(lsp::DidOpenTextDocumentParams {
232241 text_document: lsp::TextDocumentItem {
233242 uri: lsp::Url::from_file_path(state.path().unwrap()).unwrap(),
234243 language_id: "rust".to_string(), // TODO: hardcoded for now
235− version: 0,
244+ version: state.version,
236245 text: String::from(&state.doc),
237246 },
238247 })
@@ -242,14 +251,15 @@ impl Client {
242251 // TODO: trigger any time history.commit_revision happens
243252 pub async fn text_document_did_change(
244253 &mut self,
245− state: &helix_core::State,
254+ state: &State,
255+ transaction: &Transaction,
246256 ) -> anyhow::Result<()> {
247257 self.notify::<lsp::notification::DidChangeTextDocument>(lsp::DidChangeTextDocumentParams {
248258 text_document: lsp::VersionedTextDocumentIdentifier::new(
249259 lsp::Url::from_file_path(state.path().unwrap()).unwrap(),
250− 0, // TODO: version
260+ state.version,
251261 ),
252− content_changes: vec![], // TODO:
262+ content_changes: vec![], // TODO: probably need old_state here too?
253263 })
254264 .await
255265 }
helix-term/src/application.rs+7−26
@@ -568,23 +568,12 @@ impl<'a> Application<'a> {
568568 use helix_lsp::Notification;
569569 match notification {
570570 Some(Notification::PublishDiagnostics(params)) => {
571− let view = self.editor.views.iter_mut().find(|view| {
572− let path = view
573− .state
574− .path
575− .as_ref()
576− .map(|path| helix_lsp::Url::from_file_path(path).unwrap());
577−
578− eprintln!("{:?} {} {}", path, params.uri, params.diagnostics.len());
579− // HAXX
580− path == Some(params.uri.clone())
581− });
582−
583− fn lsp_pos_to_pos(doc: &helix_core::RopeSlice, pos: helix_lsp::Position) -> usize {
584− let line = doc.line_to_char(pos.line as usize);
585− let line_start = doc.char_to_utf16_cu(line);
586− doc.utf16_cu_to_char(pos.character as usize + line_start)
587− }
571+ let path = Some(params.uri.to_file_path().unwrap());
572+ let view = self
573+ .editor
574+ .views
575+ .iter_mut()
576+ .find(|view| view.state.path == path);
588577
589578 if let Some(view) = view {
590579 let doc = view.state.doc().slice(..);
@@ -592,18 +581,10 @@ impl<'a> Application<'a> {
592581 .diagnostics
593582 .into_iter()
594583 .map(|diagnostic| {
584+ use helix_lsp::util::lsp_pos_to_pos;
595585 let start = lsp_pos_to_pos(&doc, diagnostic.range.start);
596586 let end = lsp_pos_to_pos(&doc, diagnostic.range.end);
597587
598− // eprintln!(
599− // "{:?}-{:?} {}-{} {}",
600− // diagnostic.range.start,
601− // diagnostic.range.end,
602− // start,
603− // end,
604− // diagnostic.message
605− // );
606−
607588 helix_core::Diagnostic {
608589 range: (start, end),
609590 line: diagnostic.range.start.line as usize,
helix-view/src/commands.rs+6−1
@@ -391,6 +391,12 @@ fn append_changes_to_history(view: &mut View) {
391391 // annotations either add a new layer or compose into the previous one.
392392 let transaction = Transaction::from(changes).with_selection(view.state.selection().clone());
393393
394+ // increment document version
395+ // TODO: needs to happen on undo/redo too
396+ view.state.version += 1;
397+
398+ // TODO: trigger lsp/documentDidChange with changes
399+
394400 // HAXX: we need to reconstruct the state as it was before the changes..
395401 let (doc, selection) = view.state.old_state.take().unwrap();
396402 let mut old_state = State::new(doc);
@@ -399,7 +405,6 @@ fn append_changes_to_history(view: &mut View) {
399405 // TODO: take transaction by value?
400406 view.history.commit_revision(&transaction, &old_state);
401407
402− // TODO: need to start the state with these vals
403408 // HAXX
404409 view.state.old_state = Some((view.state.doc().clone(), view.state.selection.clone()));
405410}