-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Java Equivalent of the PythonStyles and IntelliJ defaults (#118)
* Add java style classes * Moved some things around and removed `id` fields --------- Co-authored-by: Knut Wannheden <[email protected]>
- Loading branch information
1 parent
488a47b
commit d87ca4c
Showing
12 changed files
with
307 additions
and
25 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
rewrite-python/src/main/java/org/openrewrite/python/style/BlankLinesStyle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
rewrite-python/src/main/java/org/openrewrite/python/style/IntelliJ.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
rewrite-python/src/main/java/org/openrewrite/python/style/OtherStyle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
rewrite-python/src/main/java/org/openrewrite/python/style/TabsAndIndentsStyle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,4 +50,5 @@ | |
# Style | ||
'Style', | ||
'NamedStyles', | ||
'GeneralFormatStyle', | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.