Change to use nodePath instead of sourceRange for Operations (#7320)
* Add NodePath to operations * Change to use nodePath to get pathToNode instead of sourceRange * Add additional node path unit test * Update output * Fix import statement NodePaths * Update output * Factor into function
This commit is contained in:
@ -60,6 +60,20 @@ pub enum Step {
|
||||
}
|
||||
|
||||
impl NodePath {
|
||||
/// Placeholder for when the AST isn't available to create a real path. It
|
||||
/// will be filled in later.
|
||||
pub(crate) fn placeholder() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
#[cfg(feature = "artifact-graph")]
|
||||
pub(crate) fn fill_placeholder(&mut self, program: &Node<Program>, cached_body_items: usize, range: SourceRange) {
|
||||
if !self.is_empty() {
|
||||
return;
|
||||
}
|
||||
*self = Self::from_range(program, cached_body_items, range).unwrap_or_default();
|
||||
}
|
||||
|
||||
/// Given a program and a [`SourceRange`], return the path to the node that
|
||||
/// contains the range.
|
||||
pub(crate) fn from_range(program: &Node<Program>, cached_body_items: usize, range: SourceRange) -> Option<Self> {
|
||||
@ -390,4 +404,19 @@ mod tests {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_node_path_from_range_import() {
|
||||
let code = r#"import "cube.step" as cube
|
||||
import "cylinder.kcl" as cylinder
|
||||
"#;
|
||||
let program = crate::Program::parse_no_errs(code).unwrap();
|
||||
// The entire cylinder import statement.
|
||||
assert_eq!(
|
||||
NodePath::from_range(&program.ast, 0, range(27, 60)).unwrap(),
|
||||
NodePath {
|
||||
steps: vec![Step::ProgramBodyItem { index: 1 }],
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user