Skip to content

Commit 5556b58

Browse files
committed
Clean up code
1 parent f4a342f commit 5556b58

1 file changed

Lines changed: 34 additions & 44 deletions

File tree

Sources/CodeEditSourceEditor/Controller/TextViewController+IndentLines.swift

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,22 @@ import AppKit
1010

1111
extension TextViewController {
1212
public func handleIndent(inwards: Bool = false) {
13+
// TODO: Get indentation chars and count form settings
14+
let spaceCount = 2
15+
let indentationChars = String(repeating: " ", count: spaceCount)
16+
17+
guard !cursorPositions.isEmpty else { return }
18+
19+
textView.undoManager?.beginUndoGrouping()
1320
for cursorPosition in self.cursorPositions {
1421
// get lineindex, i.e line-numbers+1
15-
guard let lineIndexes = getHeighlightedLines(for: cursorPosition.range) else { return }
22+
guard let lineIndexes = getHeighlightedLines(for: cursorPosition.range) else { continue }
1623

17-
// TODO: Get indentation chars and count form settings
18-
let spaceCount = 2
19-
let indentationChars = String(repeating: " ", count: spaceCount)
20-
21-
textView.undoManager?.beginUndoGrouping()
22-
for lineIndex in lineIndexes {
23-
if inwards {
24-
indentInward(lineIndex: lineIndex, spacesCount: indentationChars.count)
25-
} else {
26-
indent(lineIndex: lineIndex, indentationCharacters: indentationChars)
27-
}
24+
for lineIndex in lineIndexes {
25+
adjustIndentation(lineIndex: lineIndex, indentationChars: indentationChars, inwards: inwards)
2826
}
29-
textView.undoManager?.endUndoGrouping()
3027
}
28+
textView.undoManager?.endUndoGrouping()
3129
}
3230

3331
private func getHeighlightedLines(for range: NSRange) -> [Int]? {
@@ -47,29 +45,35 @@ extension TextViewController {
4745

4846
return Array(startLineInfo.index...endLineInfo.index)
4947
}
48+
private func adjustIndentation(lineIndex: Int, indentationChars: String, inwards: Bool) {
49+
guard let lineInfo = textView.layoutManager.textLineForIndex(lineIndex) else { return }
5050

51-
private func indent(lineIndex: Int, indentationCharacters: String) {
52-
guard let lineInfo = textView.layoutManager.textLineForIndex(lineIndex) else {
53-
return
51+
if inwards {
52+
removeLeadingSpaces(lineInfo: lineInfo, spaceCount: indentationChars.count)
53+
} else {
54+
addIndentation(lineInfo: lineInfo, indentationChars: indentationChars)
5455
}
56+
}
5557

58+
private func addIndentation(
59+
lineInfo: TextLineStorage<TextLine>.TextLinePosition,
60+
indentationChars: String
61+
) {
5662
textView.replaceCharacters(
57-
in: NSRange(location: lineInfo.range.lowerBound, length: 0),
58-
with: indentationCharacters
59-
)
63+
in: NSRange(location: lineInfo.range.lowerBound, length: 0),
64+
with: indentationChars
65+
)
6066
}
6167

62-
private func indentInward(lineIndex: Int, spacesCount: Int) {
63-
guard let lineInfo = textView.layoutManager.textLineForIndex(lineIndex) else {
64-
return
65-
}
66-
68+
private func removeLeadingSpaces(
69+
lineInfo: TextLineStorage<TextLine>.TextLinePosition,
70+
spaceCount: Int
71+
) {
6772
guard let lineContent = textView.textStorage.substring(from: lineInfo.range) else { return }
6873

69-
// Count spaces until the required amount.
70-
// E.g. if 4 are needed but only 3 are present, remove only those 3.
71-
let removeSpacesCount = countLeadingSpacesUpTo(line: lineContent, maxCount: spacesCount)
72-
guard removeSpacesCount != 0 else { return }
74+
let removeSpacesCount = countLeadingSpacesUpTo(line: lineContent, maxCount: spaceCount)
75+
76+
guard removeSpacesCount > 0 else { return }
7377

7478
textView.replaceCharacters(
7579
in: NSRange(location: lineInfo.range.lowerBound, length: removeSpacesCount),
@@ -78,21 +82,7 @@ extension TextViewController {
7882
}
7983

8084
func countLeadingSpacesUpTo(line: String, maxCount: Int) -> Int {
81-
var count = 0
82-
83-
for char in line {
84-
if char == " " {
85-
count += 1
86-
} else {
87-
break // Stop as soon as a non-space character is encountered
88-
}
89-
90-
// Stop early if we've counted the max number of spaces
91-
if count == maxCount {
92-
break
93-
}
94-
}
95-
96-
return count
85+
// Count leading spaces using prefix and `filter`
86+
return line.prefix(maxCount).filter { $0 == " " }.count
9787
}
9888
}

0 commit comments

Comments
 (0)