/** * Adds spaces after the '#' according to the configured settings. The first char of the buffer * passed (which is also the output) should always start with a '#'. */ public static void formatComment(FormatStd std, FastStringBuffer bufWithComment) { if (std.spacesInStartComment > 0) { Assert.isTrue(bufWithComment.charAt(0) == '#'); int len = bufWithComment.length(); char firstCharFound = '\n'; String bufAsString = bufWithComment.toString(); // handle cases where the code-formatting should not take place if (FileUtils.isPythonShebangLine(bufAsString)) { return; } int spacesFound = 0; String remainingStringContent = ""; for (int j = 1; j < len; j++) { // start at 1 because 0 should always be '#' if ((firstCharFound = bufWithComment.charAt(j)) != ' ') { remainingStringContent = bufAsString.substring(j).trim(); break; } spacesFound += 1; } if (firstCharFound != '\r' && firstCharFound != '\n') { // Only add spaces if it wasn't an empty line. // handle cases where the code-formatting should not take place for (String s : BLOCK_COMMENT_SKIPS) { if (remainingStringContent.endsWith(s) || remainingStringContent.startsWith(s)) { return; } } int diff = std.spacesInStartComment - spacesFound; if (diff > 0) { bufWithComment.insertN(1, ' ', diff); } } } }