Skip to content

Commit

Permalink
Support inference of options.forceClosingParenOnSameLineForFunctionCalls
Browse files Browse the repository at this point in the history
  • Loading branch information
gering authored and nicklockwood committed Jun 9, 2024
1 parent d35cc0f commit 164fb8a
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions Sources/Inference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -379,21 +379,49 @@ private struct Inference {
}

let closingParenOnSameLine = OptionInferrer { formatter, options in
var balanced = 0, sameLine = 0
var functionCallSameLine = 0, functionCallBalanced = 0
var functionDeclarationSameLine = 0, functionDeclarationBalanced = 0

formatter.forEach(.startOfScope("(")) { i, _ in
guard let closingBraceIndex = formatter.endOfScope(at: i),
let linebreakIndex = formatter.index(of: .linebreak, after: i),
formatter.index(of: .nonSpaceOrComment, after: i) == linebreakIndex
else {
return
}
if formatter.last(.nonSpaceOrComment, before: closingBraceIndex)?.isLinebreak == true {
balanced += 1

let isClosingParenOnSameLine = (formatter.last(.nonSpaceOrComment, before: closingBraceIndex)?.isLinebreak != true)

if formatter.isFunctionCall(at: i) {
if isClosingParenOnSameLine {
functionCallSameLine += 1
} else {
functionCallBalanced += 1
}
} else {
sameLine += 1
if isClosingParenOnSameLine {
functionDeclarationSameLine += 1
} else {
functionDeclarationBalanced += 1
}
}
}
options.closingParenOnSameLine = (sameLine > balanced)

// Decide on forceClosingParenOnSameLineForFunctionCalls
if functionCallSameLine > functionCallBalanced && functionDeclarationBalanced > functionDeclarationSameLine {
options.forceClosingParenOnSameLineForFunctionCalls = true
} else {
options.forceClosingParenOnSameLineForFunctionCalls = false
}

// If forceClosingParenOnSameLineForFunctionCalls is true, trust only the declarations to infer closingParenOnSameLine
if options.forceClosingParenOnSameLineForFunctionCalls {
options.closingParenOnSameLine = functionDeclarationSameLine > functionDeclarationBalanced
} else {
let balanced = functionDeclarationBalanced + functionCallBalanced
let sameLine = functionDeclarationSameLine + functionCallSameLine
options.closingParenOnSameLine = sameLine > balanced
}
}

let uppercaseHex = OptionInferrer { formatter, options in
Expand Down

0 comments on commit 164fb8a

Please sign in to comment.