Skip to content

Commit

Permalink
fix: LSP code action wasn't triggering on beginning or end of identif…
Browse files Browse the repository at this point in the history
…ier (#6616)
  • Loading branch information
asterite authored Nov 27, 2024
1 parent 1e965bc commit 1b910bc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
38 changes: 37 additions & 1 deletion compiler/noirc_errors/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ impl Span {
self.start() <= other.start() && self.end() >= other.end()
}

/// Returns `true` if any point of `self` intersects a point of `other`.
/// Adjacent spans are considered to intersect (so, for example, `0..1` intersects `1..3`).
pub fn intersects(&self, other: &Span) -> bool {
self.end() > other.start() && self.start() < other.end()
self.end() >= other.start() && self.start() <= other.end()
}

pub fn is_smaller(&self, other: &Span) -> bool {
Expand Down Expand Up @@ -140,3 +142,37 @@ impl Location {
self.file == other.file && self.span.contains(&other.span)
}
}

#[cfg(test)]
mod tests {
use crate::Span;

#[test]
fn test_intersects() {
assert!(Span::from(5..10).intersects(&Span::from(5..10)));

assert!(Span::from(5..10).intersects(&Span::from(5..5)));
assert!(Span::from(5..5).intersects(&Span::from(5..10)));

assert!(Span::from(10..10).intersects(&Span::from(5..10)));
assert!(Span::from(5..10).intersects(&Span::from(10..10)));

assert!(Span::from(5..10).intersects(&Span::from(6..9)));
assert!(Span::from(6..9).intersects(&Span::from(5..10)));

assert!(Span::from(5..10).intersects(&Span::from(4..11)));
assert!(Span::from(4..11).intersects(&Span::from(5..10)));

assert!(Span::from(5..10).intersects(&Span::from(4..6)));
assert!(Span::from(4..6).intersects(&Span::from(5..10)));

assert!(Span::from(5..10).intersects(&Span::from(9..11)));
assert!(Span::from(9..11).intersects(&Span::from(5..10)));

assert!(!Span::from(5..10).intersects(&Span::from(3..4)));
assert!(!Span::from(3..4).intersects(&Span::from(5..10)));

assert!(!Span::from(5..10).intersects(&Span::from(11..12)));
assert!(!Span::from(11..12).intersects(&Span::from(5..10)));
}
}
25 changes: 25 additions & 0 deletions tooling/lsp/src/requests/code_action/import_or_qualify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,31 @@ mod foo {
}
}
fn foo(x: SomeTypeInBar) {}"#;

assert_code_action(title, src, expected).await;
}

#[test]
async fn test_import_code_action_for_struct_at_beginning_of_name() {
let title = "Import foo::bar::SomeTypeInBar";

let src = r#"mod foo {
pub mod bar {
pub struct SomeTypeInBar {}
}
}
fn foo(x: >|<SomeTypeInBar) {}"#;

let expected = r#"use foo::bar::SomeTypeInBar;
mod foo {
pub mod bar {
pub struct SomeTypeInBar {}
}
}
fn foo(x: SomeTypeInBar) {}"#;

assert_code_action(title, src, expected).await;
Expand Down

0 comments on commit 1b910bc

Please sign in to comment.