Skip to content

Commit

Permalink
Merge pull request #29 from openrewrite/release-fixes-04
Browse files Browse the repository at this point in the history
fix ws and float literals
  • Loading branch information
garyolsen authored Mar 21, 2023
2 parents cc64895 + 65c8ce8 commit 7ac360d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
26 changes: 13 additions & 13 deletions src/main/java/org/openrewrite/python/internal/PsiPythonMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -1363,21 +1363,16 @@ public <T extends PsiElement> J.Block mapBlock(
PsiPaddingCursor paddingCursor = outerCtx.paddingCursor;
List<JRightPadded<Statement>> statements = new ArrayList<>(pyStatements.size());

@Nullable Space blockPrefix;
BlockContext innerCtx;

if (colonToken != null) {
blockPrefix = paddingCursor.consumeUntilNewlineOrRollback();;
if (blockPrefix == null) {
blockPrefix = Space.EMPTY;
}
paddingCursor.resetToSpaceAfter(colonToken);
}

@Nullable Space blockPrefix = paddingCursor.consumeUntilNewlineOrRollback();
if (colonToken == null) {
blockPrefix = paddingCursor.consumeRemaining();
}

BlockContext innerCtx;

if (blockPrefix == null) {
blockPrefix = Space.EMPTY;
innerCtx = new BlockContext("", true, paddingCursor);
} else {
Space firstPrefix = paddingCursor.withRollback(paddingCursor::consumeRemaining);
final String containerIndent = outerCtx.fullIndent;
final String fullIndent = firstPrefix.getIndent();
Expand All @@ -1392,6 +1387,9 @@ public <T extends PsiElement> J.Block mapBlock(
blockPrefix = appendWhitespace(blockPrefix, "\n" + blockIndent);

innerCtx = new BlockContext(fullIndent, false, paddingCursor);
} else {
blockPrefix = appendWhitespace(paddingCursor.consumeRemaining(), "\n");
innerCtx = new BlockContext("", true, paddingCursor);
}

boolean precededBySemicolon = false;
Expand Down Expand Up @@ -2492,7 +2490,9 @@ public J.Literal mapNumericLiteral(PyNumericLiteralExpression element) {
randomId(),
spaceBefore(element),
EMPTY,
element.getLongValue(),
element.isIntegerLiteral()
? element.getLongValue()
: element.getBigDecimalValue(),
element.getText(),
emptyList(),
JavaType.Primitive.Long
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/openrewrite/python/tree/LiteralTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class LiteralTest implements RewriteTest {
@ParameterizedTest
//language=py
@ValueSource(strings = {
"None", "True", "False", "42.2", "42", "\"hello world\"",
"None", "True", "False", "42.2", "42", "123456789.123456789E123456789",
"\"hello world\"",
"\"hello \\\"world\\\"\"", "'hello world'",
"'hello \\'world\\''"
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def f():
def g():
pass
@decorator
@anotherdec
def f():
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/openrewrite/python/tree/WhitespaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.openrewrite.python.tree;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.openrewrite.test.RewriteTest;
Expand All @@ -23,6 +24,17 @@

class WhitespaceTest implements RewriteTest {

@Test
void hashbang() {
rewriteRun(python(
"""
#!/usr/bin/env python3.6
print(42)
"""
));
}

@ParameterizedTest
@ValueSource(strings = {
"",
Expand Down

0 comments on commit 7ac360d

Please sign in to comment.