Scalafmt force curly braces style over indentation - scala

I am trying to force curly braces style for my scala project. I have written below config for scalafmt:
version = "3.7.1"
runner.dialect = scala3
maxColumn = 80
rewrite.rules = [Imports]
rewrite.insertBraces.ifElseExpressions = true
But when I apply this config for file with below lines of code, I don't see any change performed by scalafmt. How I can fix this? and Is there any better way to force curly braces style for everything i.e. function definitions, class definition etc.
if (sortedList.isEmpty) new Cons(x, empty)
else if (compare(x, sortedList.head) <= 0) new Cons(x, sortedList)
else new Cons(sortedList.head, insert(x, sortedList.tail))
SBT version: 1.6.2

Related

scalafmt class, methods params intendation

Is it possible in scalafmt to set intendation to be like in intellij formatter so just after ending of (
ie:
case class SomeMessage(
id: UUID
)
I see only ability to setup tab numbers but its counted from line start not open bracket position
By default verticalMultiline.newlineAfterOpenParen is set to false. By adding the following lines to your scalafmt file you can override this config
continuationIndent.defnSite = 2
verticalMultiline.atDefnSite = true
verticalMultiline.arityThreshold = 2
verticalMultiline.newlineAfterOpenParen = true
Please refer link for examples.

How can I print all the settings in Test configuration for a project build using SBT?

I have a scala-js project, adding a particular library dependency, is affecting the way project test cases are running. Without the library dependency everything's fine, the moment I add them, tests doesn't execute. I want to check all sbt settings, if those are getting affected. Is there any way I can print all settings and check?
BuildStructure.data seems to give access to all the settings by scope. We could access it by defining a custom command printAllTestSettings like so:
def printAllTestSettings = Command.command("printAllTestSettings") { state =>
val structure = Project.extract(state).structure
val testScope =
Scope(
Select(ProjectRef(new File("/home/mario/sandbox/hello-world-scala/"), "root")),
Select(ConfigKey("test")),
Zero,
Zero
)
structure
.data
.keys(testScope)
.foreach(key => println(s"${key.label} = ${structure.data.get(testScope, key).get}"))
state
}
commands ++= Seq(printAllTestSettings)
Here is output snippet:
...
managedSourceDirectories = List(/home/mario/sandbox/hello-world-scala/target/scala-2.12/src_managed/test)
managedResourceDirectories = List(/home/mario/sandbox/hello-world-scala/target/scala-2.12/resource_managed/test)
testLoader = Task((taskDefinitionKey: ScopedKey(Scope(Select(ProjectRef(file:/home/mario/sandbox/hello-world-scala/,root)), Select(ConfigKey(test)), Zero, Zero),testLoader)))
packageBin = Task((taskDefinitionKey: ScopedKey(Scope(Select(ProjectRef(file:/home/mario/sandbox/hello-world-scala/,root)), Select(ConfigKey(test)), Zero, Zero),packageBin)))
...

intellij scala built-in formatter messing up partial functions

i'm trying to convince the built-in intellij scala formatter to format a partial function as a parameter like this:
PersistenceQuery(context.system)
.readJournalFor[ScalaDslMongoReadJournal](MongoReadJournal.Identifier)
.eventsByPersistenceId(persistenceId, 0, Long.MaxValue)
.map(_.event)
.collect[Thing]({
case e:Foo => e // note the indentation
}) // aligned with the .
.runWith(Sink.asPublisher(fanout = false))
but, the best i can get is:
PersistenceQuery(context.system)
.readJournalFor[ScalaDslMongoReadJournal](MongoReadJournal.Identifier)
.eventsByPersistenceId(persistenceId, 0, Long.MaxValue)
.map(_.event)
.collect[Thing]({
case e:Foo => e // unindented
}) // *under* indented!
.runWith(Sink.asPublisher(fanout = false))
i'd prefer to use the built-in formatter over an external one.
anybody know what the right configuration incantation looks like?
intellij 2017.2.1
scala plugin 2017.2.13

Intellij indentation format - method definition with/without braces (in scala)

I am having a minor annoyance with intellij indentation which I hope someone can help me with.
If I write a method declaration with braces, indenting works properly:
def function(a:A): B = { // When I hit return on this line...
// the cursor goes here (4 spaces in)
}
However, if I don't include braces, I don't get any indentation:
def function(a:A): B = // When I hit return on this line...
// the cursor goes here (0 spaces in)
To make matters stranger, if I already have some code on the first line before hitting return, I do get the correct spacing
Before:
def function(a:A): B = some.code.here //cursor is immediately after the = sign
After:
def function(a:A): B =
some.code.here // Correct indentation behavior.
So what I want to know is this: is there a setting which will result in an auto-indent if I hit return at the end of a method declaration without any code behind the cursor?
Thanks!

Playframework 2.0 define function in View Template

I am working on a project using PlayFramework 2.0. After reading a bit of scala I would like to embed some dynamic code in the View template. So, I did the following:
#{
def getMystring(sequence:Int) = {
if(patternForm != null &&
patternForm.get().windowTreatments != null &&
patternForm.get().windowTreatments.size() >= sequence + 1)
sequence+""
else
""
}
}
<input type = "text" value = #getMystring(1)></input>
...
I was quite sure it was going to work but instead I got a "not found: value getMyString Error occurred" . Did I do something obviously wrong?
try starting it like a template, like this
#getMystring(sequence:Int) = {
[...]
have a look at https://github.com/playframework/Play20/blob/master/samples/scala/computer-database/app/views/list.scala.html
The problem being that play defines a very narrow scope and can't recognize defs outside its current curly brackets.
You can change the position of the last curly bracket for your def to include the input tag and then it should work.
Or you can do what opensas suggested.
#getMystring(sequence:Int) = {
[...]