Compare commits
4 Commits
jtran/requ
...
achalmers/
Author | SHA1 | Date | |
---|---|---|---|
12e8cd6a5e | |||
5a0043c959 | |||
4eeb41ba06 | |||
2693c93bb7 |
@ -169,16 +169,24 @@ describe('testing function declaration', () => {
|
||||
end: 39,
|
||||
params: [
|
||||
{
|
||||
type: 'Identifier',
|
||||
start: 12,
|
||||
end: 13,
|
||||
name: 'a',
|
||||
type: 'Parameter',
|
||||
identifier: {
|
||||
type: 'Identifier',
|
||||
start: 12,
|
||||
end: 13,
|
||||
name: 'a',
|
||||
},
|
||||
optional: false,
|
||||
},
|
||||
{
|
||||
type: 'Identifier',
|
||||
start: 15,
|
||||
end: 16,
|
||||
name: 'b',
|
||||
type: 'Parameter',
|
||||
identifier: {
|
||||
type: 'Identifier',
|
||||
start: 15,
|
||||
end: 16,
|
||||
name: 'b',
|
||||
},
|
||||
optional: false,
|
||||
},
|
||||
],
|
||||
body: {
|
||||
@ -244,16 +252,24 @@ const myVar = funcN(1, 2)`
|
||||
end: 37,
|
||||
params: [
|
||||
{
|
||||
type: 'Identifier',
|
||||
start: 12,
|
||||
end: 13,
|
||||
name: 'a',
|
||||
type: 'Parameter',
|
||||
identifier: {
|
||||
type: 'Identifier',
|
||||
start: 12,
|
||||
end: 13,
|
||||
name: 'a',
|
||||
},
|
||||
optional: false,
|
||||
},
|
||||
{
|
||||
type: 'Identifier',
|
||||
start: 15,
|
||||
end: 16,
|
||||
name: 'b',
|
||||
type: 'Parameter',
|
||||
identifier: {
|
||||
type: 'Identifier',
|
||||
start: 15,
|
||||
end: 16,
|
||||
name: 'b',
|
||||
},
|
||||
optional: false,
|
||||
},
|
||||
],
|
||||
body: {
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { getNodePathFromSourceRange, getNodeFromPath } from './queryAst'
|
||||
import { Identifier, parse, initPromise } from './wasm'
|
||||
import { Identifier, parse, initPromise, Parameter } from './wasm'
|
||||
|
||||
beforeAll(() => initPromise)
|
||||
|
||||
@ -46,7 +46,7 @@ const b1 = cube([0,0], 10)`
|
||||
|
||||
const ast = parse(code)
|
||||
const nodePath = getNodePathFromSourceRange(ast, sourceRange)
|
||||
const node = getNodeFromPath<Identifier>(ast, nodePath).node
|
||||
const node = getNodeFromPath<Parameter>(ast, nodePath).node
|
||||
|
||||
expect(nodePath).toEqual([
|
||||
['body', ''],
|
||||
@ -57,8 +57,8 @@ const b1 = cube([0,0], 10)`
|
||||
['params', 'FunctionExpression'],
|
||||
[0, 'index'],
|
||||
])
|
||||
expect(node.type).toBe('Identifier')
|
||||
expect(node.name).toBe('pos')
|
||||
expect(node.type).toBe('Parameter')
|
||||
expect(node.identifier.name).toBe('pos')
|
||||
})
|
||||
it('gets path right for deep within function definition body', () => {
|
||||
const code = `fn cube = (pos, scale) => {
|
||||
|
@ -247,10 +247,10 @@ function moreNodePathFromSourceRange(
|
||||
if (_node.type === 'FunctionExpression' && isInRange) {
|
||||
for (let i = 0; i < _node.params.length; i++) {
|
||||
const param = _node.params[i]
|
||||
if (param.start <= start && param.end >= end) {
|
||||
if (param.identifier.start <= start && param.identifier.end >= end) {
|
||||
path.push(['params', 'FunctionExpression'])
|
||||
path.push([i, 'index'])
|
||||
return moreNodePathFromSourceRange(param, sourceRange, path)
|
||||
return moreNodePathFromSourceRange(param.identifier, sourceRange, path)
|
||||
}
|
||||
}
|
||||
if (_node.body.start <= start && _node.body.end >= end) {
|
||||
|
@ -20,6 +20,7 @@ export type { ObjectExpression } from '../wasm-lib/kcl/bindings/ObjectExpression
|
||||
export type { MemberExpression } from '../wasm-lib/kcl/bindings/MemberExpression'
|
||||
export type { PipeExpression } from '../wasm-lib/kcl/bindings/PipeExpression'
|
||||
export type { VariableDeclaration } from '../wasm-lib/kcl/bindings/VariableDeclaration'
|
||||
export type { Parameter } from '../wasm-lib/kcl/bindings/Parameter'
|
||||
export type { PipeSubstitution } from '../wasm-lib/kcl/bindings/PipeSubstitution'
|
||||
export type { Identifier } from '../wasm-lib/kcl/bindings/Identifier'
|
||||
export type { UnaryExpression } from '../wasm-lib/kcl/bindings/UnaryExpression'
|
||||
|
@ -221,11 +221,11 @@ impl Program {
|
||||
if let Some(Value::FunctionExpression(ref mut function_expression)) = &mut value {
|
||||
// Check if the params to the function expression contain the position.
|
||||
for param in &mut function_expression.params {
|
||||
let param_source_range: SourceRange = param.clone().into();
|
||||
let param_source_range: SourceRange = (¶m.identifier).into();
|
||||
if param_source_range.contains(pos) {
|
||||
let old_name = param.name.clone();
|
||||
let old_name = param.identifier.name.clone();
|
||||
// Rename the param.
|
||||
param.rename(&old_name, new_name);
|
||||
param.identifier.rename(&old_name, new_name);
|
||||
// Now rename all the identifiers in the rest of the program.
|
||||
function_expression.body.rename_identifiers(&old_name, new_name);
|
||||
return;
|
||||
@ -1014,7 +1014,11 @@ impl CallExpression {
|
||||
// Add the arguments to the memory.
|
||||
let mut fn_memory = memory.clone();
|
||||
for (index, param) in function_expression.params.iter().enumerate() {
|
||||
fn_memory.add(¶m.name, fn_args.get(index).unwrap().clone(), param.into())?;
|
||||
fn_memory.add(
|
||||
¶m.identifier.name,
|
||||
fn_args.get(index).unwrap().clone(),
|
||||
param.identifier.clone().into(),
|
||||
)?;
|
||||
}
|
||||
|
||||
// Call the stdlib function
|
||||
@ -1249,10 +1253,10 @@ impl VariableDeclaration {
|
||||
symbol_kind = SymbolKind::FUNCTION;
|
||||
let mut children = vec![];
|
||||
for param in &function_expression.params {
|
||||
let param_source_range: SourceRange = param.into();
|
||||
let param_source_range: SourceRange = (¶m.identifier).into();
|
||||
#[allow(deprecated)]
|
||||
children.push(DocumentSymbol {
|
||||
name: param.name.clone(),
|
||||
name: param.identifier.name.clone(),
|
||||
detail: None,
|
||||
kind: SymbolKind::VARIABLE,
|
||||
range: param_source_range.to_lsp_range(code),
|
||||
@ -1442,7 +1446,7 @@ impl From<&Box<Literal>> for MemoryItem {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake, Eq)]
|
||||
#[databake(path = kcl_lib::ast::types)]
|
||||
#[ts(export)]
|
||||
#[serde(tag = "type")]
|
||||
@ -2621,6 +2625,18 @@ async fn execute_pipe_body(
|
||||
}
|
||||
}
|
||||
|
||||
/// Parameter of a KCL function.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||
#[databake(path = kcl_lib::ast::types)]
|
||||
#[ts(export)]
|
||||
#[serde(tag = "type")]
|
||||
pub struct Parameter {
|
||||
/// The parameter's label or name.
|
||||
pub identifier: Identifier,
|
||||
/// Is the parameter optional?
|
||||
pub optional: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema, Bake)]
|
||||
#[databake(path = kcl_lib::ast::types)]
|
||||
#[ts(export)]
|
||||
@ -2628,7 +2644,7 @@ async fn execute_pipe_body(
|
||||
pub struct FunctionExpression {
|
||||
pub start: usize,
|
||||
pub end: usize,
|
||||
pub params: Vec<Identifier>,
|
||||
pub params: Vec<Parameter>,
|
||||
pub body: Program,
|
||||
}
|
||||
|
||||
@ -2654,7 +2670,7 @@ impl FunctionExpression {
|
||||
"({}) => {{\n{}{}\n}}",
|
||||
self.params
|
||||
.iter()
|
||||
.map(|param| param.name.clone())
|
||||
.map(|param| param.identifier.name.clone())
|
||||
.collect::<Vec<String>>()
|
||||
.join(", "),
|
||||
options.get_indentation(indentation_level + 1),
|
||||
|
@ -908,9 +908,9 @@ pub async fn execute(
|
||||
// Add the arguments to the memory.
|
||||
for (index, param) in function_expression.params.iter().enumerate() {
|
||||
fn_memory.add(
|
||||
¶m.name,
|
||||
¶m.identifier.name,
|
||||
args.get(index).unwrap().clone(),
|
||||
param.into(),
|
||||
(¶m.identifier).into(),
|
||||
)?;
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ use crate::{
|
||||
ArrayExpression, BinaryExpression, BinaryOperator, BinaryPart, BodyItem, CallExpression, CommentStyle,
|
||||
ExpressionStatement, FunctionExpression, Identifier, Literal, LiteralIdentifier, LiteralValue,
|
||||
MemberExpression, MemberObject, NonCodeMeta, NonCodeNode, NonCodeValue, ObjectExpression, ObjectProperty,
|
||||
PipeExpression, PipeSubstitution, Program, ReturnStatement, UnaryExpression, UnaryOperator, Value,
|
||||
Parameter, PipeExpression, PipeSubstitution, Program, ReturnStatement, UnaryExpression, UnaryOperator, Value,
|
||||
VariableDeclaration, VariableDeclarator, VariableKind,
|
||||
},
|
||||
errors::{KclError, KclErrorDetails},
|
||||
@ -1208,26 +1208,62 @@ fn arguments(i: TokenSlice) -> PResult<Vec<Value>> {
|
||||
.parse_next(i)
|
||||
}
|
||||
|
||||
fn not_close_paren(i: TokenSlice) -> PResult<Token> {
|
||||
fn required_param(i: TokenSlice) -> PResult<Token> {
|
||||
any.verify(|token: &Token| !matches!(token.token_type, TokenType::Brace) || token.value != ")")
|
||||
.parse_next(i)
|
||||
}
|
||||
|
||||
fn optional_param(i: TokenSlice) -> PResult<Token> {
|
||||
let token = required_param.parse_next(i)?;
|
||||
let _question_mark = one_of(TokenType::QuestionMark).parse_next(i)?;
|
||||
Ok(token)
|
||||
}
|
||||
|
||||
/// Parameters are declared in a function signature, and used within a function.
|
||||
fn parameters(i: TokenSlice) -> PResult<Vec<Identifier>> {
|
||||
fn parameters(i: TokenSlice) -> PResult<Vec<Parameter>> {
|
||||
// Get all tokens until the next ), because that ends the parameter list.
|
||||
let candidates: Vec<Token> = separated(0.., not_close_paren, comma_sep)
|
||||
.context(expected("function parameters"))
|
||||
.parse_next(i)?;
|
||||
let candidates: Vec<_> = separated(
|
||||
0..,
|
||||
alt((optional_param.map(|t| (t, true)), required_param.map(|t| (t, false)))),
|
||||
comma_sep,
|
||||
)
|
||||
.context(expected("function parameters"))
|
||||
.parse_next(i)?;
|
||||
|
||||
// Make sure all those tokens are valid parameters.
|
||||
let params = candidates
|
||||
let params: Vec<Parameter> = candidates
|
||||
.into_iter()
|
||||
.map(|token| Identifier::try_from(token).and_then(Identifier::into_valid_binding_name))
|
||||
.map(|(token, optional)| {
|
||||
let identifier = Identifier::try_from(token).and_then(Identifier::into_valid_binding_name)?;
|
||||
Ok(Parameter { identifier, optional })
|
||||
})
|
||||
.collect::<Result<_, _>>()
|
||||
.map_err(|e| ErrMode::Backtrack(ContextError::from(e)))?;
|
||||
.map_err(|e: KclError| ErrMode::Backtrack(ContextError::from(e)))?;
|
||||
|
||||
// Make sure optional parameters are last.
|
||||
if let Err(e) = optional_after_required(¶ms) {
|
||||
return Err(ErrMode::Cut(ContextError::from(e)));
|
||||
}
|
||||
Ok(params)
|
||||
}
|
||||
|
||||
fn optional_after_required(params: &[Parameter]) -> Result<(), KclError> {
|
||||
let mut found_optional = false;
|
||||
for p in params {
|
||||
if p.optional {
|
||||
found_optional = true;
|
||||
}
|
||||
if !p.optional && found_optional {
|
||||
let e = KclError::Syntax(KclErrorDetails {
|
||||
source_ranges: vec![(&p.identifier).into()],
|
||||
message: "mandatory parameters must be declared before optional parameters".to_owned(),
|
||||
});
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl Identifier {
|
||||
fn into_valid_binding_name(self) -> Result<Identifier, KclError> {
|
||||
// Make sure they are not assigning a variable to a stdlib function.
|
||||
@ -1895,7 +1931,7 @@ const mySk1 = startSketchAt([0, 0])"#;
|
||||
let tokens = crate::token::lexer(input);
|
||||
let actual = parameters.parse(&tokens);
|
||||
assert!(actual.is_ok(), "could not parse test {i}");
|
||||
let actual_ids: Vec<_> = actual.unwrap().into_iter().map(|id| id.name).collect();
|
||||
let actual_ids: Vec<_> = actual.unwrap().into_iter().map(|p| p.identifier.name).collect();
|
||||
assert_eq!(actual_ids, expected);
|
||||
}
|
||||
}
|
||||
@ -2322,6 +2358,82 @@ e
|
||||
assert!(result.err().unwrap().to_string().contains("Unexpected token"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_optional_param_order() {
|
||||
for (i, (params, expect_ok)) in [
|
||||
(
|
||||
vec![Parameter {
|
||||
identifier: Identifier {
|
||||
start: 0,
|
||||
end: 0,
|
||||
name: "a".to_owned(),
|
||||
},
|
||||
optional: true,
|
||||
}],
|
||||
true,
|
||||
),
|
||||
(
|
||||
vec![Parameter {
|
||||
identifier: Identifier {
|
||||
start: 0,
|
||||
end: 0,
|
||||
name: "a".to_owned(),
|
||||
},
|
||||
optional: false,
|
||||
}],
|
||||
true,
|
||||
),
|
||||
(
|
||||
vec![
|
||||
Parameter {
|
||||
identifier: Identifier {
|
||||
start: 0,
|
||||
end: 0,
|
||||
name: "a".to_owned(),
|
||||
},
|
||||
optional: false,
|
||||
},
|
||||
Parameter {
|
||||
identifier: Identifier {
|
||||
start: 0,
|
||||
end: 0,
|
||||
name: "b".to_owned(),
|
||||
},
|
||||
optional: true,
|
||||
},
|
||||
],
|
||||
true,
|
||||
),
|
||||
(
|
||||
vec![
|
||||
Parameter {
|
||||
identifier: Identifier {
|
||||
start: 0,
|
||||
end: 0,
|
||||
name: "a".to_owned(),
|
||||
},
|
||||
optional: true,
|
||||
},
|
||||
Parameter {
|
||||
identifier: Identifier {
|
||||
start: 0,
|
||||
end: 0,
|
||||
name: "b".to_owned(),
|
||||
},
|
||||
optional: false,
|
||||
},
|
||||
],
|
||||
false,
|
||||
),
|
||||
]
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
let actual = optional_after_required(¶ms);
|
||||
assert_eq!(actual.is_ok(), expect_ok, "failed test {i}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_expand_array() {
|
||||
let code = "const myArray = [0..10]";
|
||||
@ -2801,4 +2913,5 @@ mod snapshot_tests {
|
||||
snapshot_test!(ar, r#"5 + "a""#);
|
||||
snapshot_test!(at, "line([0, l], %)");
|
||||
snapshot_test!(au, include_str!("../../../tests/executor/inputs/cylinder.kcl"));
|
||||
snapshot_test!(av, "fn f = (angle?) => { return default(angle, 360) }");
|
||||
}
|
||||
|
@ -29,10 +29,13 @@ expression: actual
|
||||
"end": 49,
|
||||
"params": [
|
||||
{
|
||||
"type": "Identifier",
|
||||
"start": 12,
|
||||
"end": 17,
|
||||
"name": "param"
|
||||
"identifier": {
|
||||
"type": "Identifier",
|
||||
"start": 12,
|
||||
"end": 17,
|
||||
"name": "param"
|
||||
},
|
||||
"optional": false
|
||||
}
|
||||
],
|
||||
"body": {
|
||||
|
@ -0,0 +1,97 @@
|
||||
---
|
||||
source: kcl/src/parser/parser_impl.rs
|
||||
expression: actual
|
||||
---
|
||||
{
|
||||
"start": 0,
|
||||
"end": 49,
|
||||
"body": [
|
||||
{
|
||||
"type": "VariableDeclaration",
|
||||
"type": "VariableDeclaration",
|
||||
"start": 0,
|
||||
"end": 49,
|
||||
"declarations": [
|
||||
{
|
||||
"type": "VariableDeclarator",
|
||||
"start": 3,
|
||||
"end": 49,
|
||||
"id": {
|
||||
"type": "Identifier",
|
||||
"start": 3,
|
||||
"end": 4,
|
||||
"name": "f"
|
||||
},
|
||||
"init": {
|
||||
"type": "FunctionExpression",
|
||||
"type": "FunctionExpression",
|
||||
"start": 7,
|
||||
"end": 49,
|
||||
"params": [
|
||||
{
|
||||
"identifier": {
|
||||
"type": "Identifier",
|
||||
"start": 8,
|
||||
"end": 13,
|
||||
"name": "angle"
|
||||
},
|
||||
"optional": true
|
||||
}
|
||||
],
|
||||
"body": {
|
||||
"start": 19,
|
||||
"end": 49,
|
||||
"body": [
|
||||
{
|
||||
"type": "ReturnStatement",
|
||||
"type": "ReturnStatement",
|
||||
"start": 21,
|
||||
"end": 47,
|
||||
"argument": {
|
||||
"type": "CallExpression",
|
||||
"type": "CallExpression",
|
||||
"start": 28,
|
||||
"end": 47,
|
||||
"callee": {
|
||||
"type": "Identifier",
|
||||
"start": 28,
|
||||
"end": 35,
|
||||
"name": "default"
|
||||
},
|
||||
"arguments": [
|
||||
{
|
||||
"type": "Identifier",
|
||||
"type": "Identifier",
|
||||
"start": 36,
|
||||
"end": 41,
|
||||
"name": "angle"
|
||||
},
|
||||
{
|
||||
"type": "Literal",
|
||||
"type": "Literal",
|
||||
"start": 43,
|
||||
"end": 46,
|
||||
"value": 360,
|
||||
"raw": "360"
|
||||
}
|
||||
],
|
||||
"optional": false
|
||||
}
|
||||
}
|
||||
],
|
||||
"nonCodeMeta": {
|
||||
"nonCodeNodes": {},
|
||||
"start": []
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"kind": "fn"
|
||||
}
|
||||
],
|
||||
"nonCodeMeta": {
|
||||
"nonCodeNodes": {},
|
||||
"start": []
|
||||
}
|
||||
}
|
@ -20,17 +20,12 @@ use crate::{
|
||||
/// Data to draw a line to a point.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
#[serde(rename_all = "camelCase", untagged)]
|
||||
pub enum LineToData {
|
||||
/// A point with a tag.
|
||||
PointWithTag {
|
||||
/// The to point.
|
||||
to: [f64; 2],
|
||||
/// The tag.
|
||||
tag: String,
|
||||
},
|
||||
/// A point.
|
||||
Point([f64; 2]),
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct LineToData {
|
||||
/// The to point.
|
||||
to: [f64; 2],
|
||||
/// The tag.
|
||||
tag: Option<String>,
|
||||
}
|
||||
|
||||
/// Draw a line to a point.
|
||||
@ -51,10 +46,8 @@ async fn inner_line_to(
|
||||
args: Args,
|
||||
) -> Result<Box<SketchGroup>, KclError> {
|
||||
let from = sketch_group.get_coords_from_paths()?;
|
||||
let to = match data {
|
||||
LineToData::PointWithTag { to, .. } => to,
|
||||
LineToData::Point(to) => to,
|
||||
};
|
||||
let to = data.to;
|
||||
let name = data.tag.unwrap_or_default();
|
||||
|
||||
let id = uuid::Uuid::new_v4();
|
||||
|
||||
@ -63,11 +56,7 @@ async fn inner_line_to(
|
||||
ModelingCmd::ExtendPath {
|
||||
path: sketch_group.id,
|
||||
segment: kittycad::types::PathSegment::Line {
|
||||
end: Point3D {
|
||||
x: to[0],
|
||||
y: to[1],
|
||||
z: 0.0,
|
||||
},
|
||||
end: point3d(to),
|
||||
relative: false,
|
||||
},
|
||||
},
|
||||
@ -78,11 +67,7 @@ async fn inner_line_to(
|
||||
base: BasePath {
|
||||
from: from.into(),
|
||||
to,
|
||||
name: if let LineToData::PointWithTag { tag, .. } = data {
|
||||
tag.to_string()
|
||||
} else {
|
||||
"".to_string()
|
||||
},
|
||||
name,
|
||||
geo_meta: GeoMeta {
|
||||
id,
|
||||
metadata: args.source_range.into(),
|
||||
@ -99,17 +84,12 @@ async fn inner_line_to(
|
||||
/// Data to draw a line to a point on an axis.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
#[serde(rename_all = "camelCase", untagged)]
|
||||
pub enum AxisLineToData {
|
||||
/// A point with a tag.
|
||||
PointWithTag {
|
||||
/// The to point.
|
||||
to: f64,
|
||||
/// The tag.
|
||||
tag: String,
|
||||
},
|
||||
/// A point.
|
||||
Point(f64),
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AxisLineToData {
|
||||
/// The to point.
|
||||
to: f64,
|
||||
/// The tag.
|
||||
tag: Option<String>,
|
||||
}
|
||||
|
||||
/// Draw a line to a point on the x-axis.
|
||||
@ -131,9 +111,9 @@ async fn inner_x_line_to(
|
||||
) -> Result<Box<SketchGroup>, KclError> {
|
||||
let from = sketch_group.get_coords_from_paths()?;
|
||||
|
||||
let line_to_data = match data {
|
||||
AxisLineToData::PointWithTag { to, tag } => LineToData::PointWithTag { to: [to, from.y], tag },
|
||||
AxisLineToData::Point(data) => LineToData::Point([data, from.y]),
|
||||
let line_to_data = LineToData {
|
||||
to: [data.to, from.y],
|
||||
tag: data.tag,
|
||||
};
|
||||
|
||||
let new_sketch_group = inner_line_to(line_to_data, sketch_group, args).await?;
|
||||
@ -160,9 +140,9 @@ async fn inner_y_line_to(
|
||||
) -> Result<Box<SketchGroup>, KclError> {
|
||||
let from = sketch_group.get_coords_from_paths()?;
|
||||
|
||||
let line_to_data = match data {
|
||||
AxisLineToData::PointWithTag { to, tag } => LineToData::PointWithTag { to: [from.x, to], tag },
|
||||
AxisLineToData::Point(data) => LineToData::Point([from.x, data]),
|
||||
let line_to_data = LineToData {
|
||||
to: [from.x, data.to],
|
||||
tag: data.tag,
|
||||
};
|
||||
|
||||
let new_sketch_group = inner_line_to(line_to_data, sketch_group, args).await?;
|
||||
@ -172,17 +152,12 @@ async fn inner_y_line_to(
|
||||
/// Data to draw a line.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
#[serde(rename_all = "camelCase", untagged)]
|
||||
pub enum LineData {
|
||||
/// A point with a tag.
|
||||
PointWithTag {
|
||||
/// The to point.
|
||||
to: [f64; 2],
|
||||
/// The tag.
|
||||
tag: String,
|
||||
},
|
||||
/// A point.
|
||||
Point([f64; 2]),
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct LineData {
|
||||
/// The to point.
|
||||
to: [f64; 2],
|
||||
/// The tag.
|
||||
tag: Option<String>,
|
||||
}
|
||||
|
||||
/// Draw a line.
|
||||
@ -199,10 +174,7 @@ pub async fn line(args: Args) -> Result<MemoryItem, KclError> {
|
||||
}]
|
||||
async fn inner_line(data: LineData, sketch_group: Box<SketchGroup>, args: Args) -> Result<Box<SketchGroup>, KclError> {
|
||||
let from = sketch_group.get_coords_from_paths()?;
|
||||
let inner_args = match &data {
|
||||
LineData::PointWithTag { to, .. } => *to,
|
||||
LineData::Point(to) => *to,
|
||||
};
|
||||
let inner_args = data.to;
|
||||
|
||||
let delta = inner_args;
|
||||
let to = [from.x + inner_args[0], from.y + inner_args[1]];
|
||||
@ -214,11 +186,7 @@ async fn inner_line(data: LineData, sketch_group: Box<SketchGroup>, args: Args)
|
||||
ModelingCmd::ExtendPath {
|
||||
path: sketch_group.id,
|
||||
segment: kittycad::types::PathSegment::Line {
|
||||
end: Point3D {
|
||||
x: delta[0],
|
||||
y: delta[1],
|
||||
z: 0.0,
|
||||
},
|
||||
end: point3d(delta),
|
||||
relative: true,
|
||||
},
|
||||
},
|
||||
@ -229,11 +197,7 @@ async fn inner_line(data: LineData, sketch_group: Box<SketchGroup>, args: Args)
|
||||
base: BasePath {
|
||||
from: from.into(),
|
||||
to,
|
||||
name: if let LineData::PointWithTag { tag, .. } = data {
|
||||
tag.to_string()
|
||||
} else {
|
||||
"".to_string()
|
||||
},
|
||||
name: data.tag.unwrap_or_default(),
|
||||
geo_meta: GeoMeta {
|
||||
id,
|
||||
metadata: args.source_range.into(),
|
||||
@ -250,17 +214,12 @@ async fn inner_line(data: LineData, sketch_group: Box<SketchGroup>, args: Args)
|
||||
/// Data to draw a line on an axis.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
#[serde(rename_all = "camelCase", untagged)]
|
||||
pub enum AxisLineData {
|
||||
/// The length with a tag.
|
||||
LengthWithTag {
|
||||
/// The length of the line.
|
||||
length: f64,
|
||||
/// The tag.
|
||||
tag: String,
|
||||
},
|
||||
/// The length.
|
||||
Length(f64),
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AxisLineData {
|
||||
/// The length of the line.
|
||||
length: f64,
|
||||
/// The tag.
|
||||
tag: Option<String>,
|
||||
}
|
||||
|
||||
/// Draw a line on the x-axis.
|
||||
@ -280,9 +239,9 @@ async fn inner_x_line(
|
||||
sketch_group: Box<SketchGroup>,
|
||||
args: Args,
|
||||
) -> Result<Box<SketchGroup>, KclError> {
|
||||
let line_data = match data {
|
||||
AxisLineData::LengthWithTag { length, tag } => LineData::PointWithTag { to: [length, 0.0], tag },
|
||||
AxisLineData::Length(length) => LineData::Point([length, 0.0]),
|
||||
let line_data = LineData {
|
||||
to: [data.length, 0.0],
|
||||
tag: data.tag,
|
||||
};
|
||||
|
||||
let new_sketch_group = inner_line(line_data, sketch_group, args).await?;
|
||||
@ -306,9 +265,9 @@ async fn inner_y_line(
|
||||
sketch_group: Box<SketchGroup>,
|
||||
args: Args,
|
||||
) -> Result<Box<SketchGroup>, KclError> {
|
||||
let line_data = match data {
|
||||
AxisLineData::LengthWithTag { length, tag } => LineData::PointWithTag { to: [0.0, length], tag },
|
||||
AxisLineData::Length(length) => LineData::Point([0.0, length]),
|
||||
let line_data = LineData {
|
||||
to: [0.0, data.length],
|
||||
tag: data.tag,
|
||||
};
|
||||
|
||||
let new_sketch_group = inner_line(line_data, sketch_group, args).await?;
|
||||
@ -318,28 +277,19 @@ async fn inner_y_line(
|
||||
/// Data to draw an angled line.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
#[serde(rename_all = "camelCase", untagged)]
|
||||
pub enum AngledLineData {
|
||||
/// An angle and length with a tag.
|
||||
AngleWithTag {
|
||||
/// The angle of the line.
|
||||
angle: f64,
|
||||
/// The length of the line.
|
||||
length: f64,
|
||||
/// The tag.
|
||||
tag: String,
|
||||
},
|
||||
/// An angle and length.
|
||||
AngleAndLength([f64; 2]),
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AngledLineData {
|
||||
/// The angle of the line.
|
||||
angle: f64,
|
||||
/// The length of the line.
|
||||
length: f64,
|
||||
/// The tag.
|
||||
tag: Option<String>,
|
||||
}
|
||||
|
||||
impl AngledLineData {
|
||||
pub fn into_inner_line(self, to: [f64; 2]) -> LineData {
|
||||
if let AngledLineData::AngleWithTag { tag, .. } = self {
|
||||
LineData::PointWithTag { to, tag }
|
||||
} else {
|
||||
LineData::Point(to)
|
||||
}
|
||||
LineData { to, tag: self.tag }
|
||||
}
|
||||
}
|
||||
|
||||
@ -361,10 +311,7 @@ async fn inner_angled_line(
|
||||
args: Args,
|
||||
) -> Result<Box<SketchGroup>, KclError> {
|
||||
let from = sketch_group.get_coords_from_paths()?;
|
||||
let (angle, length) = match &data {
|
||||
AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length),
|
||||
AngledLineData::AngleAndLength(angle_and_length) => (angle_and_length[0], angle_and_length[1]),
|
||||
};
|
||||
let AngledLineData { angle, length, tag } = data;
|
||||
|
||||
//double check me on this one - mike
|
||||
let delta: [f64; 2] = [
|
||||
@ -381,11 +328,7 @@ async fn inner_angled_line(
|
||||
base: BasePath {
|
||||
from: from.into(),
|
||||
to,
|
||||
name: if let AngledLineData::AngleWithTag { tag, .. } = data {
|
||||
tag.to_string()
|
||||
} else {
|
||||
"".to_string()
|
||||
},
|
||||
name: tag.unwrap_or_default(),
|
||||
geo_meta: GeoMeta {
|
||||
id,
|
||||
metadata: args.source_range.into(),
|
||||
@ -398,11 +341,7 @@ async fn inner_angled_line(
|
||||
ModelingCmd::ExtendPath {
|
||||
path: sketch_group.id,
|
||||
segment: kittycad::types::PathSegment::Line {
|
||||
end: Point3D {
|
||||
x: delta[0],
|
||||
y: delta[1],
|
||||
z: 0.0,
|
||||
},
|
||||
end: point3d(delta),
|
||||
relative,
|
||||
},
|
||||
},
|
||||
@ -431,12 +370,7 @@ async fn inner_angled_line_of_x_length(
|
||||
sketch_group: Box<SketchGroup>,
|
||||
args: Args,
|
||||
) -> Result<Box<SketchGroup>, KclError> {
|
||||
let (angle, length) = match &data {
|
||||
AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length),
|
||||
AngledLineData::AngleAndLength(angle_and_length) => (angle_and_length[0], angle_and_length[1]),
|
||||
};
|
||||
|
||||
let to = get_y_component(Angle::from_degrees(angle), length);
|
||||
let to = get_y_component(Angle::from_degrees(data.angle), data.length);
|
||||
|
||||
let new_sketch_group = inner_line(data.into_inner_line(to.into()), sketch_group, args).await?;
|
||||
|
||||
@ -446,28 +380,20 @@ async fn inner_angled_line_of_x_length(
|
||||
/// Data to draw an angled line to a point.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
#[serde(rename_all = "camelCase", untagged)]
|
||||
pub enum AngledLineToData {
|
||||
/// An angle and point with a tag.
|
||||
AngleWithTag {
|
||||
/// The angle of the line.
|
||||
angle: f64,
|
||||
/// The point to draw to.
|
||||
to: f64,
|
||||
/// The tag.
|
||||
tag: String,
|
||||
},
|
||||
/// An angle and point to draw to.
|
||||
AngleAndPoint([f64; 2]),
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AngledLineToData {
|
||||
/// The angle of the line.
|
||||
angle: f64,
|
||||
/// The point to draw to.
|
||||
to: f64,
|
||||
/// The tag.
|
||||
tag: Option<String>,
|
||||
}
|
||||
|
||||
impl AngledLineToData {
|
||||
pub fn into_inner_line(self, x_to: f64, y_to: f64) -> LineToData {
|
||||
if let AngledLineToData::AngleWithTag { tag, .. } = self {
|
||||
LineToData::PointWithTag { to: [x_to, y_to], tag }
|
||||
} else {
|
||||
LineToData::Point([x_to, y_to])
|
||||
}
|
||||
let tag = self.tag;
|
||||
LineToData { to: [x_to, y_to], tag }
|
||||
}
|
||||
}
|
||||
|
||||
@ -489,10 +415,7 @@ async fn inner_angled_line_to_x(
|
||||
args: Args,
|
||||
) -> Result<Box<SketchGroup>, KclError> {
|
||||
let from = sketch_group.get_coords_from_paths()?;
|
||||
let (angle, x_to) = match &data {
|
||||
AngledLineToData::AngleWithTag { angle, to, .. } => (*angle, *to),
|
||||
AngledLineToData::AngleAndPoint(angle_and_to) => (angle_and_to[0], angle_and_to[1]),
|
||||
};
|
||||
let (angle, x_to) = (data.angle, data.to);
|
||||
|
||||
let x_component = x_to - from.x;
|
||||
let y_component = x_component * f64::tan(angle.to_radians());
|
||||
@ -520,10 +443,7 @@ async fn inner_angled_line_of_y_length(
|
||||
sketch_group: Box<SketchGroup>,
|
||||
args: Args,
|
||||
) -> Result<Box<SketchGroup>, KclError> {
|
||||
let (angle, length) = match &data {
|
||||
AngledLineData::AngleWithTag { angle, length, .. } => (*angle, *length),
|
||||
AngledLineData::AngleAndLength(angle_and_length) => (angle_and_length[0], angle_and_length[1]),
|
||||
};
|
||||
let (angle, length) = (data.angle, data.length);
|
||||
|
||||
let to = get_x_component(Angle::from_degrees(angle), length);
|
||||
|
||||
@ -550,10 +470,7 @@ async fn inner_angled_line_to_y(
|
||||
args: Args,
|
||||
) -> Result<Box<SketchGroup>, KclError> {
|
||||
let from = sketch_group.get_coords_from_paths()?;
|
||||
let (angle, y_to) = match &data {
|
||||
AngledLineToData::AngleWithTag { angle, to, .. } => (*angle, *to),
|
||||
AngledLineToData::AngleAndPoint(angle_and_to) => (angle_and_to[0], angle_and_to[1]),
|
||||
};
|
||||
let (angle, y_to) = (data.angle, data.to);
|
||||
|
||||
let y_component = y_to - from.y;
|
||||
let x_component = y_component / f64::tan(angle.to_radians());
|
||||
@ -616,10 +533,9 @@ async fn inner_angled_line_that_intersects(
|
||||
from,
|
||||
);
|
||||
|
||||
let line_to_data = if let Some(tag) = data.tag {
|
||||
LineToData::PointWithTag { to: to.into(), tag }
|
||||
} else {
|
||||
LineToData::Point(to.into())
|
||||
let line_to_data = LineToData {
|
||||
to: to.into(),
|
||||
tag: data.tag,
|
||||
};
|
||||
|
||||
let new_sketch_group = inner_line_to(line_to_data, sketch_group, args).await?;
|
||||
@ -830,10 +746,7 @@ pub async fn start_profile_at(args: Args) -> Result<MemoryItem, KclError> {
|
||||
name = "startProfileAt",
|
||||
}]
|
||||
async fn inner_start_profile_at(data: LineData, plane: Box<Plane>, args: Args) -> Result<Box<SketchGroup>, KclError> {
|
||||
let to = match &data {
|
||||
LineData::PointWithTag { to, .. } => *to,
|
||||
LineData::Point(to) => *to,
|
||||
};
|
||||
let to = data.to;
|
||||
|
||||
let id = uuid::Uuid::new_v4();
|
||||
let path_id = uuid::Uuid::new_v4();
|
||||
@ -843,11 +756,7 @@ async fn inner_start_profile_at(data: LineData, plane: Box<Plane>, args: Args) -
|
||||
id,
|
||||
ModelingCmd::MovePathPen {
|
||||
path: path_id,
|
||||
to: Point3D {
|
||||
x: to[0],
|
||||
y: to[1],
|
||||
z: 0.0,
|
||||
},
|
||||
to: point3d(to),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
@ -855,11 +764,7 @@ async fn inner_start_profile_at(data: LineData, plane: Box<Plane>, args: Args) -
|
||||
let current_path = BasePath {
|
||||
from: to,
|
||||
to,
|
||||
name: if let LineData::PointWithTag { tag, .. } = data {
|
||||
tag.to_string()
|
||||
} else {
|
||||
"".to_string()
|
||||
},
|
||||
name: data.tag.unwrap_or_default(),
|
||||
geo_meta: GeoMeta {
|
||||
id,
|
||||
metadata: args.source_range.into(),
|
||||
@ -943,16 +848,7 @@ pub enum ArcData {
|
||||
/// The radius.
|
||||
radius: f64,
|
||||
/// The tag.
|
||||
tag: String,
|
||||
},
|
||||
/// Angles and radius.
|
||||
AnglesAndRadius {
|
||||
/// The start angle.
|
||||
angle_start: f64,
|
||||
/// The end angle.
|
||||
angle_end: f64,
|
||||
/// The radius.
|
||||
radius: f64,
|
||||
tag: Option<String>,
|
||||
},
|
||||
/// Center, to and radius with a tag.
|
||||
CenterToRadiusWithTag {
|
||||
@ -963,16 +859,7 @@ pub enum ArcData {
|
||||
/// The radius.
|
||||
radius: f64,
|
||||
/// The tag.
|
||||
tag: String,
|
||||
},
|
||||
/// Center, to and radius.
|
||||
CenterToRadius {
|
||||
/// The center.
|
||||
center: [f64; 2],
|
||||
/// The to point.
|
||||
to: [f64; 2],
|
||||
/// The radius.
|
||||
radius: f64,
|
||||
tag: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
@ -1003,24 +890,10 @@ async fn inner_arc(data: ArcData, sketch_group: Box<SketchGroup>, args: Args) ->
|
||||
let (center, end) = arc_center_and_end(from, a_start, a_end, *radius);
|
||||
(center, a_start, a_end, *radius, end)
|
||||
}
|
||||
ArcData::AnglesAndRadius {
|
||||
angle_start,
|
||||
angle_end,
|
||||
radius,
|
||||
} => {
|
||||
let a_start = Angle::from_degrees(*angle_start);
|
||||
let a_end = Angle::from_degrees(*angle_end);
|
||||
let (center, end) = arc_center_and_end(from, a_start, a_end, *radius);
|
||||
(center, a_start, a_end, *radius, end)
|
||||
}
|
||||
ArcData::CenterToRadiusWithTag { center, to, radius, .. } => {
|
||||
let (angle_start, angle_end) = arc_angles(from, center.into(), to.into(), *radius, args.source_range)?;
|
||||
(center.into(), angle_start, angle_end, *radius, to.into())
|
||||
}
|
||||
ArcData::CenterToRadius { center, to, radius } => {
|
||||
let (angle_start, angle_end) = arc_angles(from, center.into(), to.into(), *radius, args.source_range)?;
|
||||
(center.into(), angle_start, angle_end, *radius, to.into())
|
||||
}
|
||||
};
|
||||
|
||||
let id = uuid::Uuid::new_v4();
|
||||
@ -1047,10 +920,8 @@ async fn inner_arc(data: ArcData, sketch_group: Box<SketchGroup>, args: Args) ->
|
||||
from: from.into(),
|
||||
to: end.into(),
|
||||
name: match data {
|
||||
ArcData::AnglesAndRadiusWithTag { tag, .. } => tag.to_string(),
|
||||
ArcData::AnglesAndRadius { .. } => "".to_string(),
|
||||
ArcData::CenterToRadiusWithTag { tag, .. } => tag.to_string(),
|
||||
ArcData::CenterToRadius { .. } => "".to_string(),
|
||||
ArcData::AnglesAndRadiusWithTag { tag, .. } => tag.unwrap_or_default(),
|
||||
ArcData::CenterToRadiusWithTag { tag, .. } => tag.unwrap_or_default(),
|
||||
},
|
||||
geo_meta: GeoMeta {
|
||||
id,
|
||||
@ -1082,10 +953,8 @@ pub enum TangentialArcData {
|
||||
/// Where the arc should end. Must lie in the same plane as the current path pen position. Must not be colinear with current path pen position.
|
||||
to: [f64; 2],
|
||||
/// The tag.
|
||||
tag: String,
|
||||
tag: Option<String>,
|
||||
},
|
||||
/// A point where the arc should end. Must lie in the same plane as the current path pen position. Must not be colinear with current path pen position.
|
||||
Point([f64; 2]),
|
||||
}
|
||||
|
||||
/// Draw a tangential arc.
|
||||
@ -1135,11 +1004,6 @@ async fn inner_tangential_arc(
|
||||
TangentialArcData::PointWithTag { to, .. } => {
|
||||
args.send_modeling_cmd(id, tan_arc_to(&sketch_group, to)).await?;
|
||||
|
||||
*to
|
||||
}
|
||||
TangentialArcData::Point(to) => {
|
||||
args.send_modeling_cmd(id, tan_arc_to(&sketch_group, to)).await?;
|
||||
|
||||
*to
|
||||
}
|
||||
};
|
||||
@ -1169,11 +1033,7 @@ fn tan_arc_to(sketch_group: &SketchGroup, to: &[f64; 2]) -> ModelingCmd {
|
||||
path: sketch_group.id,
|
||||
segment: kittycad::types::PathSegment::TangentialArcTo {
|
||||
angle_snap_increment: None,
|
||||
to: Point3D {
|
||||
x: to[0],
|
||||
y: to[1],
|
||||
z: 0.0,
|
||||
},
|
||||
to: point3d(*to),
|
||||
},
|
||||
}
|
||||
}
|
||||
@ -1181,17 +1041,12 @@ fn tan_arc_to(sketch_group: &SketchGroup, to: &[f64; 2]) -> ModelingCmd {
|
||||
/// Data to draw a tangential arc to a specific point.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, JsonSchema, ts_rs::TS)]
|
||||
#[ts(export)]
|
||||
#[serde(rename_all = "camelCase", untagged)]
|
||||
pub enum TangentialArcToData {
|
||||
/// A point with a tag.
|
||||
PointWithTag {
|
||||
/// Where the arc should end. Must lie in the same plane as the current path pen position. Must not be colinear with current path pen position.
|
||||
to: [f64; 2],
|
||||
/// The tag.
|
||||
tag: String,
|
||||
},
|
||||
/// A point where the arc should end. Must lie in the same plane as the current path pen position. Must not be colinear with current path pen position.
|
||||
Point([f64; 2]),
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TangentialArcToData {
|
||||
/// Where the arc should end. Must lie in the same plane as the current path pen position. Must not be colinear with current path pen position.
|
||||
to: [f64; 2],
|
||||
/// The tag.
|
||||
tag: Option<String>,
|
||||
}
|
||||
|
||||
/// Draw a tangential arc to a specific point.
|
||||
@ -1212,10 +1067,8 @@ async fn inner_tangential_arc_to(
|
||||
args: Args,
|
||||
) -> Result<Box<SketchGroup>, KclError> {
|
||||
let from: Point2d = sketch_group.get_coords_from_paths()?;
|
||||
let to = match &data {
|
||||
TangentialArcToData::PointWithTag { to, .. } => to,
|
||||
TangentialArcToData::Point(to) => to,
|
||||
};
|
||||
let to = data.to;
|
||||
let name = data.tag.unwrap_or_default().to_owned();
|
||||
|
||||
let delta = [to[0] - from.x, to[1] - from.y];
|
||||
let id = uuid::Uuid::new_v4();
|
||||
@ -1224,12 +1077,8 @@ async fn inner_tangential_arc_to(
|
||||
let current_path = Path::ToPoint {
|
||||
base: BasePath {
|
||||
from: from.into(),
|
||||
to: *to,
|
||||
name: if let TangentialArcToData::PointWithTag { tag, .. } = data {
|
||||
tag.to_string()
|
||||
} else {
|
||||
"".to_string()
|
||||
},
|
||||
to,
|
||||
name,
|
||||
geo_meta: GeoMeta {
|
||||
id,
|
||||
metadata: args.source_range.into(),
|
||||
@ -1246,28 +1095,16 @@ async fn inner_tangential_arc_to(
|
||||
/// Data to draw a bezier curve.
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||
#[ts(export)]
|
||||
#[serde(rename_all = "camelCase", untagged)]
|
||||
pub enum BezierData {
|
||||
/// Points with a tag.
|
||||
PointsWithTag {
|
||||
/// The to point.
|
||||
to: [f64; 2],
|
||||
/// The first control point.
|
||||
control1: [f64; 2],
|
||||
/// The second control point.
|
||||
control2: [f64; 2],
|
||||
/// The tag.
|
||||
tag: String,
|
||||
},
|
||||
/// Points.
|
||||
Points {
|
||||
/// The to point.
|
||||
to: [f64; 2],
|
||||
/// The first control point.
|
||||
control1: [f64; 2],
|
||||
/// The second control point.
|
||||
control2: [f64; 2],
|
||||
},
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct BezierData {
|
||||
/// The to point.
|
||||
to: [f64; 2],
|
||||
/// The first control point.
|
||||
control1: [f64; 2],
|
||||
/// The second control point.
|
||||
control2: [f64; 2],
|
||||
/// The tag.
|
||||
tag: Option<String>,
|
||||
}
|
||||
|
||||
/// Draw a bezier curve.
|
||||
@ -1289,12 +1126,13 @@ async fn inner_bezier_curve(
|
||||
) -> Result<Box<SketchGroup>, KclError> {
|
||||
let from = sketch_group.get_coords_from_paths()?;
|
||||
|
||||
let (to, control1, control2) = match &data {
|
||||
BezierData::PointsWithTag {
|
||||
to, control1, control2, ..
|
||||
} => (to, control1, control2),
|
||||
BezierData::Points { to, control1, control2 } => (to, control1, control2),
|
||||
};
|
||||
let BezierData {
|
||||
to,
|
||||
control1,
|
||||
control2,
|
||||
tag,
|
||||
} = data;
|
||||
let name = tag.unwrap_or_default();
|
||||
|
||||
let relative = true;
|
||||
let delta = to;
|
||||
@ -1307,21 +1145,9 @@ async fn inner_bezier_curve(
|
||||
ModelingCmd::ExtendPath {
|
||||
path: sketch_group.id,
|
||||
segment: kittycad::types::PathSegment::Bezier {
|
||||
control1: Point3D {
|
||||
x: control1[0],
|
||||
y: control1[1],
|
||||
z: 0.0,
|
||||
},
|
||||
control2: Point3D {
|
||||
x: control2[0],
|
||||
y: control2[1],
|
||||
z: 0.0,
|
||||
},
|
||||
end: Point3D {
|
||||
x: delta[0],
|
||||
y: delta[1],
|
||||
z: 0.0,
|
||||
},
|
||||
control1: point3d(control1),
|
||||
control2: point3d(control2),
|
||||
end: point3d(delta),
|
||||
relative,
|
||||
},
|
||||
},
|
||||
@ -1332,11 +1158,7 @@ async fn inner_bezier_curve(
|
||||
base: BasePath {
|
||||
from: from.into(),
|
||||
to,
|
||||
name: if let BezierData::PointsWithTag { tag, .. } = data {
|
||||
tag.to_string()
|
||||
} else {
|
||||
"".to_string()
|
||||
},
|
||||
name,
|
||||
geo_meta: GeoMeta {
|
||||
id,
|
||||
metadata: args.source_range.into(),
|
||||
@ -1394,6 +1216,10 @@ async fn inner_hole(
|
||||
Ok(sketch_group)
|
||||
}
|
||||
|
||||
fn point3d([x, y]: [f64; 2]) -> Point3D {
|
||||
Point3D { x, y, z: 0.0 }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
@ -1403,21 +1229,30 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_line_data() {
|
||||
let data = LineData::Point([0.0, 1.0]);
|
||||
let data = LineData {
|
||||
to: [0.0, 1.0],
|
||||
tag: None,
|
||||
};
|
||||
let mut str_json = serde_json::to_string(&data).unwrap();
|
||||
assert_eq!(str_json, "[0.0,1.0]");
|
||||
|
||||
str_json = "[0, 1]".to_string();
|
||||
let data: LineData = serde_json::from_str(&str_json).unwrap();
|
||||
assert_eq!(data, LineData::Point([0.0, 1.0]));
|
||||
assert_eq!(
|
||||
data,
|
||||
LineData {
|
||||
to: [0.0, 1.0],
|
||||
tag: None
|
||||
}
|
||||
);
|
||||
|
||||
str_json = "{ \"to\": [0.0, 1.0], \"tag\": \"thing\" }".to_string();
|
||||
let data: LineData = serde_json::from_str(&str_json).unwrap();
|
||||
assert_eq!(
|
||||
data,
|
||||
LineData::PointWithTag {
|
||||
LineData {
|
||||
to: [0.0, 1.0],
|
||||
tag: "thing".to_string()
|
||||
tag: Some("thing".to_string()),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -47,6 +47,8 @@ pub enum TokenType {
|
||||
Function,
|
||||
/// Unknown lexemes.
|
||||
Unknown,
|
||||
/// The ? symbol, used for optional values.
|
||||
QuestionMark,
|
||||
}
|
||||
|
||||
/// Most KCL tokens correspond to LSP semantic tokens (but not all).
|
||||
@ -58,6 +60,7 @@ impl TryFrom<TokenType> for SemanticTokenType {
|
||||
TokenType::Word => Self::VARIABLE,
|
||||
TokenType::Keyword => Self::KEYWORD,
|
||||
TokenType::Operator => Self::OPERATOR,
|
||||
TokenType::QuestionMark => Self::OPERATOR,
|
||||
TokenType::String => Self::STRING,
|
||||
TokenType::LineComment => Self::COMMENT,
|
||||
TokenType::BlockComment => Self::COMMENT,
|
||||
|
@ -21,6 +21,7 @@ pub fn token(i: &mut Located<&str>) -> PResult<Token> {
|
||||
'{' | '(' | '[' => brace_start,
|
||||
'}' | ')' | ']' => brace_end,
|
||||
',' => comma,
|
||||
'?' => question_mark,
|
||||
'0'..='9' => number,
|
||||
':' => colon,
|
||||
'.' => alt((number, double_period, period)),
|
||||
@ -108,6 +109,11 @@ fn comma(i: &mut Located<&str>) -> PResult<Token> {
|
||||
Ok(Token::from_range(range, TokenType::Comma, value.to_string()))
|
||||
}
|
||||
|
||||
fn question_mark(i: &mut Located<&str>) -> PResult<Token> {
|
||||
let (value, range) = '?'.with_span().parse_next(i)?;
|
||||
Ok(Token::from_range(range, TokenType::QuestionMark, value.to_string()))
|
||||
}
|
||||
|
||||
fn colon(i: &mut Located<&str>) -> PResult<Token> {
|
||||
let (value, range) = ':'.with_span().parse_next(i)?;
|
||||
Ok(Token::from_range(range, TokenType::Colon, value.to_string()))
|
||||
|
Reference in New Issue
Block a user