Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @objc public class func lonelyReturnInOneLine(_ formatter: Formatter) {
- formatter.forEach(.keyword) { i, token in
- guard ["return"].contains(token.string),
- let startOfScope = formatter.index(of: .startOfScope, before: i) else {
- return
- }
- var hasOnlySpacesBetweenStartOfScopeAndReturn = true
- for index in startOfScope + 1 ..< i {
- if !formatter.tokens[index].isSpaceOrLinebreak {
- hasOnlySpacesBetweenStartOfScopeAndReturn = false
- break
- }
- }
- if hasOnlySpacesBetweenStartOfScopeAndReturn {
- // This is for {return}
- formatter.removeTokens(inRange: startOfScope + 1 ..< i)
- // This is for {\nreturn\n}
- formatter.insertToken(.linebreak(formatter.options.linebreak), at: startOfScope + 1)
- }
- }
- formatter.forEach(.keyword) { i, token in
- guard ["return"].contains(token.string),
- let endOfScope = formatter.index(of: .endOfScope, after: i) else {
- return
- }
- var hasOnlySpacesBetweenReturnAndEndOfScope = true
- for indexEnd in i + 1 ..< endOfScope {
- if !formatter.tokens[indexEnd].isSpaceOrLinebreak {
- hasOnlySpacesBetweenReturnAndEndOfScope = false
- break
- }
- }
- if hasOnlySpacesBetweenReturnAndEndOfScope {
- // This is for {return}
- formatter.removeTokens(inRange: i + 1 ..< endOfScope)
- // This is for {\nreturn\n}
- guard let newEndOfScope = formatter.index(of: .endOfScope, after: i) else {
- return
- }
- formatter.insertToken(.linebreak(formatter.options.linebreak), at: newEndOfScope)
- }
- }
- }
Add Comment
Please, Sign In to add comment