Skip to content

Commit

Permalink
Add pinMain command
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored and nvarner committed Dec 29, 2023
1 parent 58f0b82 commit 6079959
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
29 changes: 28 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use itertools::Itertools;
use serde::Deserialize;
use serde_json::{Map, Value};
use tower_lsp::lsp_types::{
self, ConfigurationItem, InitializeParams, PositionEncodingKind, Registration,
self, ConfigurationItem, InitializeParams, PositionEncodingKind, Registration, Url,
};
use tracing::warn;

use crate::ext::InitializeParamsExt;

Expand Down Expand Up @@ -58,6 +59,7 @@ const CONFIG_ITEMS: &[&str] = &[

#[derive(Default)]
pub struct Config {
pub main_file: Option<Url>,
pub export_pdf: ExportPdfMode,
pub root_path: Option<PathBuf>,
pub semantic_tokens: SemanticTokensMode,
Expand Down Expand Up @@ -150,8 +152,33 @@ impl Config {
self.formatter = formatter;
}

self.validate_main_file();
Ok(())
}

pub async fn update_main_file(&mut self, main_file: Option<Url>) -> anyhow::Result<()> {
self.main_file = main_file;

self.validate_main_file();
Ok(())
}

fn validate_main_file(&mut self) {
if let Some(main_file) = &self.main_file {
if let Some(root_path) = &self.root_path {
if let Ok(main_file) = main_file.to_file_path() {
if !main_file.starts_with(root_path) {
warn!(
"main file {main_file} is not in the workspace root {root_path}",
main_file = main_file.display(),
root_path = root_path.display(),
);
self.main_file = None;
}
}
}
}
}
}

impl fmt::Debug for Config {
Expand Down
38 changes: 37 additions & 1 deletion src/server/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use super::TypstServer;
pub enum LspCommand {
ExportPdf,
ClearCache,
PinMain,
}

impl From<LspCommand> for String {
fn from(command: LspCommand) -> Self {
match command {
LspCommand::ExportPdf => "typst-lsp.doPdfExport".to_string(),
LspCommand::ClearCache => "typst-lsp.doClearCache".to_string(),
LspCommand::PinMain => "typst-lsp.doPinMain".to_string(),
}
}
}
Expand All @@ -28,12 +30,17 @@ impl LspCommand {
match command {
"typst-lsp.doPdfExport" => Some(Self::ExportPdf),
"typst-lsp.doClearCache" => Some(Self::ClearCache),
"typst-lsp.doPinMain" => Some(Self::PinMain),
_ => None,
}
}

pub fn all_as_string() -> Vec<String> {
vec![Self::ExportPdf.into(), Self::ClearCache.into()]
vec![
Self::ExportPdf.into(),
Self::ClearCache.into(),
Self::PinMain.into(),
]
}
}

Expand Down Expand Up @@ -71,4 +78,33 @@ impl TypstServer {

Ok(())
}

/// Pin main file to some path.
#[tracing::instrument(skip_all)]
pub async fn command_pin_main(&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 = if file_uri == "detached" {
None
} else {
Some(
Url::parse(file_uri)
.map_err(|_| Error::invalid_params("Parameter is not a valid URI"))?,
)
};

self.config
.write()
.await
.update_main_file(file_uri)
.await
.map_err(|err| {
error!(%err, "could not set main file");
jsonrpc::Error::internal_error()
})
}
}
3 changes: 3 additions & 0 deletions src/server/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ impl LanguageServer for TypstServer {
Some(LspCommand::ClearCache) => {
self.command_clear_cache(arguments).await?;
}
Some(LspCommand::PinMain) => {
self.command_pin_main(arguments).await?;
}
None => {
error!("asked to execute unknown command");
return Err(jsonrpc::Error::method_not_found());
Expand Down

0 comments on commit 6079959

Please sign in to comment.