2020-10-21 11:42:45 +04:00
|
|
|
mod client;
|
2020-10-19 11:09:44 +04:00
|
|
|
mod transport;
|
|
|
|
|
2020-10-21 11:42:45 +04:00
|
|
|
pub use client::Client;
|
2021-06-07 23:11:17 +04:00
|
|
|
pub use futures_executor::block_on;
|
|
|
|
pub use jsonrpc::Call;
|
|
|
|
pub use jsonrpc_core as jsonrpc;
|
2020-10-21 11:42:45 +04:00
|
|
|
pub use lsp::{Position, Url};
|
2021-06-07 23:11:17 +04:00
|
|
|
pub use lsp_types as lsp;
|
2020-10-20 10:42:53 +04:00
|
|
|
|
2021-06-07 23:11:17 +04:00
|
|
|
use futures_util::stream::select_all::SelectAll;
|
2021-03-14 12:13:55 +04:00
|
|
|
use helix_core::syntax::LanguageConfiguration;
|
|
|
|
|
2021-06-07 22:50:20 +04:00
|
|
|
use std::{
|
|
|
|
collections::{hash_map::Entry, HashMap},
|
2021-06-18 07:42:34 +04:00
|
|
|
sync::{
|
|
|
|
atomic::{AtomicUsize, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
2021-06-07 22:50:20 +04:00
|
|
|
};
|
2020-11-05 10:15:19 +04:00
|
|
|
|
2021-04-14 10:30:15 +04:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-06-07 23:11:17 +04:00
|
|
|
use thiserror::Error;
|
2021-05-06 08:56:34 +04:00
|
|
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
|
|
|
|
2021-06-07 23:11:17 +04:00
|
|
|
pub type Result<T> = core::result::Result<T, Error>;
|
|
|
|
type LanguageId = String;
|
2021-05-12 12:24:55 +04:00
|
|
|
|
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),
|
2021-06-07 23:11:17 +04:00
|
|
|
#[error("IO Error: {0}")]
|
|
|
|
IO(#[from] std::io::Error),
|
2020-10-21 11:42:45 +04:00
|
|
|
#[error("request timed out")]
|
|
|
|
Timeout,
|
2021-06-07 22:50:20 +04:00
|
|
|
#[error("server closed the stream")]
|
|
|
|
StreamClosed,
|
|
|
|
#[error("LSP not defined")]
|
|
|
|
LspNotDefined,
|
2020-10-21 11:42:45 +04:00
|
|
|
#[error(transparent)]
|
|
|
|
Other(#[from] anyhow::Error),
|
|
|
|
}
|
2020-10-18 13:01:06 +04:00
|
|
|
|
2021-04-14 10:30:15 +04:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum OffsetEncoding {
|
|
|
|
/// UTF-8 code units aka bytes
|
|
|
|
#[serde(rename = "utf-8")]
|
|
|
|
Utf8,
|
|
|
|
/// UTF-16 code units
|
|
|
|
#[serde(rename = "utf-16")]
|
|
|
|
Utf16,
|
|
|
|
}
|
|
|
|
|
2020-10-21 08:47:20 +04:00
|
|
|
pub mod util {
|
|
|
|
use super::*;
|
2021-03-18 10:23:42 +04:00
|
|
|
use helix_core::{Range, Rope, Transaction};
|
2020-10-21 08:47:20 +04:00
|
|
|
|
2021-06-12 11:04:30 +04:00
|
|
|
/// Converts [`lsp::Position`] to a position in the document.
|
|
|
|
///
|
|
|
|
/// Returns `None` if position exceeds document length or an operation overflows.
|
2021-04-14 10:30:15 +04:00
|
|
|
pub fn lsp_pos_to_pos(
|
|
|
|
doc: &Rope,
|
|
|
|
pos: lsp::Position,
|
|
|
|
offset_encoding: OffsetEncoding,
|
2021-06-12 11:04:30 +04:00
|
|
|
) -> Option<usize> {
|
|
|
|
let max_line = doc.lines().count().saturating_sub(1);
|
|
|
|
let pos_line = pos.line as usize;
|
|
|
|
let pos_line = if pos_line > max_line {
|
|
|
|
return None;
|
|
|
|
} else {
|
|
|
|
pos_line
|
|
|
|
};
|
2021-04-14 10:30:15 +04:00
|
|
|
match offset_encoding {
|
|
|
|
OffsetEncoding::Utf8 => {
|
2021-06-12 11:04:30 +04:00
|
|
|
let max_char = doc
|
|
|
|
.line_to_char(max_line)
|
|
|
|
.checked_add(doc.line(max_line).len_chars())?;
|
|
|
|
let line = doc.line_to_char(pos_line);
|
|
|
|
let pos = line.checked_add(pos.character as usize)?;
|
|
|
|
if pos <= max_char {
|
|
|
|
Some(pos)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2021-04-14 10:30:15 +04:00
|
|
|
}
|
|
|
|
OffsetEncoding::Utf16 => {
|
2021-06-12 11:04:30 +04:00
|
|
|
let max_char = doc
|
|
|
|
.line_to_char(max_line)
|
|
|
|
.checked_add(doc.line(max_line).len_chars())?;
|
|
|
|
let max_cu = doc.char_to_utf16_cu(max_char);
|
|
|
|
let line = doc.line_to_char(pos_line);
|
2021-04-14 10:30:15 +04:00
|
|
|
let line_start = doc.char_to_utf16_cu(line);
|
2021-06-12 11:04:30 +04:00
|
|
|
let pos = line_start.checked_add(pos.character as usize)?;
|
|
|
|
if pos <= max_cu {
|
|
|
|
Some(doc.utf16_cu_to_char(pos))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2021-04-14 10:30:15 +04:00
|
|
|
}
|
|
|
|
}
|
2020-10-21 08:47:20 +04:00
|
|
|
}
|
2021-06-12 11:04:30 +04:00
|
|
|
|
|
|
|
/// Converts position in the document to [`lsp::Position`].
|
|
|
|
///
|
|
|
|
/// Panics when `pos` is out of `doc` bounds or operation overflows.
|
2021-04-14 10:30:15 +04:00
|
|
|
pub fn pos_to_lsp_pos(
|
|
|
|
doc: &Rope,
|
|
|
|
pos: usize,
|
|
|
|
offset_encoding: OffsetEncoding,
|
|
|
|
) -> lsp::Position {
|
|
|
|
match offset_encoding {
|
|
|
|
OffsetEncoding::Utf8 => {
|
|
|
|
let line = doc.char_to_line(pos);
|
|
|
|
let line_start = doc.line_to_char(line);
|
|
|
|
let col = pos - line_start;
|
|
|
|
|
|
|
|
lsp::Position::new(line as u32, col as u32)
|
|
|
|
}
|
|
|
|
OffsetEncoding::Utf16 => {
|
|
|
|
let line = doc.char_to_line(pos);
|
|
|
|
let line_start = doc.char_to_utf16_cu(doc.line_to_char(line));
|
|
|
|
let col = doc.char_to_utf16_cu(pos) - line_start;
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-06-12 11:04:30 +04:00
|
|
|
/// Converts a range in the document to [`lsp::Range`].
|
2021-04-14 10:30:15 +04:00
|
|
|
pub fn range_to_lsp_range(
|
|
|
|
doc: &Rope,
|
|
|
|
range: Range,
|
|
|
|
offset_encoding: OffsetEncoding,
|
|
|
|
) -> lsp::Range {
|
|
|
|
let start = pos_to_lsp_pos(doc, range.from(), offset_encoding);
|
|
|
|
let end = pos_to_lsp_pos(doc, range.to(), offset_encoding);
|
2021-02-26 10:52:43 +04:00
|
|
|
|
|
|
|
lsp::Range::new(start, end)
|
|
|
|
}
|
|
|
|
|
2021-06-12 16:45:21 +04:00
|
|
|
pub fn lsp_range_to_range(
|
|
|
|
doc: &Rope,
|
|
|
|
range: lsp::Range,
|
|
|
|
offset_encoding: OffsetEncoding,
|
|
|
|
) -> Option<Range> {
|
|
|
|
let start = lsp_pos_to_pos(doc, range.start, offset_encoding)?;
|
|
|
|
let end = lsp_pos_to_pos(doc, range.end, offset_encoding)?;
|
|
|
|
|
|
|
|
Some(Range::new(start, end))
|
|
|
|
}
|
|
|
|
|
2021-04-14 10:30:15 +04:00
|
|
|
pub fn generate_transaction_from_edits(
|
|
|
|
doc: &Rope,
|
|
|
|
edits: Vec<lsp::TextEdit>,
|
|
|
|
offset_encoding: OffsetEncoding,
|
|
|
|
) -> Transaction {
|
2020-12-23 12:03:20 +04:00
|
|
|
Transaction::change(
|
2021-03-18 08:45:57 +04:00
|
|
|
doc,
|
2020-12-23 12:03:20 +04:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2021-06-12 11:04:30 +04:00
|
|
|
let start =
|
|
|
|
if let Some(start) = lsp_pos_to_pos(doc, edit.range.start, offset_encoding) {
|
|
|
|
start
|
|
|
|
} else {
|
|
|
|
return (0, 0, None);
|
|
|
|
};
|
|
|
|
let end = if let Some(end) = lsp_pos_to_pos(doc, edit.range.end, offset_encoding) {
|
|
|
|
end
|
|
|
|
} else {
|
|
|
|
return (0, 0, None);
|
|
|
|
};
|
2020-12-23 12:03:20 +04:00
|
|
|
(start, end, replacement)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
2020-10-18 13:01:06 +04:00
|
|
|
}
|
|
|
|
|
2021-06-18 07:37:40 +04:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum MethodCall {
|
|
|
|
WorkDoneProgressCreate(lsp::WorkDoneProgressCreateParams),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MethodCall {
|
|
|
|
pub fn parse(method: &str, params: jsonrpc::Params) -> Option<MethodCall> {
|
|
|
|
use lsp::request::Request;
|
|
|
|
let request = match method {
|
|
|
|
lsp::request::WorkDoneProgressCreate::METHOD => {
|
|
|
|
let params: lsp::WorkDoneProgressCreateParams = params
|
|
|
|
.parse()
|
|
|
|
.expect("Failed to parse WorkDoneCreate params");
|
|
|
|
Self::WorkDoneProgressCreate(params)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
log::warn!("unhandled lsp request: {}", method);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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),
|
2021-04-15 12:34:38 +04:00
|
|
|
ShowMessage(lsp::ShowMessageParams),
|
|
|
|
LogMessage(lsp::LogMessageParams),
|
2021-06-11 07:42:16 +04:00
|
|
|
ProgressMessage(lsp::ProgressParams),
|
2020-10-20 08:58:34 +04:00
|
|
|
}
|
2020-10-19 11:09:44 +04:00
|
|
|
|
|
|
|
impl Notification {
|
2021-05-08 13:17:13 +04:00
|
|
|
pub fn parse(method: &str, params: jsonrpc::Params) -> Option<Notification> {
|
2020-10-19 11:09:44 +04:00
|
|
|
use lsp::notification::Notification as _;
|
|
|
|
|
2021-05-08 13:17:13 +04:00
|
|
|
let notification = match method {
|
2020-10-19 11:09:44 +04:00
|
|
|
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
|
2021-05-09 12:13:59 +04:00
|
|
|
Self::PublishDiagnostics(params)
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
2021-04-15 12:34:38 +04:00
|
|
|
|
|
|
|
lsp::notification::ShowMessage::METHOD => {
|
2021-06-11 07:42:16 +04:00
|
|
|
let params: lsp::ShowMessageParams = params.parse().ok()?;
|
2021-04-15 12:34:38 +04:00
|
|
|
|
2021-05-09 12:13:59 +04:00
|
|
|
Self::ShowMessage(params)
|
2021-04-15 12:34:38 +04:00
|
|
|
}
|
|
|
|
lsp::notification::LogMessage::METHOD => {
|
2021-06-11 07:42:16 +04:00
|
|
|
let params: lsp::LogMessageParams = params.parse().ok()?;
|
2021-04-15 12:34:38 +04:00
|
|
|
|
2021-05-09 12:13:59 +04:00
|
|
|
Self::LogMessage(params)
|
2021-04-15 12:34:38 +04:00
|
|
|
}
|
2021-06-11 07:42:16 +04:00
|
|
|
lsp::notification::Progress::METHOD => {
|
|
|
|
let params: lsp::ProgressParams = params.parse().ok()?;
|
|
|
|
|
|
|
|
Self::ProgressMessage(params)
|
|
|
|
}
|
2021-05-08 13:17:13 +04:00
|
|
|
_ => {
|
|
|
|
log::error!("unhandled LSP notification: {}", method);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(notification)
|
2020-10-19 11:09:44 +04:00
|
|
|
}
|
|
|
|
}
|
2020-10-23 13:48:03 +04:00
|
|
|
|
2021-06-07 18:34:19 +04:00
|
|
|
#[derive(Debug)]
|
2020-11-05 10:15:19 +04:00
|
|
|
pub struct Registry {
|
2021-06-18 07:42:34 +04:00
|
|
|
inner: HashMap<LanguageId, (usize, Arc<Client>)>,
|
2020-12-23 10:50:16 +04:00
|
|
|
|
2021-06-18 07:42:34 +04:00
|
|
|
counter: AtomicUsize,
|
|
|
|
pub incoming: SelectAll<UnboundedReceiverStream<(usize, 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 {
|
2020-11-05 10:15:19 +04:00
|
|
|
Self {
|
2021-03-14 12:13:55 +04:00
|
|
|
inner: HashMap::new(),
|
2021-06-18 07:42:34 +04:00
|
|
|
counter: AtomicUsize::new(0),
|
2020-12-23 10:50:16 +04:00
|
|
|
incoming: SelectAll::new(),
|
2020-11-05 10:15:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 07:42:34 +04:00
|
|
|
pub fn get_by_id(&mut self, id: usize) -> Option<&Client> {
|
|
|
|
self.inner
|
|
|
|
.values()
|
|
|
|
.find(|(client_id, _)| client_id == &id)
|
|
|
|
.map(|(_, client)| client.as_ref())
|
|
|
|
}
|
|
|
|
|
2021-06-07 22:50:20 +04:00
|
|
|
pub fn get(&mut self, language_config: &LanguageConfiguration) -> Result<Arc<Client>> {
|
2021-03-25 10:26:25 +04:00
|
|
|
if let Some(config) = &language_config.language_server {
|
2021-03-14 12:13:55 +04:00
|
|
|
// avoid borrow issues
|
|
|
|
let inner = &mut self.inner;
|
2021-06-03 20:48:59 +04:00
|
|
|
let s_incoming = &mut self.incoming;
|
2021-03-14 12:13:55 +04:00
|
|
|
|
2021-06-07 22:50:20 +04:00
|
|
|
match inner.entry(language_config.scope.clone()) {
|
2021-06-18 07:42:34 +04:00
|
|
|
Entry::Occupied(entry) => Ok(entry.get().1.clone()),
|
2021-06-07 22:50:20 +04:00
|
|
|
Entry::Vacant(entry) => {
|
2020-11-05 10:15:19 +04:00
|
|
|
// initialize a new client
|
2021-06-18 07:42:34 +04:00
|
|
|
let id = self.counter.fetch_add(1, Ordering::Relaxed);
|
|
|
|
let (mut client, incoming) = Client::start(&config.command, &config.args, id)?;
|
2020-12-23 10:50:16 +04:00
|
|
|
// TODO: run this async without blocking
|
2021-06-07 22:50:20 +04:00
|
|
|
futures_executor::block_on(client.initialize())?;
|
2021-05-06 08:56:34 +04:00
|
|
|
s_incoming.push(UnboundedReceiverStream::new(incoming));
|
2021-06-07 22:50:20 +04:00
|
|
|
let client = Arc::new(client);
|
2020-12-23 10:50:16 +04:00
|
|
|
|
2021-06-18 07:42:34 +04:00
|
|
|
entry.insert((id, client.clone()));
|
2021-06-07 22:50:20 +04:00
|
|
|
Ok(client)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Err(Error::LspNotDefined)
|
2021-03-14 12:13:55 +04:00
|
|
|
}
|
2020-11-05 10:15:19 +04:00
|
|
|
}
|
2021-06-19 06:56:50 +04:00
|
|
|
|
|
|
|
pub fn iter_clients(&self) -> impl Iterator<Item = &Arc<Client>> {
|
|
|
|
self.inner.values().map(|(_, client)| client)
|
|
|
|
}
|
2020-11-05 10:15:19 +04:00
|
|
|
}
|
|
|
|
|
2021-06-18 07:37:40 +04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ProgressStatus {
|
|
|
|
Created,
|
|
|
|
Started(lsp::WorkDoneProgress),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProgressStatus {
|
|
|
|
pub fn progress(&self) -> Option<&lsp::WorkDoneProgress> {
|
|
|
|
match &self {
|
|
|
|
ProgressStatus::Created => None,
|
2021-06-27 08:27:35 +04:00
|
|
|
ProgressStatus::Started(progress) => Some(progress),
|
2021-06-18 07:37:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
/// Acts as a container for progress reported by language servers. Each server
|
|
|
|
/// has a unique id assigned at creation through [`Registry`]. This id is then used
|
|
|
|
/// to store the progress in this map.
|
|
|
|
pub struct LspProgressMap(HashMap<usize, HashMap<lsp::ProgressToken, ProgressStatus>>);
|
|
|
|
|
|
|
|
impl LspProgressMap {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a map of all tokens coresponding to the lanaguage server with `id`.
|
|
|
|
pub fn progress_map(&self, id: usize) -> Option<&HashMap<lsp::ProgressToken, ProgressStatus>> {
|
|
|
|
self.0.get(&id)
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:31:45 +04:00
|
|
|
pub fn is_progressing(&self, id: usize) -> bool {
|
|
|
|
self.0.get(&id).map(|it| !it.is_empty()).unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
2021-06-18 07:37:40 +04:00
|
|
|
/// Returns last progress status for a given server with `id` and `token`.
|
|
|
|
pub fn progress(&self, id: usize, token: &lsp::ProgressToken) -> Option<&ProgressStatus> {
|
|
|
|
self.0.get(&id).and_then(|values| values.get(token))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if progress `token` for server with `id` is created.
|
|
|
|
pub fn is_created(&mut self, id: usize, token: &lsp::ProgressToken) -> bool {
|
|
|
|
self.0
|
|
|
|
.get(&id)
|
|
|
|
.map(|values| values.get(token).is_some())
|
|
|
|
.unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create(&mut self, id: usize, token: lsp::ProgressToken) {
|
|
|
|
self.0
|
|
|
|
.entry(id)
|
|
|
|
.or_default()
|
|
|
|
.insert(token, ProgressStatus::Created);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Ends the progress by removing the `token` from server with `id`, if removed returns the value.
|
|
|
|
pub fn end_progress(
|
|
|
|
&mut self,
|
|
|
|
id: usize,
|
|
|
|
token: &lsp::ProgressToken,
|
|
|
|
) -> Option<ProgressStatus> {
|
|
|
|
self.0.get_mut(&id).and_then(|vals| vals.remove(token))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Updates the progess of `token` for server with `id` to `status`, returns the value replaced or `None`.
|
|
|
|
pub fn update(
|
|
|
|
&mut self,
|
|
|
|
id: usize,
|
|
|
|
token: lsp::ProgressToken,
|
|
|
|
status: lsp::WorkDoneProgress,
|
|
|
|
) -> Option<ProgressStatus> {
|
|
|
|
self.0
|
|
|
|
.entry(id)
|
|
|
|
.or_default()
|
|
|
|
.insert(token, ProgressStatus::Started(status))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:15:19 +04:00
|
|
|
// 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!
|
2021-06-12 11:04:30 +04:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::{lsp, util::*, OffsetEncoding};
|
|
|
|
use helix_core::Rope;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn converts_lsp_pos_to_pos() {
|
|
|
|
macro_rules! test_case {
|
|
|
|
($doc:expr, ($x:expr, $y:expr) => $want:expr) => {
|
|
|
|
let doc = Rope::from($doc);
|
|
|
|
let pos = lsp::Position::new($x, $y);
|
|
|
|
assert_eq!($want, lsp_pos_to_pos(&doc, pos, OffsetEncoding::Utf16));
|
|
|
|
assert_eq!($want, lsp_pos_to_pos(&doc, pos, OffsetEncoding::Utf8))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test_case!("", (0, 0) => Some(0));
|
|
|
|
test_case!("", (0, 1) => None);
|
|
|
|
test_case!("", (1, 0) => None);
|
|
|
|
test_case!("\n\n", (0, 0) => Some(0));
|
|
|
|
test_case!("\n\n", (1, 0) => Some(1));
|
|
|
|
test_case!("\n\n", (1, 1) => Some(2));
|
|
|
|
test_case!("\n\n", (2, 0) => Some(2));
|
|
|
|
test_case!("\n\n", (3, 0) => None);
|
|
|
|
test_case!("test\n\n\n\ncase", (4, 3) => Some(11));
|
|
|
|
test_case!("test\n\n\n\ncase", (4, 4) => Some(12));
|
|
|
|
test_case!("test\n\n\n\ncase", (4, 5) => None);
|
|
|
|
test_case!("", (u32::MAX, u32::MAX) => None);
|
|
|
|
}
|
|
|
|
}
|