Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

double-clicking a model file (obj, stl, etc) will auto generate a file with an import statement and open it #2400

Merged
merged 3 commits into from
May 20, 2024
Merged
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
38 changes: 38 additions & 0 deletions src/wasm-lib/kcl/src/settings/types/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,44 @@
.await
.map_err(|e| anyhow::anyhow!("Error loading project from path {}: {:?}", source_path.display(), e))?;

// If we got a import model file, we need to check if we have a file in the project for
// this import model.
if crate::settings::utils::IMPORT_FILE_EXTENSIONS.contains(&ext) {
let import_file_name = source_path
.file_name()
.ok_or_else(|| anyhow::anyhow!("Error getting the file name of the file: {}", source_path.display()))?
.to_string_lossy()
.to_string();
// Check if we have a file in the project for this import model.
let kcl_wrapper_filename = format!("{}.kcl", import_file_name);
let kcl_wrapper_file_path = parent.join(&kcl_wrapper_filename);

if !kcl_wrapper_file_path.exists() {

Check warning on line 108 in src/wasm-lib/kcl/src/settings/types/file.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/settings/types/file.rs#L98-L108

Added lines #L98 - L108 were not covered by tests
// Create the file in the project.
// With the default import content.
tokio::fs::write(
&kcl_wrapper_file_path,
format!(
r#"// This file was automatically generated by the application when you
// double-clicked on the model file.
// You can edit this file to add your own content.
// But we recommend you keep the import statement as it is.
// For more information on the import statement, see the documentation at:
// https://zoo.dev/docs/kcl/import
const model = import("{}")"#,
import_file_name
)
.as_bytes(),
)
.await?;
}

Check warning on line 126 in src/wasm-lib/kcl/src/settings/types/file.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/settings/types/file.rs#L111-L126

Added lines #L111 - L126 were not covered by tests

return Ok(ProjectState {
project,
current_file: Some(kcl_wrapper_file_path.display().to_string()),
});
}

Check warning on line 133 in src/wasm-lib/kcl/src/settings/types/file.rs

View check run for this annotation

Codecov / codecov/patch

src/wasm-lib/kcl/src/settings/types/file.rs#L128-L133

Added lines #L128 - L133 were not covered by tests
Ok(ProjectState {
project,
current_file: Some(source_path.display().to_string()),
Expand Down
13 changes: 10 additions & 3 deletions src/wasm-lib/kcl/src/settings/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@ use clap::ValueEnum;
use crate::settings::types::file::FileEntry;

lazy_static::lazy_static! {
pub static ref RELEVANT_EXTENSIONS: Vec<String> = {
let mut relevant_extensions = vec!["kcl".to_string(), "stp".to_string(), "glb".to_string(), "fbxb".to_string()];

pub static ref IMPORT_FILE_EXTENSIONS: Vec<String> = {
let mut import_file_extensions = vec!["stp".to_string(), "glb".to_string(), "fbxb".to_string()];
let named_extensions = kittycad::types::FileImportFormat::value_variants()
.iter()
.map(|x| format!("{}", x))
.collect::<Vec<String>>();
// Add all the default import formats.
relevant_extensions.extend_from_slice(&named_extensions);
import_file_extensions.extend_from_slice(&named_extensions);
import_file_extensions
};

pub static ref RELEVANT_EXTENSIONS: Vec<String> = {
let mut relevant_extensions = IMPORT_FILE_EXTENSIONS.clone();
relevant_extensions.push("kcl".to_string());
relevant_extensions
};
}
Expand Down
Loading