Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

live preview #60

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
9 changes: 8 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ license = "Apache-2.0 OR MIT"

[dependencies]
append-only-vec = "0.1.2"
base64 = "0.21.0"
codespan-reporting = "0.11"
comemo = "0.2"
dirs = "4"
Expand Down
77 changes: 65 additions & 12 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
use std::fs;
use std::{fs, path::Path};

use base64::Engine as _;
use base64::engine::general_purpose::STANDARD as BASE64;
use serde_json::Value;
use tower_lsp::{
jsonrpc::{Error, Result},
lsp_types::Url,
lsp_types::{MessageType, Url},
};
use typst::geom::Color;

use crate::Backend;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LspCommand {
ExportPdf,
GeneratePreview,
}

impl From<LspCommand> for String {
fn from(command: LspCommand) -> Self {
match command {
LspCommand::ExportPdf => "typst-lsp.doPdfExport".to_string(),
LspCommand::GeneratePreview => "typst-lsp.generatePreview".to_string(),
}
}
}
Expand All @@ -25,6 +30,7 @@ impl LspCommand {
pub fn parse(command: &str) -> Option<Self> {
match command {
"typst-lsp.doPdfExport" => Some(Self::ExportPdf),
"typst-lsp.generatePreview" => Some(Self::GeneratePreview),
_ => None,
}
}
Expand All @@ -34,20 +40,23 @@ impl LspCommand {
}
}

fn validate_uri_argument(arguments: Vec<Value>) -> Result<Url> {
if arguments.is_empty() {
return Err(Error::invalid_params("Missing file URI argument"));
}
let Some(file_uri) = arguments.first().and_then(|v| v.as_str()) else {
return Err(Error::invalid_params(
"Missing file URI as first argument",
));
};
Url::parse(file_uri).map_err(|_| Error::invalid_params("Parameter is not a valid URI"))
}

/// Here are implemented the handlers for each command.
impl Backend {
/// Export the current document as a PDF file. The client is reponsible for passing the correct file URI.
pub async fn command_export_pdf(&self, arguments: Vec<Value>) -> Result<()> {
if arguments.is_empty() {
return Err(Error::invalid_params("Missing file URI argument"));
}
let Some(file_uri) = arguments.first().and_then(|v| v.as_str()) else {
return Err(Error::invalid_params(
"Missing file URI as first argument",
));
};
let file_uri = Url::parse(file_uri)
.map_err(|_| Error::invalid_params("Parameter is not a valid URI"))?;
let file_uri = validate_uri_argument(arguments)?;
let text = fs::read_to_string(
file_uri
.to_file_path()
Expand All @@ -57,4 +66,48 @@ impl Backend {
self.compile_diags_export(file_uri, text, true).await;
Ok(())
}

pub async fn command_generate_preview(&self, arguments: Vec<Value>) -> Result<Option<Value>> {
let file_uri = validate_uri_argument(arguments)?;
let text = fs::read_to_string(
file_uri
.to_file_path()
.map_err(|_| Error::invalid_params("Could not convert file URI to path"))?,
)
.map_err(|_| Error::internal_error())?;

let mut world_lock = self.world.write().await;
let world = world_lock.as_mut().unwrap();

world.reset();

match world.resolve_with(Path::new(&file_uri.to_file_path().unwrap()), &text) {
Ok(id) => {
world.main = id;
}
Err(e) => {
self.client
.log_message(MessageType::ERROR, format!("{:?}", e))
.await;
}
}

match typst::compile(world) {
Ok(document) => {
let data_urls: Vec<serde_json::Value> = document
.pages
.iter()
.map(|frame| typst::export::render(frame, 1.5, Color::WHITE))
.map(|pixmap| pixmap.encode_png().unwrap())
.map(|buf| BASE64.encode(buf))
.map(|encoded| Value::String("data:image/png;base64,".to_string() + &encoded))
.collect();

return Ok(Some(serde_json::Value::Array(data_urls)));
}
Err(_errors) => {}
};

Ok(None)
}
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ impl LanguageServer for Backend {
Some(LspCommand::ExportPdf) => {
self.command_export_pdf(arguments).await?;
}
Some(LspCommand::GeneratePreview) => {
return Ok(self.command_generate_preview(arguments).await?)
}
None => {
return Err(Error::method_not_found());
}
Expand Down