Code Monkey home page Code Monkey logo

Comments (12)

kitbellew avatar kitbellew commented on May 23, 2024

@kubukoz why did you need to disable significant indentation?

from scalafmt.

kubukoz avatar kubukoz commented on May 23, 2024

@kubukoz why did you need to disable significant indentation?

I didn't need to, I wanted to :) my team chose to keep using braces, and by having Scalafmt know about this, we let it fix the indents on e.g. copy-pasted code.

from scalafmt.

tgodzik avatar tgodzik commented on May 23, 2024

This is expected runner.dialectOverride.allowSignificantIndentation is what enables the parser to actually parse significant indentation and if-then is a part of that. We would need to make a separate dialect option for that and I don't believe it's really needed.

Wouldn't it make sense to just disable it via compiler options?

from scalafmt.

tgodzik avatar tgodzik commented on May 23, 2024

scalacOptions += "-no-indent" works the way you want

from scalafmt.

kitbellew avatar kitbellew commented on May 23, 2024

the question is: if you keep indentation enabled, does scalameta parse incorrectly? doc says that code is not simply parsed as if there had been no significant indentation but may not be written with multiple indented expressions without explicit braces.

or, perhaps, even if scalameta parses correctly, does scalafmt format in a way that the compiler doesn't like? that is, if RedundantBraces is not used.

if the answers are both no, then why disable the flag?

from scalafmt.

kubukoz avatar kubukoz commented on May 23, 2024

scalacOptions += "-no-indent" works the way you want

Yes, I'm using that - I have no trouble with the compiler's side of things.

The problem is, if I remove the allowSignificantIndentation = false, part, then scalafmt doesn't by itself fix the indentation, so I have to rely on compiler warnings to do so:

image

RedundantBraces isn't enabled here, but I do use it in the real project. With that option, we get a completely different behavior, fixing of which takes an additional step (removing another indent after you still see the warning). It may be troublesome to do it every time you copy-paste / move a chunk of code.

image

to sum up, I'm looking for something like -no-indent but for Scalafmt. if then is the recommended conditional syntax in the docs, so I'd like to keep using that.

Side note, when braceless syntax was being introduced we were told it'd still be possible to write code with braces - that is all I'm trying to achieve here :)

from scalafmt.

kubukoz avatar kubukoz commented on May 23, 2024

Admittedly, Metals/vscode has gotten a bit better at moving lines up and down, so maybe the issue/risk isn't significant enough and I can remove the flag now. That said, it'd be a bit disappointing/surprising if the formatter is able to change code behavior without an opt-out key (for non-indent users).

from scalafmt.

tgodzik avatar tgodzik commented on May 23, 2024

Ach ok, I see the exact scenario, I couldn't figure out the problem previously. There is no way to do that now, we would need to change the dialect to separate then etc. options in scalameta

from scalafmt.

kitbellew avatar kitbellew commented on May 23, 2024

@kubukoz when you say "fix the indentation", what exactly do you mean? if your indentation was incorrect to begin with, did you expect the formatter to fix it (and how)? if that indentation was in fact correct, then what happened?

what is the original code you have (before scalafmt formatting)? and what's the output?

from scalafmt.

kubukoz avatar kubukoz commented on May 23, 2024

what is the original code you have (before scalafmt formatting)? and what's the output?

Original:

//> using scala 3.4.1
//> using option -no-indent

object foo {
  def greet() = println("hello")
  def sayGoodbye() = println("goodbye")
  def openDoor() = println("door opened")

  def demo() =
    if (true) greet()
    else
      sayGoodbye()
      openDoor()

}

Formatted with:

version = 3.8.1
runner.dialect = scala3

it's the same as the input.

Expectation (also what happens in dialect = scala213):

//> using scala 3.4.1
//> using option -no-indent

object foo {
  def greet() = println("hello")
  def sayGoodbye() = println("goodbye")
  def openDoor() = println("door opened")

  def demo() =
    if (true) greet()
    else
      sayGoodbye()
  openDoor()

}

if your indentation was incorrect to begin with, did you expect the formatter to fix it

It's correct in Scala 2 style: AFAIK nesting code too far in S2 isn't considered incorrect, and the formatter normally turns cases like that into consistently/correctly indented code.

from scalafmt.

kitbellew avatar kitbellew commented on May 23, 2024

according to 2.2 (https://docs.scala-lang.org/scala3/reference/other-new-features/indentation.html), your original code is invalid under -no-indent. does it compile with scala 3 compiler?

from scalafmt.

kubukoz avatar kubukoz commented on May 23, 2024

It does, with warnings.

from scalafmt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.