2020-10-21 11:42:45 +04:00
|
|
|
use crate::{
|
|
|
|
transport::{Payload, Transport},
|
2021-04-14 10:30:15 +04:00
|
|
|
Call, Error, OffsetEncoding, Result,
|
2020-10-21 11:42:45 +04:00
|
|
|
};
|
|
|
|
|
2021-06-07 16:32:01 +04:00
|
|
|
use helix_core::{find_root, ChangeSet, Rope};
|
2020-10-21 11:42:45 +04:00
|
|
|
use jsonrpc_core as jsonrpc;
|
|
|
|
use lsp_types as lsp;
|
|
|
|
use serde_json::Value;
|
2021-06-07 23:11:17 +04:00
|
|
|
use std::future::Future;
|
2021-05-06 08:56:34 +04:00
|
|
|
use std::process::Stdio;
|
2021-06-07 23:11:17 +04:00
|
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
2021-05-06 08:56:34 +04:00
|
|
|
use tokio::{
|
2020-10-21 11:42:45 +04:00
|
|
|
io::{BufReader, BufWriter},
|
2021-05-06 08:56:34 +04:00
|
|
|
process::{Child, Command},
|
|
|
|
sync::mpsc::{channel, UnboundedReceiver, UnboundedSender},
|
2020-10-21 11:42:45 +04:00
|
|
|
};
|
|
|
|
|
2021-06-06 19:55:05 +04:00
|
|
|
#[cfg_attr(feature = "debug", derive(Debug))]
|
2020-10-21 11:42:45 +04:00
|
|
|
pub struct Client {
|
|
|
|
_process: Child,
|
2021-06-07 23:11:17 +04:00
|
|
|
server_tx: UnboundedSender<Payload>,
|
|
|
|
request_counter: AtomicU64,
|
2020-10-21 11:42:45 +04:00
|
|
|
capabilities: Option<lsp::ServerCapabilities>,
|
2021-04-14 10:30:15 +04:00
|
|
|
offset_encoding: OffsetEncoding,
|
2020-10-21 11:42:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2021-05-06 08:56:34 +04:00
|
|
|
pub fn start(cmd: &str, args: &[String]) -> Result<(Self, UnboundedReceiver<Call>)> {
|
2021-04-07 08:38:24 +04:00
|
|
|
let process = Command::new(cmd)
|
2020-10-21 11:42:45 +04:00
|
|
|
.args(args)
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.stderr(Stdio::piped())
|
2021-05-06 08:56:34 +04:00
|
|
|
// make sure the process is reaped on drop
|
|
|
|
.kill_on_drop(true)
|
2021-04-07 08:38:24 +04:00
|
|
|
.spawn();
|
|
|
|
|
2021-06-07 23:11:17 +04:00
|
|
|
let mut process = process?;
|
2020-10-21 11:42:45 +04:00
|
|
|
|
|
|
|
// TODO: do we need bufreader/writer here? or do we use async wrappers on unblock?
|
|
|
|
let writer = BufWriter::new(process.stdin.take().expect("Failed to open stdin"));
|
|
|
|
let reader = BufReader::new(process.stdout.take().expect("Failed to open stdout"));
|
|
|
|
let stderr = BufReader::new(process.stderr.take().expect("Failed to open stderr"));
|
|
|
|
|
2021-06-07 23:11:17 +04:00
|
|
|
let (server_rx, server_tx) = Transport::start(reader, writer, stderr);
|
2020-10-21 11:42:45 +04:00
|
|
|
|
2021-05-06 12:20:00 +04:00
|
|
|
let client = Self {
|
2020-10-21 11:42:45 +04:00
|
|
|
_process: process,
|
2021-06-07 23:11:17 +04:00
|
|
|
server_tx,
|
2020-10-29 09:06:33 +04:00
|
|
|
request_counter: AtomicU64::new(0),
|
2020-10-21 11:42:45 +04:00
|
|
|
capabilities: None,
|
2021-04-14 10:30:15 +04:00
|
|
|
offset_encoding: OffsetEncoding::Utf8,
|
2020-12-23 10:50:16 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: async client.initialize()
|
|
|
|
// maybe use an arc<atomic> flag
|
|
|
|
|
2021-06-07 23:11:17 +04:00
|
|
|
Ok((client, server_rx))
|
2020-10-21 11:42:45 +04:00
|
|
|
}
|
|
|
|
|
2020-10-29 09:06:33 +04:00
|
|
|
fn next_request_id(&self) -> jsonrpc::Id {
|
|
|
|
let id = self.request_counter.fetch_add(1, Ordering::Relaxed);
|
|
|
|
jsonrpc::Id::Num(id)
|
2020-10-21 11:42:45 +04:00
|
|
|
}
|
|
|
|
|
2021-01-08 11:31:19 +04:00
|
|
|
fn value_into_params(value: Value) -> jsonrpc::Params {
|
2020-10-21 11:42:45 +04:00
|
|
|
use jsonrpc::Params;
|
|
|
|
|
2021-01-08 11:31:19 +04:00
|
|
|
match value {
|
2020-10-21 11:42:45 +04:00
|
|
|
Value::Null => Params::None,
|
|
|
|
Value::Bool(_) | Value::Number(_) | Value::String(_) => Params::Array(vec![value]),
|
|
|
|
Value::Array(vec) => Params::Array(vec),
|
|
|
|
Value::Object(map) => Params::Map(map),
|
2021-01-08 11:31:19 +04:00
|
|
|
}
|
2020-10-21 11:42:45 +04:00
|
|
|
}
|
|
|
|
|
2021-03-24 13:17:00 +04:00
|
|
|
pub fn capabilities(&self) -> &lsp::ServerCapabilities {
|
|
|
|
self.capabilities
|
|
|
|
.as_ref()
|
|
|
|
.expect("language server not yet initialized!")
|
|
|
|
}
|
|
|
|
|
2021-04-14 10:30:15 +04:00
|
|
|
pub fn offset_encoding(&self) -> OffsetEncoding {
|
|
|
|
self.offset_encoding
|
|
|
|
}
|
|
|
|
|
2020-10-23 13:48:03 +04:00
|
|
|
/// Execute a RPC request on the language server.
|
2021-06-07 23:11:17 +04:00
|
|
|
async fn request<R: lsp::request::Request>(&self, params: R::Params) -> Result<R::Result>
|
2020-10-21 11:42:45 +04:00
|
|
|
where
|
|
|
|
R::Params: serde::Serialize,
|
|
|
|
R::Result: core::fmt::Debug, // TODO: temporary
|
2021-03-26 11:02:13 +04:00
|
|
|
{
|
|
|
|
// a future that resolves into the response
|
2021-05-06 10:08:59 +04:00
|
|
|
let json = self.call::<R>(params).await?;
|
2021-03-26 11:02:13 +04:00
|
|
|
let response = serde_json::from_value(json)?;
|
|
|
|
Ok(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute a RPC request on the language server.
|
2021-06-07 23:11:17 +04:00
|
|
|
fn call<R: lsp::request::Request>(
|
2021-03-26 11:02:13 +04:00
|
|
|
&self,
|
|
|
|
params: R::Params,
|
2021-05-06 10:08:59 +04:00
|
|
|
) -> impl Future<Output = Result<Value>>
|
2021-03-26 11:02:13 +04:00
|
|
|
where
|
|
|
|
R::Params: serde::Serialize,
|
2020-10-21 11:42:45 +04:00
|
|
|
{
|
2021-06-07 23:11:17 +04:00
|
|
|
let server_tx = self.server_tx.clone();
|
2021-05-06 10:08:59 +04:00
|
|
|
let id = self.next_request_id();
|
2020-10-21 11:42:45 +04:00
|
|
|
|
2021-05-06 10:08:59 +04:00
|
|
|
async move {
|
2021-06-07 23:11:17 +04:00
|
|
|
use std::time::Duration;
|
|
|
|
use tokio::time::timeout;
|
|
|
|
|
2021-05-06 10:08:59 +04:00
|
|
|
let params = serde_json::to_value(params)?;
|
2020-10-21 11:42:45 +04:00
|
|
|
|
2021-05-06 10:08:59 +04:00
|
|
|
let request = jsonrpc::MethodCall {
|
|
|
|
jsonrpc: Some(jsonrpc::Version::V2),
|
|
|
|
id,
|
|
|
|
method: R::METHOD.to_string(),
|
|
|
|
params: Self::value_into_params(params),
|
|
|
|
};
|
2020-10-21 11:42:45 +04:00
|
|
|
|
2021-05-06 10:08:59 +04:00
|
|
|
let (tx, mut rx) = channel::<Result<Value>>(1);
|
2020-10-21 11:42:45 +04:00
|
|
|
|
2021-06-07 23:11:17 +04:00
|
|
|
server_tx
|
2021-05-06 10:08:59 +04:00
|
|
|
.send(Payload::Request {
|
|
|
|
chan: tx,
|
|
|
|
value: request,
|
|
|
|
})
|
|
|
|
.map_err(|e| Error::Other(e.into()))?;
|
|
|
|
|
2021-05-06 08:56:34 +04:00
|
|
|
timeout(Duration::from_secs(2), rx.recv())
|
2021-03-26 11:02:13 +04:00
|
|
|
.await
|
2021-05-06 12:15:49 +04:00
|
|
|
.map_err(|_| Error::Timeout)? // return Timeout
|
2021-06-07 22:50:20 +04:00
|
|
|
.ok_or(Error::StreamClosed)?
|
2021-05-06 10:08:59 +04:00
|
|
|
}
|
2020-10-21 11:42:45 +04:00
|
|
|
}
|
|
|
|
|
2020-10-23 13:48:03 +04:00
|
|
|
/// Send a RPC notification to the language server.
|
2021-06-07 23:11:17 +04:00
|
|
|
fn notify<R: lsp::notification::Notification>(
|
2021-05-06 08:56:34 +04:00
|
|
|
&self,
|
|
|
|
params: R::Params,
|
|
|
|
) -> impl Future<Output = Result<()>>
|
2020-10-21 11:42:45 +04:00
|
|
|
where
|
|
|
|
R::Params: serde::Serialize,
|
|
|
|
{
|
2021-06-07 23:11:17 +04:00
|
|
|
let server_tx = self.server_tx.clone();
|
2020-10-21 11:42:45 +04:00
|
|
|
|
2021-05-06 08:56:34 +04:00
|
|
|
async move {
|
|
|
|
let params = serde_json::to_value(params)?;
|
2020-10-21 11:42:45 +04:00
|
|
|
|
2021-05-06 08:56:34 +04:00
|
|
|
let notification = jsonrpc::Notification {
|
|
|
|
jsonrpc: Some(jsonrpc::Version::V2),
|
|
|
|
method: R::METHOD.to_string(),
|
|
|
|
params: Self::value_into_params(params),
|
|
|
|
};
|
2020-10-21 11:42:45 +04:00
|
|
|
|
2021-06-07 23:11:17 +04:00
|
|
|
server_tx
|
2021-05-06 08:56:34 +04:00
|
|
|
.send(Payload::Notification(notification))
|
|
|
|
.map_err(|e| Error::Other(e.into()))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-10-21 11:42:45 +04:00
|
|
|
}
|
|
|
|
|
2020-10-23 13:48:03 +04:00
|
|
|
/// Reply to a language server RPC call.
|
|
|
|
pub async fn reply(
|
2020-10-29 09:06:33 +04:00
|
|
|
&self,
|
2020-10-23 13:48:03 +04:00
|
|
|
id: jsonrpc::Id,
|
|
|
|
result: core::result::Result<Value, jsonrpc::Error>,
|
|
|
|
) -> Result<()> {
|
|
|
|
use jsonrpc::{Failure, Output, Success, Version};
|
|
|
|
|
|
|
|
let output = match result {
|
|
|
|
Ok(result) => Output::Success(Success {
|
|
|
|
jsonrpc: Some(Version::V2),
|
|
|
|
id,
|
|
|
|
result,
|
|
|
|
}),
|
|
|
|
Err(error) => Output::Failure(Failure {
|
|
|
|
jsonrpc: Some(Version::V2),
|
|
|
|
id,
|
|
|
|
error,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
2021-06-07 23:11:17 +04:00
|
|
|
self.server_tx
|
2020-10-23 13:48:03 +04:00
|
|
|
.send(Payload::Response(output))
|
|
|
|
.map_err(|e| Error::Other(e.into()))?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-21 11:42:45 +04:00
|
|
|
// -------------------------------------------------------------------------------------------
|
|
|
|
// General messages
|
|
|
|
// -------------------------------------------------------------------------------------------
|
|
|
|
|
2021-06-07 23:11:17 +04:00
|
|
|
pub(crate) async fn initialize(&mut self) -> Result<()> {
|
2020-10-21 11:42:45 +04:00
|
|
|
// TODO: delay any requests that are triggered prior to initialize
|
2021-06-07 16:32:01 +04:00
|
|
|
let root = find_root(None).and_then(|root| lsp::Url::from_file_path(root).ok());
|
2020-10-21 11:42:45 +04:00
|
|
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
let params = lsp::InitializeParams {
|
2020-12-01 04:53:17 +04:00
|
|
|
process_id: Some(std::process::id()),
|
2021-06-07 16:32:01 +04:00
|
|
|
// root_path is obsolete, use root_uri
|
2020-10-21 11:42:45 +04:00
|
|
|
root_path: None,
|
2021-06-07 16:32:01 +04:00
|
|
|
root_uri: root,
|
2020-10-21 11:42:45 +04:00
|
|
|
initialization_options: None,
|
2020-10-23 13:48:03 +04:00
|
|
|
capabilities: lsp::ClientCapabilities {
|
2021-02-22 09:42:12 +04:00
|
|
|
text_document: Some(lsp::TextDocumentClientCapabilities {
|
|
|
|
completion: Some(lsp::CompletionClientCapabilities {
|
|
|
|
completion_item: Some(lsp::CompletionItemCapability {
|
2021-03-16 10:30:29 +04:00
|
|
|
snippet_support: Some(false),
|
2021-02-22 09:42:12 +04:00
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
completion_item_kind: Some(lsp::CompletionItemKindCapability {
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
context_support: None, // additional context information Some(true)
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-02-25 13:07:47 +04:00
|
|
|
hover: Some(lsp::HoverClientCapabilities {
|
|
|
|
// if not specified, rust-analyzer returns plaintext marked as markdown but
|
|
|
|
// badly formatted.
|
|
|
|
content_format: Some(vec![lsp::MarkupKind::Markdown]),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-02-22 09:42:12 +04:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2020-10-23 13:48:03 +04:00
|
|
|
..Default::default()
|
|
|
|
},
|
2020-10-21 11:42:45 +04:00
|
|
|
trace: None,
|
|
|
|
workspace_folders: None,
|
|
|
|
client_info: None,
|
2020-12-01 04:53:17 +04:00
|
|
|
locale: None, // TODO
|
2020-10-21 11:42:45 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
let response = self.request::<lsp::request::Initialize>(params).await?;
|
|
|
|
self.capabilities = Some(response.capabilities);
|
|
|
|
|
|
|
|
// next up, notify<initialized>
|
|
|
|
self.notify::<lsp::notification::Initialized>(lsp::InitializedParams {})
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-10-29 09:06:33 +04:00
|
|
|
pub async fn shutdown(&self) -> Result<()> {
|
2020-10-21 11:42:45 +04:00
|
|
|
self.request::<lsp::request::Shutdown>(()).await
|
|
|
|
}
|
|
|
|
|
2021-05-06 10:08:59 +04:00
|
|
|
pub fn exit(&self) -> impl Future<Output = Result<()>> {
|
|
|
|
self.notify::<lsp::notification::Exit>(())
|
2020-10-21 11:42:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------------------------
|
|
|
|
// Text document
|
|
|
|
// -------------------------------------------------------------------------------------------
|
|
|
|
|
2021-05-06 10:08:59 +04:00
|
|
|
pub fn text_document_did_open(
|
2020-12-25 12:20:09 +04:00
|
|
|
&self,
|
|
|
|
uri: lsp::Url,
|
|
|
|
version: i32,
|
|
|
|
doc: &Rope,
|
2021-03-16 11:13:41 +04:00
|
|
|
language_id: String,
|
2021-05-06 10:08:59 +04:00
|
|
|
) -> impl Future<Output = Result<()>> {
|
2020-10-21 11:42:45 +04:00
|
|
|
self.notify::<lsp::notification::DidOpenTextDocument>(lsp::DidOpenTextDocumentParams {
|
|
|
|
text_document: lsp::TextDocumentItem {
|
2020-12-25 12:20:09 +04:00
|
|
|
uri,
|
2021-03-16 11:13:41 +04:00
|
|
|
language_id,
|
2020-12-25 12:20:09 +04:00
|
|
|
version,
|
|
|
|
text: String::from(doc),
|
2020-10-21 11:42:45 +04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-16 10:39:41 +04:00
|
|
|
pub fn changeset_to_changes(
|
2020-12-25 12:42:50 +04:00
|
|
|
old_text: &Rope,
|
2021-02-17 12:28:55 +04:00
|
|
|
new_text: &Rope,
|
2020-12-25 12:42:50 +04:00
|
|
|
changeset: &ChangeSet,
|
2021-04-14 10:30:15 +04:00
|
|
|
offset_encoding: OffsetEncoding,
|
2020-12-25 12:42:50 +04:00
|
|
|
) -> Vec<lsp::TextDocumentContentChangeEvent> {
|
2020-10-23 13:48:03 +04:00
|
|
|
let mut iter = changeset.changes().iter().peekable();
|
|
|
|
let mut old_pos = 0;
|
2021-02-17 12:28:55 +04:00
|
|
|
let mut new_pos = 0;
|
2020-10-23 13:48:03 +04:00
|
|
|
|
|
|
|
let mut changes = Vec::new();
|
|
|
|
|
|
|
|
use crate::util::pos_to_lsp_pos;
|
|
|
|
use helix_core::Operation::*;
|
|
|
|
|
2021-02-24 11:07:39 +04:00
|
|
|
// 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.
|
|
|
|
|
2021-02-17 12:28:55 +04:00
|
|
|
// TODO: stolen from syntax.rs, share
|
|
|
|
use helix_core::RopeSlice;
|
|
|
|
fn traverse(pos: lsp::Position, text: RopeSlice) -> lsp::Position {
|
|
|
|
let lsp::Position {
|
|
|
|
mut line,
|
|
|
|
mut character,
|
|
|
|
} = pos;
|
|
|
|
|
|
|
|
for ch in text.chars() {
|
|
|
|
if ch == '\n' {
|
|
|
|
line += 1;
|
|
|
|
character = 0;
|
|
|
|
} else {
|
|
|
|
character += ch.len_utf16() as u32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lsp::Position { line, character }
|
|
|
|
}
|
|
|
|
|
2020-12-25 12:42:50 +04:00
|
|
|
let old_text = old_text.slice(..);
|
|
|
|
|
2020-10-23 13:48:03 +04:00
|
|
|
while let Some(change) = iter.next() {
|
|
|
|
let len = match change {
|
|
|
|
Delete(i) | Retain(i) => *i,
|
|
|
|
Insert(_) => 0,
|
|
|
|
};
|
2021-02-16 06:08:06 +04:00
|
|
|
let mut old_end = old_pos + len;
|
2020-10-23 13:48:03 +04:00
|
|
|
|
|
|
|
match change {
|
2021-02-17 12:28:55 +04:00
|
|
|
Retain(i) => {
|
|
|
|
new_pos += i;
|
|
|
|
}
|
2020-10-23 13:48:03 +04:00
|
|
|
Delete(_) => {
|
2021-04-14 10:30:15 +04:00
|
|
|
let start = pos_to_lsp_pos(new_text, new_pos, offset_encoding);
|
2021-02-17 12:28:55 +04:00
|
|
|
let end = traverse(start, old_text.slice(old_pos..old_end));
|
2020-10-23 13:48:03 +04:00
|
|
|
|
2021-02-16 06:08:06 +04:00
|
|
|
// deletion
|
|
|
|
changes.push(lsp::TextDocumentContentChangeEvent {
|
|
|
|
range: Some(lsp::Range::new(start, end)),
|
|
|
|
text: "".to_string(),
|
|
|
|
range_length: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Insert(s) => {
|
2021-04-14 10:30:15 +04:00
|
|
|
let start = pos_to_lsp_pos(new_text, new_pos, offset_encoding);
|
2021-02-17 12:28:55 +04:00
|
|
|
|
|
|
|
new_pos += s.chars().count();
|
2021-02-16 06:08:06 +04:00
|
|
|
|
|
|
|
// a subsequent delete means a replace, consume it
|
|
|
|
let end = if let Some(Delete(len)) = iter.peek() {
|
|
|
|
old_end = old_pos + len;
|
2021-02-17 12:28:55 +04:00
|
|
|
let end = traverse(start, old_text.slice(old_pos..old_end));
|
2021-02-16 06:08:06 +04:00
|
|
|
|
2020-10-23 13:48:03 +04:00
|
|
|
iter.next();
|
|
|
|
|
|
|
|
// replacement
|
2021-02-16 06:08:06 +04:00
|
|
|
end
|
2020-10-23 13:48:03 +04:00
|
|
|
} else {
|
2021-02-16 06:08:06 +04:00
|
|
|
// insert
|
|
|
|
start
|
2020-10-23 13:48:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
changes.push(lsp::TextDocumentContentChangeEvent {
|
2021-02-16 06:08:06 +04:00
|
|
|
range: Some(lsp::Range::new(start, end)),
|
2020-10-23 13:48:03 +04:00
|
|
|
text: s.into(),
|
|
|
|
range_length: None,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
old_pos = old_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
changes
|
|
|
|
}
|
|
|
|
|
2021-05-06 08:56:34 +04:00
|
|
|
pub fn text_document_did_change(
|
2020-12-23 10:50:16 +04:00
|
|
|
&self,
|
2020-12-25 12:20:09 +04:00
|
|
|
text_document: lsp::VersionedTextDocumentIdentifier,
|
2020-12-25 12:42:50 +04:00
|
|
|
old_text: &Rope,
|
2021-02-17 12:28:55 +04:00
|
|
|
new_text: &Rope,
|
2020-12-25 12:20:09 +04:00
|
|
|
changes: &ChangeSet,
|
2021-05-06 08:56:34 +04:00
|
|
|
) -> Option<impl Future<Output = Result<()>>> {
|
2020-10-23 13:48:03 +04:00
|
|
|
// figure out what kind of sync the server supports
|
|
|
|
|
2021-03-16 10:30:29 +04:00
|
|
|
let capabilities = self.capabilities.as_ref().unwrap();
|
2020-10-23 13:48:03 +04:00
|
|
|
|
|
|
|
let sync_capabilities = match capabilities.text_document_sync {
|
2021-04-06 14:45:14 +04:00
|
|
|
Some(lsp::TextDocumentSyncCapability::Kind(kind))
|
|
|
|
| Some(lsp::TextDocumentSyncCapability::Options(lsp::TextDocumentSyncOptions {
|
|
|
|
change: Some(kind),
|
|
|
|
..
|
|
|
|
})) => kind,
|
2020-10-23 13:48:03 +04:00
|
|
|
// None | SyncOptions { changes: None }
|
2021-05-06 08:56:34 +04:00
|
|
|
_ => return None,
|
2020-10-23 13:48:03 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
let changes = match sync_capabilities {
|
|
|
|
lsp::TextDocumentSyncKind::Full => {
|
|
|
|
vec![lsp::TextDocumentContentChangeEvent {
|
|
|
|
// range = None -> whole document
|
|
|
|
range: None, //Some(Range)
|
|
|
|
range_length: None, // u64 apparently deprecated
|
|
|
|
text: "".to_string(),
|
2021-03-16 10:30:29 +04:00
|
|
|
}]
|
2020-10-23 13:48:03 +04:00
|
|
|
}
|
2021-02-17 12:28:55 +04:00
|
|
|
lsp::TextDocumentSyncKind::Incremental => {
|
2021-04-14 10:30:15 +04:00
|
|
|
Self::changeset_to_changes(old_text, new_text, changes, self.offset_encoding)
|
2021-02-17 12:28:55 +04:00
|
|
|
}
|
2021-05-06 08:56:34 +04:00
|
|
|
lsp::TextDocumentSyncKind::None => return None,
|
2020-10-23 13:48:03 +04:00
|
|
|
};
|
|
|
|
|
2021-05-06 08:56:34 +04:00
|
|
|
Some(self.notify::<lsp::notification::DidChangeTextDocument>(
|
|
|
|
lsp::DidChangeTextDocumentParams {
|
|
|
|
text_document,
|
|
|
|
content_changes: changes,
|
|
|
|
},
|
|
|
|
))
|
2020-10-21 11:42:45 +04:00
|
|
|
}
|
|
|
|
|
2021-05-06 10:08:59 +04:00
|
|
|
pub fn text_document_did_close(
|
2020-12-25 12:20:09 +04:00
|
|
|
&self,
|
|
|
|
text_document: lsp::TextDocumentIdentifier,
|
2021-05-06 10:08:59 +04:00
|
|
|
) -> impl Future<Output = Result<()>> {
|
2020-10-21 11:42:45 +04:00
|
|
|
self.notify::<lsp::notification::DidCloseTextDocument>(lsp::DidCloseTextDocumentParams {
|
2020-12-25 12:20:09 +04:00
|
|
|
text_document,
|
2020-10-21 11:42:45 +04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// will_save / will_save_wait_until
|
|
|
|
|
2021-02-24 11:07:39 +04:00
|
|
|
pub async fn text_document_did_save(
|
|
|
|
&self,
|
|
|
|
text_document: lsp::TextDocumentIdentifier,
|
2021-03-12 11:20:56 +04:00
|
|
|
text: &Rope,
|
2021-02-24 11:07:39 +04:00
|
|
|
) -> Result<()> {
|
2021-03-16 10:30:29 +04:00
|
|
|
let capabilities = self.capabilities.as_ref().unwrap();
|
2021-03-12 11:20:56 +04:00
|
|
|
|
|
|
|
let include_text = match &capabilities.text_document_sync {
|
|
|
|
Some(lsp::TextDocumentSyncCapability::Options(lsp::TextDocumentSyncOptions {
|
|
|
|
save: Some(options),
|
|
|
|
..
|
|
|
|
})) => match options {
|
|
|
|
lsp::TextDocumentSyncSaveOptions::Supported(true) => false,
|
|
|
|
lsp::TextDocumentSyncSaveOptions::SaveOptions(lsp_types::SaveOptions {
|
|
|
|
include_text,
|
|
|
|
}) => include_text.unwrap_or(false),
|
|
|
|
// Supported(false)
|
|
|
|
_ => return Ok(()),
|
|
|
|
},
|
|
|
|
// unsupported
|
|
|
|
_ => return Ok(()),
|
|
|
|
};
|
|
|
|
|
2021-02-24 11:07:39 +04:00
|
|
|
self.notify::<lsp::notification::DidSaveTextDocument>(lsp::DidSaveTextDocumentParams {
|
|
|
|
text_document,
|
2021-03-12 11:20:56 +04:00
|
|
|
text: include_text.then(|| text.into()),
|
2021-02-24 11:07:39 +04:00
|
|
|
})
|
|
|
|
.await
|
2020-10-21 11:42:45 +04:00
|
|
|
}
|
2020-12-23 11:20:49 +04:00
|
|
|
|
2021-05-06 10:08:59 +04:00
|
|
|
pub fn completion(
|
2020-12-25 12:20:09 +04:00
|
|
|
&self,
|
|
|
|
text_document: lsp::TextDocumentIdentifier,
|
|
|
|
position: lsp::Position,
|
2021-05-06 10:08:59 +04:00
|
|
|
) -> impl Future<Output = Result<Value>> {
|
2021-03-26 11:02:13 +04:00
|
|
|
// ) -> Result<Vec<lsp::CompletionItem>> {
|
2020-12-23 11:20:49 +04:00
|
|
|
let params = lsp::CompletionParams {
|
|
|
|
text_document_position: lsp::TextDocumentPositionParams {
|
2020-12-25 12:20:09 +04:00
|
|
|
text_document,
|
|
|
|
position,
|
2020-12-23 11:20:49 +04:00
|
|
|
},
|
|
|
|
// TODO: support these tokens by async receiving and updating the choice list
|
|
|
|
work_done_progress_params: lsp::WorkDoneProgressParams {
|
|
|
|
work_done_token: None,
|
|
|
|
},
|
|
|
|
partial_result_params: lsp::PartialResultParams {
|
|
|
|
partial_result_token: None,
|
|
|
|
},
|
|
|
|
context: None,
|
|
|
|
// lsp::CompletionContext { trigger_kind: , trigger_character: Some(), }
|
|
|
|
};
|
|
|
|
|
2021-05-06 10:08:59 +04:00
|
|
|
self.call::<lsp::request::Completion>(params)
|
2020-12-23 11:20:49 +04:00
|
|
|
}
|
2021-02-25 13:07:47 +04:00
|
|
|
|
2021-05-06 12:15:49 +04:00
|
|
|
pub fn text_document_signature_help(
|
2021-02-25 13:07:47 +04:00
|
|
|
&self,
|
|
|
|
text_document: lsp::TextDocumentIdentifier,
|
|
|
|
position: lsp::Position,
|
2021-05-06 12:15:49 +04:00
|
|
|
) -> impl Future<Output = Result<Value>> {
|
2021-02-25 13:07:47 +04:00
|
|
|
let params = lsp::SignatureHelpParams {
|
|
|
|
text_document_position_params: lsp::TextDocumentPositionParams {
|
|
|
|
text_document,
|
|
|
|
position,
|
|
|
|
},
|
|
|
|
work_done_progress_params: lsp::WorkDoneProgressParams {
|
|
|
|
work_done_token: None,
|
|
|
|
},
|
|
|
|
context: None,
|
|
|
|
// lsp::SignatureHelpContext
|
|
|
|
};
|
|
|
|
|
2021-05-06 12:15:49 +04:00
|
|
|
self.call::<lsp::request::SignatureHelpRequest>(params)
|
2021-02-25 13:07:47 +04:00
|
|
|
}
|
|
|
|
|
2021-05-06 12:15:49 +04:00
|
|
|
pub fn text_document_hover(
|
2021-02-25 13:07:47 +04:00
|
|
|
&self,
|
|
|
|
text_document: lsp::TextDocumentIdentifier,
|
|
|
|
position: lsp::Position,
|
2021-05-06 12:15:49 +04:00
|
|
|
) -> impl Future<Output = Result<Value>> {
|
2021-02-25 13:07:47 +04:00
|
|
|
let params = lsp::HoverParams {
|
|
|
|
text_document_position_params: lsp::TextDocumentPositionParams {
|
|
|
|
text_document,
|
|
|
|
position,
|
|
|
|
},
|
|
|
|
work_done_progress_params: lsp::WorkDoneProgressParams {
|
|
|
|
work_done_token: None,
|
|
|
|
},
|
|
|
|
// lsp::SignatureHelpContext
|
|
|
|
};
|
|
|
|
|
2021-05-06 12:15:49 +04:00
|
|
|
self.call::<lsp::request::HoverRequest>(params)
|
2021-02-25 13:07:47 +04:00
|
|
|
}
|
2021-02-26 10:52:43 +04:00
|
|
|
|
|
|
|
// formatting
|
|
|
|
|
|
|
|
pub async fn text_document_formatting(
|
|
|
|
&self,
|
|
|
|
text_document: lsp::TextDocumentIdentifier,
|
|
|
|
options: lsp::FormattingOptions,
|
|
|
|
) -> anyhow::Result<Vec<lsp::TextEdit>> {
|
2021-03-16 10:30:29 +04:00
|
|
|
let capabilities = self.capabilities.as_ref().unwrap();
|
2021-02-26 10:52:43 +04:00
|
|
|
|
|
|
|
// check if we're able to format
|
2021-03-31 18:42:16 +04:00
|
|
|
match capabilities.document_formatting_provider {
|
2021-04-06 14:45:14 +04:00
|
|
|
Some(lsp::OneOf::Left(true)) | Some(lsp::OneOf::Right(_)) => (),
|
2021-02-26 10:52:43 +04:00
|
|
|
// None | Some(false)
|
|
|
|
_ => return Ok(Vec::new()),
|
|
|
|
};
|
|
|
|
// TODO: return err::unavailable so we can fall back to tree sitter formatting
|
|
|
|
|
|
|
|
let params = lsp::DocumentFormattingParams {
|
|
|
|
text_document,
|
|
|
|
options,
|
|
|
|
work_done_progress_params: lsp::WorkDoneProgressParams {
|
|
|
|
work_done_token: None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let response = self.request::<lsp::request::Formatting>(params).await?;
|
|
|
|
|
|
|
|
Ok(response.unwrap_or_default())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn text_document_range_formatting(
|
|
|
|
&self,
|
|
|
|
text_document: lsp::TextDocumentIdentifier,
|
|
|
|
range: lsp::Range,
|
|
|
|
options: lsp::FormattingOptions,
|
|
|
|
) -> anyhow::Result<Vec<lsp::TextEdit>> {
|
2021-03-16 10:30:29 +04:00
|
|
|
let capabilities = self.capabilities.as_ref().unwrap();
|
2021-02-26 10:52:43 +04:00
|
|
|
|
|
|
|
// check if we're able to format
|
2021-03-31 18:42:16 +04:00
|
|
|
match capabilities.document_range_formatting_provider {
|
2021-04-06 14:45:14 +04:00
|
|
|
Some(lsp::OneOf::Left(true)) | Some(lsp::OneOf::Right(_)) => (),
|
2021-02-26 10:52:43 +04:00
|
|
|
// None | Some(false)
|
|
|
|
_ => return Ok(Vec::new()),
|
|
|
|
};
|
|
|
|
// TODO: return err::unavailable so we can fall back to tree sitter formatting
|
|
|
|
|
|
|
|
let params = lsp::DocumentRangeFormattingParams {
|
|
|
|
text_document,
|
|
|
|
range,
|
|
|
|
options,
|
|
|
|
work_done_progress_params: lsp::WorkDoneProgressParams {
|
|
|
|
work_done_token: None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let response = self
|
|
|
|
.request::<lsp::request::RangeFormatting>(params)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(response.unwrap_or_default())
|
|
|
|
}
|
2021-02-22 02:22:38 +04:00
|
|
|
|
2021-05-06 12:15:49 +04:00
|
|
|
fn goto_request<
|
2021-03-11 08:01:28 +04:00
|
|
|
T: lsp::request::Request<
|
|
|
|
Params = lsp::GotoDefinitionParams,
|
|
|
|
Result = Option<lsp::GotoDefinitionResponse>,
|
|
|
|
>,
|
|
|
|
>(
|
2021-03-03 02:57:18 +04:00
|
|
|
&self,
|
2021-03-11 02:35:12 +04:00
|
|
|
text_document: lsp::TextDocumentIdentifier,
|
|
|
|
position: lsp::Position,
|
2021-05-06 12:15:49 +04:00
|
|
|
) -> impl Future<Output = Result<Value>> {
|
2021-03-11 02:35:12 +04:00
|
|
|
let params = lsp::GotoDefinitionParams {
|
|
|
|
text_document_position_params: lsp::TextDocumentPositionParams {
|
|
|
|
text_document,
|
|
|
|
position,
|
|
|
|
},
|
|
|
|
work_done_progress_params: lsp::WorkDoneProgressParams {
|
|
|
|
work_done_token: None,
|
|
|
|
},
|
|
|
|
partial_result_params: lsp::PartialResultParams {
|
|
|
|
partial_result_token: None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-05-06 12:15:49 +04:00
|
|
|
self.call::<T>(params)
|
2021-03-03 02:57:18 +04:00
|
|
|
}
|
|
|
|
|
2021-05-06 12:15:49 +04:00
|
|
|
pub fn goto_definition(
|
2021-02-22 02:22:38 +04:00
|
|
|
&self,
|
|
|
|
text_document: lsp::TextDocumentIdentifier,
|
|
|
|
position: lsp::Position,
|
2021-05-06 12:15:49 +04:00
|
|
|
) -> impl Future<Output = Result<Value>> {
|
2021-03-11 08:01:28 +04:00
|
|
|
self.goto_request::<lsp::request::GotoDefinition>(text_document, position)
|
2021-03-03 02:57:18 +04:00
|
|
|
}
|
|
|
|
|
2021-05-06 12:15:49 +04:00
|
|
|
pub fn goto_type_definition(
|
2021-03-03 02:57:18 +04:00
|
|
|
&self,
|
|
|
|
text_document: lsp::TextDocumentIdentifier,
|
|
|
|
position: lsp::Position,
|
2021-05-06 12:15:49 +04:00
|
|
|
) -> impl Future<Output = Result<Value>> {
|
2021-03-11 08:01:28 +04:00
|
|
|
self.goto_request::<lsp::request::GotoTypeDefinition>(text_document, position)
|
2021-03-03 02:57:18 +04:00
|
|
|
}
|
|
|
|
|
2021-05-06 12:15:49 +04:00
|
|
|
pub fn goto_implementation(
|
2021-03-03 02:57:18 +04:00
|
|
|
&self,
|
|
|
|
text_document: lsp::TextDocumentIdentifier,
|
|
|
|
position: lsp::Position,
|
2021-05-06 12:15:49 +04:00
|
|
|
) -> impl Future<Output = Result<Value>> {
|
2021-03-11 08:01:28 +04:00
|
|
|
self.goto_request::<lsp::request::GotoImplementation>(text_document, position)
|
2021-03-03 02:57:18 +04:00
|
|
|
}
|
|
|
|
|
2021-05-06 12:15:49 +04:00
|
|
|
pub fn goto_reference(
|
2021-03-03 02:57:18 +04:00
|
|
|
&self,
|
|
|
|
text_document: lsp::TextDocumentIdentifier,
|
|
|
|
position: lsp::Position,
|
2021-05-06 12:15:49 +04:00
|
|
|
) -> impl Future<Output = Result<Value>> {
|
2021-03-03 02:57:18 +04:00
|
|
|
let params = lsp::ReferenceParams {
|
|
|
|
text_document_position: lsp::TextDocumentPositionParams {
|
|
|
|
text_document,
|
|
|
|
position,
|
|
|
|
},
|
|
|
|
context: lsp::ReferenceContext {
|
|
|
|
include_declaration: true,
|
|
|
|
},
|
|
|
|
work_done_progress_params: lsp::WorkDoneProgressParams {
|
|
|
|
work_done_token: None,
|
|
|
|
},
|
|
|
|
partial_result_params: lsp::PartialResultParams {
|
|
|
|
partial_result_token: None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-05-06 12:15:49 +04:00
|
|
|
self.call::<lsp::request::References>(params)
|
2021-02-22 02:22:38 +04:00
|
|
|
}
|
2020-10-21 11:42:45 +04:00
|
|
|
}
|