2020-10-21 11:42:45 +04:00
|
|
|
mod client;
|
2020-12-23 10:50:16 +04:00
|
|
|
mod select_all;
|
2020-10-19 11:09:44 +04:00
|
|
|
mod transport;
|
|
|
|
|
2020-10-23 13:48:03 +04:00
|
|
|
pub use jsonrpc_core as jsonrpc;
|
|
|
|
pub use lsp_types as lsp;
|
2020-10-18 13:01:06 +04:00
|
|
|
|
2020-11-05 10:15:19 +04:00
|
|
|
pub use once_cell::sync::{Lazy, OnceCell};
|
|
|
|
|
2020-10-21 11:42:45 +04:00
|
|
|
pub use client::Client;
|
|
|
|
pub use lsp::{Position, Url};
|
2020-10-20 10:42:53 +04:00
|
|
|
|
2020-10-21 11:42:45 +04:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2020-11-05 10:15:19 +04:00
|
|
|
use std::{collections::HashMap, sync::Arc};
|
|
|
|
|
2020-10-21 11:42:45 +04:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
#[error("protocol error: {0}")]
|
|
|
|
Rpc(#[from] jsonrpc::Error),
|
|
|
|
#[error("failed to parse: {0}")]
|
|
|
|
Parse(#[from] serde_json::Error),
|
|
|
|
#[error("request timed out")]
|
|
|
|
Timeout,
|
|
|
|
#[error(transparent)]
|
|
|
|
Other(#[from] anyhow::Error),
|
|
|
|
}
|
2020-10-18 13:01:06 +04:00
|
|
|
|
2020-10-21 08:47:20 +04:00
|
|
|
pub mod util {
|
|
|
|
use super::*;
|
2020-12-23 12:03:20 +04:00
|
|
|
use helix_core::{RopeSlice, State, Transaction};
|
2020-10-21 08:47:20 +04:00
|
|
|
|
2020-12-23 12:03:20 +04:00
|
|
|
pub fn lsp_pos_to_pos(doc: &RopeSlice, pos: lsp::Position) -> usize {
|
2020-10-21 08:47:20 +04:00
|
|
|
let line = doc.line_to_char(pos.line as usize);
|
|
|
|
let line_start = doc.char_to_utf16_cu(line);
|
|
|
|
doc.utf16_cu_to_char(pos.character as usize + line_start)
|
|
|
|
}
|
2020-12-23 12:03:20 +04:00
|
|
|
pub fn pos_to_lsp_pos(doc: &RopeSlice, pos: usize) -> lsp::Position {
|
2020-10-23 13:48:03 +04:00
|
|
|
let line = doc.char_to_line(pos);
|
2020-12-25 12:42:50 +04:00
|
|
|
let line_start = doc.char_to_utf16_cu(doc.line_to_char(line));
|
2020-10-23 13:48:03 +04:00
|
|
|
let col = doc.char_to_utf16_cu(pos) - line_start;
|
2020-10-21 08:47:20 +04:00
|
|
|
|
2020-12-01 04:53:17 +04:00
|
|
|
lsp::Position::new(line as u32, col as u32)
|
2020-10-23 13:48:03 +04:00
|
|
|
}
|
2020-12-23 12:03:20 +04:00
|
|
|
|
|
|
|
pub fn generate_transaction_from_edits(
|
|
|
|
state: &State,
|
|
|
|
edits: Vec<lsp::TextEdit>,
|
|
|
|
) -> Transaction {
|
|
|
|
let doc = state.doc.slice(..);
|
|
|
|
Transaction::change(
|
|
|
|
state,
|
|
|
|
edits.into_iter().map(|edit| {
|
|
|
|
// simplify "" into None for cleaner changesets
|
|
|
|
let replacement = if !edit.new_text.is_empty() {
|
|
|
|
Some(edit.new_text.into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let start = lsp_pos_to_pos(&doc, edit.range.start);
|
|
|
|
let end = lsp_pos_to_pos(&doc, edit.range.end);
|
|
|
|
(start, end, replacement)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply_insert_replace_edit
|
2020-10-18 13:01:06 +04:00
|
|
|
}
|
|
|
|
|
2020-10-19 11:09:44 +04:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2020-10-20 08:58:34 +04:00
|
|
|
pub enum Notification {
|
|
|
|
PublishDiagnostics(lsp::PublishDiagnosticsParams),
|
|
|
|
}
|
2020-10-19 11:09:44 +04:00
|
|
|
|
|
|
|
impl Notification {
|
2020-10-20 08:58:34 +04:00
|
|
|
pub fn parse(method: &str, params: jsonrpc::Params) -> Notification {
|
2020-10-19 11:09:44 +04:00
|
|
|
use lsp::notification::Notification as _;
|
|
|
|
|
|
|
|
match method {
|
|
|
|
lsp::notification::PublishDiagnostics::METHOD => {
|
|
|
|
let params: lsp::PublishDiagnosticsParams = params
|
|
|
|
.parse()
|
|
|
|
.expect("Failed to parse PublishDiagnostics params");
|
|
|
|
|
|
|
|
// TODO: need to loop over diagnostics and distinguish them by URI
|
2020-10-20 08:58:34 +04:00
|
|
|
Notification::PublishDiagnostics(params)
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
2020-10-20 08:58:34 +04:00
|
|
|
_ => unimplemented!("unhandled notification: {}", method),
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 13:48:03 +04:00
|
|
|
|
|
|
|
pub use jsonrpc::Call;
|
2020-11-05 10:15:19 +04:00
|
|
|
|
|
|
|
type LanguageId = String;
|
|
|
|
|
2020-12-23 10:50:16 +04:00
|
|
|
use crate::select_all::SelectAll;
|
|
|
|
use smol::channel::Receiver;
|
2020-11-05 10:15:19 +04:00
|
|
|
|
|
|
|
pub struct Registry {
|
|
|
|
inner: HashMap<LanguageId, OnceCell<Arc<Client>>>,
|
2020-12-23 10:50:16 +04:00
|
|
|
|
|
|
|
pub incoming: SelectAll<Receiver<Call>>,
|
2020-11-05 10:15:19 +04:00
|
|
|
}
|
|
|
|
|
2021-01-08 11:31:19 +04:00
|
|
|
impl Default for Registry {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:15:19 +04:00
|
|
|
impl Registry {
|
2020-12-23 10:50:16 +04:00
|
|
|
pub fn new() -> Self {
|
|
|
|
let mut inner = HashMap::new();
|
|
|
|
|
|
|
|
inner.insert("rust".to_string(), OnceCell::new());
|
|
|
|
|
2020-11-05 10:15:19 +04:00
|
|
|
Self {
|
2020-12-23 10:50:16 +04:00
|
|
|
inner,
|
|
|
|
incoming: SelectAll::new(),
|
2020-11-05 10:15:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, id: &str, ex: &smol::Executor) -> Option<Arc<Client>> {
|
|
|
|
// TODO: use get_or_try_init and propagate the error
|
|
|
|
self.inner
|
|
|
|
.get(id)
|
|
|
|
.map(|cell| {
|
|
|
|
cell.get_or_init(|| {
|
|
|
|
// TODO: lookup defaults for id (name, args)
|
|
|
|
|
|
|
|
// initialize a new client
|
2020-12-23 10:50:16 +04:00
|
|
|
let (mut client, incoming) = Client::start(&ex, "rust-analyzer", &[]);
|
|
|
|
// TODO: run this async without blocking
|
|
|
|
smol::block_on(client.initialize()).unwrap();
|
|
|
|
|
|
|
|
self.incoming.push(incoming);
|
|
|
|
|
2020-11-05 10:15:19 +04:00
|
|
|
Arc::new(client)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.cloned()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// REGISTRY = HashMap<LanguageId, Lazy/OnceCell<Arc<RwLock<Client>>>
|
|
|
|
// spawn one server per language type, need to spawn one per workspace if server doesn't support
|
|
|
|
// workspaces
|
|
|
|
//
|
|
|
|
// could also be a client per root dir
|
|
|
|
//
|
|
|
|
// storing a copy of Option<Arc<RwLock<Client>>> on Document would make the LSP client easily
|
|
|
|
// accessible during edit/save callbacks
|
|
|
|
//
|
|
|
|
// the event loop needs to process all incoming streams, maybe we can just have that be a separate
|
|
|
|
// task that's continually running and store the state on the client, then use read lock to
|
|
|
|
// retrieve data during render
|
|
|
|
// -> PROBLEM: how do you trigger an update on the editor side when data updates?
|
|
|
|
//
|
|
|
|
// -> The data updates should pull all events until we run out so we don't frequently re-render
|
2020-12-23 10:50:16 +04:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// v2:
|
|
|
|
//
|
|
|
|
// there should be a registry of lsp clients, one per language type (or workspace).
|
|
|
|
// the clients should lazy init on first access
|
|
|
|
// the client.initialize() should be called async and we buffer any requests until that completes
|
|
|
|
// there needs to be a way to process incoming lsp messages from all clients.
|
|
|
|
// -> notifications need to be dispatched to wherever
|
|
|
|
// -> requests need to generate a reply and travel back to the same lsp!
|