Skip to content

Commit

Permalink
chore: upgrade dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Cubxity committed Sep 16, 2023
1 parent 12a2331 commit 5872fd0
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 355 deletions.
505 changes: 227 additions & 278 deletions src-tauri/Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tauri = { version = "1.2", features = ["devtools", "dialog-all"] }
anyhow = "1.0"
thiserror = "1.0"
enumset = { version = "1.0", features = ["serde"] }
siphasher = "0.3"
siphasher = "1.0"
once_cell = "1.17"
elsa = "1.8"
hex = "0.4"
Expand All @@ -38,8 +38,8 @@ dirs = "5.0"
walkdir = "2.3"
memmap2 = "0.7"

typst = { git = "https://github.com/typst/typst", tag = "v0.7.0" }
typst-library = { git = "https://github.com/typst/typst", tag = "v0.7.0" }
typst = { git = "https://github.com/typst/typst", tag = "v0.8.0" }
typst-library = { git = "https://github.com/typst/typst", tag = "v0.8.0" }
comemo = "0.3"

[features]
Expand Down
14 changes: 7 additions & 7 deletions src-tauri/src/ipc/commands/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub async fn fs_read_file_binary<R: Runtime>(
path: PathBuf,
) -> Result<Vec<u8>> {
let (_, path) = project_path(&window, &project_manager, path)?;
fs::read(&path).map_err(Into::into)
fs::read(path).map_err(Into::into)
}

#[tauri::command]
Expand All @@ -45,7 +45,7 @@ pub async fn fs_read_file_text<R: Runtime>(
path: PathBuf,
) -> Result<String> {
let (_, path) = project_path(&window, &project_manager, path)?;
fs::read_to_string(&path).map_err(Into::into)
fs::read_to_string(path).map_err(Into::into)
}

#[tauri::command]
Expand Down Expand Up @@ -78,7 +78,7 @@ pub async fn fs_write_file_binary<R: Runtime>(
content: Vec<u8>,
) -> Result<()> {
let (_, path) = project_path(&window, &project_manager, path)?;
fs::write(&path, content).map_err(Into::into)
fs::write(path, content).map_err(Into::into)
}

