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
//! A text edit.
use ink_analyzer_ir::syntax::{AstNode, SyntaxKind, SyntaxToken, TextRange, TextSize};
use ink_analyzer_ir::{InkEntity, InkFile};
use once_cell::sync::Lazy;
use regex::Regex;
use super::utils;
/// A text edit (with an optional snippet - i.e tab stops and/or placeholders).
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct TextEdit {
/// Replacement text for the text edit.
pub text: String,
/// Range to which the text edit will be applied.
pub range: TextRange,
/// Formatted snippet for the text edit (includes tab stops and/or placeholders).
pub snippet: Option<String>,
}
impl TextEdit {
/// Creates text edit.
pub fn new(text: String, range: TextRange, snippet: Option<String>) -> Self {
Self {
text,
range,
snippet,
}
}
/// Creates text edit for inserting at the given offset.
pub fn insert(text: String, offset: TextSize) -> Self {
Self::insert_with_snippet(text, offset, None)
}
/// Creates text edit for inserting at the given offset (including an optional snippet).
pub fn insert_with_snippet(text: String, offset: TextSize, snippet: Option<String>) -> Self {
Self {
text,
range: TextRange::new(offset, offset),
snippet,
}
}
/// Creates text edit for replacing the given range.
pub fn replace(text: String, range: TextRange) -> Self {
Self::replace_with_snippet(text, range, None)
}
/// Creates text edit for replacing the given range (including an optional snippet) - i.e an alias of [`Self::new`].
pub fn replace_with_snippet(text: String, range: TextRange, snippet: Option<String>) -> Self {
Self::new(text, range, snippet)
}
/// Creates a text edit for deleting the specified range.
pub fn delete(range: TextRange) -> Self {
Self {
text: String::new(),
range,
snippet: None,
}
}
}
/// Format text edits (i.e. add indenting and new lines based on context).
pub fn format_edits<'a>(
edits: impl Iterator<Item = TextEdit> + 'a,
file: &'a InkFile,
) -> impl Iterator<Item = TextEdit> + 'a {
edits.map(|item| format_edit(item, file))
}
/// Format text edit (i.e. add indenting and new lines based on context).
pub fn format_edit(mut edit: TextEdit, file: &InkFile) -> TextEdit {
// Adds affixes to a text edit.
fn add_affixes(edit: &mut TextEdit, prefix: Option<String>, suffix: Option<String>) {
if prefix.is_some() || suffix.is_some() {
edit.text = format!(
"{}{}{}",
prefix.as_deref().unwrap_or_default(),
edit.text,
suffix.as_deref().unwrap_or_default(),
);
edit.snippet = edit.snippet.as_ref().map(|snippet| {
format!(
"{}{snippet}{}",
prefix.as_deref().unwrap_or_default(),
suffix.as_deref().unwrap_or_default()
)
});
}
}
// Generates affixes for edits between new lines and/or file boundaries.
let affix_edit_between_whitespace_or_file_boundaries =
|whitespace_before: Option<&str>, whitespace_after: Option<&str>| {
let build_affix = |ws_text: &str| {
(!starts_with_two_or_more_newlines(ws_text)).then(|| "\n".to_owned())
};
match (
whitespace_before.map(|ws_before| (ws_before.contains('\n'), ws_before)),
whitespace_after.map(|ws_after| (ws_after.contains('\n'), ws_after)),
) {
// Handles edits between new lines and/or file boundaries.
(Some((true, ws_text_before)), Some((true, ws_text_after))) => {
(build_affix(ws_text_before), build_affix(ws_text_after))
}
(Some((true, ws_text_before)), None) => (build_affix(ws_text_before), None),
(None, Some((true, ws_text_after))) => (None, build_affix(ws_text_after)),
_ => (None, None),
}
};
// Handles edits inside whitespace.
let covering_whitespace = file
.syntax()
.covering_element(edit.range)
.into_token()
.filter(|token| {
token.kind() == SyntaxKind::WHITESPACE
&& token.text_range().start() < edit.range.start()
&& edit.range.end() < token.text_range().end()
});
if let Some(whitespace_token) = covering_whitespace {
if edit.text.is_empty() {
return edit;
}
let whitespace_text = whitespace_token.text();
let start = edit.range.start() - whitespace_token.text_range().start();
let (whitespace_before, whitespace_after) = if edit.range.is_empty() {
whitespace_text.split_at(start.into())
} else {
let end = whitespace_token.text_range().end() - edit.range.end();
let (whitespace_before, _) = whitespace_text.split_at(start.into());
let (_, whitespace_after) = whitespace_text.split_at(end.into());
(whitespace_before, whitespace_after)
};
let (prefix, suffix) = affix_edit_between_whitespace_or_file_boundaries(
Some(whitespace_before),
Some(whitespace_after),
);
add_affixes(&mut edit, prefix, suffix);
return edit;
}
// Determines the token right before the start of the edit offset.
let token_before_option = file
.syntax()
.token_at_offset(edit.range.start())
.left_biased()
.and_then(|token| {
if token.text_range().end() <= edit.range.start() {
Some(token)
} else {
token.prev_token()
}
});
// Determines the token right after the end of the edit offset.
let token_after_option = file
.syntax()
.token_at_offset(edit.range.end())
.right_biased()
.and_then(|token| {
if token.text_range().start() >= edit.range.end() {
Some(token)
} else {
token.next_token()
}
});
if edit.text.is_empty() {
// Handles deletes.
// Removes whitespace immediately following a delete if the text is surrounded by whitespace,
// but only when the token right after the whitespace is not a closing curly bracket
// (because it would otherwise break the indenting of the closing curly bracket).
if let Some(token_after) = token_after_option {
let is_whitespace_or_bof_before =
token_before_option.as_ref().map_or(true, |token_before| {
token_before.kind() == SyntaxKind::WHITESPACE
});
let is_at_the_end_block = token_after
.next_token()
.is_some_and(|it| it.kind() == SyntaxKind::R_CURLY);
if is_whitespace_or_bof_before
&& token_after.kind() == SyntaxKind::WHITESPACE
&& !is_at_the_end_block
{
edit.range = TextRange::new(edit.range.start(), token_after.text_range().end());
}
}
} else {
// Handles insert and replace.
let affix_edit_after_whitespace_or_bof =
|token_before_option: Option<&SyntaxToken>,
token_after_option: Option<&SyntaxToken>| {
let is_whitespace_or_bof_before =
token_before_option.as_ref().map_or(true, |token_before| {
token_before.kind() == SyntaxKind::WHITESPACE
});
let is_whitespace_or_eof_after =
token_after_option.as_ref().map_or(true, |token_after| {
token_after.kind() == SyntaxKind::WHITESPACE
});
if is_whitespace_or_bof_before && is_whitespace_or_eof_after {
// Handles edits between whitespace and/or file boundaries.
affix_edit_between_whitespace_or_file_boundaries(
token_before_option.map(SyntaxToken::text),
token_after_option.map(SyntaxToken::text),
)
} else if is_whitespace_or_bof_before && !is_whitespace_or_eof_after {
// Handles preserving formatting for next non-whitespace item.
(
// No formatting prefix.
None,
// Adds formatting suffix only if the edit is not surrounded by whitespace
// (treats end of the file like whitespace)
// and its preceding whitespace contains a new line.
match token_before_option {
// Handles beginning of file.
None => Some("\n".to_owned()),
Some(token_before) => token_before.text().contains('\n').then(|| {
format!("\n{}", utils::end_indenting(token_before.text()))
}),
},
)
} else {
(None, None)
}
};
let (prefix, suffix) = if let Some(token_before) = token_before_option {
match token_before.kind() {
// Handles edits after whitespace.
SyntaxKind::WHITESPACE => affix_edit_after_whitespace_or_bof(
Some(&token_before),
token_after_option.as_ref(),
),
// Handles edits at the beginning of blocks (i.e right after the opening curly bracket).
SyntaxKind::L_CURLY => {
(
// Adds formatting prefix only if the edit doesn't start with a new line
// and then only add indenting if the edit doesn't start with a space (i.e ' ') or a tab (i.e. '\t').
(!edit.text.starts_with('\n')).then(|| {
format!(
"\n{}",
(!edit.text.starts_with(' ') && !edit.text.starts_with('\t'))
.then(|| {
ink_analyzer_ir::parent_ast_item(&token_before)
.map(|it| utils::item_children_indenting(it.syntax()))
})
.flatten()
.as_deref()
.unwrap_or_default()
)
}),
// Adds formatting suffix if the edit is followed by either a non-whitespace character
// or whitespace that doesn't start with at least 2 new lines
// (the new lines can be interspersed with other whitespace)
// and the edit doesn't end with 2 new lines.
token_after_option.as_ref().and_then(|token_after| {
((token_after.kind() != SyntaxKind::WHITESPACE
|| !starts_with_two_or_more_newlines(token_after.text()))
&& !edit.text.ends_with("\n\n"))
.then(|| {
format!(
"\n{}",
if token_after.text().starts_with('\n') {
""
} else {
"\n"
}
)
})
}),
)
}
// Handles edits at the end of a statement or block or after a comment.
SyntaxKind::SEMICOLON | SyntaxKind::R_CURLY | SyntaxKind::COMMENT => {
(
// Adds formatting prefix only if the edit doesn't start with a new line
// and then only add indenting if the edit doesn't start with a space (i.e ' ') or a tab (i.e. '\t').
(!edit.text.starts_with('\n')).then(|| {
format!(
"\n{}{}",
// Extra new line at the end of statements and blocks.
if token_before.kind() == SyntaxKind::COMMENT {
""
} else {
"\n"
},
if !edit.text.starts_with(' ') && !edit.text.starts_with('\t') {
ink_analyzer_ir::parent_ast_item(&token_before)
.and_then(|it| utils::item_indenting(it.syntax()))
} else {
None
}
.as_deref()
.unwrap_or_default(),
)
}),
// No formatting suffix.
None,
)
}
// Ignores all other cases.
_ => (None, None),
}
} else {
affix_edit_after_whitespace_or_bof(None, token_after_option.as_ref())
};
// Adds formatting if necessary.
add_affixes(&mut edit, prefix, suffix);
}
edit
}
// Checks whether the given text starts with at least 2 new lines
// (the new lines can be interspersed with other whitespace).
fn starts_with_two_or_more_newlines(text: &str) -> bool {
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"^([^\S\n]*\n[^\S\n]*){2,}").unwrap());
RE.is_match(text)
}
#[cfg(test)]
mod tests {
use super::*;
use test_utils::parse_offset_at;
#[test]
fn format_insert_and_replace_works() {
for (input, output, source, start_pat, end_pat) in [
// Insert after whitespace.
(
"#[ink::contract]",
"\n#[ink::contract]",
r#"
#![cfg_attr(not(feature = "std"), no_std, no_main)]
"#,
Some("\n->"),
Some("\n->"),
),
(
"mod contract {}",
"\nmod contract {}\n",
r#"
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#[cfg(test)]
mod tests {}"#,
Some("<-\n#[cfg(test)]"),
Some("<-\n#[cfg(test)]"),
),
(
"mod contract {}",
"\nmod contract {}\n",
"#![cfg_attr(not(feature = \"std\"), no_std)]\n \n#[cfg(test)]\nmod tests {}",
Some("<- \n#[cfg(test)]"),
Some("<-\n#[cfg(test)]"),
),
(
"mod contract {}",
"mod contract {}\n",
r#"
#![cfg_attr(not(feature = "std"), no_std, no_main)]
#[cfg(test)]
mod tests {}"#,
Some("<-\n#[cfg(test)]"),
Some("<-\n#[cfg(test)]"),
),
(
"#[ink::contract]",
"#[ink::contract]\n",
"mod contract {}",
Some("<-mod contract {"),
Some("<-mod contract {"),
),
(
"#[ink::contract]",
"#[ink::contract]\n",
r"
#[doc(hidden)]
mod contract {}",
Some("<-mod contract {"),
Some("<-mod contract {"),
),
(
"#[ink::contract]",
"#[ink::contract]\n",
"#[doc(hidden)]\nmod contract {}",
Some("<-#[doc(hidden)]"),
Some("<-#[doc(hidden)]"),
),
(
"#[ink::contract]",
"#[ink::contract]\n",
r"
#[doc(hidden)]
mod contract {}",
Some("<-#[doc(hidden)]"),
Some("<-#[doc(hidden)]"),
),
(
"#[ink::contract]",
"#[ink::contract]\n",
r"
#[doc(hidden)]
mod contract {}",
Some("<-mod contract {"),
Some("<-mod contract {"),
),
(
"#[ink(storage)]",
"#[ink(storage)]\n ",
r"
mod contract {
struct MyContract {}
}",
Some("<-struct MyContract {}"),
Some("<-struct MyContract {}"),
),
(
"#[ink(topic)]",
"#[ink(topic)]\n ",
r"
mod contract {
struct MyEvent {
status: bool,
}
}",
Some("<-status: bool,"),
Some("<-status: bool,"),
),
(
"#[ink(impl)]",
"#[ink(impl)]\n ",
r"
mod contract {
impl MyContract {}
}",
Some("<-impl MyContract {}"),
Some("<-impl MyContract {}"),
),
(
"#[ink(impl)]",
"#[ink(impl)]\n ",
r#"
mod contract {
#[ink(namespace = "my_namespace")]
impl MyContract {}
}
"#,
Some(r#"<-#[ink(namespace = "my_namespace")]"#),
Some(r#"<-#[ink(namespace = "my_namespace")]"#),
),
(
"#[ink(message)]",
"#[ink(message)]\n ",
r"
mod contract {
impl MyContract {
pub fn message(&self) {}
}
}",
Some("<-pub fn message(&self) {}"),
Some("<-pub fn message(&self) {}"),
),
// Insert at the beginning of block.
(
"struct MyContract {}",
"\n struct MyContract {}\n",
r"
mod contract {
}",
Some("mod contract {"),
Some("mod contract {"),
),
(
"status: bool,",
"\n status: bool,\n",
r"
mod contract {
struct MyContract {
}
}",
Some("struct MyContract {"),
Some("struct MyContract {"),
),
(
"impl MyContract {}",
"\n impl MyContract {}\n",
r"
mod contract {
}",
Some("mod contract {"),
Some("mod contract {"),
),
(
"pub fn message(&self) {}",
"\n pub fn message(&self) {}\n",
r"
mod contract {
impl MyContract {
}
}",
Some("impl MyContract {"),
Some("impl MyContract {"),
),
// Insert at the end of a statement or block or after a comment.
(
"struct MyEvent {}",
"\n\n struct MyEvent {}",
r"
mod contract {
struct MyContract {}
}",
Some("struct MyContract {}"),
Some("struct MyContract {}"),
),
(
"struct MyEvent {}",
"\n\n struct MyEvent {}",
r"
mod contract {
struct MyContract;
}",
Some("struct MyContract;"),
Some("struct MyContract;"),
),
(
"struct MyEvent {}",
"\n\n struct MyEvent {}",
r"
mod contract {
struct MyContract {}
struct MyOtherEvent {}
}",
Some("struct MyContract {}"),
Some("struct MyContract {}"),
),
(
"struct MyEvent {}",
"\n\n struct MyEvent {}",
r"
mod contract {
struct MyContract {}
struct MyOtherEvent {}
}",
Some("struct MyContract {}"),
Some("struct MyContract {}"),
),
(
"pub fn message(&self) {}",
"\n\n pub fn message(&self) {}",
r"
mod contract {
impl MyContract {
pub fn constructor() {}
}
}",
Some("pub fn constructor() {}"),
Some("pub fn constructor() {}"),
),
(
"pub fn message(&self) {}",
"\n\n pub fn message(&self) {}",
r"
mod contract {
impl MyContract {
pub fn constructor() {}
pub fn message2(&self) {}
}
}",
Some("pub fn constructor() {}"),
Some("pub fn constructor() {}"),
),
(
"pub fn message(&self) {}",
"\n\n pub fn message(&self) {}",
r"
mod contract {
impl MyContract {
pub fn constructor() {}
pub fn message2(&self) {}
}
}",
Some("pub fn constructor() {}"),
Some("pub fn constructor() {}"),
),
// Everything else should remain unchanged.
(
"(env = crate::MyEnvironment)",
"(env = crate::MyEnvironment)",
r"
#[ink::contract]
mod contract {}",
Some("#[ink::contract"),
Some("#[ink::contract"),
),
(
"env = crate::MyEnvironment",
"env = crate::MyEnvironment",
r"
#[ink::contract()]
mod contract {}",
Some("#[ink::contract("),
Some("#[ink::contract("),
),
(
r#", keep_attr = "foo,bar""#,
r#", keep_attr = "foo,bar""#,
r"
#[ink::contract(env = crate::MyEnvironment)]
mod contract {}",
Some("#[ink::contract(env = crate::MyEnvironment"),
Some("#[ink::contract(env = crate::MyEnvironment"),
),
(
"crate::MyEnvironment",
"crate::MyEnvironment",
r"
#[ink::contract(env = self::MyEnvironment)]
mod contract {}",
Some("#[ink::contract(env = "),
Some("#[ink::contract(env = self::MyEnvironment"),
),
(
" crate::MyEnvironment",
" crate::MyEnvironment",
r"
#[ink::contract(env = self::MyEnvironment)]
mod contract {}",
Some("#[ink::contract(env ="),
Some("#[ink::contract(env = self::MyEnvironment"),
),
(
"&self",
"&self",
"pub fn message() {}",
Some("pub fn message("),
Some("pub fn message("),
),
(
", status: bool",
", status: bool",
"pub fn message(&self) {}",
Some("pub fn message(&self"),
Some("pub fn message(&self"),
),
(
" status: bool",
" status: bool",
"pub fn message(&self,) {}",
Some("pub fn message(&self,"),
Some("pub fn message(&self,"),
),
(
"status: bool",
"status: bool",
"pub fn message(&self, ) {}",
Some("pub fn message(&self, "),
Some("pub fn message(&self, "),
),
(
" -> u8",
" -> u8",
"pub fn message(&self) {}",
Some("pub fn message(&self)"),
Some("pub fn message(&self)"),
),
(
"-> u8",
"-> u8",
"pub fn message(&self) {}",
Some("pub fn message(&self) "),
Some("pub fn message(&self) "),
),
] {
let file = InkFile::parse(source);
let range = TextRange::new(
TextSize::from(parse_offset_at(source, start_pat).unwrap() as u32),
TextSize::from(parse_offset_at(source, end_pat).unwrap() as u32),
);
let edit = TextEdit {
text: input.to_owned(),
range,
snippet: None,
};
let result = format_edit(edit, &file);
let expected = TextEdit {
text: output.to_owned(),
range,
snippet: None,
};
assert_eq!(result, expected);
}
}
#[test]
fn format_delete_works() {
for (start_pat_input, end_pat_input, pat_range_output, source) in [
// Removes space after delete if its surrounded by whitespace and
// the next token after trailing whitespace is not a closing curly bracket.
(
Some("<-#[ink::contract]"),
Some("#[ink::contract]"),
Some((Some("<-#[ink::contract]"), Some("<-mod contract {}"))),
"#[ink::contract]\nmod contract {}",
),
(
Some("<-#[ink::contract]"),
Some("#[ink::contract]"),
Some((Some("<-#[ink::contract]"), Some("<-mod contract {}"))),
r"
#[ink::contract]
mod contract {}
",
),
(
Some("<-#[ink::contract]"),
Some("#[ink::contract]"),
Some((Some("<-#[ink::contract]"), Some("<-mod contract {}"))),
r"
#[doc(hidden)]
#[ink::contract]
mod contract {}",
),
(
Some("<-#[ink::contract]"),
Some("#[ink::contract]"),
Some((Some("<-#[ink::contract]"), Some("<-#[doc(hidden)]"))),
r"
#[ink::contract]
#[doc(hidden)]
mod contract {}",
),
(
Some("<-#[ink(storage)]"),
Some("#[ink(storage)]"),
Some((Some("<-#[ink(storage)]"), Some("<-struct MyContract {}"))),
r"
mod contract {
#[ink(storage)]
struct MyContract {}
}",
),
(
Some("<-#[ink(topic)]"),
Some("#[ink(topic)]"),
Some((Some("<-#[ink(topic)]"), Some("<-status: bool,"))),
r"
mod contract {
struct MyEvent {
#[ink(topic)]
status: bool,
}
}",
),
(
Some("<-#[ink(impl)]"),
Some("#[ink(impl)]"),
Some((Some("<-#[ink(impl)]"), Some("<-impl MyContract {}"))),
r"
mod contract {
#[ink(impl)]
impl MyContract {}
}",
),
(
Some("<-#[ink(impl)]"),
Some("#[ink(impl)]"),
Some((
Some("<-#[ink(impl)]"),
Some(r#"<-#[ink(namespace = "my_namespace")]"#),
)),
r#"
mod contract {
#[ink(impl)]
#[ink(namespace = "my_namespace")]
impl MyContract {}
}"#,
),
(
Some("<-#[ink(message)]"),
Some("#[ink(message)]"),
Some((
Some("<-#[ink(message)]"),
Some("<-pub fn message(&self) {}"),
)),
r"
mod contract {
impl MyContract {
#[ink(message)]
pub fn message(&self) {}
}
}",
),
(
Some("<--> u8"),
Some("-> u8"),
Some((Some("<--> u8"), Some("-> u8 "))),
"pub fn message(&self) -> u8 {}",
),
(
Some("<-struct MyEvent {}"),
Some("struct MyEvent {}"),
Some((
Some("<-struct MyEvent {}"),
Some("<-struct MyOtherEvent {}"),
)),
r"
mod contract {
struct MyContract {}
struct MyEvent {}
struct MyOtherEvent {}
}",
),
(
Some("<-struct MyEvent {}"),
Some("struct MyEvent {}"),
Some((
Some("<-struct MyEvent {}"),
Some("<-struct MyOtherEvent {}"),
)),
r"
mod contract {
struct MyContract {}
struct MyEvent {}
struct MyOtherEvent {}
}",
),
(
Some("<-pub fn message(&self) {}"),
Some("pub fn message(&self) {}"),
Some((
Some("<-pub fn message(&self) {}"),
Some("<-pub fn message2(&self) {}"),
)),
r"
mod contract {
impl MyContract {
pub fn constructor() {}
pub fn message(&self) {}
pub fn message2(&self) {}
}
}",
),
(
Some("<-pub fn message(&self) {}"),
Some("pub fn message(&self) {}"),
Some((
Some("<-pub fn message(&self) {}"),
Some("<-pub fn message2(&self) {}"),
)),
r"
mod contract {
impl MyContract {
pub fn constructor() {}
pub fn message(&self) {}
pub fn message2(&self) {}
}
}",
),
// Everything else should remain unchanged.
(
Some("<-struct MyContract {}"),
Some("struct MyContract {}"),
None,
r"
mod contract {
struct MyContract {}
}",
),
(
Some("<-status: bool,"),
Some("status: bool,"),
None,
r"
mod contract {
struct MyContract {
status: bool,
}
}",
),
(
Some("<-impl MyContract {}"),
Some("impl MyContract {}"),
None,
r"
mod contract {
impl MyContract {}
}",
),
(
Some("<-pub fn message(&self) {}"),
Some("pub fn message(&self) {}"),
None,
r"
mod contract {
impl MyContract {
pub fn message(&self) {}
}
}",
),
(
Some("<-struct MyEvent {}"),
Some("struct MyEvent {}"),
None,
r"
mod contract {
struct MyContract {}
struct MyEvent {}
}",
),
(
Some("<-struct MyEvent {}"),
Some("struct MyEvent {}"),
None,
r"
mod contract {
struct MyContract;
struct MyEvent {}
}",
),
(
Some("<-pub fn message(&self) {}"),
Some("pub fn message(&self) {}"),
None,
r"
mod contract {
impl MyContract {
pub fn constructor() {}
pub fn message(&self) {}
}
}",
),
(
Some("<-(env = crate::MyEnvironment)"),
Some("(env = crate::MyEnvironment)"),
None,
r"
#[ink::contract(env = crate::MyEnvironment)]
mod contract {}",
),
(
Some("<-env = crate::MyEnvironment"),
Some("env = crate::MyEnvironment"),
None,
r"
#[ink::contract(env = crate::MyEnvironment)]
mod contract {}",
),
(
Some(r#"<-, keep_attr = "foo,bar""#),
Some(r#", keep_attr = "foo,bar""#),
None,
r#"
#[ink::contract(env = crate::MyEnvironment, keep_attr = "foo,bar")]
mod contract {}"#,
),
(
Some("<-crate::MyEnvironment"),
Some("crate::MyEnvironment"),
None,
r"
#[ink::contract(env = crate::MyEnvironment)]
mod contract {}",
),
(
Some("<- crate::MyEnvironment"),
Some(" crate::MyEnvironment"),
None,
r"
#[ink::contract(env = crate::MyEnvironment)]
mod contract {}",
),
(
Some("<-&self"),
Some("&self"),
None,
"pub fn message(&self) {}",
),
(
Some("<-, status: bool"),
Some(", status: bool"),
None,
"pub fn message(&self, status: bool) {}",
),
(
Some("<- status: bool"),
Some(" status: bool"),
None,
"pub fn message(&self, status: bool) {}",
),
(
Some("<-status: bool"),
Some("status: bool"),
None,
"pub fn message(&self, status: bool) {}",
),
(
Some("<- -> u8"),
Some(" -> u8"),
None,
"pub fn message(&self) -> u8 {}",
),
] {
let file = InkFile::parse(source);
let range_input = TextRange::new(
TextSize::from(parse_offset_at(source, start_pat_input).unwrap() as u32),
TextSize::from(parse_offset_at(source, end_pat_input).unwrap() as u32),
);
let range_output =
pat_range_output.map_or(range_input, |(start_pat_output, end_pat_output)| {
TextRange::new(
TextSize::from(parse_offset_at(source, start_pat_output).unwrap() as u32),
TextSize::from(parse_offset_at(source, end_pat_output).unwrap() as u32),
)
});
let edit = TextEdit {
text: String::new(),
range: range_input,
snippet: None,
};
let result = format_edit(edit, &file);
let expected = TextEdit {
text: String::new(),
range: range_output,
snippet: None,
};
assert_eq!(result, expected);
}
}
}