Skip to content

Commit

Permalink
Allow alternate binding assertion anchor for targets at the same colu…
Browse files Browse the repository at this point in the history
…mn as the comment
  • Loading branch information
ggiraldez committed Jul 25, 2024
1 parent 8b2fb28 commit e7a8124
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl<'a> fmt::Display for DisplayCursor<'a> {
/// uint x;
/// // ^def:1
///
/// asserts that at the CST node above the caret symbol (same column, not
/// asserts that at the CST node above the caret symbol `^` (same column, not
/// necessarily in the previous line), the identifier `x` should create a
/// binding definition, and assigns it an ID of '1'.
///
Expand All @@ -142,6 +142,13 @@ impl<'a> fmt::Display for DisplayCursor<'a> {
/// '1'; and that the CST identifier node `y` should be a binding reference that
/// is unresolved for version at or above 0.5.0.
///
/// For assertion targets that are located at the column where the comment
/// begins the alternative anchor `<` can be used. For example:
///
/// x = y + 1;
/// // ^ref:2
/// //<ref:1
///
pub fn collect_assertions(cursor: Cursor, version: &Version) -> Result<Assertions, AssertionError> {
let mut assertions = Assertions::new();

Expand Down Expand Up @@ -173,7 +180,7 @@ enum Assertion {
}

static ASSERTION_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"[\^](?<type>ref|def):(?<id>[0-9a-zA-Z_-]+|!)([\t ]*\((?<version>[^)]+)\))?")
Regex::new(r"(?<anchor>[\^]|[<])(?<type>ref|def):(?<id>[0-9a-zA-Z_-]+|!)([\t ]*\((?<version>[^)]+)\))?")
.unwrap()
});

Expand All @@ -191,7 +198,13 @@ fn find_assertion_in_comment(

let assertion_id = captures.name("id").unwrap().as_str();
let assertion_type = captures.name("type").unwrap().as_str();
let assertion_col = comment_col + captures.get(0).unwrap().start();
let assertion_anchor = captures.name("anchor").unwrap();
let assertion_col = comment_col
+ if assertion_anchor.as_str() == "^" {
assertion_anchor.start()
} else {
0
};
let version_req = match captures.name("version") {
Some(version) => {
let Ok(version_req) = VersionReq::parse(version.as_str()) else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ contract First {
enum Choice { One, Two }
// ^def:2
Choice choice;
// ^ref:2
//<ref:2
// ^def:3
function get_choice() public returns (Choice) {
// ^def:4
Expand All @@ -14,7 +14,7 @@ contract First {

contract Second {
First.Choice choice;
// ^ref:1
//<ref:1
// ^ref:2
// ^def:5

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ contract Foo {
// ^def:2
// ^def:1
Answer choice = Answer.Yes;
// ^ref:1
//<ref:1
// ^ref:2
// ^ref:1
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ contract Test {
// ^ref:5
// ^def:3
pointless(z, x);
// ^ref:4
//<ref:4
// ^ref:1
// ^ref:3
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ interface Example {

contract Test {
Example.Choice choice = Example.Choice.One;
// ^ref:1
//<ref:1
// ^ref:2
// ^def:7
// ^ref:1
// ^ref:2
// ^ref:5
Example.Book book;
// ^ref:1
//<ref:1
// ^ref:3

function my_test() returns (int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract Foo {
function another_func() returns (uint y) {
// ^def:5
y = x + w;
// ^ref:5
//<ref:5
// ^ref:! (>=0.5.0)
// ^ref:6 (<0.5.0)
// ^ref:1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ contract Foo {
function bar() returns (int x) {
// ^def:1
x = y + z;
// ^ref:1
//<ref:1
// ^ref:3 (<0.5.0)
// ^ref:! (>=0.5.0)
// ^ref:2 (<0.5.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ library Test {
// ^ref:7
// ^ref:5
(, Choice c) = (Choice.Yes, Choice.No);
// ^ref:1
// ^ref:1
// ^def:8
// ^ref:1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ contract Foo {
// ^def:3
y = x + 5;
// ^ref:3
// ^ref:2
//<ref:2
}
}

0 comments on commit e7a8124

Please sign in to comment.