Skip to content

Commit

Permalink
Move LineRangeCacheable to LineEnding library
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Jul 19, 2024
1 parent a30af0b commit 4dbe23e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
10 changes: 0 additions & 10 deletions CotEditor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@
2A10D10A1E708CDF0027192A /* KeyBindingTreeView.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2A10D1081E708CDF0027192A /* KeyBindingTreeView.storyboard */; };
2A10D1281E714D230027192A /* ThemeListView.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2A10D1261E714D230027192A /* ThemeListView.storyboard */; };
2A10D1381E715E5B0027192A /* SyntaxListView.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 2A10D1361E715E5B0027192A /* SyntaxListView.storyboard */; };
2A1125C123F180FF006A1DB2 /* LineRangeCacheableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A1125C023F180FF006A1DB2 /* LineRangeCacheableTests.swift */; };
2A1125C323F1A86B006A1DB2 /* LineRangeCacheable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A1125C223F1A86B006A1DB2 /* LineRangeCacheable.swift */; };
2A1125C423F1A86B006A1DB2 /* LineRangeCacheable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A1125C223F1A86B006A1DB2 /* LineRangeCacheable.swift */; };
2A1125C623F6EFB2006A1DB2 /* URLDetector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A1125C523F6EFB2006A1DB2 /* URLDetector.swift */; };
2A1125C723F6EFB2006A1DB2 /* URLDetector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A1125C523F6EFB2006A1DB2 /* URLDetector.swift */; };
2A11F2131E669BFA005E1675 /* PointerBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A11F2121E669BFA005E1675 /* PointerBridge.swift */; };
Expand Down Expand Up @@ -785,8 +782,6 @@
2A10D1091E708CDF0027192A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/KeyBindingTreeView.storyboard; sourceTree = "<group>"; };
2A10D1271E714D230027192A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/ThemeListView.storyboard; sourceTree = "<group>"; };
2A10D1371E715E5B0027192A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/SyntaxListView.storyboard; sourceTree = "<group>"; };
2A1125C023F180FF006A1DB2 /* LineRangeCacheableTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LineRangeCacheableTests.swift; sourceTree = "<group>"; };
2A1125C223F1A86B006A1DB2 /* LineRangeCacheable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LineRangeCacheable.swift; sourceTree = "<group>"; };
2A1125C523F6EFB2006A1DB2 /* URLDetector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLDetector.swift; sourceTree = "<group>"; };
2A11F2121E669BFA005E1675 /* PointerBridge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PointerBridge.swift; sourceTree = "<group>"; };
2A1311D52127DCE1001D52C5 /* NSTextView+CurrentLineHighlighting.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSTextView+CurrentLineHighlighting.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1811,7 +1806,6 @@
children = (
2AD7B9AE1D3E832E00E5D6D7 /* EditorCounter.swift */,
2A80BE8C27FFA61700D2F7FF /* LineEndingScanner.swift */,
2A1125C223F1A86B006A1DB2 /* LineRangeCacheable.swift */,
2A1125C523F6EFB2006A1DB2 /* URLDetector.swift */,
);
name = Scanners;
Expand Down Expand Up @@ -2019,7 +2013,6 @@
2AC72EA1253478D5001D3CA0 /* FileDropItemTests.swift */,
2ABEFB6923DC0CA0008769F4 /* EditorCounterTests.swift */,
2A80BE8F27FFFA8900D2F7FF /* LineEndingScannerTests.swift */,
2A1125C023F180FF006A1DB2 /* LineRangeCacheableTests.swift */,
2A3F8F672429E04000CBBA89 /* DebouncerTests.swift */,
2A04E9BA27FD6911008C82D8 /* SnippetTests.swift */,
2A57B991294EDD9600771696 /* FormatStylesTests.swift */,
Expand Down Expand Up @@ -2682,7 +2675,6 @@
2AA375481D40BDCB0080C27C /* LineEnding+Localization.swift in Sources */,
2A80BE8D27FFA61700D2F7FF /* LineEndingScanner.swift in Sources */,
2A6416A41D2F9F7200FA9E1A /* LineNumberView.swift in Sources */,
2A1125C423F1A86B006A1DB2 /* LineRangeCacheable.swift in Sources */,
2A59B7032957089A0094F03B /* LinkButton.swift in Sources */,
2AE144C42B0222DB005E8CF1 /* LiveTextInsertionView.swift in Sources */,
2A8961931DB76A3400E9E0EC /* MainMenu.swift in Sources */,
Expand Down Expand Up @@ -2843,7 +2835,6 @@
2AF0C1281D3DA6F800B6FCB6 /* FourCharCodeTests.swift in Sources */,
2A9082EF1D325ED900228F50 /* GeometryTests.swift in Sources */,
2A80BE9227FFFA8900D2F7FF /* LineEndingScannerTests.swift in Sources */,
2A1125C123F180FF006A1DB2 /* LineRangeCacheableTests.swift in Sources */,
2A89160C2394B87100AC13EE /* NSLayoutManagerTests.swift in Sources */,
2A1380072C225FAF00093BF3 /* NSTextStorageTests.swift in Sources */,
2A04E9BB27FD6911008C82D8 /* SnippetTests.swift in Sources */,
Expand Down Expand Up @@ -2976,7 +2967,6 @@
2AA375471D40BDCB0080C27C /* LineEnding+Localization.swift in Sources */,
2A80BE8E27FFA61700D2F7FF /* LineEndingScanner.swift in Sources */,
2A6416A31D2F9F7200FA9E1A /* LineNumberView.swift in Sources */,
2A1125C323F1A86B006A1DB2 /* LineRangeCacheable.swift in Sources */,
2A59B7042957089A0094F03B /* LinkButton.swift in Sources */,
2AE144C52B0222DB005E8CF1 /* LiveTextInsertionView.swift in Sources */,
2A8961921DB76A3400E9E0EC /* MainMenu.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public extension Array {
/// - items: The items to replace with.
/// - editedRange: The edited range.
/// - delta: The change in length.
mutating func replace<Value>(items: [Element], in editedRange: NSRange, changeInLength delta: Int) where Element == ValueRange<Value> {
mutating func replace<Value>(items: [Element], in editedRange: NSRange, changeInLength delta: Int = 0) where Element == ValueRange<Value> {

guard let lowerEditedIndex = self.binarySearchedFirstIndex(where: { $0.lowerBound >= editedRange.lowerBound }) else {
self += items
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// LineRangeCacheable.swift
// LineEnding
//
// CotEditor
// https://coteditor.com
Expand All @@ -24,21 +25,22 @@
//

import Foundation
import StringBasics

final class LineCounter: LineRangeCacheable {
public final class LineCounter: LineRangeCacheable {

let string: NSString
var lineRangeCache = LineRangeCache()
public let string: NSString
public var lineRangeCache = LineRangeCache()


init(_ string: NSString) {
public init(_ string: NSString) {

self.string = string
}
}


protocol LineRangeCacheable: AnyObject {
public protocol LineRangeCacheable: AnyObject {

var string: NSString { get }
var lineRangeCache: LineRangeCache { get set }
Expand All @@ -54,11 +56,11 @@ protocol LineRangeCacheable: AnyObject {
}


struct LineRangeCache {
public struct LineRangeCache {

fileprivate var lineStartIndexes = IndexSet()
fileprivate var parsedIndexes = IndexSet()
fileprivate var firstUncountedIndex = 0
var lineStartIndexes = IndexSet()
var parsedIndexes = IndexSet()
var firstUncountedIndex = 0
}


Expand All @@ -71,7 +73,7 @@ extension LineRangeCacheable {
///
/// - Parameter index: The character index.
/// - Returns: The 1-based line number.
func lineNumber(at index: Int) -> Int {
public func lineNumber(at index: Int) -> Int {

assert(index <= self.string.length)

Expand Down Expand Up @@ -125,7 +127,7 @@ extension LineRangeCacheable {
///
/// - Parameter range: The range of character for finding the line range.
/// - Returns: The character range of the contents line.
func lineContentsRange(for range: NSRange) -> NSRange {
public func lineContentsRange(for range: NSRange) -> NSRange {

let lineRange = self.lineRange(for: range)

Expand Down Expand Up @@ -163,7 +165,7 @@ extension LineRangeCacheable {
/// - Parameters:
/// - newRange: The range in the final string that was edited.
/// - delta: The length delta for the editing changes.
func invalidateLineRanges(in newRange: NSRange, changeInLength delta: Int) {
public func invalidateLineRanges(in newRange: NSRange, changeInLength delta: Int) {

self.lineRangeCache.invalidate(in: newRange, changeInLength: delta)
}
Expand Down Expand Up @@ -226,9 +228,9 @@ private extension LineRangeCache {
/// Updates the first uncounted index.
private mutating func invalidateFirstUncountedIndex() {

let firstInvalidIndex = self.parsedIndexes.contains(0)
let firstInvalidCharacterIndex = self.parsedIndexes.contains(0)
? self.parsedIndexes.rangeView.first?.upperBound ?? 0
: 0
self.firstUncountedIndex = self.lineStartIndexes.integerLessThanOrEqualTo(firstInvalidIndex) ?? 0
self.firstUncountedIndex = self.lineStartIndexes.integerLessThanOrEqualTo(firstInvalidCharacterIndex) ?? 0
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// LineRangeCacheableTests.swift
// LineEndingTests
//
// CotEditor
// https://coteditor.com
Expand All @@ -25,7 +26,7 @@

import Foundation
import Testing
@testable import CotEditor
@testable import LineEnding

struct LineRangeCacheableTests {

Expand Down

0 comments on commit 4dbe23e

Please sign in to comment.