use std::io::Error;
use std::sync::Arc;
use tower_lsp::lsp_types::{CompletionParams, MessageType};
use crate::backend::Backend;
use libjuno::ast::*;
use libjuno::{pest::Parser, *};
pub(super) async fn get_program(
backend: &Backend,
params: CompletionParams,
) -> Result<Program, Error> {
let document = backend
.workspace
.source(¶ms.text_document_position.text_document.uri)
.unwrap();
let pairs = match JunoParser::parse(Rule::program, &document) {
Ok(pairs) => pairs,
Err(e) => {
let _msg = format!("{}", e);
return Err(Error::other("parse error"));
}
};
let pair = pairs.into_iter().next().unwrap();
let result = parse_program(
pair,
"debug::lsp".to_string(),
Arc::from(document.as_str()),
params
.text_document_position
.text_document
.uri
.to_string()
.into(),
);
match result {
Ok(program) => Ok(program),
Err(e) => {
let msg = format!("{}", e);
backend.client.log_message(MessageType::ERROR, msg).await;
Err(Error::other("program error"))
}
}
}