-
-
Notifications
You must be signed in to change notification settings - Fork 443
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
363 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...tor/Sources/Collection+BinarySearch.swift → .../LineEnding/Collection+BinarySearch.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
Packages/EditorCore/Sources/LineEnding/Collection+ValueRange.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// Collection+ValueRange.swift | ||
// LineEnding | ||
// | ||
// CotEditor | ||
// https://coteditor.com | ||
// | ||
// Created by 1024jp on 2024-07-13. | ||
// | ||
// --------------------------------------------------------------------------- | ||
// | ||
// © 2022-2024 1024jp | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import ValueRange | ||
|
||
public extension Array { | ||
|
||
/// Replace the elements in the specified range with the given items. | ||
/// | ||
/// This API assumes the elements are sorted by range location. | ||
/// | ||
/// - Parameters: | ||
/// - 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> { | ||
|
||
guard let lowerEditedIndex = self.binarySearchedFirstIndex(where: { $0.lowerBound >= editedRange.lowerBound }) else { | ||
self += items | ||
return | ||
} | ||
|
||
if let upperEditedIndex = self[lowerEditedIndex...].firstIndex(where: { $0.lowerBound >= (editedRange.upperBound - delta) }) { | ||
for index in upperEditedIndex..<self.endIndex { | ||
self[index].shift(by: delta) | ||
} | ||
self.removeSubrange(lowerEditedIndex..<upperEditedIndex) | ||
} else { | ||
self.removeSubrange(lowerEditedIndex...) | ||
} | ||
|
||
self.insert(contentsOf: items, at: lowerEditedIndex) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
Packages/EditorCore/Sources/LineEnding/LineRangeCalculating.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// LineRangeCalculating.swift | ||
// LineEnding | ||
// | ||
// CotEditor | ||
// https://coteditor.com | ||
// | ||
// Created by 1024jp on 2024-07-13. | ||
// | ||
// --------------------------------------------------------------------------- | ||
// | ||
// © 2024 1024jp | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import ValueRange | ||
|
||
public protocol LineRangeCalculating { | ||
|
||
/// The text contents. | ||
var string: NSString { get } | ||
|
||
/// Line Endings sorted by location. | ||
var lineEndings: [ValueRange<LineEnding>] { get } | ||
} | ||
|
||
|
||
public extension LineRangeCalculating { | ||
|
||
/// Returns the 1-based line number at the given character index. | ||
/// | ||
/// - Parameter characterIndex: The character index. | ||
/// - Returns: The 1-based line number. | ||
func lineNumber(at characterIndex: Int) -> Int { | ||
|
||
if let index = self.lineEndings.binarySearchedFirstIndex(where: { $0.upperBound > characterIndex }) { | ||
index + 1 | ||
} else if let last = self.lineEndings.last, last.upperBound <= characterIndex { | ||
self.lineEndings.endIndex + 1 | ||
} else { | ||
1 | ||
} | ||
} | ||
} |
Oops, something went wrong.