|
1 | | -import Foundation |
2 | | -import TextStory |
3 | | - |
4 | | -extension TextStoring { |
5 | | - func findFirstLinePreceeding(location: Int, satisifying predicate: TextualIndenter.ReferenceLinePredicate) -> NSRange? { |
6 | | - var startLoc = findStartOfLine(containing: location) |
7 | | - |
8 | | - if startLoc == 0 { |
9 | | - return nil |
10 | | - } |
11 | | - |
12 | | - startLoc -= 1 |
13 | | - |
14 | | - while startLoc > 0 { |
15 | | - let preceedingStart = findStartOfLine(containing: startLoc) |
16 | | - let length = startLoc - preceedingStart |
17 | | - |
18 | | - assert(length >= 0) |
19 | | - let range = NSRange(location: preceedingStart, length: length) |
20 | | - |
21 | | - if predicate(self, range) { |
22 | | - return range |
23 | | - } |
24 | | - |
25 | | - startLoc = preceedingStart - 1 |
26 | | - } |
27 | | - |
28 | | - return .zero |
29 | | - } |
30 | | - |
31 | | - func leadingIndentingWhitespace(at location: Int) -> String? { |
32 | | - let set = CharacterSet.whitespacesWithoutNewlines.inverted |
33 | | - |
34 | | - let start = findStartOfLine(containing: location) |
35 | | - let end = findNextOccurrenceOfCharacter(in: set, from: start) ?? length |
36 | | - |
37 | | - let indentRange = NSRange(start..<end) |
38 | | - |
39 | | - return substring(from: indentRange) |
40 | | - } |
41 | | -} |
42 | | - |
43 | | -extension TextStoring { |
44 | | - public func whitespaceStringResult(with indentation: Indentation, using indentUnit: String, width: Int) -> Result<String, IndentationError> { |
45 | | - assert(width > 0) |
46 | | - |
47 | | - let range = indentation.range |
48 | | - guard let referenceWhitespace = leadingIndentingWhitespace(at: range.location) else { |
49 | | - return .failure(.unableToComputeReferenceRange) |
50 | | - } |
51 | | - |
52 | | - // here, we have to determine how many units of indentation currently exist |
53 | | - let spaceOnlyReference = referenceWhitespace.replacingOccurrences(of: "\t", with: String(repeating: " ", count: width)) |
54 | | - let spaceCount = spaceOnlyReference.utf8.count |
55 | | - let referenceCount = spaceCount / width |
56 | | - let remainder = spaceCount % width |
57 | | - |
58 | | - switch indentation { |
59 | | - case .relativeIncrease: |
60 | | - let value = String(repeating: indentUnit, count: referenceCount + 1) + String(repeating: " ", count: remainder) |
61 | | - |
62 | | - return .success(value) |
63 | | - case .relativeDecrease: |
64 | | - guard let indentUnitStringRange = referenceWhitespace.range(of: indentUnit) else { |
65 | | - return .failure(.unableToComputeReferenceRange) |
66 | | - } |
67 | | - |
68 | | - var updatedWhitespace = referenceWhitespace |
69 | | - |
70 | | - updatedWhitespace.removeSubrange(indentUnitStringRange) |
71 | | - |
72 | | - return .success(updatedWhitespace) |
73 | | - case .equal: |
74 | | - let value = String(repeating: indentUnit, count: referenceCount) + String(repeating: " ", count: remainder) |
75 | | - |
76 | | - return .success(value) |
77 | | - } |
78 | | - } |
79 | | -} |
| 1 | +//import Foundation |
| 2 | +//import TextStory |
| 3 | +// |
| 4 | +//extension TextStoring { |
| 5 | +// func findFirstLinePreceeding(location: Int, satisifying predicate: TextualIndenter.ReferenceLinePredicate) -> NSRange? { |
| 6 | +// var startLoc = findStartOfLine(containing: location) |
| 7 | +// |
| 8 | +// if startLoc == 0 { |
| 9 | +// return nil |
| 10 | +// } |
| 11 | +// |
| 12 | +// startLoc -= 1 |
| 13 | +// |
| 14 | +// while startLoc > 0 { |
| 15 | +// let preceedingStart = findStartOfLine(containing: startLoc) |
| 16 | +// let length = startLoc - preceedingStart |
| 17 | +// |
| 18 | +// assert(length >= 0) |
| 19 | +// let range = NSRange(location: preceedingStart, length: length) |
| 20 | +// |
| 21 | +// if predicate(self, range) { |
| 22 | +// return range |
| 23 | +// } |
| 24 | +// |
| 25 | +// startLoc = preceedingStart - 1 |
| 26 | +// } |
| 27 | +// |
| 28 | +// return .zero |
| 29 | +// } |
| 30 | +// |
| 31 | +// func leadingIndentingWhitespace(at location: Int) -> String? { |
| 32 | +// let set = CharacterSet.whitespacesWithoutNewlines.inverted |
| 33 | +// |
| 34 | +// let start = findStartOfLine(containing: location) |
| 35 | +// let end = findNextOccurrenceOfCharacter(in: set, from: start) ?? length |
| 36 | +// |
| 37 | +// let indentRange = NSRange(start..<end) |
| 38 | +// |
| 39 | +// return substring(from: indentRange) |
| 40 | +// } |
| 41 | +//} |
| 42 | +// |
| 43 | +//extension TextStoring { |
| 44 | +// public func whitespaceStringResult(with indentation: Indentation, using indentUnit: String, width: Int) -> Result<String, IndentationError> { |
| 45 | +// assert(width > 0) |
| 46 | +// |
| 47 | +// let range = indentation.range |
| 48 | +// guard let referenceWhitespace = leadingIndentingWhitespace(at: range.location) else { |
| 49 | +// return .failure(.unableToComputeReferenceRange) |
| 50 | +// } |
| 51 | +// |
| 52 | +// // here, we have to determine how many units of indentation currently exist |
| 53 | +// let spaceOnlyReference = referenceWhitespace.replacingOccurrences(of: "\t", with: String(repeating: " ", count: width)) |
| 54 | +// let spaceCount = spaceOnlyReference.utf8.count |
| 55 | +// let referenceCount = spaceCount / width |
| 56 | +// let remainder = spaceCount % width |
| 57 | +// |
| 58 | +// switch indentation { |
| 59 | +// case .relativeIncrease: |
| 60 | +// let value = String(repeating: indentUnit, count: referenceCount + 1) + String(repeating: " ", count: remainder) |
| 61 | +// |
| 62 | +// return .success(value) |
| 63 | +// case .relativeDecrease: |
| 64 | +// guard let indentUnitStringRange = referenceWhitespace.range(of: indentUnit) else { |
| 65 | +// return .failure(.unableToComputeReferenceRange) |
| 66 | +// } |
| 67 | +// |
| 68 | +// var updatedWhitespace = referenceWhitespace |
| 69 | +// |
| 70 | +// updatedWhitespace.removeSubrange(indentUnitStringRange) |
| 71 | +// |
| 72 | +// return .success(updatedWhitespace) |
| 73 | +// case .equal: |
| 74 | +// let value = String(repeating: indentUnit, count: referenceCount) + String(repeating: " ", count: remainder) |
| 75 | +// |
| 76 | +// return .success(value) |
| 77 | +// } |
| 78 | +// } |
| 79 | +//} |
0 commit comments