|
8 | 8 | package net.revelc.code.formatter.xml.lib; |
9 | 9 |
|
10 | 10 | import java.util.ArrayList; |
| 11 | +import java.util.Arrays; |
11 | 12 | import java.util.List; |
12 | | -import java.util.regex.Matcher; |
13 | | -import java.util.regex.Pattern; |
| 13 | +import org.slf4j.Logger; |
| 14 | +import org.slf4j.LoggerFactory; |
14 | 15 |
|
15 | 16 | public class CommentFormatter { |
16 | 17 |
|
17 | | - private static final Pattern ORIGINAL_INDENT_PATTERN = Pattern.compile("^(?<indent>\\s*)-->"); |
| 18 | + private static final Logger logger = LoggerFactory.getLogger(CommentFormatter.class); |
18 | 19 |
|
19 | | - public String format(String tagText, String indent, String lineDelimiter) { |
| 20 | + public String format(String tagText, String indent, String lineDelimiter, FormattingPreferences prefs) { |
20 | 21 | String[] lines = tagText.split(lineDelimiter, -1); |
21 | | - String originalIndent = resolveOriginalIndent(lines); |
22 | 22 |
|
23 | | - List<String> newLines = new ArrayList<>(); |
24 | | - for (String line : lines) { |
25 | | - newLines.add(indent + removeOriginalIndent(line, originalIndent)); |
| 23 | + if (logger.isDebugEnabled()) { |
| 24 | + logger.debug("input: {}\n", Arrays.toString(lines)); |
26 | 25 | } |
27 | 26 |
|
28 | | - return String.join(lineDelimiter, newLines); |
29 | | - } |
30 | | - |
31 | | - private String resolveOriginalIndent(String[] lines) { |
32 | | - // only multi-line comments need replace original indentation. |
33 | | - if (lines.length < 2) { |
34 | | - return null; |
| 27 | + // Caller sets initial indents to method (no indent before <!-- and indent before -->) |
| 28 | + List<String> newLines = new ArrayList<>(); |
| 29 | + for (String line : lines) { |
| 30 | + // do not trim leading space on multi-line comments (just add one indent) |
| 31 | + if (lines.length < 2) { |
| 32 | + newLines.add(indent + line); |
| 33 | + } else if (line.trim().equals("<!--") || line.trim().equals("-->") || line.contains("<!--")) { |
| 34 | + // add one indent for begin comment / end comment / line starting comment with text |
| 35 | + // allowing last line with comment to be indented twice on else |
| 36 | + newLines.add(indent + line.stripLeading()); |
| 37 | + } else if (indent.equals("") && !line.stripLeading().equals("")) { |
| 38 | + // If indent is empty and line is not empty, use canonicalIndent |
| 39 | + newLines.add(prefs.getCanonicalIndent() + line.stripLeading()); |
| 40 | + } else { |
| 41 | + // Add two intent for others |
| 42 | + newLines.add(indent + indent + line.stripLeading()); |
| 43 | + } |
35 | 44 | } |
36 | 45 |
|
37 | | - for (int i = lines.length - 1; i >= 0; i--) { |
38 | | - String line = lines[i]; |
| 46 | + logger.debug("output: {}\n", newLines); |
39 | 47 |
|
40 | | - if (line.trim().endsWith("-->")) { |
41 | | - Matcher m = ORIGINAL_INDENT_PATTERN.matcher(line); |
42 | | - if (m.matches()) { |
43 | | - return m.group("indent"); |
44 | | - } |
45 | | - return null; |
46 | | - } |
47 | | - } |
48 | | - return null; |
| 48 | + // Returned data to caller will be properly positioned by caller |
| 49 | + return String.join(lineDelimiter, newLines); |
49 | 50 | } |
50 | 51 |
|
51 | | - private static String removeOriginalIndent(String line, String indent) { |
52 | | - if (indent != null && line.startsWith(indent)) { |
53 | | - return line.substring(indent.length()); |
54 | | - } |
55 | | - return line; |
56 | | - } |
57 | 52 | } |
0 commit comments