Skip to content

Commit

Permalink
feat: Add support for /// comments in Rust. #18
Browse files Browse the repository at this point in the history
  • Loading branch information
LiLittleCat committed Apr 28, 2023
1 parent 1cb0aeb commit 93a2e16
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/java/com/lilittlecat/plugin/pangu/Pangu.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public Pangu() {
"((\\S+)#)" +
"([\\p{InHiragana}\\p{InKatakana}\\p{InBopomofo}\\p{InCJKCompatibilityIdeographs}\\p{InCJKUnifiedIdeographs}])"
);
private static final Pattern DOUBLE_SLASH_ANSG = Pattern.compile("^//[\\u0370-\\u03ff\\u1f00-\\u1fffa-z0-9]");
private static final Pattern TRIPLE_SLASH_ANSG = Pattern.compile("^///[\\u0370-\\u03ff\\u1f00-\\u1fffa-z0-9]");

/**
* Performs a paranoid text spacing on {@code text}.
Expand Down Expand Up @@ -133,8 +135,14 @@ public String spacingText(String text) {
Matcher acMatcher = ANSG_CJK.matcher(text);
text = acMatcher.replaceAll("$1 $2");

// replace the first `//some comment` of a line to `// some comment` todo add more comment patterns from different languages
text = text.replaceAll("(?<=^|\\s)//(\\S.*)", "// $1");
// TODO add more comment patterns from different languages
if (DOUBLE_SLASH_ANSG.matcher(text).find()) {
// replace the first `//some comment` of a line to `// some comment`
text = text.replaceFirst("^//", "// ");
} else if (TRIPLE_SLASH_ANSG.matcher(text).find()) {
// `///` is a kind of comment syntax in Rust
text = text.replaceFirst("^///", "/// ");
}

return text;
}
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/lilittlecat/plugin/PanguFormatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ public void test(){
System.out.println(myFixture.getFile().getText());
}

@Test
public void testComment(){
myFixture.configureByText("test.rs", "///some comment");
myFixture.testAction(new PanguFormatAction());
System.out.println(myFixture.getFile().getText());
}

}

0 comments on commit 93a2e16

Please sign in to comment.