#[tauri::command]
Expand All @@ -89,7 +89,7 @@ pub async fn fs_write_file_text<R: Runtime>(
content: String,
) -> Result<()> {
let (project, absolute_path) = project_path(&window, &project_manager, &path)?;
let _ = File::create(&absolute_path)
let _ = File::create(absolute_path)
.map(|mut f| f.write_all(content.as_bytes()))
.map_err(Into::<Error>::into)?;

Expand All @@ -108,10 +108,10 @@ pub async fn fs_list_dir<R: Runtime>(
path: PathBuf,
) -> Result<Vec<FileItem>> {
let (_, path) = project_path(&window, &project_manager, path)?;
let list = fs::read_dir(&path).map_err(Into::<Error>::into)?;
let list = fs::read_dir(path).map_err(Into::<Error>::into)?;

let mut files: Vec<FileItem> = vec![];
for entry in list {
list.into_iter().for_each(|entry| {
if let Ok(entry) = entry {
if let (Ok(file_type), Ok(name)) = (entry.file_type(), entry.file_name().into_string())
{
Expand All @@ -125,7 +125,7 @@ pub async fn fs_list_dir<R: Runtime>(
files.push(FileItem { name, file_type: t });
}
}
}
});

files.sort_by(|a, b| {
if a.file_type == FileType::Directory && b.file_type == FileType::File {
Expand Down
29 changes: 20 additions & 9 deletions src-tauri/src/ipc/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ pub use fs::*;

use crate::project::{Project, ProjectManager};
use ::typst::diag::FileError;
use ::typst::util::PathExt;
use serde::{Serialize, Serializer};
use std::io;
use std::path::{Path, PathBuf};
use std::path::{Component, Path, PathBuf};
use std::sync::Arc;
use tauri::{Runtime, State, Window};

Expand Down Expand Up @@ -45,7 +44,7 @@ pub fn project<R: Runtime>(
project_manager: &State<Arc<ProjectManager<R>>>,
) -> Result<Arc<Project>> {
project_manager
.get_project(&window)
.get_project(window)
.ok_or(Error::UnknownProject)
}

Expand All @@ -58,11 +57,23 @@ pub fn project_path<R: Runtime, P: AsRef<Path>>(
path: P,
) -> Result<(Arc<Project>, PathBuf)> {
let project = project_manager
.get_project(&window)
.get_project(window)
.ok_or(Error::UnknownProject)?;
let path = project
.root
.join_rooted(path.as_ref())
.ok_or(Error::UnrelatedPath)?;
Ok((project, path))
let root_len = project.root.as_os_str().len();
let mut out = project.root.to_path_buf();
for component in path.as_ref().components() {
match component {
Component::Prefix(_) => {}
Component::RootDir => {}
Component::CurDir => {}
Component::ParentDir => {
out.pop();
if out.as_os_str().len() < root_len {
return Err(Error::UnrelatedPath);
}
}
Component::Normal(_) => out.push(component),
}
}
Ok((project, out))
}
10 changes: 6 additions & 4 deletions src-tauri/src/ipc/commands/typst.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{Error, Result};
use crate::ipc::commands::{project, project_path};
use crate::ipc::commands::project;
use crate::ipc::model::TypstRenderResponse;
use crate::ipc::{TypstCompileEvent, TypstDocument, TypstSourceError};
use crate::project::ProjectManager;
Expand All @@ -26,6 +26,7 @@ pub enum TypstCompletionKind {
Parameter = 3,
Constant = 4,
Symbol = 5,
Type = 6,
}

#[derive(Serialize, Debug)]
Expand All @@ -51,6 +52,7 @@ impl From<Completion> for TypstCompletion {
CompletionKind::Param => TypstCompletionKind::Parameter,
CompletionKind::Constant => TypstCompletionKind::Constant,
CompletionKind::Symbol(_) => TypstCompletionKind::Symbol,
CompletionKind::Type => TypstCompletionKind::Type,
},
label: value.label.to_string(),
apply: value.apply.map(|s| s.to_string()),
Expand All @@ -75,15 +77,15 @@ pub async fn typst_compile<R: Runtime>(

if !world.is_main_set() {
let config = project.config.read().unwrap();
if config.apply_main(&*project, &mut *world).is_err() {
if config.apply_main(&project, &mut world).is_err() {
debug!("skipped compilation for {:?} (main not set)", project);
return Ok(());
}
}

debug!("compiling {:?}: {:?}", path, project);
let now = Instant::now();
let mut tracer = Tracer::new(None);
let mut tracer = Tracer::new();
match typst::compile(&*world, &mut tracer) {
Ok(doc) => {
let elapsed = now.elapsed();
Expand Down Expand Up @@ -127,7 +129,7 @@ pub async fn typst_compile<R: Runtime>(
let errors: Vec<TypstSourceError> = match source {
Ok(source) => errors
.iter()
.filter(|e| e.span.id() == source_id)
.filter(|e| e.span.id() == Some(source_id))
.filter_map(|e| {
let span = source.find(e.span)?;
let range = span.range();
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async fn main() {

let project_manager = Arc::new(ProjectManager::<Wry>::new());
if let Ok(watcher) = ProjectManager::init_watcher(project_manager.clone()) {
let _ = project_manager.set_watcher(watcher);
project_manager.set_watcher(watcher);
}

tauri::Builder::default()
Expand Down
20 changes: 10 additions & 10 deletions src-tauri/src/project/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ use crate::project::ProjectWorld;
use log::debug;
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Formatter};
use std::path::{Component, Path, PathBuf};
use std::path::{Path, PathBuf};
use std::sync::{Mutex, RwLock};
use std::{fs, io};
use thiserror::Error;
use typst::diag::{FileError, FileResult};
use typst::doc::Document;
use typst::syntax::VirtualPath;

const PATH_PROJECT_CONFIG_FILE: &str = ".typstudio/project.json";

Expand Down Expand Up @@ -39,7 +40,7 @@ pub enum ProjectConfigError {
impl ProjectConfig {
pub fn read_from_file<P: AsRef<Path>>(path: P) -> Result<ProjectConfig, ProjectConfigError> {
let json = fs::read_to_string(path).map_err(Into::<ProjectConfigError>::into)?;
serde_json::from_str(&*json).map_err(Into::into)
serde_json::from_str(&json).map_err(Into::into)
}

pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), ProjectConfigError> {
Expand All @@ -49,7 +50,7 @@ impl ProjectConfig {

pub fn apply(&self, project: &Project) {
let mut world = project.world.lock().unwrap();
match self.apply_main(project, &mut *world) {
match self.apply_main(project, &mut world) {
Ok(_) => debug!(
"applied main source configuration for project {:?}",
project
Expand All @@ -63,17 +64,16 @@ impl ProjectConfig {

pub fn apply_main(&self, project: &Project, world: &mut ProjectWorld) -> FileResult<()> {
if let Some(main) = self.main.as_ref() {
if main.components().next() == Some(Component::RootDir) {
debug!("setting main path {:?} for {:?}", main, project);
world.set_main_path(&main);
return Ok(());
}
let vpath = VirtualPath::new(main);
debug!("setting main path {:?} for {:?}", main, project);
world.set_main_path(vpath);
return Ok(());
}

// ??
world.set_main(None);

Err(FileError::Other)
Err(FileError::NotSource)
}
}

Expand All @@ -89,7 +89,7 @@ impl Project {
pub fn load_from_path(path: PathBuf) -> Self {
let path = fs::canonicalize(&path).unwrap_or(path);
let config =
ProjectConfig::read_from_file(&path.join(PATH_PROJECT_CONFIG_FILE)).unwrap_or_default();
ProjectConfig::read_from_file(path.join(PATH_PROJECT_CONFIG_FILE)).unwrap_or_default();

Self {
world: ProjectWorld::new(path.clone()).into(),
Expand Down
72 changes: 29 additions & 43 deletions src-tauri/src/project/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use std::sync::Arc;
use typst::diag::{FileError, FileResult, PackageError, PackageResult};
use typst::eval::{Bytes, Datetime, Library};
use typst::font::{Font, FontBook};
use typst::syntax::{FileId, PackageSpec, Source};
use typst::util::PathExt;
use typst::syntax::{FileId, PackageSpec, Source, VirtualPath};
use typst::World;

pub struct ProjectWorld {
Expand All @@ -33,40 +32,33 @@ impl ProjectWorld {
path: P,
content: Option<String>,
) -> FileResult<FileId> {
let id = FileId::new(None, path.as_ref());
let vpath = VirtualPath::new(path);
let id = FileId::new(None, vpath.clone());
let mut slot = self.slot(id)?;

match slot.buffer.get_mut() {
// Only update existing buffers. There is no need to insert new buffers
Some(res) => {
// TODO: Avoid cloning?
let bytes = self.take_or_read_bytes(&path, content.clone())?;
match res {
Ok(b) => {
*b = bytes;
}
Err(_) => {
*res = Ok(bytes);
}
if let Some(res) = slot.buffer.get_mut() {
// TODO: Avoid cloning?
let bytes = self.take_or_read_bytes(&vpath, content.clone())?;
match res {
Ok(b) => {
*b = bytes;
}
Err(_) => {
*res = Ok(bytes);
}
}
None => {}
};
match slot.source.get_mut() {
// Only update existing sources. There is no need to insert new sources
Some(res) => {
let content = self.take_or_read(&path, content)?;
match res {
Ok(src) => {
// TODO: incremental edits
src.replace(content);
}
Err(_) => {
*res = Ok(Source::new(id, content));
}
if let Some(res) = slot.source.get_mut() {
let content = self.take_or_read(&vpath, content)?;
match res {
Ok(src) => {
// TODO: incremental edits
src.replace(content);
}
Err(_) => {
*res = Ok(Source::new(id, content));
}
}
None => {}
};
Ok(id)
}
Expand All @@ -75,8 +67,8 @@ impl ProjectWorld {
self.main = id
}

pub fn set_main_path<P: AsRef<Path>>(&mut self, main: P) {
self.set_main(Some(FileId::new(None, main.as_ref())))
pub fn set_main_path(&mut self, main: VirtualPath) {
self.set_main(Some(FileId::new(None, main)))
}

pub fn is_main_set(&self) -> bool {
Expand Down Expand Up @@ -105,7 +97,7 @@ impl ProjectWorld {

// This will disallow paths outside of the root directory. Note that this will
// still allow symlinks.
path = root.join_rooted(id.path()).ok_or(FileError::AccessDenied)?;
path = id.vpath().resolve(root).ok_or(FileError::AccessDenied)?;
}

Ok(RefMut::map(slots, |slots| {
Expand All @@ -118,31 +110,25 @@ impl ProjectWorld {
}))
}

fn take_or_read<P: AsRef<Path>>(&self, path: P, content: Option<String>) -> FileResult<String> {
fn take_or_read(&self, vpath: &VirtualPath, content: Option<String>) -> FileResult<String> {
if let Some(content) = content {
return Ok(content);
}

let path = self
.root
.join_rooted(path.as_ref())
.ok_or(FileError::AccessDenied)?;
let path = vpath.resolve(&self.root).ok_or(FileError::AccessDenied)?;
fs::read_to_string(&path).map_err(|e| FileError::from_io(e, &path))
}

fn take_or_read_bytes<P: AsRef<Path>>(
fn take_or_read_bytes(
&self,
path: P,
vpath: &VirtualPath,
content: Option<String>,
) -> FileResult<Bytes> {
if let Some(content) = content {
return Ok(Bytes::from(content.into_bytes()));
}

let path = self
.root
.join_rooted(path.as_ref())
.ok_or(FileError::AccessDenied)?;
let path = vpath.resolve(&self.root).ok_or(FileError::AccessDenied)?;
fs::read(&path)
.map_err(|e| FileError::from_io(e, &path))
.map(Bytes::from)
Expand Down
3 changes: 3 additions & 0 deletions src/lib/editor/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export class TypstCompletionProvider implements languages.CompletionItemProvider
case TypstCompletionKind.Symbol:
kind = languages.CompletionItemKind.Keyword;
break;
case TypstCompletionKind.Type:
kind = languages.CompletionItemKind.Class;
break;
}

let count = 0;
Expand Down
1 change: 1 addition & 0 deletions src/lib/ipc/typst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export enum TypstCompletionKind {
Parameter = 3,
Constant = 4,
Symbol = 5,
Type = 6,
}

export interface TypstCompletion {
Expand Down

0 comments on commit 5872fd0

Please sign in to comment.