Skip to content

Indentation Syntax #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0d00987
Added new rule for indentation syntax
kotleta2007 Oct 30, 2023
c2377c8
Implemented indentation syntax for if-statements
kotleta2007 Oct 30, 2023
35d0129
Implemented indentation syntax for control structures
kotleta2007 Oct 30, 2023
16483c8
Implemented indentation syntax for case blocks (try-catch and pattern…
kotleta2007 Oct 30, 2023
07a182c
Implemented new syntax for templates
kotleta2007 Oct 30, 2023
0e3e599
Added new tests
kotleta2007 Nov 4, 2023
b9b2be0
Added end markers and removing whitespace before colon
kotleta2007 Nov 11, 2023
cb016cd
Added RLEIndent and addEndMarker method
kotleta2007 Nov 13, 2023
e678fa3
Added matchTreeTypeWithEndMarkers
kotleta2007 Nov 13, 2023
b6857b3
Added splitOn; added end markers for if-statements
kotleta2007 Nov 13, 2023
dfbc57f
Added tests for end markers
kotleta2007 Nov 14, 2023
d779ea3
Added new end markers
kotleta2007 Nov 14, 2023
ced7ec7
Added support for modifier keywords
kotleta2007 Nov 17, 2023
f49954e
Added support for extensions
kotleta2007 Nov 17, 2023
5c1be18
Added comparison operators for RLEIndent
kotleta2007 Nov 17, 2023
3098c90
Reference example of end markers works
kotleta2007 Nov 20, 2023
f06a60f
Commiting before refactoring indentation patches
kotleta2007 Nov 29, 2023
19f1ac3
Added brace patches
kotleta2007 Nov 29, 2023
6f82429
replace right brace with end marker
kotleta2007 Nov 29, 2023
ae64df9
indentation works
kotleta2007 Nov 29, 2023
8abfa80
Added new rule for end markers; added support for enums and package o…
kotleta2007 Dec 12, 2023
bd699ce
Added new reference test
kotleta2007 Dec 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions input/src/main/scala/fix/EndMarker_Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fix
/*
rules = [
AddEndMarkers
]
AddEndMarkers.addEndMarkers = true
*/
object EndMarker_Test:
if true then
println(true)

class MyNumber(number: Int) extends AnyRef:
println(number)

case class MyText(text: String):
println(text)

extension (m: MyText)
def myPrint = println(m.text)

enum Color:
case Red, Green, Blue

package object A
74 changes: 74 additions & 0 deletions input/src/main/scala/fix/IndentationSyntax_Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
rules = [
IndentationSyntax
]

IndentationSyntax.addEndMarkers = true
*/
package fix

object IndentationSyntax_Test {
if (true) {
println("a")
println("b")
println("c")
}

if (true) {
println("a2")
println("b2")
println("c2")
}

if (true) {
println("a")
println("b")
println("c")
}

if (true)
println("a")
println("b")
println("c")

while (true) {
println("a")
println("b")
println("c")
}

// spaces instead of tabs
while (true) {
println("a")
println("b")
println("c")
}

val xs = List(1, 2, 3)
for (x <- xs) {
println("a")
println("b")
println("c")
}

for (x <- xs) yield {
val myFactor = 2
myFactor * x
}

object MyObject:
override def toString(): String = "myObject"

if (true) {
if (true) {
println("true")
}
}

val num = 2
val numRes = num match {
case 2 => "two"
case _ => "not two"
}

}
282 changes: 282 additions & 0 deletions input/src/main/scala/fix/ReferenceTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
/*
rules = [
IndentationSyntax
]

IndentationSyntax.addEndMarkers = false
*/
// A collection of patterns found when rewriting the community build to indent

trait C1 {

class CC1
// do not remove braces if empty region
class CC2 {

}
// do not remove braces if open brace is not followed by new line
def m1(x: Int) =
{ x
.toString
}
// add indent to pass an argument (fewer braces)
def m2: String = {
m1 {
5
}
}
// indent inner method
def m3: Int = {
def seq = {
Seq(
"1",
"2"
)
}
seq
(1)
.toInt
}
// indent refinement
def m4: Any {
def foo: String
}
=
new {
def foo: String =
"""
Hello, World!
"""
}
// indent end marker
end m4

// fix off-by-one indentation
// val x = ""

// def m5(x: String): String = {
// def inner: Boolean = {
// true
// }
// x
// }

// unindent properly when needed
def m6(xs: Seq[String]): String = {
xs
.map {
x => x
}
.filter {
x => x.size > 0
}
println("foo")

def foo: String =
""
foo
}

// do not remove braces if closing braces not followed by new line
def m7: String = {
val x = "Hi"
x
}; def m8(x: String): String = {
s"""Bye $x ${
x
}
do not indent in a multiline string"""
}
def m9 = {
val foo = ""
val x = Seq(
s"${foo}",
""
)
}

// do not remove braces after closing brace
def m10(x: Int)(y: String) = y * x
m10 { 5 } {
"foo"
}

// preserve indent of chained calls
def m11(xs: Seq[String]) = {
xs
.filter {
_ => true
}
xs
.map { x =>
val y =
if (x == "") "empty"
else x.size.toString
val z = x + y
z
}
.map { x => xs }.flatMap { xs => xs.map { x =>
val y =
if (x == "") "empty"
else x.size.toString
val z = x + y
z
}}
.map {
x => val y =
if (x == "") "empty"
else x.size.toString
val z = x + y
z
}
.map {
x =>
val y =
if (x == "") "empty"
else x.size.toString
val z = x + y
z
}
}

// do not remove braces inside (...) or [...]
// remove braces after =>
// def m12(xs: List[Int]) = {
// println(
// xs.size match {
// case 1 =>
// xs match {
// case 1 :: Nil => "1"
// case _ => s"${xs.head} :: Nil"
// }
// case _ => {
// "xs"
// }
// }
// )
// println(
// if (xs.size > 0) {
// "foo"
// } else {
// "bar"
// }
// )
// xs.map(
// x => {
// x
// }
// ).map {
// x => {
// x
// }
// }
// }
// import reflect.Selectable.reflectiveSelectable
// def m13(xs: List[
// Any {
// def foo: String
// }
// ]) =
// xs.map(x => x.foo)

// preserve indentation style before 'case'
// but fix indentation inside 'case'
def m14(o: Option[String]) = {
o match
case Some(x) => x
case None => ""

o match
case Some(x) => x
case None => ""
end match

o match {
case None =>
""
case Some(x) =>
x
}
}
def m15(xs: List[Int]): String = {
xs match {
case _ :: tail => {
if tail.size == 0 then
println("log")
}
"foo"
case Nil =>
"bar"
}
}

// add backticks around operator
object *:{
def foo = ???
}
def m16 =
val x = 5 * {
2
} == 10 || {
false
}
x `&&` {
true
}

// leading infix operator
def m17 =
true
&& {
false
}

// ident ending with '_'
def m_(x: String) = ???
m_ {
"foo"
}

// do not remove braces in sequence of blocks
def m18(using ctx: String) = println(ctx)
{
given String = "foo"
m18
}
{
given String = "bar"
m18
}
def m19(x: String) = {
{
given String = "foo"
m18
}
{
given String = "bar"
m18
}
}

// back-quote end indent before match
// def m20 =
// val end = "Foo"
// end match {
// case "Foo" =>
// case _ =>
// }
// end take 3
}

// indent template after self type
class C2 { self =>
val x = ""
}
trait C3 {
self =>
val x = ""
}
case class C4() {
self =>
val y = ""
}
Loading