use std::collections::HashSet;
#[cfg(not(target_arch = "wasm32"))]
use eure::query::TextFileContent;
use eure::query::{Glob, TextFile};
use query_flow::RevisionCounter;
use serde_json::Value;
use crate::queries::{LspDiagnostics, LspFileDiagnostics, LspSemanticTokens};
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
pub enum CoreRequestId {
Int(i32),
Str(String),
}
impl CoreRequestId {
pub fn as_str(&self) -> String {
match self {
CoreRequestId::Int(n) => n.to_string(),
CoreRequestId::Str(s) => s.clone(),
}
}
}
impl From<i32> for CoreRequestId {
fn from(n: i32) -> Self {
CoreRequestId::Int(n)
}
}
impl From<String> for CoreRequestId {
fn from(s: String) -> Self {
CoreRequestId::Str(s)
}
}
impl From<&str> for CoreRequestId {
fn from(s: &str) -> Self {
CoreRequestId::Str(s.to_string())
}
}
impl From<&Value> for CoreRequestId {
fn from(v: &Value) -> Self {
serde_json::from_value(v.clone()).unwrap()
}
}
#[cfg(not(target_arch = "wasm32"))]
impl From<lsp_server::RequestId> for CoreRequestId {
fn from(id: lsp_server::RequestId) -> Self {
serde_json::from_value(serde_json::to_value(&id).unwrap()).unwrap()
}
}
#[cfg(not(target_arch = "wasm32"))]
impl From<&CoreRequestId> for lsp_server::RequestId {
fn from(id: &CoreRequestId) -> Self {
serde_json::from_value(serde_json::to_value(id).unwrap()).unwrap()
}
}
#[derive(Debug, Clone)]
pub enum Effect {
FetchFile(TextFile),
ExpandGlob {
id: String,
glob: Glob,
},
}
#[derive(Debug, Clone)]
pub struct LspError {
pub code: i32,
pub message: String,
}
impl LspError {
pub fn new(code: i32, message: impl Into<String>) -> Self {
Self {
code,
message: message.into(),
}
}
pub fn internal_error(message: impl Into<String>) -> Self {
Self::new(-32603, message)
}
pub fn invalid_params(message: impl Into<String>) -> Self {
Self::new(-32602, message)
}
pub fn method_not_found(method: &str) -> Self {
Self::new(-32601, format!("Method not found: {}", method))
}
}
#[derive(Debug, Clone)]
pub enum LspOutput {
Response {
id: CoreRequestId,
result: Result<Value, LspError>,
},
Notification { method: String, params: Value },
}
#[derive(Clone, Debug)]
pub struct CompletionRequest {
pub file: TextFile,
pub offset: u32,
}
#[derive(Clone, Debug)]
pub struct HoverRequest {
pub file: TextFile,
pub offset: u32,
}
#[derive(Clone, Debug)]
pub struct DefinitionRequest {
pub file: TextFile,
pub position: lsp_types::Position,
}
#[derive(Clone)]
pub enum CommandQuery {
SemanticTokensFull(LspSemanticTokens),
Completion(CompletionRequest),
Hover(HoverRequest),
Definition(DefinitionRequest),
SchemaContent(TextFile),
}
impl CommandQuery {
pub fn file(&self) -> &TextFile {
match self {
CommandQuery::SemanticTokensFull(q) => &q.file,
CommandQuery::Completion(q) => &q.file,
CommandQuery::Hover(q) => &q.file,
CommandQuery::Definition(q) => &q.file,
CommandQuery::SchemaContent(file) => file,
}
}
pub fn name(&self) -> &'static str {
match self {
CommandQuery::SemanticTokensFull(_) => "SemanticTokens",
CommandQuery::Completion(_) => "Completion",
CommandQuery::Hover(_) => "Hover",
CommandQuery::Definition(_) => "Definition",
CommandQuery::SchemaContent(_) => "SchemaContent",
}
}
}
pub enum CommandResult {
SemanticTokens(Option<lsp_types::SemanticTokens>),
Completion(Vec<lsp_types::CompletionItem>),
Hover(Option<lsp_types::Hover>),
Definition(Vec<lsp_types::LocationLink>),
SchemaContent(String),
}
pub struct PendingRequest {
pub id: CoreRequestId,
pub command: CommandQuery,
pub waiting_for: HashSet<TextFile>,
}
#[derive(Clone)]
pub struct DiagnosticsSubscription {
pub query: LspDiagnostics,
pub last_revision: RevisionCounter,
}
#[derive(Clone)]
pub struct FileDiagnosticsSubscription {
pub file: TextFile,
pub query: LspFileDiagnostics,
pub last_revision: RevisionCounter,
}
#[cfg(not(target_arch = "wasm32"))]
pub struct IoRequest {
pub file: TextFile,
}
#[cfg(not(target_arch = "wasm32"))]
pub struct IoResponse {
pub file: TextFile,
pub result: Result<TextFileContent, anyhow::Error>,
}