Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions prqlc/prqlc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cli = [
"walkdir",
]
default = ["cli"]
lsp = ["lsp-server", "lsp-types"] # Just a stub without any real functionality
serde_yaml = ["prqlc-parser/serde_yaml", "dep:serde_yaml"]
test-dbs = [
"rusqlite",
Expand Down Expand Up @@ -73,6 +74,8 @@ sqlformat = "0.3.5"
sqlparser = { version = "0.53.0", features = ["serde"] }
strum = { version = "0.27.0", features = ["std", "derive"] }
strum_macros = "0.27.0"
lsp-server = { version = "0.7.8", optional = true }
lsp-types = { version = "0.97.0", optional = true }

[build-dependencies]
vergen-gitcl = { version = "1.0.0", features = ["build"] }
Expand Down
91 changes: 91 additions & 0 deletions prqlc/prqlc/src/cli/lsp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use std::error::Error;

use lsp_types::OneOf;
use lsp_types::{
request::GotoDefinition, GotoDefinitionResponse, InitializeParams, ServerCapabilities,
};

use lsp_server::{Connection, ExtractError, Message, Request, RequestId, Response};

pub fn run() -> Result<(), Box<dyn Error + Sync + Send>> {
// Note that we must have our logging only write out to stderr.
eprintln!("starting PRQL LSP server");

// Create the transport. Includes the stdio (stdin and stdout) versions but this could
// also be implemented to use sockets or HTTP.
let (connection, io_threads) = Connection::stdio();

// Run the server and wait for the two threads to end (typically by trigger LSP Exit event).
let server_capabilities = serde_json::to_value(&ServerCapabilities {
definition_provider: Some(OneOf::Left(true)),
..Default::default()
})
.unwrap();
let initialization_params = match connection.initialize(server_capabilities) {
Ok(it) => it,
Err(e) => {
if e.channel_is_disconnected() {
io_threads.join()?;
}
return Err(e.into());
}
};
main_loop(connection, initialization_params)?;
io_threads.join()?;

// Shut down gracefully.
eprintln!("shutting down server");
Ok(())
}

fn main_loop(
connection: Connection,
params: serde_json::Value,
) -> Result<(), Box<dyn Error + Sync + Send>> {
let _params: InitializeParams = serde_json::from_value(params).unwrap();
eprintln!("starting main loop");
for msg in &connection.receiver {
eprintln!("got msg: {msg:?}");
match msg {
Message::Request(req) => {
if connection.handle_shutdown(&req)? {
return Ok(());
}
eprintln!("got request: {req:?}");
match cast::<GotoDefinition>(req) {
Ok((id, params)) => {
eprintln!("got gotoDefinition request #{id}: {params:?}");
let result = Some(GotoDefinitionResponse::Array(Vec::new()));
let result = serde_json::to_value(&result).unwrap();
let resp = Response {
id,
result: Some(result),
error: None,
};
connection.sender.send(Message::Response(resp))?;
continue;
}
Err(err @ ExtractError::JsonError { .. }) => panic!("{err:?}"),
Err(ExtractError::MethodMismatch(req)) => req,
};
// ...
}
Message::Response(resp) => {
eprintln!("got response: {resp:?}");
}
Message::Notification(not) => {
eprintln!("got notification: {not:?}");
return Ok(());
}
}
}
Ok(())
}

fn cast<R>(req: Request) -> Result<(RequestId, R::Params), ExtractError<Request>>
where
R: lsp_types::request::Request,
R::Params: serde::de::DeserializeOwned,
{
req.extract(R::METHOD)
}
11 changes: 11 additions & 0 deletions prqlc/prqlc/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ use prqlc::{Options, SourceTree, Target};
mod docs_generator;
mod highlight;
mod jinja;
#[cfg(feature = "lsp")]
mod lsp;
#[cfg(test)]
mod test;
mod watch;
Expand Down Expand Up @@ -165,6 +167,10 @@ enum Command {
#[command(name = "list-targets")]
ListTargets,

/// Language Server Protocol
#[command(hide = true)]
Lsp,

/// Print a shell completion for supported shells
#[command(name = "shell-completion")]
ShellCompletion {
Expand Down Expand Up @@ -332,6 +338,11 @@ impl Command {
io::stdout().write_all(&serde_json::to_string_pretty(&schema)?.into_bytes())?;
Ok(())
}
#[cfg(feature = "lsp")]
Command::Lsp => match lsp::run() {
Ok(_) => Ok(()),
Err(err) => Err(anyhow!(err)),
},
_ => self.run_io_command(),
}
}
Expand Down
Loading
Loading