optional space before backslash (on by default) in python code style settings (PY-5674)

This commit is contained in:
Dmitry Jemerov
2013-01-29 20:31:08 +01:00
parent b796386013
commit 950c820cc0
13 changed files with 33 additions and 14 deletions
@@ -13,6 +13,7 @@ public class PyCodeStyleSettings extends CustomCodeStyleSettings {
public boolean SPACE_BEFORE_LBRACKET = false;
public boolean SPACE_AROUND_EQ_IN_NAMED_PARAMETER = false;
public boolean SPACE_AROUND_EQ_IN_KEYWORD_ARGUMENT = false;
public boolean SPACE_BEFORE_BACKSLASH = true;
public int BLANK_LINES_AROUND_TOP_LEVEL_CLASSES_FUNCTIONS = 2;
@@ -58,6 +58,7 @@ public class PyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSettin
consumer.showCustomOption(PyCodeStyleSettings.class, "SPACE_WITHIN_BRACES", "Braces", SPACES_WITHIN);
consumer.showCustomOption(PyCodeStyleSettings.class, "SPACE_BEFORE_PY_COLON", ApplicationBundle.message("checkbox.spaces.before.colon"), SPACES_OTHER);
consumer.showCustomOption(PyCodeStyleSettings.class, "SPACE_AFTER_PY_COLON", ApplicationBundle.message("checkbox.spaces.after.colon"), SPACES_OTHER);
consumer.showCustomOption(PyCodeStyleSettings.class, "SPACE_BEFORE_BACKSLASH", "Before \\", SPACES_OTHER);
}
else if (settingsType == SettingsType.BLANK_LINES_SETTINGS) {
consumer.showStandardOptions("BLANK_LINES_AROUND_CLASS",
@@ -1,5 +1,6 @@
package com.jetbrains.python.formatter;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.formatter.StaticSymbolWhiteSpaceDefinitionStrategy;
import gnu.trove.TIntIntHashMap;
import gnu.trove.TIntProcedure;
@@ -21,10 +22,12 @@ public class PyWhiteSpaceFormattingStrategy extends StaticSymbolWhiteSpaceDefini
* <p/>
* Hence, we need to preserve them during white space manipulation.
*
*
* @param whiteSpaceText white space text to use by default for replacing sub-sequence of the given text
* @param text target text which region is to be replaced by the given white space symbols
* @param startOffset start offset to use with the given text (inclusive)
* @param endOffset end offset to use with the given text (exclusive)
* @param codeStyleSettings the code style settings
* @return symbols to use for replacing <code>[startOffset; endOffset)</code> sub-sequence of the given text
*/
@NotNull
@@ -32,7 +35,8 @@ public class PyWhiteSpaceFormattingStrategy extends StaticSymbolWhiteSpaceDefini
public CharSequence adjustWhiteSpaceIfNecessary(@NotNull CharSequence whiteSpaceText,
@NotNull CharSequence text,
int startOffset,
int endOffset)
int endOffset,
CodeStyleSettings codeStyleSettings)
{
// The general idea is that '\' symbol before line feed should be preserved.
TIntIntHashMap initialBackSlashes = countBackSlashes(text, startOffset, endOffset);
@@ -56,6 +60,7 @@ public class PyWhiteSpaceFormattingStrategy extends StaticSymbolWhiteSpaceDefini
return whiteSpaceText;
}
PyCodeStyleSettings settings = codeStyleSettings.getCustomSettings(PyCodeStyleSettings.class);
StringBuilder result = new StringBuilder();
int line = 0;
for (int i = 0; i < whiteSpaceText.length(); i++) {
@@ -65,6 +70,9 @@ public class PyWhiteSpaceFormattingStrategy extends StaticSymbolWhiteSpaceDefini
continue;
}
if (!newBackSlashes.contains(line++)) {
if ((i == 0 || (i > 0 && whiteSpaceText.charAt(i - 1) != ' ')) && settings.SPACE_BEFORE_BACKSLASH) {
result.append(' ');
}
result.append('\\');
}
result.append(c);
@@ -1,2 +1,2 @@
if isintance(True, bool) and\
if isintance(True, bool) and \
isinstance(1, int): pass
@@ -1,3 +1,3 @@
term1 = BNF.lpar + expr + BNF.rpar\
| if_expr\
term1 = BNF.lpar + expr + BNF.rpar \
| if_expr \
| numeric_value
@@ -1,2 +1,2 @@
a = "a"\
a = "a" \
"b"
@@ -1,2 +1,2 @@
foo = bar\
foo = bar \
if bar is not None else None
@@ -1,2 +1,2 @@
import contextlib,\
import contextlib, \
math, decimal
@@ -0,0 +1,2 @@
import contextlib,\
math,decimal
@@ -0,0 +1,2 @@
import contextlib,\
math, decimal
@@ -1,13 +1,13 @@
n = 8
a = "{n:d} bottles of {what:s} on the {where:s}"\
a = "{n:d} bottles of {what:s} on the {where:s}" \
"""
{n:d} bottles
of {what:s}
"""\
r'\n/ take {howmuch!r:s} down \n/'\
ur"pass it {how:>8s}"\
"{new_n:#d} {{that is, {percent:+3.2f}% less}} "\
"bottles of {what:>6s} on the {where:s}"\
""" \
r'\n/ take {howmuch!r:s} down \n/' \
ur"pass it {how:>8s}" \
"{new_n:#d} {{that is, {percent:+3.2f}% less}} " \
"bottles of {what:>6s} on the {where:s}" \
.format(n=n, where="wall", howmuch=u'one', how="'round\t", new_n=(n - 1), percent=100 * (1 - ((n - 1.0) / n)),
what="beer")
print a
@@ -1,3 +1,3 @@
a = "foo"\
a = "foo" \
"bar"
x(a)
@@ -268,6 +268,11 @@ public class PyFormatterTest extends PyTestCase {
doTest();
}
public void testSpaceBeforeBackslash() {
settings().getCustomSettings(PyCodeStyleSettings.class).SPACE_BEFORE_BACKSLASH = false;
doTest();
}
private void doTest() {
myFixture.configureByFile("formatter/" + getTestName(true) + ".py");
ApplicationManager.getApplication().runWriteAction(new Runnable() {