Skip to content

Commit

Permalink
release: v1.2.3 (#21)
Browse files Browse the repository at this point in the history
* don't format quoted content in code
  • Loading branch information
LiLittleCat committed Aug 2, 2023
1 parent b959527 commit 93951ad
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.idea
.qodana
build
/.idea/
32 changes: 16 additions & 16 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
# intellij-pangu Changelog

## [Unreleased]
- fix: Do not format content within quotes in code. [#20](https://github.com/LiLittleCat/intellij-pangu/issues/20)

## [1.2.2] - 2023-04-28
- Add support for `///` comments in Rust. [#18](https://github.com/LiLittleCat/intellij-pangu/issues/18)%0D%0A%0A[Unreleased]: https://github.com/LiLittleCat/intellij-pangu/compare/v1.2.1...HEAD
- Add support for `///` comments in Rust. [#18](https://github.com/LiLittleCat/intellij-pangu/issues/18)

## [1.2.1] - 2023-04-05
- Support for all future IDE builds. [#15](https://github.com/LiLittleCat/intellij-pangu/issues/15)
Expand Down Expand Up @@ -39,8 +40,8 @@
- Spacing selected content.
- Spacing whole file.

[Unreleased]: null/compare/v1.2.2...HEAD
[1.2.2]: null/compare/v1.2.1...v1.2.2
[Unreleased]: https://github.com/LiLittleCat/intellij-pangu/compare/v1.2.2...HEAD
[1.2.2]: https://github.com/LiLittleCat/intellij-pangu/compare/v1.2.1...v1.2.2
[1.2.1]: https://github.com/LiLittleCat/intellij-pangu/compare/v1.2.0...v1.2.1
[1.2.0]: https://github.com/LiLittleCat/intellij-pangu/compare/v1.1.3...v1.2.0
[1.1.3]: https://github.com/LiLittleCat/intellij-pangu/compare/v1.1.2...v1.1.3
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pluginGroup = com.lilittlecat.plugin.intellij-pangu
pluginName = Pangu
# SemVer format -> https://semver.org
pluginVersion = 1.2.2
pluginVersion = 1.2.3

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand Down
48 changes: 33 additions & 15 deletions src/main/java/com/lilittlecat/plugin/pangu/Pangu.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public Pangu() {
);
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]");
private static final Pattern DOUBLE_SLASH_PATTERN = Pattern.compile("^ *//[^/\\s]");
private static final Pattern DOUBLE_SLASH_BEGIN = Pattern.compile("^ *//");
private static final Pattern TRIPLE_SLASH_PATTERN = Pattern.compile("^ *///[^/\\s]");
private static final Pattern TRIPLE_SLASH_BEGIN = Pattern.compile("^ *///");

/**
* Performs a paranoid text spacing on {@code text}.
Expand All @@ -100,9 +104,8 @@ public String spacingText(String text) {
Matcher qcMatcher = QUOTE_CJK.matcher(text);
text = qcMatcher.replaceAll("$1 $2");

// todo, ignore fix quote for now, consider the content between quotes is not meant to be modified
// Matcher fixQuoteMatcher = FIX_QUOTE.matcher(text);
// text = fixQuoteMatcher.replaceAll("$1$3$5");
Matcher fixQuoteMatcher = FIX_QUOTE.matcher(text);
text = fixQuoteMatcher.replaceAll("$1$3$5");

// CJK and brackets
String oldText = text;
Expand Down Expand Up @@ -136,14 +139,13 @@ public String spacingText(String text) {
text = acMatcher.replaceAll("$1 $2");

// 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()) {
// replace the first `//some comment` of a line to `// some comment`
if (DOUBLE_SLASH_PATTERN.matcher(text).find()) {
text = DOUBLE_SLASH_BEGIN.matcher(text).replaceFirst("$0 ");
} else if (TRIPLE_SLASH_PATTERN.matcher(text).find()) {
// `///` is a kind of comment syntax in Rust
text = text.replaceFirst("^///", "/// ");
text = TRIPLE_SLASH_BEGIN.matcher(text).replaceFirst("$0 ");
}

return text;
}

Expand Down Expand Up @@ -185,15 +187,32 @@ public void spacingFile(File inputFile, File outputFile) throws IOException {


/**
* format text
* format text, consider the content between quotes is not meant to be modified
*
* @param text text to format
* @return formatted text
*/
public String formatText(String text) {
List<String> formattedLines = new ArrayList<>();
for (String s : splitText(text)) {
formattedLines.add(spacingText(s));
for (String string : splitText(text)) {
StringBuilder formattedString = new StringBuilder();
// Use regular expression to separate quoted and unquoted content
Pattern quotePattern = Pattern.compile("('.*?'|\".*?\")");
Matcher matcher = quotePattern.matcher(string);
int start = 0;
while (matcher.find()) {
// Unquoted content
String unquoted = string.substring(start, matcher.start());
formattedString.append(spacingText(unquoted));
// Quoted content as original
String quoted = string.substring(matcher.start(), matcher.end());
formattedString.append(quoted);
start = matcher.end();
}
// Last unquoted content
String unquoted = string.substring(start);
formattedString.append(spacingText(unquoted));
formattedLines.add(formattedString.toString());
}
return joinText(formattedLines);
}
Expand All @@ -218,9 +237,8 @@ private List<String> splitText(String text) {
*/
private String joinText(List<String> text) {
final StringBuilder stringBuilder = new StringBuilder();
final Iterator<String> iterator = text.iterator();
while (iterator.hasNext()) {
stringBuilder.append(iterator.next());
for (String s : text) {
stringBuilder.append(s);
}
return stringBuilder.toString();
}
Expand Down

0 comments on commit 93951ad

Please sign in to comment.