This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Configuring the Output Directory #39
Open
liamrosenfeld
wants to merge
7
commits into
nvarner:master
Choose a base branch
from
liamrosenfeld:output-dir
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d7c6dd7
feat: configuring the output directory
liamrosenfeld a57412c
Split Output Directory into Root and Relative Path
liamrosenfeld ae5197b
Absolute Option for Output Root
liamrosenfeld cf4abea
Merge branch 'master' into output-dir
liamrosenfeld 6f2898d
Compatibility Between Command and Output Dir
liamrosenfeld a17d797
Absolute Path in Output Path is Actually Absolute
liamrosenfeld 4fe2d05
Merge branch 'master' into output-dir
liamrosenfeld File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,7 +196,30 @@ impl LanguageServer for Backend { | |
_ => config::ExportPdfMode::OnSave, | ||
}) | ||
.unwrap_or_default(); | ||
|
||
let output_root = settings | ||
.get("outputRoot") | ||
.map(|val| match val { | ||
JsonValue::String(val) => match val.as_str() { | ||
"source" => config::OutputRoot::Source, | ||
"workspace" => config::OutputRoot::Workspace, | ||
"absolute" => config::OutputRoot::Absolute, | ||
_ => config::OutputRoot::default(), | ||
}, | ||
_ => config::OutputRoot::default(), | ||
}) | ||
.unwrap_or_default(); | ||
|
||
let output_path = settings | ||
.get("outputPath") | ||
.and_then(|x| x.as_str()) | ||
.unwrap_or_default() | ||
.to_string(); | ||
|
||
config.export_pdf = export_pdf; | ||
config.output_root = output_root; | ||
config.output_path = output_path; | ||
|
||
self.client | ||
.log_message(MessageType::INFO, "New settings applied") | ||
.await; | ||
|
@@ -226,6 +249,8 @@ impl Backend { | |
uri, | ||
text, | ||
matches!(config.export_pdf, config::ExportPdfMode::OnType), | ||
config.output_root, | ||
&config.output_path, | ||
) | ||
.await; | ||
} | ||
|
@@ -236,17 +261,26 @@ impl Backend { | |
uri, | ||
text, | ||
matches!(config.export_pdf, config::ExportPdfMode::OnSave), | ||
config.output_root, | ||
&config.output_path, | ||
) | ||
.await; | ||
} | ||
|
||
async fn compile_diags_export(&self, uri: Url, text: String, export: bool) { | ||
async fn compile_diags_export( | ||
&self, | ||
file: Url, | ||
text: String, | ||
export: bool, | ||
output_root: config::OutputRoot, | ||
relative_dir: &str, | ||
) { | ||
let mut world_lock = self.world.write().await; | ||
let world = world_lock.as_mut().unwrap(); | ||
|
||
world.reset(); | ||
|
||
match world.resolve_with(Path::new(&uri.to_file_path().unwrap()), &text) { | ||
match world.resolve_with(Path::new(&file.to_file_path().unwrap()), &text) { | ||
Ok(id) => { | ||
world.main = id; | ||
} | ||
|
@@ -263,7 +297,38 @@ impl Backend { | |
Ok(document) => { | ||
let buffer = typst::export::pdf(&document); | ||
if export { | ||
let output_path = uri.to_file_path().unwrap().with_extension("pdf"); | ||
let output_path = { | ||
let root_dir = match output_root { | ||
config::OutputRoot::Source => { | ||
file.to_file_path().unwrap().parent().unwrap().to_path_buf() | ||
} | ||
config::OutputRoot::Workspace => world.root().to_path_buf(), | ||
config::OutputRoot::Absolute => home::home_dir().unwrap(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would make the root path an Optional, and make it |
||
}; | ||
|
||
let file_name = format!( | ||
"{}.pdf", | ||
file.to_file_path() | ||
.unwrap() | ||
.file_stem() | ||
.unwrap() | ||
.to_string_lossy() | ||
); | ||
|
||
let path: PathBuf = [root_dir.to_str().unwrap(), relative_dir, &file_name] | ||
.iter() | ||
.collect(); | ||
|
||
// create intermediate dirs if missing | ||
if let Some(parent) = path.parent() { | ||
// discard result because if this failed and was necessary, | ||
// the save will fail and the error will be handled there | ||
let _ = fs::create_dir_all(parent); | ||
} | ||
|
||
path | ||
}; | ||
|
||
fs_message = match fs::write(&output_path, buffer) | ||
.map_err(|_| "failed to write PDF file".to_string()) | ||
{ | ||
|
@@ -273,7 +338,7 @@ impl Backend { | |
}), | ||
Err(e) => Some(LogMessage { | ||
message_type: MessageType::ERROR, | ||
message: format!("{:?}", e), | ||
message: format!("{:?} (writing to: {:?})", e, output_path), | ||
}), | ||
}; | ||
} | ||
|
@@ -291,7 +356,7 @@ impl Backend { | |
|
||
self.client | ||
.publish_diagnostics( | ||
uri.clone(), | ||
file.clone(), | ||
messages | ||
.into_iter() | ||
.map(|(message, range)| Diagnostic { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be "The root of the filesystem"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would make sense. I was designing it with a Unix filesystem in mind where VSCode would only have permission to save into the home directory anyway. I guess that is misleading when calling it "Absolute". I can change it to absolute and ensure it can handle
~/
.