Skip to content

Commit

Permalink
Let lsp be aware of the pinned file
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 6079959 commit 2f829f9
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 21 deletions.
18 changes: 16 additions & 2 deletions editors/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@
"enum": [
"never",
"onSave",
"onType"
"onPinnedMainSave",
"onType",
"onPinnedMainType"
],
"enumDescriptions": [
"Never export PDFs, you will manually run typst.",
"Export PDFs when you save a file.",
"Export PDFs as you type in a file."
"Export PDFs when you save the pinned file.",
"Export PDFs as you type in a file.",
"Export PDFs as you type in the pinned file."
]
},
"typst-lsp.rootPath": {
Expand Down Expand Up @@ -274,6 +278,16 @@
"title": "Export the currently open file as PDF",
"category": "Typst"
},
{
"command": "typst-lsp.pinMainToCurrent",
"title": "Pin the main file to the currently openning document",
"category": "Typst"
},
{
"command": "typst-lsp.unpinMain",
"title": "Unpin the main file",
"category": "Typst"
},
{
"command": "typst-lsp.showPdf",
"title": "Show the compiled PDF of the currently opened typst file",
Expand Down
27 changes: 27 additions & 0 deletions editors/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ async function startClient(context: ExtensionContext): Promise<void> {
context.subscriptions.push(
commands.registerCommand("typst-lsp.exportCurrentPdf", commandExportCurrentPdf)
);
context.subscriptions.push(
commands.registerCommand("typst-lsp.pinMainToCurrent", () => commandPinMain(true))
);
context.subscriptions.push(
commands.registerCommand("typst-lsp.unpinMain", () => commandPinMain(false))
);
context.subscriptions.push(commands.registerCommand("typst-lsp.showPdf", commandShowPdf));
context.subscriptions.push(commands.registerCommand("typst-lsp.clearCache", commandClearCache));

Expand Down Expand Up @@ -127,6 +133,27 @@ async function commandExportCurrentPdf(): Promise<void> {
});
}

async function commandPinMain(isPin: boolean): Promise<void> {
if (!isPin) {
await client?.sendRequest("workspace/executeCommand", {
command: "typst-lsp.doPinMain",
arguments: ["detached"],
});
}

const activeEditor = window.activeTextEditor;
if (activeEditor === undefined) {
return;
}

const uri = activeEditor.document.uri.toString();

await client?.sendRequest("workspace/executeCommand", {
command: "typst-lsp.doPinMain",
arguments: [uri],
});
}

/**
* Implements the functionality for the 'Show PDF' button shown in the editor title
* if a `.typ` file is opened.
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ pub enum ExportPdfMode {
Never,
#[default]
OnSave,
OnPinnedMainSave,
OnType,
OnPinnedMainType,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Deserialize)]
Expand Down
24 changes: 14 additions & 10 deletions src/server/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tower_lsp::{
jsonrpc::{Error, Result},
lsp_types::Url,
};
use tracing::error;
use tracing::{error, info};

use super::TypstServer;

Expand Down Expand Up @@ -97,14 +97,18 @@ impl TypstServer {
)
};

self.config
.write()
.await
.update_main_file(file_uri)
.await
.map_err(|err| {
error!(%err, "could not set main file");
jsonrpc::Error::internal_error()
})
let update_result = self.config.write().await.update_main_file(file_uri).await;

update_result.map_err(|err| {
error!(%err, "could not set main file");
jsonrpc::Error::internal_error()
})?;

info!(
"main file pinned: {main_url:?}",
main_url = self.main_url().await
);

Ok(())
}
}
7 changes: 7 additions & 0 deletions src/server/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ impl TypstServer {
let config = self.config.read().await;
match config.export_pdf {
ExportPdfMode::OnType => self.run_diagnostics_and_export(uri).await?,
ExportPdfMode::OnPinnedMainType => {
if let Some(main_uri) = self.main_url().await {
self.run_diagnostics_and_export(&main_uri).await?
} else {
self.run_diagnostics(uri).await?
}
}
_ => self.run_diagnostics(uri).await?,
}

Expand Down
5 changes: 3 additions & 2 deletions src/server/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ impl TypstServer {

let doc = self.document.lock().await.clone();

let fid = self.workspace().read().await.full_id(uri)?;
let result = self
.thread_with_world(uri)
.thread_with_world(self.main_url().await.as_ref().unwrap_or(uri))
.await?
.run(move |world| {
let source = world.main();
let source = world.source(fid.into()).ok()?;

let typst_offset =
lsp_to_typst::position_to_offset(position, position_encoding, &source);
Expand Down
25 changes: 18 additions & 7 deletions src/server/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,18 @@ impl LanguageServer for TypstServer {

let config = self.config.read().await;

if config.export_pdf == ExportPdfMode::OnSave {
if let Err(err) = self.run_diagnostics_and_export(&uri).await {
error!(%err, %uri, "could not handle source save");
};
}
let uri = match config.export_pdf {
ExportPdfMode::OnPinnedMainSave => Some(self.main_url().await.unwrap_or(uri)),
ExportPdfMode::OnSave => Some(uri),
_ => None,
};
let Some(uri) = uri else {
return;
};

if let Err(err) = self.run_diagnostics_and_export(&uri).await {
error!(%err, %uri, "could not handle source save");
};
}

#[tracing::instrument(skip(self))]
Expand Down Expand Up @@ -400,15 +407,19 @@ impl LanguageServer for TypstServer {

let position_encoding = self.const_config().position_encoding;
let doc = { self.document.lock().await.clone() };
let fid = self.workspace().read().await.full_id(&uri).map_err(|err| {
error!(%err, %uri, "error getting completion");
jsonrpc::Error::internal_error()
})?;
let completions = self
.thread_with_world(&uri)
.thread_with_world(self.main_url().await.as_ref().unwrap_or(&uri))
.await
.map_err(|err| {
error!(%err, %uri, "error getting completion");
jsonrpc::Error::internal_error()
})?
.run(move |world| {
let source = world.main();
let source = world.source(fid.into()).ok()?;

let typst_offset =
lsp_to_typst::position_to_offset(position, position_encoding, &source);
Expand Down
4 changes: 4 additions & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ impl TypstServer {
.expect("workspace should be initialized")
}

pub async fn main_url(&self) -> Option<Url> {
self.config.read().await.main_file.clone()
}

pub fn typst_global_scopes(&self) -> typst::foundations::Scopes {
typst::foundations::Scopes::new(Some(&TYPST_STDLIB))
}
Expand Down

0 comments on commit 2f829f9

Please sign in to comment.