Skip to content

Commit

Permalink
Add LSP request for input boundaries (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- authored Sep 18, 2024
1 parent 1b44ea7 commit 65a86ac
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 3 deletions.
9 changes: 7 additions & 2 deletions crates/ark/src/analysis/parse_boundaries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ use harp::vector::CharacterVector;
use harp::vector::Vector;
use harp::ParseResult;
use harp::RObject;
use serde::Serialize;

use crate::coordinates::LineRange;

/// Boundaries are ranges over lines of text.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct ParseBoundary {
pub range: LineRange,

#[serde(flatten)]
pub kind: ParseBoundaryKind,
}

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", content = "data")]
#[serde(rename_all = "snake_case")]
pub enum ParseBoundaryKind {
Whitespace,
Complete,
Expand Down
4 changes: 3 additions & 1 deletion crates/ark/src/coordinates/line_range.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::cmp;

use serde::Serialize;

/// Range over lines of text
///
/// The API is (incompletely) modeled after `text_size::TextRange` but the inner
/// types are not as opaque so it's easy to work with line numbers coming from
/// external sources. The type name mentions "line" to be self-documenting about
/// what the range represents.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
pub struct LineRange {
start: u32,
end: u32,
Expand Down
20 changes: 20 additions & 0 deletions crates/ark/src/lsp/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ use crate::lsp::handlers::ARK_VDOC_REQUEST;
use crate::lsp::help_topic;
use crate::lsp::help_topic::HelpTopicParams;
use crate::lsp::help_topic::HelpTopicResponse;
use crate::lsp::input_boundaries;
use crate::lsp::input_boundaries::InputBoundariesParams;
use crate::lsp::input_boundaries::InputBoundariesResponse;
use crate::lsp::main_loop::Event;
use crate::lsp::main_loop::GlobalState;
use crate::lsp::main_loop::TokioUnboundedSender;
Expand Down Expand Up @@ -92,6 +95,7 @@ pub(crate) enum LspRequest {
HelpTopic(HelpTopicParams),
OnTypeFormatting(DocumentOnTypeFormattingParams),
VirtualDocument(VirtualDocumentParams),
InputBoundaries(InputBoundariesParams),
}

#[derive(Debug)]
Expand All @@ -113,6 +117,7 @@ pub(crate) enum LspResponse {
HelpTopic(Option<HelpTopicResponse>),
OnTypeFormatting(Option<Vec<TextEdit>>),
VirtualDocument(VirtualDocumentResponse),
InputBoundaries(InputBoundariesResponse),
}

#[derive(Debug)]
Expand Down Expand Up @@ -347,6 +352,16 @@ impl Backend {
)
}

async fn input_boundaries(
&self,
params: InputBoundariesParams,
) -> tower_lsp::jsonrpc::Result<InputBoundariesResponse> {
cast_response!(
self.request(LspRequest::InputBoundaries(params)).await,
LspResponse::InputBoundaries
)
}

async fn notification(&self, params: Option<Value>) {
log::info!("Received Positron notification: {:?}", params);
}
Expand Down Expand Up @@ -400,6 +415,11 @@ pub fn start_lsp(runtime: Arc<Runtime>, address: String, conn_init_tx: Sender<bo
)
.custom_method(help_topic::POSITRON_HELP_TOPIC_REQUEST, Backend::help_topic)
.custom_method(ARK_VDOC_REQUEST, Backend::virtual_document)
// In principle this should probably be a Jupyter request
.custom_method(
input_boundaries::POSITRON_INPUT_BOUNDARIES_REQUEST,
Backend::input_boundaries,
)
.custom_method("positron/notification", Backend::notification)
.finish();

Expand Down
10 changes: 10 additions & 0 deletions crates/ark/src/lsp/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use tower_lsp::Client;
use tracing::Instrument;
use tree_sitter::Point;

use crate::analysis::parse_boundaries::parse_boundaries;
use crate::lsp;
use crate::lsp::completions::provide_completions;
use crate::lsp::completions::resolve_completion;
Expand All @@ -51,6 +52,8 @@ use crate::lsp::help_topic::HelpTopicParams;
use crate::lsp::help_topic::HelpTopicResponse;
use crate::lsp::hover::r_hover;
use crate::lsp::indent::indent_edit;
use crate::lsp::input_boundaries::InputBoundariesParams;
use crate::lsp::input_boundaries::InputBoundariesResponse;
use crate::lsp::main_loop::LspState;
use crate::lsp::offset::IntoLspOffset;
use crate::lsp::references::find_references;
Expand Down Expand Up @@ -393,3 +396,10 @@ pub(crate) fn handle_virtual_document(
Err(anyhow!("Can't find virtual document {}", params.path))
}
}

pub(crate) fn handle_input_boundaries(
params: InputBoundariesParams,
) -> anyhow::Result<InputBoundariesResponse> {
let boundaries = r_task(|| parse_boundaries(&params.text))?;
Ok(InputBoundariesResponse { boundaries })
}
17 changes: 17 additions & 0 deletions crates/ark/src/lsp/input_boundaries.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use serde::Deserialize;
use serde::Serialize;

use crate::analysis::parse_boundaries::ParseBoundary;

pub static POSITRON_INPUT_BOUNDARIES_REQUEST: &'static str = "positron/inputBoundaries";

#[derive(Debug, Eq, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InputBoundariesParams {
pub text: String,
}

#[derive(Debug, Eq, PartialEq, Clone, Serialize)]
pub struct InputBoundariesResponse {
pub boundaries: Vec<ParseBoundary>,
}
3 changes: 3 additions & 0 deletions crates/ark/src/lsp/main_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ impl GlobalState {
LspRequest::VirtualDocument(params) => {
respond(tx, handlers::handle_virtual_document(params), LspResponse::VirtualDocument)?;
},
LspRequest::InputBoundaries(params) => {
respond(tx, handlers::handle_input_boundaries(params), LspResponse::InputBoundaries)?;
},
};
},
},
Expand Down
1 change: 1 addition & 0 deletions crates/ark/src/lsp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod help_topic;
pub mod hover;
pub mod indent;
pub mod indexer;
pub mod input_boundaries;
pub mod main_loop;
pub mod markdown;
pub mod offset;
Expand Down

0 comments on commit 65a86ac

Please sign in to comment.