Skip to content

Commit

Permalink
Extract helper isFunctionCall(at:)
Browse files Browse the repository at this point in the history
  • Loading branch information
gering authored and nicklockwood committed Jun 9, 2024
1 parent 1b3f7f2 commit d35cc0f
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions Sources/FormattingHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -396,23 +396,7 @@ extension Formatter {
keepParameterLabelsOnSameLine(startOfScope: i,
endOfScope: &endOfScope)

let isFunctionCall: Bool = {
if let openingParenIndex = self.index(of: .startOfScope("("), before: i + 1) {
if let prevTokenIndex = self.index(of: .nonSpaceOrCommentOrLinebreak, before: openingParenIndex),
tokens[prevTokenIndex].isIdentifier
{
if let keywordIndex = self.index(of: .nonSpaceOrCommentOrLinebreak, before: prevTokenIndex),
tokens[keywordIndex] == .keyword("func") || tokens[keywordIndex] == .keyword("init")
{
return false
}
return true
}
}
return false
}()

if isFunctionCall, options.forceClosingParenOnSameLineForFunctionCalls {
if options.forceClosingParenOnSameLineForFunctionCalls, isFunctionCall(at: i) {
removeLinebreakBeforeEndOfScope(at: &endOfScope)
} else if endOfScopeOnSameLine {
removeLinebreakBeforeEndOfScope(at: &endOfScope)
Expand Down Expand Up @@ -1380,6 +1364,23 @@ extension Formatter {
return allSatisfy
}

/// Whether the given index is in a function call (not declaration)
func isFunctionCall(at index: Int) -> Bool {
if let openingParenIndex = self.index(of: .startOfScope("("), before: index + 1) {
if let prevTokenIndex = self.index(of: .nonSpaceOrCommentOrLinebreak, before: openingParenIndex),
tokens[prevTokenIndex].isIdentifier
{
if let keywordIndex = self.index(of: .nonSpaceOrCommentOrLinebreak, before: prevTokenIndex),
tokens[keywordIndex] == .keyword("func") || tokens[keywordIndex] == .keyword("init")
{
return false
}
return true
}
}
return false
}

/// Whether the given index is directly within the body of the given scope, or part of a nested closure
func indexIsWithinNestedClosure(_ index: Int, startOfScopeIndex: Int) -> Bool {
let startOfScopeAtIndex: Int
Expand Down

0 comments on commit d35cc0f

Please sign in to comment.