Guest User

Untitled

a guest
May 24th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. @objc public class func lonelyReturnInOneLine(_ formatter: Formatter) {
  2. formatter.forEach(.keyword) { i, token in
  3. guard ["return"].contains(token.string),
  4. let startOfScope = formatter.index(of: .startOfScope, before: i) else {
  5. return
  6. }
  7.  
  8. var hasOnlySpacesBetweenStartOfScopeAndReturn = true
  9. for index in startOfScope + 1 ..< i {
  10. if !formatter.tokens[index].isSpaceOrLinebreak {
  11. hasOnlySpacesBetweenStartOfScopeAndReturn = false
  12. break
  13. }
  14. }
  15. if hasOnlySpacesBetweenStartOfScopeAndReturn {
  16. // This is for {return}
  17. formatter.removeTokens(inRange: startOfScope + 1 ..< i)
  18.  
  19. // This is for {\nreturn\n}
  20. formatter.insertToken(.linebreak(formatter.options.linebreak), at: startOfScope + 1)
  21. }
  22. }
  23. formatter.forEach(.keyword) { i, token in
  24. guard ["return"].contains(token.string),
  25. let endOfScope = formatter.index(of: .endOfScope, after: i) else {
  26. return
  27. }
  28.  
  29. var hasOnlySpacesBetweenReturnAndEndOfScope = true
  30. for indexEnd in i + 1 ..< endOfScope {
  31. if !formatter.tokens[indexEnd].isSpaceOrLinebreak {
  32. hasOnlySpacesBetweenReturnAndEndOfScope = false
  33. break
  34. }
  35. }
  36.  
  37. if hasOnlySpacesBetweenReturnAndEndOfScope {
  38. // This is for {return}
  39. formatter.removeTokens(inRange: i + 1 ..< endOfScope)
  40.  
  41. // This is for {\nreturn\n}
  42. guard let newEndOfScope = formatter.index(of: .endOfScope, after: i) else {
  43. return
  44. }
  45. formatter.insertToken(.linebreak(formatter.options.linebreak), at: newEndOfScope)
  46. }
  47. }
  48. }
Add Comment
Please, Sign In to add comment