Skip to main content

kcl_api/ast/
node_path.rs

1use schemars::JsonSchema;
2use serde::Deserialize;
3use serde::Serialize;
4
5/// A traversal path through the AST to a node.
6///
7/// Similar to the idea of a `NodeId`, a `NodePath` uniquely identifies a node,
8/// assuming you know the root node.
9///
10/// The implementation doesn't cover all parts of the tree. It currently only
11/// works on parts of the tree that the frontend uses.
12#[derive(Debug, Default, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash, ts_rs::TS)]
13#[ts(export_to = "NodePath.ts")]
14pub struct NodePath {
15    pub steps: Vec<Step>,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash, ts_rs::TS)]
19#[ts(export_to = "NodePath.ts")]
20#[serde(tag = "type")]
21pub enum Step {
22    ProgramBodyItem { index: usize },
23    CallCallee,
24    CallArg { index: usize },
25    CallKwCallee,
26    CallKwUnlabeledArg,
27    CallKwArg { index: usize },
28    BinaryLeft,
29    BinaryRight,
30    UnaryArg,
31    PipeBodyItem { index: usize },
32    ArrayElement { index: usize },
33    ArrayRangeStart,
34    ArrayRangeEnd,
35    ObjectProperty { index: usize },
36    ObjectPropertyKey,
37    ObjectPropertyValue,
38    ExpressionStatementExpr,
39    VariableDeclarationDeclaration,
40    VariableDeclarationInit,
41    FunctionExpressionName,
42    FunctionExpressionParam { index: usize },
43    FunctionExpressionBody,
44    FunctionExpressionBodyItem { index: usize },
45    ReturnStatementArg,
46    MemberExpressionObject,
47    MemberExpressionProperty,
48    IfExpressionCondition,
49    IfExpressionThen,
50    IfExpressionElseIf { index: usize },
51    IfExpressionElseIfCond,
52    IfExpressionElseIfBody,
53    IfExpressionElse,
54    ImportStatementItem { index: usize },
55    ImportStatementItemName,
56    ImportStatementItemAlias,
57    LabeledExpressionExpr,
58    LabeledExpressionLabel,
59    AscribedExpressionExpr,
60    SketchBlockArgs,
61    SketchBlockBody,
62    SketchBlockBodyItem { index: usize },
63    SketchVar,
64}