Skip to content

Commit

Permalink
Add Java Equivalent of the PythonStyles and IntelliJ defaults (#118)
Browse files Browse the repository at this point in the history
* Add java style classes

* Moved some things around and removed `id` fields

---------

Co-authored-by: Knut Wannheden <[email protected]>
  • Loading branch information
nielsdebruin and knutwannheden authored Jan 10, 2025
1 parent 488a47b commit d87ca4c
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.python.style;

import lombok.Value;
import lombok.With;
import org.openrewrite.style.Style;
import org.openrewrite.style.StyleHelper;

@Value
@With
public class BlankLinesStyle implements PythonStyle {

KeepMaximum keepMaximum;
Minimum minimum;

@Value
@With
public static class KeepMaximum {
int inDeclarations;
int inCode;
}

@Value
@With
public static class Minimum {
int afterTopLevelImports;
int aroundClass;
int aroundMethod;
int aroundTopLevelClassesFunctions;
int afterLocalImports;
int beforeFirstMethod;
}

@Override
public Style applyDefaults() {
return StyleHelper.merge(IntelliJ.blankLines(), this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.python.style;

import org.openrewrite.style.NamedStyles;

import java.util.Arrays;
import java.util.Collections;

import static org.openrewrite.Tree.randomId;


public class IntelliJ extends NamedStyles {
private static final IntelliJ INSTANCE = new IntelliJ();

public IntelliJ() {
super(randomId(),
"org.openrewrite.python.style.IntelliJ",
"IntelliJ IDEA",
"IntelliJ IDEA default Python style.",
Collections.emptySet(),
Arrays.asList(
spaces(),
wrappingAndBraces(),
tabsAndIndents(),
blankLines(),
other()
)
);
}

public static IntelliJ defaults() {
return INSTANCE;
}

public static SpacesStyle spaces() {
return new SpacesStyle(
new SpacesStyle.BeforeParentheses(
false, // methodCall
false, // methodDeclaration
false // leftBracket
),
new SpacesStyle.AroundOperators(
true, // assignment
true, // equality
true, // relational
true, // bitwise
true, // additive
true, // multiplicative
true, // shift
true, // power
false, // eqInNamedParameter
false // eqInKeywordArgument
),
new SpacesStyle.Within(
false, // brackets
false, // methodDeclarationParentheses
false, // emptyMethodDeclarationParentheses
false, // methodCallParentheses
false, // emptyMethodCallParentheses
false // braces
),
new SpacesStyle.Other(
false, // beforeComma
true, // afterComma
false, // beforeForSemicolon
false, // beforeColon
true, // afterColon
true, // beforeBackslash
true, // beforeHash
true // afterHash
)
);
}

public static WrappingAndBracesStyle wrappingAndBraces() {
return new WrappingAndBracesStyle();
}

public static TabsAndIndentsStyle tabsAndIndents() {
return new TabsAndIndentsStyle(
false, // useTabCharacter
4, // tabSize
4, // indentSize
8, // continuationIndent
false // keepIndentsOnEmptyLines
);
}

public static BlankLinesStyle blankLines() {
return new BlankLinesStyle(
new BlankLinesStyle.KeepMaximum(
1, // inDeclarations
1 // inCode
),
new BlankLinesStyle.Minimum(
1, // afterTopLevelImports
1, // aroundClass
1, // aroundMethod
2, // aroundTopLevelClassesFunctions
0, // afterLocalImports
0 // beforeFirstMethod
)
);
}

public static OtherStyle other() {
return new OtherStyle(
new OtherStyle.UseContinuationIndent(
false, // methodCallArguments
true, // methodDeclarationParameters
false // collectionsAndComprehensions
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.python.style;

import lombok.Value;
import lombok.With;
import org.openrewrite.style.Style;
import org.openrewrite.style.StyleHelper;

@Value
@With
public class OtherStyle implements PythonStyle {

UseContinuationIndent useContinuationIndent;

@Value
@With
public static class UseContinuationIndent {
boolean methodCallArguments;
boolean methodDeclarationParameters;
boolean collectionsAndComprehensions;
}

@Override
public Style applyDefaults() {
return StyleHelper.merge(IntelliJ.other(), this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

import lombok.Value;
import lombok.With;

import java.util.UUID;
import org.openrewrite.style.Style;
import org.openrewrite.style.StyleHelper;

@Value
@With
public class SpacesStyle implements PythonStyle {

UUID id;
BeforeParentheses beforeParentheses;
AroundOperators aroundOperators;
Within within;
Expand Down Expand Up @@ -77,8 +76,8 @@ public static class Other {
Boolean afterHash;
}

// @Override
// public Style applyDefaults() {
// return StyleHelper.merge(IntelliJ.spaces(), this);
// }
@Override
public Style applyDefaults() {
return StyleHelper.merge(IntelliJ.spaces(), this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.python.style;

import lombok.Value;
import lombok.With;
import org.openrewrite.style.Style;
import org.openrewrite.style.StyleHelper;

@Value
@With
public class TabsAndIndentsStyle implements PythonStyle {

boolean useTabCharacter;
int tabSize;
int indentSize;
int continuationIndent;
boolean keepIndentsOnEmptyLines;

@Override
public Style applyDefaults() {
return StyleHelper.merge(IntelliJ.tabsAndIndents(), this);
}
}
1 change: 1 addition & 0 deletions rewrite/rewrite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@
# Style
'Style',
'NamedStyles',
'GeneralFormatStyle',
]
1 change: 1 addition & 0 deletions rewrite/rewrite/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
'TabsAndIndentsStyle',
'WrappingAndBracesStyle',
'BlankLinesStyle',
'OtherStyle',
'IntelliJ',

# Formatter
Expand Down
3 changes: 2 additions & 1 deletion rewrite/rewrite/python/format/auto_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
from .remove_trailing_whitespace_visitor import RemoveTrailingWhitespaceVisitor
from .spaces_visitor import SpacesVisitor
from .tabs_and_indents_visitor import TabsAndIndentsVisitor
from .. import TabsAndIndentsStyle, GeneralFormatStyle, OtherStyle
from .. import TabsAndIndentsStyle, OtherStyle
from ..style import BlankLinesStyle, SpacesStyle, IntelliJ
from ..visitor import PythonVisitor
from ... import Recipe, Tree, Cursor
from ...java import JavaSourceFile
from ...style import GeneralFormatStyle
from ...visitor import P, T


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

from typing import Optional, TypeVar, Union, cast

from rewrite import Tree, P, Cursor, list_map, Marker
from rewrite import Tree, P, Cursor, list_map, Marker, GeneralFormatStyle
from rewrite.java import J, Space, Comment, TextComment, TrailingComma
from rewrite.python import PythonVisitor, PySpace, GeneralFormatStyle, PyComment
from rewrite.python import PythonVisitor, PySpace, PyComment
from rewrite.visitor import T

J2 = TypeVar('J2', bound=J)


class NormalizeLineBreaksVisitor(PythonVisitor):
def __init__(self, style: GeneralFormatStyle, stop_after: Tree = None):
def __init__(self, style: GeneralFormatStyle, stop_after: Optional[Tree] = None):
self._stop_after = stop_after
self._stop = False
self._style = style
Expand Down
35 changes: 22 additions & 13 deletions rewrite/rewrite/python/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from dataclasses import dataclass, replace

from .. import random_id
from ..style import Style, NamedStyles


Expand Down Expand Up @@ -449,19 +450,6 @@ def with_minimum(self, minimum: Minimum) -> BlankLinesStyle:
return self if minimum is self._minimum else replace(self, _minimum=minimum)


@dataclass(frozen=True)
class GeneralFormatStyle(PythonStyle):
_use_crlf_new_lines: bool

@property
def use_crlf_new_lines(self) -> bool:
return self._use_crlf_new_lines

def with_use_crlf_new_lines(self, use_crlf_new_lines: bool) -> GeneralFormatStyle:
return self if use_crlf_new_lines is self._use_crlf_new_lines else replace(self,
_use_crlf_new_lines=use_crlf_new_lines)


@dataclass(frozen=True)
class OtherStyle(PythonStyle):
@dataclass(frozen=True)
Expand Down Expand Up @@ -504,6 +492,27 @@ def with_use_continuation_indent(self, use_continuation_indent: UseContinuationI


class IntelliJ(NamedStyles):

def __init__(self):
super().__init__(
_id=random_id(),
_name='org.openrewrite.python.style.IntelliJ',
_display_name='IntelliJ IDEA',
_description='IntelliJ IDEA default Python style.',
_tags={},
_styles=(
IntelliJ.spaces(),
IntelliJ.wrapping_and_braces(),
IntelliJ.tabs_and_indents(),
IntelliJ.blank_lines(),
IntelliJ.other(),
)
)

@classmethod
def defaults(cls) -> IntelliJ:
return IntelliJ()

@classmethod
def spaces(cls) -> SpacesStyle:
return SpacesStyle(
Expand Down
Loading

0 comments on commit d87ca4c

Please sign in to comment.