feat: debug lsp with mirror feature

This commit is contained in:
Myriad-Dreamin 2024-03-09 15:25:03 +08:00
parent b1d977cfbe
commit 23d6237dfe
8 changed files with 134 additions and 15 deletions

View file

@ -18,6 +18,7 @@ once_cell.workspace = true
anyhow.workspace = true
comemo.workspace = true
tokio.workspace = true
tokio-util.workspace = true
futures.workspace = true
itertools.workspace = true
async-trait.workspace = true
@ -28,6 +29,12 @@ serde_json.workspace = true
parking_lot.workspace = true
paste = "1.0"
clap = { workspace = true, optional = true }
clap_builder.workspace = true
clap_complete.workspace = true
clap_complete_fig.workspace = true
clap_mangen.workspace = true
typst.workspace = true
typst-pdf.workspace = true
typst-assets = { workspace = true, features = ["fonts"] }
@ -43,5 +50,14 @@ typst-preview.workspace = true
tower.workspace = true
tower-lsp.workspace = true
[features]
default = ["cli"]
cli = ["clap"]
[build-dependencies]
anyhow.workspace = true
vergen.workspace = true
cargo_metadata = "0.18.0"
# [lints]
# workspace = true

23
crates/tinymist/build.rs Normal file
View file

@ -0,0 +1,23 @@
use anyhow::Result;
use vergen::EmitBuilder;
fn main() -> Result<()> {
// Emit the instructions
EmitBuilder::builder()
.all_cargo()
.build_timestamp()
.git_sha(false)
.git_describe(true, true, None)
.all_rustc()
.emit()?;
let metadata = cargo_metadata::MetadataCommand::new().exec().unwrap();
let typst = metadata
.packages
.iter()
.find(|package| package.name == "typst")
.expect("Typst should be a dependency");
println!("cargo:rustc-env=TYPST_VERSION={}", typst.version);
Ok(())
}

View file

@ -0,0 +1,34 @@
use once_cell::sync::Lazy;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "clap", derive(clap::Parser))]
#[cfg_attr(feature = "clap", clap(name = "tinymist", author, version, about, long_version(LONG_VERSION.as_str())))]
pub struct CliArguments {
/// Mirror the stdin to the specified file
#[cfg_attr(feature = "clap", clap(long, default_value = "", value_name = "FILE"))]
pub mirror: String,
/// Mirror input from the file
#[cfg_attr(feature = "clap", clap(long, default_value = "", value_name = "FILE"))]
pub mirror_input: String,
}
pub static LONG_VERSION: Lazy<String> = Lazy::new(|| {
format!(
"
Build Timestamp: {}
Build Git Describe: {}
Commit SHA: {}
Commit Date: {}
Commit Branch: {}
Cargo Target Triple: {}
Typst Version: {}
",
env!("VERGEN_BUILD_TIMESTAMP"),
env!("VERGEN_GIT_DESCRIBE"),
option_env!("VERGEN_GIT_SHA").unwrap_or("None"),
option_env!("VERGEN_GIT_COMMIT_TIMESTAMP").unwrap_or("None"),
option_env!("VERGEN_GIT_BRANCH").unwrap_or("None"),
env!("VERGEN_CARGO_TARGET_TRIPLE"),
env!("TYPST_VERSION"),
)
});

View file

@ -2,30 +2,23 @@
use core::fmt;
use core::task::{Context, Poll};
use std::io::Write;
use std::time::Instant;
use clap::Parser;
use futures::future::BoxFuture;
use log::info;
use tinymist::TypstServer;
use tokio::io::AsyncRead;
use tokio_util::io::InspectReader;
use tower_lsp::{
jsonrpc::{Request, Response},
LspService, Server,
};
// #[derive(Debug, Clone)]
// struct Args {}
use crate::args::CliArguments;
// fn arg_parser() -> OptionParser<Args> {
// construct!(Args {}).to_options().version(
// format!(
// "{}, commit {} (Typst version {TYPST_VERSION})",
// env!("CARGO_PKG_VERSION"),
// env!("GIT_COMMIT")
// )
// .as_str(),
// )
// }
// pub const TYPST_VERSION: &str = env!("TYPST_VERSION");
mod args;
#[tokio::main]
async fn main() {
@ -41,7 +34,20 @@ async fn main() {
.filter_module("typst_ts_compiler::service::watch", log::LevelFilter::Debug)
.try_init();
let stdin = tokio::io::stdin();
let args = CliArguments::parse();
info!("Arguments: {:#?}", args);
let stdin: Box<dyn AsyncRead + Unpin> = if !args.mirror_input.is_empty() {
let file = tokio::fs::File::open(&args.mirror_input).await.unwrap();
Box::new(file)
} else if args.mirror.is_empty() {
Box::new(tokio::io::stdin())
} else {
let mut file = std::fs::File::create(&args.mirror).unwrap();
Box::new(InspectReader::new(tokio::io::stdin(), move |bytes| {
file.write_all(bytes).unwrap();
}))
};
let stdout = tokio::io::stdout();
let (inner, socket) = LspService::new(TypstServer::new);