Compare commits
	
		
			8 Commits
		
	
	
		
			revert-706
			...
			franknoiro
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 2c186c6cc1 | |||
| 7c830839de | |||
| 980c139060 | |||
| 4ee1c96e78 | |||
| 5e89683801 | |||
| e2dd6e742a | |||
| b3acb348d9 | |||
| 0aed462360 | 
| @ -31,6 +31,9 @@ import { useSettingsAuthContext } from 'hooks/useSettingsAuthContext' | |||||||
| import { markOnce } from 'lib/performance' | import { markOnce } from 'lib/performance' | ||||||
| import { commandBarActor } from 'machines/commandBarMachine' | import { commandBarActor } from 'machines/commandBarMachine' | ||||||
| import { useToken } from 'machines/appMachine' | import { useToken } from 'machines/appMachine' | ||||||
|  | import { unit } from '@kittycad/lib/dist/types/src' | ||||||
|  | import { changeKclSettings, unitLengthToUnitLen } from 'lang/wasm' | ||||||
|  | import { err } from 'lib/trap' | ||||||
|  |  | ||||||
| type MachineContext<T extends AnyStateMachine> = { | type MachineContext<T extends AnyStateMachine> = { | ||||||
|   state: StateFrom<T> |   state: StateFrom<T> | ||||||
| @ -159,7 +162,14 @@ export const FileMachineProvider = ({ | |||||||
|                 createdPath |                 createdPath | ||||||
|               ) |               ) | ||||||
|             } else { |             } else { | ||||||
|               await window.electron.writeFile(createdPath, input.content ?? '') |               const codeToWrite = changeKclSettings(input.content ?? '', { | ||||||
|  |                 defaultLengthUnits: unitLengthToUnitLen( | ||||||
|  |                   settings.context.modeling.defaultUnit.current | ||||||
|  |                 ), | ||||||
|  |                 defaultAngleUnits: { type: 'Degrees' }, | ||||||
|  |               }) | ||||||
|  |               if (err(codeToWrite)) return Promise.reject(codeToWrite) | ||||||
|  |               await window.electron.writeFile(createdPath, codeToWrite) | ||||||
|             } |             } | ||||||
|           } |           } | ||||||
|  |  | ||||||
| @ -188,7 +198,15 @@ export const FileMachineProvider = ({ | |||||||
|             }) |             }) | ||||||
|             createdName = name |             createdName = name | ||||||
|             createdPath = path |             createdPath = path | ||||||
|             await window.electron.writeFile(createdPath, input.content ?? '') |  | ||||||
|  |             const codeToWrite = changeKclSettings(input.content ?? '', { | ||||||
|  |               defaultLengthUnits: unitLengthToUnitLen( | ||||||
|  |                 settings.context.modeling.defaultUnit.current | ||||||
|  |               ), | ||||||
|  |               defaultAngleUnits: { type: 'Degrees' }, | ||||||
|  |             }) | ||||||
|  |             if (err(codeToWrite)) return Promise.reject(codeToWrite) | ||||||
|  |             await window.electron.writeFile(createdPath, codeToWrite) | ||||||
|           } |           } | ||||||
|  |  | ||||||
|           return { |           return { | ||||||
|  | |||||||
| @ -32,6 +32,8 @@ import { | |||||||
| } from 'lib/constants' | } from 'lib/constants' | ||||||
| import { codeManager, kclManager } from 'lib/singletons' | import { codeManager, kclManager } from 'lib/singletons' | ||||||
| import { Project } from 'lib/project' | import { Project } from 'lib/project' | ||||||
|  | import { changeKclSettings, unitLengthToUnitLen } from 'lang/wasm' | ||||||
|  | import { err } from 'lib/trap' | ||||||
|  |  | ||||||
| type MachineContext<T extends AnyStateMachine> = { | type MachineContext<T extends AnyStateMachine> = { | ||||||
|   state?: StateFrom<T> |   state?: StateFrom<T> | ||||||
| @ -122,7 +124,14 @@ const ProjectsContextWeb = ({ children }: { children: React.ReactNode }) => { | |||||||
|         createFile: fromPromise(async ({ input }) => { |         createFile: fromPromise(async ({ input }) => { | ||||||
|           // Browser version doesn't navigate, just overwrites the current file |           // Browser version doesn't navigate, just overwrites the current file | ||||||
|           clearImportSearchParams() |           clearImportSearchParams() | ||||||
|           codeManager.updateCodeStateEditor(input.code || '') |           const codeToWrite = changeKclSettings(input.code ?? '', { | ||||||
|  |             defaultLengthUnits: unitLengthToUnitLen( | ||||||
|  |               settings.modeling.defaultUnit.current | ||||||
|  |             ), | ||||||
|  |             defaultAngleUnits: { type: 'Degrees' }, | ||||||
|  |           }) | ||||||
|  |           if (err(codeToWrite)) return Promise.reject(codeToWrite) | ||||||
|  |           codeManager.updateCodeStateEditor(codeToWrite) | ||||||
|           await codeManager.writeToFile() |           await codeManager.writeToFile() | ||||||
|           await kclManager.executeCode(true) |           await kclManager.executeCode(true) | ||||||
|  |  | ||||||
| @ -406,7 +415,14 @@ const ProjectsContextDesktop = ({ | |||||||
|           }) |           }) | ||||||
|  |  | ||||||
|           fileName = name |           fileName = name | ||||||
|           await window.electron.writeFile(path, input.code || '') |           const codeToWrite = changeKclSettings(input.code ?? '', { | ||||||
|  |             defaultLengthUnits: unitLengthToUnitLen( | ||||||
|  |               settings.modeling.defaultUnit.current | ||||||
|  |             ), | ||||||
|  |             defaultAngleUnits: { type: 'Degrees' }, | ||||||
|  |           }) | ||||||
|  |           if (err(codeToWrite)) return Promise.reject(codeToWrite) | ||||||
|  |           await window.electron.writeFile(path, codeToWrite) | ||||||
|  |  | ||||||
|           return { |           return { | ||||||
|             message, |             message, | ||||||
|  | |||||||
| @ -4,10 +4,12 @@ import { codeManager, kclManager } from './singletons' | |||||||
| import { isDesktop } from './isDesktop' | import { isDesktop } from './isDesktop' | ||||||
| import { FILE_EXT } from './constants' | import { FILE_EXT } from './constants' | ||||||
| import { UnitLength_type } from '@kittycad/lib/dist/types/src/models' | import { UnitLength_type } from '@kittycad/lib/dist/types/src/models' | ||||||
| import { reportRejection } from './trap' | import { err, reportRejection } from './trap' | ||||||
| import { IndexLoaderData } from './types' | import { IndexLoaderData } from './types' | ||||||
| import { IS_NIGHTLY_OR_DEBUG } from 'routes/Settings' | import { IS_NIGHTLY_OR_DEBUG } from 'routes/Settings' | ||||||
| import { copyFileShareLink } from './links' | import { copyFileShareLink } from './links' | ||||||
|  | import { changeKclSettings, unitLengthToUnitLen } from 'lang/wasm' | ||||||
|  | import toast from 'react-hot-toast' | ||||||
|  |  | ||||||
| interface OnSubmitProps { | interface OnSubmitProps { | ||||||
|   sampleName: string |   sampleName: string | ||||||
| @ -150,5 +152,52 @@ export function kclCommands(commandProps: KclCommandConfig): Command[] { | |||||||
|         }).catch(reportRejection) |         }).catch(reportRejection) | ||||||
|       }, |       }, | ||||||
|     }, |     }, | ||||||
|  |     { | ||||||
|  |       name: 'change-length-units', | ||||||
|  |       displayName: `Set file's default length unit`, | ||||||
|  |       description: `Change the length unit to be used unless overriden inline`, | ||||||
|  |       groupId: 'code', | ||||||
|  |       needsReview: false, | ||||||
|  |       icon: 'dimension', | ||||||
|  |       args: { | ||||||
|  |         unit: { | ||||||
|  |           inputType: 'options', | ||||||
|  |           required: true, | ||||||
|  |           options: () => { | ||||||
|  |             const current = kclManager.fileSettings.defaultLengthUnit | ||||||
|  |             return [ | ||||||
|  |               { value: 'mm', name: 'Millimeters', isCurrent: current === 'mm' }, | ||||||
|  |               { value: 'cm', name: 'Centimeters', isCurrent: current === 'cm' }, | ||||||
|  |               { value: 'm', name: 'Meters', isCurrent: current === 'm' }, | ||||||
|  |               { value: 'in', name: 'Inches', isCurrent: current === 'in' }, | ||||||
|  |               { value: 'ft', name: 'Feet', isCurrent: current === 'ft' }, | ||||||
|  |               { value: 'yd', name: 'Yards', isCurrent: current === 'yd' }, | ||||||
|  |             ] | ||||||
|  |           }, | ||||||
|  |           defaultValue: commandProps.settings.defaultUnit, | ||||||
|  |         }, | ||||||
|  |       }, | ||||||
|  |       onSubmit: (data) => { | ||||||
|  |         if (!(data?.unit && commandProps.projectData.file)) { | ||||||
|  |           return | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         const codeToWrite = changeKclSettings(codeManager.code, { | ||||||
|  |           defaultLengthUnits: unitLengthToUnitLen(data.unit), | ||||||
|  |           defaultAngleUnits: { type: 'Degrees' }, | ||||||
|  |         }) | ||||||
|  |         if (err(codeToWrite)) return Promise.reject(codeToWrite) | ||||||
|  |         codeManager.updateCodeStateEditor(codeToWrite) | ||||||
|  |         codeManager | ||||||
|  |           .writeToFile() | ||||||
|  |           .then(() => { | ||||||
|  |             kclManager.executeCode(true) | ||||||
|  |           }) | ||||||
|  |           .then(() => { | ||||||
|  |             toast.success(`Updated per-file units to ${data.unit}`) | ||||||
|  |           }) | ||||||
|  |           .catch(reportRejection) | ||||||
|  |       }, | ||||||
|  |     }, | ||||||
|   ] |   ] | ||||||
| } | } | ||||||
|  | |||||||
| @ -273,7 +273,9 @@ export function createSettings() { | |||||||
|        */ |        */ | ||||||
|       defaultUnit: new Setting<BaseUnit>({ |       defaultUnit: new Setting<BaseUnit>({ | ||||||
|         defaultValue: 'mm', |         defaultValue: 'mm', | ||||||
|         description: 'The default unit to use in modeling dimensions', |         description: 'The default units to be set on new files', | ||||||
|  |         title: 'New file units', | ||||||
|  |         hideOnLevel: 'project', | ||||||
|         validate: (v) => baseUnitsUnion.includes(v as BaseUnit), |         validate: (v) => baseUnitsUnion.includes(v as BaseUnit), | ||||||
|         commandConfig: { |         commandConfig: { | ||||||
|           inputType: 'options', |           inputType: 'options', | ||||||
|  | |||||||
| @ -16,12 +16,17 @@ impl Program { | |||||||
|     pub fn recast(&self, options: &FormatOptions, indentation_level: usize) -> String { |     pub fn recast(&self, options: &FormatOptions, indentation_level: usize) -> String { | ||||||
|         let indentation = options.get_indentation(indentation_level); |         let indentation = options.get_indentation(indentation_level); | ||||||
|  |  | ||||||
|         let result = self |         let mut result = self | ||||||
|             .shebang |             .shebang | ||||||
|             .as_ref() |             .as_ref() | ||||||
|             .map(|sh| format!("{}\n\n", sh.inner.content)) |             .map(|sh| format!("{}\n\n", sh.inner.content)) | ||||||
|             .unwrap_or_default(); |             .unwrap_or_default(); | ||||||
|  |  | ||||||
|  |         for start in &self.non_code_meta.start_nodes { | ||||||
|  |             result.push_str(&start.recast(options, indentation_level)); | ||||||
|  |         } | ||||||
|  |         let result = result; // Remove mutation. | ||||||
|  |  | ||||||
|         let result = self |         let result = self | ||||||
|             .body |             .body | ||||||
|             .iter() |             .iter() | ||||||
| @ -48,17 +53,9 @@ impl Program { | |||||||
|             }) |             }) | ||||||
|             .enumerate() |             .enumerate() | ||||||
|             .fold(result, |mut output, (index, recast_str)| { |             .fold(result, |mut output, (index, recast_str)| { | ||||||
|                 let start_string = if index == 0 { |                 let start_string = if index == 0 && self.non_code_meta.start_nodes.is_empty() { | ||||||
|                     // We need to indent. |                     // We need to indent. | ||||||
|                     if self.non_code_meta.start_nodes.is_empty() { |                     indentation.to_string() | ||||||
|                         indentation.to_string() |  | ||||||
|                     } else { |  | ||||||
|                         self.non_code_meta |  | ||||||
|                             .start_nodes |  | ||||||
|                             .iter() |  | ||||||
|                             .map(|start| start.recast(options, indentation_level)) |  | ||||||
|                             .collect() |  | ||||||
|                     } |  | ||||||
|                 } else { |                 } else { | ||||||
|                     // Do nothing, we already applied the indentation elsewhere. |                     // Do nothing, we already applied the indentation elsewhere. | ||||||
|                     String::new() |                     String::new() | ||||||
| @ -795,6 +792,38 @@ mod tests { | |||||||
|     use super::*; |     use super::*; | ||||||
|     use crate::{parsing::ast::types::FormatOptions, source_range::ModuleId}; |     use crate::{parsing::ast::types::FormatOptions, source_range::ModuleId}; | ||||||
|  |  | ||||||
|  |     #[test] | ||||||
|  |     fn test_recast_annotations_without_body_items() { | ||||||
|  |         let input = r#"@settings(defaultLengthUnit = in) | ||||||
|  | "#; | ||||||
|  |         let program = crate::parsing::top_level_parse(input).unwrap(); | ||||||
|  |         let output = program.recast(&Default::default(), 0); | ||||||
|  |         assert_eq!(output, input); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     #[test] | ||||||
|  |     fn test_recast_annotations_in_function_body() { | ||||||
|  |         let input = r#"fn myFunc() { | ||||||
|  |   @meta(yes = true) | ||||||
|  |   x = 2 | ||||||
|  | } | ||||||
|  | "#; | ||||||
|  |         let program = crate::parsing::top_level_parse(input).unwrap(); | ||||||
|  |         let output = program.recast(&Default::default(), 0); | ||||||
|  |         assert_eq!(output, input); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     #[test] | ||||||
|  |     fn test_recast_annotations_in_function_body_without_items() { | ||||||
|  |         let input = r#"fn myFunc() { | ||||||
|  |   @meta(yes = true) | ||||||
|  | } | ||||||
|  | "#; | ||||||
|  |         let program = crate::parsing::top_level_parse(input).unwrap(); | ||||||
|  |         let output = program.recast(&Default::default(), 0); | ||||||
|  |         assert_eq!(output, input); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     #[test] |     #[test] | ||||||
|     fn test_recast_if_else_if_same() { |     fn test_recast_if_else_if_same() { | ||||||
|         let input = r#"b = if false { |         let input = r#"b = if false { | ||||||
| @ -1327,6 +1356,18 @@ part001 = startSketchOn('XY') | |||||||
|         ); |         ); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     #[test] | ||||||
|  |     fn test_recast_empty_function_body_with_comments() { | ||||||
|  |         let input = r#"fn myFunc() { | ||||||
|  |   // Yo yo my comments. | ||||||
|  | } | ||||||
|  | "#; | ||||||
|  |  | ||||||
|  |         let program = crate::parsing::top_level_parse(input).unwrap(); | ||||||
|  |         let output = program.recast(&Default::default(), 0); | ||||||
|  |         assert_eq!(output, input); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     #[test] |     #[test] | ||||||
|     fn test_recast_large_file() { |     fn test_recast_large_file() { | ||||||
|         let some_program_string = r#"@settings(units=mm) |         let some_program_string = r#"@settings(units=mm) | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	