diff --git a/.gitignore b/.gitignore
index e2e5d94..757e1f8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@
.idea
.qodana
build
+/.idea/
diff --git a/.idea/gradle.xml b/.idea/gradle.xml
index 6622921..3cec992 100644
--- a/.idea/gradle.xml
+++ b/.idea/gradle.xml
@@ -1,20 +1,20 @@
-
-
-
-
+
\ No newline at end of file
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 28a031d..81d6087 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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)
@@ -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
diff --git a/gradle.properties b/gradle.properties
index afcc3b1..fc08785 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -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.
diff --git a/src/main/java/com/lilittlecat/plugin/pangu/Pangu.java b/src/main/java/com/lilittlecat/plugin/pangu/Pangu.java
index 6a6bf2d..866abaa 100644
--- a/src/main/java/com/lilittlecat/plugin/pangu/Pangu.java
+++ b/src/main/java/com/lilittlecat/plugin/pangu/Pangu.java
@@ -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}.
@@ -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;
@@ -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;
}
@@ -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 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);
}
@@ -218,9 +237,8 @@ private List splitText(String text) {
*/
private String joinText(List text) {
final StringBuilder stringBuilder = new StringBuilder();
- final Iterator iterator = text.iterator();
- while (iterator.hasNext()) {
- stringBuilder.append(iterator.next());
+ for (String s : text) {
+ stringBuilder.append(s);
}
return stringBuilder.toString();
}