PY-31991 Formatter strips spaces around topmost expressions in f-strings fragments

Thus we forcibly remove them whenever they are added as a result of "Extract variable"
refactoring.

Probably we could add a dedicated formatter option for that in future.
This commit is contained in:
Mikhail Golubev
2018-11-20 17:13:08 +03:00
parent 01b72f856a
commit 67ac69f683
7 changed files with 19 additions and 2 deletions

View File

@@ -17,7 +17,6 @@ package com.jetbrains.python.formatter;
import com.intellij.formatting.*;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleSettings;
@@ -113,6 +112,8 @@ public class PythonFormattingModelBuilder implements FormattingModelBuilderEx, C
.between(COMMA, RBRACKET).spaceIf(commonSettings.SPACE_WITHIN_BRACKETS | commonSettings.SPACE_AFTER_COMMA)
.withinPair(LBRACKET, RBRACKET).spaceIf(commonSettings.SPACE_WITHIN_BRACKETS)
.withinPair(FSTRING_FRAGMENT_START, FSTRING_FRAGMENT_END).spaces(0)
.before(COLON).spaceIf(pySettings.SPACE_BEFORE_PY_COLON)
.afterInside(LPAR, FROM_IMPORT_STATEMENT).spaces(0, pySettings.FROM_IMPORT_NEW_LINE_AFTER_LEFT_PARENTHESIS)
.betweenInside(COMMA, RPAR, FROM_IMPORT_STATEMENT).spaceIf(commonSettings.SPACE_AFTER_COMMA,

View File

@@ -0,0 +1 @@
s = f'foo{ 42 }bar'

View File

@@ -0,0 +1 @@
s = f'foo{42}bar'

View File

@@ -0,0 +1,2 @@
a = 42
s = f'foo{a}bar'

View File

@@ -0,0 +1 @@
s = f'foo{<selection>42</selection>}bar'

View File

@@ -928,6 +928,11 @@ public class PyFormatterTest extends PyTestCase {
}
public void testMultilineFStringExpressions() {
doTest();
runWithLanguageLevel(LanguageLevel.PYTHON36, this::doTest);
}
// PY-31991
public void testSpacesAroundFStringFragmentExpressionStripped() {
runWithLanguageLevel(LanguageLevel.PYTHON36, this::doTest);
}
}

View File

@@ -5,6 +5,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.testFramework.TestDataPath;
import com.jetbrains.python.PythonFileType;
import com.jetbrains.python.psi.LanguageLevel;
import com.jetbrains.python.psi.PyCallExpression;
import com.jetbrains.python.psi.PyExpression;
import com.jetbrains.python.psi.PyFunction;
@@ -315,6 +316,11 @@ public class PyIntroduceVariableTest extends PyIntroduceTestCase {
doTest();
}
// PY-31991
public void testNoExtraSpacesAroundFStringFragmentExpression() {
runWithLanguageLevel(LanguageLevel.PYTHON36, this::doTest);
}
@Override
protected String getTestDataPath() {
return super.getTestDataPath() + "/refactoring/introduceVariable";