From b2cab3274b7522f493665a7a4d27ca91a5ffda3c Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Thu, 5 Sep 2024 18:17:40 -0500 Subject: [PATCH] fix --ide-ast when there are errors (#13737) # Description This PR fixes #13732. However, I don't think it's a proper fix. 1. It doesn't really show what the problem is. 2. It kind of side-steps the error entirely. I do think the change in span.rs may be valid because I don't think span.end should ever be 0. In the example in 13732 the span end was always 0 and so that made contains_span() return true, which seems like a false positive. The `checked_sub()` in ide.rs kind of just stops it from failing outloud. I'll leave it to smarter folks than me to land this if they think it's worthy. --- crates/nu-protocol/src/span.rs | 2 +- src/ide.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/nu-protocol/src/span.rs b/crates/nu-protocol/src/span.rs index cb6d96421c..9fe42c2530 100644 --- a/crates/nu-protocol/src/span.rs +++ b/crates/nu-protocol/src/span.rs @@ -130,7 +130,7 @@ impl Span { } pub fn contains_span(&self, span: Self) -> bool { - self.start <= span.start && span.end <= self.end + self.start <= span.start && span.end <= self.end && span.end != 0 } /// Point to the space just past this span, useful for missing values diff --git a/src/ide.rs b/src/ide.rs index a08e7afcdf..948539d839 100644 --- a/src/ide.rs +++ b/src/ide.rs @@ -642,8 +642,8 @@ pub fn ast(engine_state: &mut EngineState, file_path: &str) { { "type": "ast", "span": { - "start": span.start - offset, - "end": span.end - offset, + "start": span.start.checked_sub(offset), + "end": span.end.checked_sub(offset), }, "shape": shape.to_string(), "content": content // may not be necessary, but helpful for debugging