Skip to content

Commit

Permalink
PolicyOps: move some policies from FormatOps
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Dec 15, 2021
1 parent 7f10770 commit 9a7e19c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1085,45 +1085,6 @@ class FormatOps(
} else Seq(Split(Space, 0), Split(Newline, 1))
}

def decideNewlinesOnlyBeforeClose(
close: Token
)(implicit fileLine: FileLine): Policy =
decideNewlinesOnlyBeforeClose(Split(Newline, 0))(close)

def decideNewlinesOnlyBeforeCloseOnBreak(
close: Token
)(implicit fileLine: FileLine): Policy =
delayedBreakPolicyFor(close)(decideNewlinesOnlyBeforeClose)

def decideNewlinesOnlyBeforeClose(
split: Split
)(close: Token)(implicit fileLine: FileLine): Policy =
Policy.on(close) {
case d: Decision if d.formatToken.right eq close =>
d.onlyNewlinesWithFallback(split)
}

def decideNewlinesOnlyAfterClose(
close: Token
)(implicit fileLine: FileLine): Policy =
decideNewlinesOnlyAfterClose(Split(Newline, 0))(close)

def decideNewlinesOnlyAfterClose(
split: Split
)(close: Token)(implicit fileLine: FileLine): Policy =
Policy.after(close) {
case d: Decision if d.formatToken.left eq close =>
d.onlyNewlinesWithFallback(split)
}

def decideNewlinesOnlyAfterToken(
token: Token
)(implicit fileLine: FileLine): Policy =
Policy.after(token) {
case d: Decision if d.formatToken.left eq token =>
d.onlyNewlinesWithoutFallback
}

def getForceConfigStyle: (Set[Tree], Set[TokenHash]) = {
val maxDistance = runner.optimizer.forceConfigStyleOnOffset
if (maxDistance < 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.scalafmt.util
import scala.meta.tokens.{Token => T}

import org.scalafmt.internal.Decision
import org.scalafmt.internal.Newline
import org.scalafmt.internal.Policy
import org.scalafmt.internal.Policy.End
import org.scalafmt.internal.Split
Expand Down Expand Up @@ -133,4 +134,43 @@ object PolicyOps {
)(f: T => Policy)(implicit fileLine: FileLine): Policy =
delayedBreakPolicyBefore(token)(f(token))

def decideNewlinesOnlyBeforeClose(close: T)(implicit
fileLine: FileLine
): Policy =
decideNewlinesOnlyBeforeClose(Split(Newline, 0))(close)

def decideNewlinesOnlyBeforeCloseOnBreak(close: T)(implicit
fileLine: FileLine
): Policy =
delayedBreakPolicyFor(close)(decideNewlinesOnlyBeforeClose)

def decideNewlinesOnlyBeforeClose(
split: Split
)(close: T)(implicit fileLine: FileLine): Policy =
Policy.on(close) {
case d: Decision if d.formatToken.right eq close =>
d.onlyNewlinesWithFallback(split)
}

def decideNewlinesOnlyAfterClose(close: T)(implicit
fileLine: FileLine
): Policy =
decideNewlinesOnlyAfterClose(Split(Newline, 0))(close)

def decideNewlinesOnlyAfterClose(
split: Split
)(close: T)(implicit fileLine: FileLine): Policy =
Policy.after(close) {
case d: Decision if d.formatToken.left eq close =>
d.onlyNewlinesWithFallback(split)
}

def decideNewlinesOnlyAfterToken(
token: T
)(implicit fileLine: FileLine): Policy =
Policy.after(token) {
case d: Decision if d.formatToken.left eq token =>
d.onlyNewlinesWithoutFallback
}

}

0 comments on commit 9a7e19c

Please sign in to comment.