1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
//! ink! attribute code/intent actions.
use either::Either;
use ink_analyzer_ir::ast::{HasAttrs, HasDocComments};
use ink_analyzer_ir::syntax::{
AstNode, AstToken, SyntaxElement, SyntaxKind, SyntaxNode, TextRange, TextSize,
};
use ink_analyzer_ir::{ast, FromAST, FromSyntax, InkAttributeKind, InkFile, IsInkEntity};
use super::utils;
/// An ink! attribute code/intent action.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Action {
/// Label which identifies the action.
pub label: String,
/// Range where the action will be applied.
pub range: TextRange,
/// Replacement text for the action.
pub edit: String,
}
/// Computes ink! attribute actions for the given offset.
pub fn actions(file: &InkFile, offset: TextSize) -> Vec<Action> {
let mut results = Vec::new();
// Compute AST item-based ink! attribute actions.
ast_item_actions(&mut results, file, offset);
// Compute ink! attribute actions based on focused ink! attribute.
ink_attribute_actions(&mut results, file, offset);
results
}
/// Computes AST item-based ink! attribute actions at the given offset.
pub fn ast_item_actions(results: &mut Vec<Action>, file: &InkFile, offset: TextSize) {
let item_at_offset = file.item_at_offset(offset);
// Only computes actions if a focused token can be determined.
if let Some(focused_token) = item_at_offset.focused_token() {
// Only computes actions if the focused token isn't part of an attribute.
if item_at_offset.parent_attr().is_none() {
// Only computes actions if the parent AST item can be determined.
if let Some(ast_item) = item_at_offset.parent_ast_item() {
// Gets the covering struct record field if the AST item is a struct.
let record_field: Option<ast::RecordField> = match &ast_item {
ast::Item::Struct(_) => {
ink_analyzer_ir::closest_ancestor_ast_type(focused_token)
}
_ => None,
};
// Only computes actions if the focus is on either a struct record field or
// an AST item's declaration (i.e not inside the AST item's item list or body) for
// an item that can be annotated with ink! attributes.
if record_field.is_some() || is_focused_on_ast_item_declaration(&ast_item, offset) {
// Set the target node as either the covering struct field (if present) or
// the parent AST item (for all other cases).
let target = record_field
.as_ref()
.map_or(ast_item.syntax(), AstNode::syntax);
// Gets the last attribute or doc comment for the target node (if any).
let last_attr_or_doc_comment = record_field
.as_ref()
.map_or(
ast_item.doc_comments_and_attrs(),
HasDocComments::doc_comments_and_attrs,
)
.last();
// Determines the insertion point (and indenting - so that we preserve formatting) for the action,
// if the target node has attributes or doc comments,
// then the new ink! attribute is inserted at the end of that list otherwise,
// it's inserted at the beginning of the target node.
let get_insert_indenting = |prev_sibling_or_token: Option<SyntaxElement>| {
prev_sibling_or_token
.and_then(|prev_elem| {
(prev_elem.kind() == SyntaxKind::WHITESPACE)
.then_some(prev_elem.to_string())
})
.unwrap_or(String::new())
};
let (insert_offset, insert_indenting, insert_near_start) =
last_attr_or_doc_comment
.as_ref()
.map(|item| match item {
Either::Left(attr) => (
attr.syntax().text_range().end(),
get_insert_indenting(attr.syntax().prev_sibling_or_token()),
attr.syntax().text_range().start(),
),
Either::Right(comment) => (
comment.syntax().text_range().end(),
get_insert_indenting(comment.syntax().prev_sibling_or_token()),
comment.syntax().text_range().start(),
),
})
.unwrap_or((
target.text_range().start(),
get_insert_indenting(target.prev_sibling_or_token()),
target.text_range().start(),
));
// Computes the text range for the edit.
let edit_range = TextRange::new(insert_offset, insert_offset);
// Convenience closure for adding ink! attribute actions.
let mut add_action_to_accumulator =
|attr: &str, attr_kind: &str, label_kind: &str| {
results.push(Action {
label: format!("Add ink! {attr_kind} attribute {label_kind}."),
range: edit_range,
edit: if insert_offset > insert_near_start {
format!("{insert_indenting}{attr}")
} else {
format!("{attr}{insert_indenting}")
},
});
};
// Only suggest ink! attribute macros if the AST item has no other ink! attributes.
if ink_analyzer_ir::ink_attrs(target).next().is_none() {
// Suggests ink! attribute macros based on the context.
let mut ink_macro_suggestions =
utils::valid_ink_macros_by_syntax_kind(target.kind());
// Filters out duplicate and invalid ink! attribute macro actions based on parent ink! scope (if any).
utils::remove_duplicate_ink_macro_suggestions(
&mut ink_macro_suggestions,
target,
);
utils::remove_invalid_ink_macro_suggestions_for_parent_ink_scope(
&mut ink_macro_suggestions,
target,
);
// Add ink! attribute macro actions to accumulator.
for macro_kind in ink_macro_suggestions {
add_action_to_accumulator(
&format!("#[ink::{macro_kind}]"),
¯o_kind.to_string(),
"macro",
);
}
}
// Suggests ink! attribute arguments based on the context.
let mut ink_arg_suggestions =
utils::valid_ink_ink_args_by_syntax_kind(target.kind());
// Filters out duplicate ink! attribute argument actions.
utils::remove_duplicate_ink_arg_suggestions(&mut ink_arg_suggestions, target);
// Filters out conflicting ink! attribute argument actions.
utils::remove_conflicting_ink_arg_suggestions(&mut ink_arg_suggestions, target);
// Filters out invalid (based on parent ink! scope) ink! attribute argument actions.
utils::remove_invalid_ink_arg_suggestions_for_parent_ink_scope(
&mut ink_arg_suggestions,
target,
);
// Add ink! attribute argument actions to accumulator.
for arg_kind in ink_arg_suggestions {
add_action_to_accumulator(
&format!(
"#[ink({})]",
utils::ink_arg_insertion_text(
arg_kind,
edit_range.end(),
ast_item.syntax(),
)
),
&arg_kind.to_string(),
"argument",
);
}
}
}
}
}
}
/// Computes AST item-based ink! attribute actions at the given offset.
pub fn ink_attribute_actions(results: &mut Vec<Action>, file: &InkFile, offset: TextSize) {
let item_at_offset = file.item_at_offset(offset);
// Only computes actions if the focused token is part of an ink! attribute.
if let Some(ink_attr) = item_at_offset.parent_ink_attr() {
// Only computes actions for closed attributes because
// unclosed attributes are too tricky for useful contextual edits.
if let Some(r_bracket) = ink_attr.ast().r_brack_token() {
// Suggests ink! attribute arguments based on the context.
let mut ink_arg_suggestions = utils::valid_sibling_ink_args(*ink_attr.kind());
if let Some(attr_parent) = ink_attr.syntax().parent() {
// Filters out duplicate ink! attribute argument actions.
utils::remove_duplicate_ink_arg_suggestions(&mut ink_arg_suggestions, &attr_parent);
// Filters out conflicting ink! attribute argument actions.
utils::remove_conflicting_ink_arg_suggestions(
&mut ink_arg_suggestions,
&attr_parent,
);
// Filters out invalid (based on parent ink! scope) ink! attribute argument actions,
// Doesn't apply to ink! attribute macros as their arguments are not influenced by the parent scope.
if let InkAttributeKind::Arg(_) = ink_attr.kind() {
utils::remove_invalid_ink_arg_suggestions_for_parent_ink_scope(
&mut ink_arg_suggestions,
&attr_parent,
);
}
}
// Determines the insertion point for the action as well as the affixes that should
// surround the ink! attribute text (i.e whitespace and delimiters e.g `(`, `,` and `)`).
let attr = ink_attr.ast();
let (insert_offset, insert_prefix, insert_suffix) = attr.token_tree().as_ref().map_or(
(r_bracket.text_range().start(), "(", ")"),
|token_tree| {
(
// Computes the insertion offset.
token_tree
.r_paren_token()
// Inserts just before right parenthesis if it's exists.
.map_or(token_tree.syntax().text_range().end(), |r_paren| {
r_paren.text_range().start()
}),
// Determines the prefix to insert before the ink! attribute text.
match token_tree.l_paren_token() {
Some(_) => token_tree
.r_paren_token()
.and_then(|r_paren| {
r_paren.prev_token().map(|penultimate_token| {
match penultimate_token.kind() {
SyntaxKind::COMMA | SyntaxKind::L_PAREN => "",
// Adds a comma if the token before the right parenthesis is
// neither a comma nor the left parenthesis.
_ => ", ",
}
})
})
.unwrap_or(
match ink_analyzer_ir::last_child_token(token_tree.syntax()) {
Some(last_token) => match last_token.kind() {
SyntaxKind::COMMA
| SyntaxKind::L_PAREN
| SyntaxKind::R_PAREN => "",
// Adds a comma if there is no right parenthesis and the last token is
// neither a comma nor the left parenthesis
// (the right parenthesis in the pattern above will likely never match anything,
// but parsers is weird :-) so we leave it for robustness? and clarity).
_ => ", ",
},
None => "",
},
),
// Adds a left parenthesis if non already exists.
None => "(",
},
// Determines the suffix to insert before the ink! attribute text.
match token_tree.r_paren_token() {
Some(_) => "",
// Adds a right parenthesis if non already exists.
None => ")",
},
)
},
);
// Computes the text range for the edit.
let edit_range = TextRange::new(insert_offset, insert_offset);
// Add ink! attribute argument actions to accumulator.
for arg_kind in ink_arg_suggestions {
results.push(Action {
label: format!("Add ink! {arg_kind} attribute argument."),
range: edit_range,
edit: format!(
"{insert_prefix}{}{insert_suffix}",
utils::ink_arg_insertion_text(
arg_kind,
edit_range.end(),
ink_attr.syntax(),
)
),
});
}
}
}
}
/// Determines if the offset is focused on an AST item's declaration
/// (i.e not inside the AST item's item list or body) for an item that can be annotated with ink! attributes.
fn is_focused_on_ast_item_declaration(item: &ast::Item, offset: TextSize) -> bool {
// Shared logic that ensures the offset in not inside the AST item's item list or body.
let is_focused_on_declaration_impl = |item_list: Option<&SyntaxNode>| {
item_list.map_or(true, |node| {
let opening_curly_end = ink_analyzer_ir::first_child_token(node).and_then(|token| {
(token.kind() == SyntaxKind::L_CURLY).then_some(token.text_range().end())
});
let closing_curly_start = ink_analyzer_ir::last_child_token(node).and_then(|token| {
(token.kind() == SyntaxKind::R_CURLY).then_some(token.text_range().start())
});
offset <= opening_curly_end.unwrap_or(node.text_range().start())
|| closing_curly_start.unwrap_or(node.text_range().end()) <= offset
})
};
// Gets the last attribute for the AST item (if any).
let last_attr = item.attrs().last();
// Ensures offset is either after the last attribute (if any) or after the beginning of the AST item.
last_attr
.map_or(item.syntax().text_range().start(), |attr| attr.syntax().text_range().end())
<= offset
// Ensures offset is before the end of the AST item.
&& offset <= item.syntax().text_range().end()
// Ensures the offset in not inside the AST item's item list or body.
&& match item {
// We only care about AST items that can be annotated with ink! attributes.
ast::Item::Module(item) => {
is_focused_on_declaration_impl(item.item_list().as_ref().map(AstNode::syntax))
}
ast::Item::Trait(item) => is_focused_on_declaration_impl(
item.assoc_item_list().as_ref().map(AstNode::syntax),
),
ast::Item::Enum(item) => is_focused_on_declaration_impl(
item.variant_list().as_ref().map(AstNode::syntax),
),
ast::Item::Struct(item) => {
is_focused_on_declaration_impl(item.field_list().as_ref().map(AstNode::syntax))
}
ast::Item::Union(item) => is_focused_on_declaration_impl(
item.record_field_list().as_ref().map(AstNode::syntax),
),
ast::Item::Fn(item) => {
is_focused_on_declaration_impl(item.body().as_ref().map(AstNode::syntax))
}
ast::Item::Impl(item) => is_focused_on_declaration_impl(
item.assoc_item_list().as_ref().map(AstNode::syntax),
),
// Everything else is ignored.
_ => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
use ink_analyzer_ir::FromSyntax;
use test_utils::parse_offset_at;
#[test]
fn ast_item_actions_works() {
for (code, pat, expected_results) in [
// (code, pat, [(edit, pat_start, pat_end)]) where:
// code = source code,
// pat = substring used to find the cursor offset (see `test_utils::parse_offset_at` doc),
// edit = the text that will inserted (represented without whitespace for simplicity),
// pat_start = substring used to find the start of the edit offset (see `test_utils::parse_offset_at` doc),
// pat_end = substring used to find the end of the edit offset (see `test_utils::parse_offset_at` doc).
// No AST item declaration in focus.
("// A comment in focus.", None, vec![]),
(
r#"
mod my_module {
// The module declaration is out of focus when this comment is in focus.
}
"#,
Some("<-//"),
vec![],
),
// Module focus.
(
r#"
mod my_contract {
}
"#,
Some("<-mod"),
vec![("#[ink::contract]", Some("<-mod"), Some("<-mod"))],
),
(
r#"
mod my_contract {
}
"#,
Some("my_con"),
vec![("#[ink::contract]", Some("<-mod"), Some("<-mod"))],
),
(
r#"
mod my_contract {
}
"#,
Some("<-{"),
vec![("#[ink::contract]", Some("<-mod"), Some("<-mod"))],
),
(
r#"
mod my_contract {
}
"#,
Some("{"),
vec![("#[ink::contract]", Some("<-mod"), Some("<-mod"))],
),
(
r#"
mod my_contract {
}
"#,
Some("}"),
vec![("#[ink::contract]", Some("<-mod"), Some("<-mod"))],
),
(
r#"
mod my_contract {
}
"#,
Some("<-}"),
vec![("#[ink::contract]", Some("<-mod"), Some("<-mod"))],
),
(
r#"
#[ink::contract]
mod my_contract {
}
"#,
Some("<-mod"),
vec![],
),
(
r#"
#[foo]
mod my_contract {
}
"#,
Some("<-mod"),
vec![("#[ink::contract]", Some("foo]"), Some("foo]"))],
),
// Trait focus.
(
r#"
pub trait MyTrait {
}
"#,
Some("<-pub"),
vec![
("#[ink::chain_extension]", Some("<-pub"), Some("<-pub")),
("#[ink::trait_definition]", Some("<-pub"), Some("<-pub")),
],
),
// ADT focus.
(
r#"
enum MyEnum {
}
"#,
Some("<-enum"),
vec![("#[ink::storage_item]", Some("<-enum"), Some("<-enum"))],
),
(
r#"
struct MyStruct {
}
"#,
Some("<-struct"),
vec![
("#[ink::storage_item]", Some("<-struct"), Some("<-struct")),
("#[ink(anonymous)]", Some("<-struct"), Some("<-struct")),
("#[ink(event)]", Some("<-struct"), Some("<-struct")),
("#[ink(storage)]", Some("<-struct"), Some("<-struct")),
],
),
(
r#"
union MyUnion {
}
"#,
Some("<-union"),
vec![("#[ink::storage_item]", Some("<-union"), Some("<-union"))],
),
// Struct field focus.
(
r#"
struct MyStruct {
value: bool,
}
"#,
Some("<-value"),
vec![("#[ink(topic)]", Some("<-value"), Some("<-value"))],
),
// Fn focus.
(
r#"
fn my_fn() {
}
"#,
Some("<-fn"),
vec![
("#[ink::test]", Some("<-fn"), Some("<-fn")),
("#[ink(constructor)]", Some("<-fn"), Some("<-fn")),
("#[ink(default)]", Some("<-fn"), Some("<-fn")),
("#[ink(extension=)]", Some("<-fn"), Some("<-fn")),
("#[ink(handle_status=)]", Some("<-fn"), Some("<-fn")),
("#[ink(message)]", Some("<-fn"), Some("<-fn")),
("#[ink(payable)]", Some("<-fn"), Some("<-fn")),
("#[ink(selector=)]", Some("<-fn"), Some("<-fn")),
],
),
(
r#"
#[ink::test]
fn my_fn() {
}
"#,
Some("<-fn"),
vec![],
),
(
r#"
#[ink(constructor)]
fn my_fn() {
}
"#,
Some("<-fn"),
vec![
(
"#[ink(default)]",
Some("#[ink(constructor)]"),
Some("#[ink(constructor)]"),
),
(
"#[ink(payable)]",
Some("#[ink(constructor)]"),
Some("#[ink(constructor)]"),
),
(
"#[ink(selector=)]",
Some("#[ink(constructor)]"),
Some("#[ink(constructor)]"),
),
],
),
] {
let offset = TextSize::from(parse_offset_at(code, pat).unwrap() as u32);
let mut results = Vec::new();
ast_item_actions(&mut results, &InkFile::parse(code), offset);
assert_eq!(
results
.iter()
.map(|action| (action.edit.trim(), action.range))
.collect::<Vec<(&str, TextRange)>>(),
expected_results
.into_iter()
.map(|(edit, pat_start, pat_end)| (
edit,
TextRange::new(
TextSize::from(parse_offset_at(code, pat_start).unwrap() as u32),
TextSize::from(parse_offset_at(code, pat_end).unwrap() as u32)
)
))
.collect::<Vec<(&str, TextRange)>>(),
"code: {code}"
);
}
}
#[test]
fn ink_attribute_actions_works() {
for (code, pat, expected_results) in [
// (code, pat, [(edit, pat_start, pat_end)]) where:
// code = source code,
// pat = substring used to find the cursor offset (see `test_utils::parse_offset_at` doc),
// edit = the text that will inserted (represented without whitespace for simplicity),
// pat_start = substring used to find the start of the edit offset (see `test_utils::parse_offset_at` doc),
// pat_end = substring used to find the end of the edit offset (see `test_utils::parse_offset_at` doc).
// No ink! attribute in focus.
("// A comment in focus.", None, vec![]),
(
r#"
#[foo]
mod my_module {
}
"#,
Some("<-#["),
vec![],
),
(
r#"
#[foo]
mod my_module {
}
"#,
Some("[fo"),
vec![],
),
(
r#"
#[foo]
mod my_module {
}
"#,
Some("foo]"),
vec![],
),
(
r#"
#[ink::contract]
mod my_module {
}
"#,
Some("<-mod"),
vec![],
),
(
r#"
#[ink::contract]
mod my_module {
}
"#,
Some("my_"),
vec![],
),
// ink! attribute macros.
(
r#"
#[ink::contract]
mod my_contract {
}
"#,
Some("<-#["),
vec![
("(env=)", Some("<-]"), Some("<-]")),
("(keep_attr=)", Some("<-]"), Some("<-]")),
],
),
(
r#"
#[ink::contract]
mod my_contract {
}
"#,
Some("ink::"),
vec![
("(env=)", Some("<-]"), Some("<-]")),
("(keep_attr=)", Some("<-]"), Some("<-]")),
],
),
(
r#"
#[ink::contract]
mod my_contract {
}
"#,
Some("contract]"),
vec![
("(env=)", Some("<-]"), Some("<-]")),
("(keep_attr=)", Some("<-]"), Some("<-]")),
],
),
(
r#"
#[ink::contract(env=my::env::Types)]
mod my_contract {
}
"#,
Some("<-#["),
vec![(", keep_attr=", Some("<-)]"), Some("<-)]"))],
),
(
r#"
#[ink::contract(env=my::env::Types,)]
mod my_contract {
}
"#,
Some("<-#["),
vec![("keep_attr=", Some("<-)]"), Some("<-)]"))],
),
(
r#"
#[ink::chain_extension]
pub trait MyTrait {
}
"#,
Some("<-#["),
vec![],
),
(
r#"
#[ink::trait_definition]
pub trait MyTrait {
}
"#,
Some("<-#["),
vec![
("(keep_attr=)", Some("<-]"), Some("<-]")),
("(namespace=)", Some("<-]"), Some("<-]")),
],
),
(
r#"
#[ink::trait_definition(namespace="my_namespace")]
pub trait MyTrait {
}
"#,
Some("<-#["),
vec![(", keep_attr=", Some("<-)]"), Some("<-)]"))],
),
(
r#"
#[ink::storage_item]
enum MyEnum {
}
"#,
Some("<-#["),
vec![("(derive=)", Some("<-]"), Some("<-]"))],
),
(
r#"
#[ink::storage_item]
struct MyStruct {
}
"#,
Some("<-#["),
vec![("(derive=)", Some("<-]"), Some("<-]"))],
),
(
r#"
#[ink::storage_item]
union MyUnion {
}
"#,
Some("<-#["),
vec![("(derive=)", Some("<-]"), Some("<-]"))],
),
(
r#"
#[ink::test]
fn my_fn() {
}
"#,
Some("<-#["),
vec![],
),
// ink! attribute arguments.
(
r#"
#[ink(storage)]
pub struct MyStruct {
}
"#,
Some("<-#["),
vec![],
),
(
r#"
#[ink(event)]
pub struct MyStruct {
}
"#,
Some("<-#["),
vec![(", anonymous", Some("<-)]"), Some("<-)]"))],
),
(
r#"
#[ink(event)]
pub struct MyStruct {
}
"#,
Some("ink("),
vec![(", anonymous", Some("<-)]"), Some("<-)]"))],
),
(
r#"
#[ink(event)]
pub struct MyStruct {
}
"#,
Some("event)]"),
vec![(", anonymous", Some("<-)]"), Some("<-)]"))],
),
(
r#"
#[ink(event,)]
pub struct MyStruct {
}
"#,
Some("<-#["),
vec![("anonymous", Some("<-)]"), Some("<-)]"))],
),
(
r#"
#[ink(event)]
pub struct MyStruct {
#[ink(topic)]
value: bool,
}
"#,
Some("#[ink(top"),
vec![],
),
(
r#"
#[ink(event)]
pub struct MyStruct {
#[ink(topic)]
value: bool,
}
"#,
Some("<-#[->"),
vec![],
),
(
r#"
#[ink(constructor)]
pub fn my_fn() {
}
"#,
Some("<-#["),
vec![
(", default", Some("<-)]"), Some("<-)]")),
(", payable", Some("<-)]"), Some("<-)]")),
(", selector=", Some("<-)]"), Some("<-)]")),
],
),
(
r#"
#[ink(constructor, payable)]
pub fn my_fn() {
}
"#,
Some("<-#["),
vec![
(", default", Some("<-)]"), Some("<-)]")),
(", selector=", Some("<-)]"), Some("<-)]")),
],
),
(
r#"
#[ink(constructor)]
#[ink(payable)]
pub fn my_fn() {
}
"#,
Some("<-#["),
vec![
(", default", Some("<-)]"), Some("<-)]")),
(", selector=", Some("<-)]"), Some("<-)]")),
],
),
(
r#"
#[ink(constructor)]
#[ink(payable)]
pub fn my_fn() {
}
"#,
Some("<-#[->"),
vec![
(", default", Some("<-)]->"), Some("<-)]->")),
(", selector=", Some("<-)]->"), Some("<-)]->")),
],
),
(
r#"
#[ink(message)]
pub fn my_fn() {
}
"#,
Some("<-#["),
vec![
(", default", Some("<-)]"), Some("<-)]")),
(", payable", Some("<-)]"), Some("<-)]")),
(", selector=", Some("<-)]"), Some("<-)]")),
],
),
(
r#"
#[ink(extension=1)]
pub fn my_fn() {
}
"#,
Some("<-#["),
vec![(", handle_status=", Some("<-)]"), Some("<-)]"))],
),
] {
let offset = TextSize::from(parse_offset_at(code, pat).unwrap() as u32);
let mut results = Vec::new();
ink_attribute_actions(&mut results, &InkFile::parse(code), offset);
assert_eq!(
results
.iter()
.map(|action| (action.edit.trim(), action.range))
.collect::<Vec<(&str, TextRange)>>(),
expected_results
.into_iter()
.map(|(edit, pat_start, pat_end)| (
edit,
TextRange::new(
TextSize::from(parse_offset_at(code, pat_start).unwrap() as u32),
TextSize::from(parse_offset_at(code, pat_end).unwrap() as u32)
)
))
.collect::<Vec<(&str, TextRange)>>(),
"code: {code}"
);
}
}
#[test]
fn is_focused_on_ast_item_declaration_works() {
for (code, test_cases) in [
// (code, [(pat, result)]) where:
// code = source code,
// pat = substring used to find the cursor offset (see `test_utils::parse_offset_at` doc),
// result = expected result from calling `is_focused_on_ast_item_declaration`,
// Module.
(
r#"
#[abc]
#[ink::contract]
mod my_module {
// The module declaration is out of focus when this comment is in focus.
}
"#,
vec![
(Some("<-#[a"), false),
(Some("#[ab"), false),
(Some("abc]"), false),
(Some("<-#[ink"), false),
(Some("#[in"), false),
(Some("ink::"), false),
(Some("::con"), false),
(Some("contract]"), true),
(Some("<-mod"), true),
(Some("mo"), true),
(Some("mod"), true),
(Some("<-my_module"), true),
(Some("my_"), true),
(Some("<-my_module"), true),
(Some("<-{"), true),
(Some("{"), true),
(Some("<-//"), false),
(Some("<-}"), true),
(Some("}"), true),
],
),
// Trait.
(
r#"
#[abc]
#[ink::trait_definition]
pub trait MyTrait {
// The trait declaration is out of focus when this comment is in focus.
}
"#,
vec![
(Some("<-#[a"), false),
(Some("#[ab"), false),
(Some("abc]"), false),
(Some("<-#[ink"), false),
(Some("#[in"), false),
(Some("ink::"), false),
(Some("::trait"), false),
(Some("definition]"), true),
(Some("<-pub"), true),
(Some("pu"), true),
(Some("pub"), true),
(Some("<-trait MyTrait"), true),
(Some("pub tr"), true),
(Some("pub trait"), true),
(Some("<-MyTrait"), true),
(Some("My"), true),
(Some("<-MyTrait"), true),
(Some("<-{"), true),
(Some("{"), true),
(Some("<-//"), false),
(Some("<-}"), true),
(Some("}"), true),
],
),
// Enum.
(
r#"
#[abc]
#[ink::storage_item]
pub enum MyEnum {
// The enum declaration is out of focus when this comment is in focus.
}
"#,
vec![
(Some("<-#[a"), false),
(Some("#[ab"), false),
(Some("abc]"), false),
(Some("<-#[ink"), false),
(Some("#[in"), false),
(Some("ink::"), false),
(Some("::storage"), false),
(Some("storage_item]"), true),
(Some("<-pub"), true),
(Some("pu"), true),
(Some("pub"), true),
(Some("<-enum"), true),
(Some("en"), true),
(Some("enum"), true),
(Some("<-MyEnum"), true),
(Some("My"), true),
(Some("<-MyEnum"), true),
(Some("<-{"), true),
(Some("{"), true),
(Some("<-//"), false),
(Some("<-}"), true),
(Some("}"), true),
],
),
// Struct.
(
r#"
#[abc]
#[ink(event, anonymous)]
pub struct MyStruct {
// The struct declaration is out of focus when this comment is in focus.
}
"#,
vec![
(Some("<-#[a"), false),
(Some("#[ab"), false),
(Some("abc]"), false),
(Some("<-#[ink"), false),
(Some("#[in"), false),
(Some("ink("), false),
(Some("(eve"), false),
(Some("(event,"), false),
(Some(", anon"), false),
(Some("anonymous)]"), true),
(Some("<-pub"), true),
(Some("pu"), true),
(Some("pub"), true),
(Some("<-struct"), true),
(Some("st"), true),
(Some("struct"), true),
(Some("<-MyStruct"), true),
(Some("My"), true),
(Some("<-MyStruct"), true),
(Some("<-{"), true),
(Some("{"), true),
(Some("<-//"), false),
(Some("<-}"), true),
(Some("}"), true),
],
),
// Union.
(
r#"
#[abc]
#[ink::storage_item]
pub union MyUnion {
// The union declaration is out of focus when this comment is in focus.
}
"#,
vec![
(Some("<-#[a"), false),
(Some("#[ab"), false),
(Some("abc]"), false),
(Some("<-#[ink"), false),
(Some("#[in"), false),
(Some("ink::"), false),
(Some("::storage"), false),
(Some("storage_item]"), true),
(Some("<-pub"), true),
(Some("pu"), true),
(Some("pub"), true),
(Some("<-union"), true),
(Some("un"), true),
(Some("union"), true),
(Some("<-MyUnion"), true),
(Some("My"), true),
(Some("<-MyUnion"), true),
(Some("<-{"), true),
(Some("{"), true),
(Some("<-//"), false),
(Some("<-}"), true),
(Some("}"), true),
],
),
// Fn.
(
r#"
#[abc]
#[ink(constructor, selector=1)]
#[ink(payable)]
pub fn my_fn() {
// The fn declaration is out of focus when this comment is in focus.
}
"#,
vec![
(Some("<-#[a"), false),
(Some("#[ab"), false),
(Some("abc]"), false),
(Some("<-#[ink"), false),
(Some("#[in"), false),
(Some("ink("), false),
(Some("(con"), false),
(Some("(constructor,"), false),
(Some(", select"), false),
(Some("selector=1)]"), false),
(Some("(pay"), false),
(Some("payable)]"), true),
(Some("<-pub"), true),
(Some("pu"), true),
(Some("pub"), true),
(Some("<-fn"), true),
(Some("f"), true),
(Some("fn"), true),
(Some("<-my_fn"), true),
(Some("my_"), true),
(Some("<-my_fn"), true),
(Some("<-{"), true),
(Some("{"), true),
(Some("<-//"), false),
(Some("<-}"), true),
(Some("}"), true),
],
),
] {
for (pat, expected_result) in test_cases {
let offset = TextSize::from(parse_offset_at(code, pat).unwrap() as u32);
let ast_item = InkFile::parse(code)
.syntax()
.descendants()
.filter_map(ast::Item::cast)
.next()
.unwrap();
assert_eq!(
is_focused_on_ast_item_declaration(&ast_item, offset),
expected_result,
"code: {code}"
);
}
}
}
}