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 3ec044e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 48 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
@@ -1,8 +1,11 @@
package org.scalafmt.util

import scala.meta.Tree
import scala.meta.tokens.{Token => T}

import org.scalafmt.internal.Decision
import org.scalafmt.internal.FormatToken
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 @@ -118,19 +121,58 @@ object PolicyOps {
}
}

def delayedBreakPolicy(
end: Policy.End.WithPos
)(onBreakPolicy: Policy)(implicit fileLine: FileLine): Policy =
def delayedBreakPolicy(end: Policy.End.WithPos)(onBreakPolicy: Policy)(
implicit fileLine: FileLine
): Policy =
Policy.Proxy(onBreakPolicy, end)(delayedBreakPolicyFactory)

def delayedBreakPolicyBefore(
token: T
)(onBreakPolicy: Policy)(implicit fileLine: FileLine): Policy =
def delayedBreakPolicyBefore(token: T)(onBreakPolicy: Policy)(implicit
fileLine: FileLine
): Policy =
delayedBreakPolicy(Policy.End.Before(token))(onBreakPolicy)

def delayedBreakPolicyFor(
token: T
)(f: T => Policy)(implicit fileLine: FileLine): Policy =
def delayedBreakPolicyFor(token: T)(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 3ec044e

Please sign in to comment.