Skip to content

Commit

Permalink
Remove .nsRange extension
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Nov 23, 2024
1 parent 62a61a0 commit 83e50f0
Show file tree
Hide file tree
Showing 20 changed files with 40 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ private extension NSTextStorage {
/// Clears all background highlight (including text finder's highlights).
@MainActor func clearAllMarkup() {

let range = self.string.nsRange
let range = self.string.range

for manager in self.layoutManagers {
manager.removeTemporaryAttribute(.backgroundColor, forCharacterRange: range)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ final class PrintTextView: NSTextView {
paragraphStyle.lineHeightMultiple = self.lineHeight
self.defaultParagraphStyle = paragraphStyle
self.typingAttributes[.paragraphStyle] = paragraphStyle
self.textStorage?.addAttribute(.paragraphStyle, value: paragraphStyle, range: self.string.nsRange)
self.textStorage?.addAttribute(.paragraphStyle, value: paragraphStyle, range: self.string.range)

// set font also to layout manager
(self.layoutManager as? LayoutManager)?.textFont = font
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extension EditorTextView {
@IBAction func sortLinesAscending(_ sender: Any?) {

// process whole document if no text selected
let range = self.selectedRange.isEmpty ? self.string.nsRange : self.selectedRange
let range = self.selectedRange.isEmpty ? self.string.range : self.selectedRange

guard let context = self.string.sortLinesAscending(in: range) else { return }

Expand All @@ -74,7 +74,7 @@ extension EditorTextView {
@IBAction func reverseLines(_ sender: Any?) {

// process whole document if no text selected
let range = self.selectedRange.isEmpty ? self.string.nsRange : self.selectedRange
let range = self.selectedRange.isEmpty ? self.string.range : self.selectedRange

guard let context = self.string.reverseLines(in: range) else { return }

Expand All @@ -86,7 +86,7 @@ extension EditorTextView {
@IBAction func shuffleLines(_ sender: Any?) {

// process whole document if no text selected
let range = self.selectedRange.isEmpty ? self.string.nsRange : self.selectedRange
let range = self.selectedRange.isEmpty ? self.string.range : self.selectedRange

guard let context = self.string.shuffleLines(in: range) else { return }

Expand All @@ -100,7 +100,7 @@ extension EditorTextView {
guard let selectedRanges = self.rangesForUserTextChange?.map(\.rangeValue) else { return }

// process whole document if no text selected
let ranges = self.selectedRange.isEmpty ? [self.string.nsRange] : selectedRanges
let ranges = self.selectedRange.isEmpty ? [self.string.range] : selectedRanges

guard let context = self.string.deleteDuplicateLine(in: ranges) else { return }

Expand Down Expand Up @@ -187,7 +187,7 @@ extension EditorTextView {
private func sortLines(pattern: some SortPattern, options: SortOptions) {

// process whole document if no text selected
let range = self.selectedRange.isEmpty ? self.string.nsRange : self.selectedRange
let range = self.selectedRange.isEmpty ? self.string.range : self.selectedRange

let string = self.string as NSString
let lineRange = string.lineContentsRange(for: range)
Expand Down
14 changes: 7 additions & 7 deletions CotEditor/Sources/Document Window/Text View/EditorTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ final class EditorTextView: NSTextView, CurrentLineHighlighting, MultiCursorEdit
.sink { [unowned self] _ in self.setNeedsDisplay(self.frame, avoidAdditionalLayout: true) },
defaults.publisher(for: .highlightSelectionInstance)
.filter { !$0 }
.sink { [unowned self] _ in self.layoutManager?.removeTemporaryAttribute(.roundedBackgroundColor, forCharacterRange: self.string.nsRange) },
.sink { [unowned self] _ in self.layoutManager?.removeTemporaryAttribute(.roundedBackgroundColor, forCharacterRange: self.string.range) },
]
}

Expand Down Expand Up @@ -782,7 +782,7 @@ final class EditorTextView: NSTextView, CurrentLineHighlighting, MultiCursorEdit
if UserDefaults.standard[.highlightSelectionInstance] {
self.instanceHighlightTask?.cancel()
if let layoutManager = self.layoutManager, layoutManager.hasTemporaryAttribute(.roundedBackgroundColor) {
layoutManager.removeTemporaryAttribute(.roundedBackgroundColor, forCharacterRange: self.string.nsRange)
layoutManager.removeTemporaryAttribute(.roundedBackgroundColor, forCharacterRange: self.string.range)
}
}

Expand Down Expand Up @@ -893,10 +893,10 @@ final class EditorTextView: NSTextView, CurrentLineHighlighting, MultiCursorEdit

if font.isFixedPitch {
self.typingAttributes[.kern] = 0
self.textStorage?.addAttribute(.kern, value: 0, range: self.string.nsRange)
self.textStorage?.addAttribute(.kern, value: 0, range: self.string.range)
} else {
self.typingAttributes[.kern] = nil
self.textStorage?.removeAttribute(.kern, range: self.string.nsRange)
self.textStorage?.removeAttribute(.kern, range: self.string.range)
}

// let LayoutManager keep the set font to avoid an inconsistent line height
Expand Down Expand Up @@ -1442,7 +1442,7 @@ final class EditorTextView: NSTextView, CurrentLineHighlighting, MultiCursorEdit

self.defaultParagraphStyle = paragraphStyle
self.typingAttributes[.paragraphStyle] = paragraphStyle
self.textStorage?.addAttribute(.paragraphStyle, value: paragraphStyle, range: self.string.nsRange)
self.textStorage?.addAttribute(.paragraphStyle, value: paragraphStyle, range: self.string.range)

// tell line height also to scroll view so that scroll view can scroll line by line
if let lineHeight = (self.layoutManager as? LayoutManager)?.lineHeight {
Expand Down Expand Up @@ -1529,7 +1529,7 @@ final class EditorTextView: NSTextView, CurrentLineHighlighting, MultiCursorEdit
let substring = (string as NSString).substring(with: selectedRange)
let pattern = "\\b" + NSRegularExpression.escapedPattern(for: substring) + "\\b"
let regex = try! NSRegularExpression(pattern: pattern)
let ranges = try regex.cancellableMatches(in: string, range: string.nsRange)
let ranges = try regex.cancellableMatches(in: string, range: string.range)
.map(\.range)
.filter { $0 != selectedRange }

Expand Down Expand Up @@ -1643,7 +1643,7 @@ extension EditorTextView {
let pattern = "(?:^|\\b|(?<=\\W))" + NSRegularExpression.escapedPattern(for: partialWord) + "\\w+?(?:$|\\b)"
let regex = try! NSRegularExpression(pattern: pattern)

return regex.matches(in: self.string, range: self.string.nsRange).map { (self.string as NSString).substring(with: $0.range) }
return regex.matches(in: self.string, range: self.string.range).map { (self.string as NSString).substring(with: $0.range) }
}()
candidateWords.append(contentsOf: documentWords)
}
Expand Down
2 changes: 1 addition & 1 deletion CotEditor/Sources/Models/Snippet/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ final class Tokenizer: Sendable {
/// - keywordRange: Range of keyword.
func tokenize(_ string: String, range: NSRange? = nil, block: (_ token: String, _ range: NSRange, _ keywordRange: NSRange) -> Void) {

self.regex.enumerateMatches(in: string, range: range ?? string.nsRange) { (match, _, _) in
self.regex.enumerateMatches(in: string, range: range ?? string.range) { (match, _, _) in

guard let match else { return }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,9 @@ extension Document {
return 0
}

count = regex.replaceMatches(in: mutableString, range: string.nsRange, withTemplate: replacementString)
count = regex.replaceMatches(in: mutableString, range: string.range, withTemplate: replacementString)
} else {
count = mutableString.replaceOccurrences(of: searchString, with: replacementString, options: options, range: string.nsRange)
count = mutableString.replaceOccurrences(of: searchString, with: replacementString, options: options, range: string.range)
}

guard count > 0 else { return 0 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extension NSTextView {
case .afterSelection:
NSRange(location: self.selectedRange.upperBound, length: 0)
case .replaceAll:
self.string.nsRange
self.string.range
case .afterAll:
NSRange(location: (self.string as NSString).length, length: 0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ extension NSTextView {
// apply to the text view
if let layoutManager = self.layoutManager {
let color = NSColor.textHighlighterColor
layoutManager.groupTemporaryAttributesUpdate(in: string.nsRange) {
layoutManager.removeTemporaryAttribute(.backgroundColor, forCharacterRange: string.nsRange)
layoutManager.groupTemporaryAttributesUpdate(in: string.range) {
layoutManager.removeTemporaryAttribute(.backgroundColor, forCharacterRange: string.range)
for range in ranges {
layoutManager.addTemporaryAttribute(.backgroundColor, value: color, forCharacterRange: range)
}
Expand Down Expand Up @@ -120,7 +120,7 @@ extension NSTextView {

if progress.count > 0 {
// apply to the text view
self.replace(with: [result.string], ranges: [string.nsRange], selectedRanges: result.selectedRanges, actionName: String(localized: "Replace All", table: "TextFind"))
self.replace(with: [result.string], ranges: [string.range], selectedRanges: result.selectedRanges, actionName: String(localized: "Replace All", table: "TextFind"))
} else {
NSSound.beep()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ final class FindPanelTextView: RegexTextView {
override func becomeFirstResponder() -> Bool {

// select whole string on focus (standard NSTextField behavior)
self.selectedRange = self.string.nsRange
self.selectedRange = self.string.range

return super.becomeFirstResponder()
}
Expand Down Expand Up @@ -139,7 +139,7 @@ final class FindPanelTextView: RegexTextView {
/// Clears the current text.
@IBAction func clear(_ sender: Any?) {

guard self.shouldChangeText(in: self.string.nsRange, replacementString: "") else { return }
guard self.shouldChangeText(in: self.string.range, replacementString: "") else { return }

self.window?.makeFirstResponder(self)
self.string = ""
Expand Down
8 changes: 4 additions & 4 deletions CotEditor/Sources/Text Finder/TextFinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ struct TextFindAllResult {

// mark all matches
if isIncremental, let layoutManager = client.layoutManager {
layoutManager.groupTemporaryAttributesUpdate(in: client.string.nsRange) {
layoutManager.removeTemporaryAttribute(.backgroundColor, forCharacterRange: client.string.nsRange)
layoutManager.groupTemporaryAttributesUpdate(in: client.string.range) {
layoutManager.removeTemporaryAttribute(.backgroundColor, forCharacterRange: client.string.range)
for range in matches {
layoutManager.addTemporaryAttribute(.backgroundColor, value: NSColor.unemphasizedSelectedTextBackgroundColor, forCharacterRange: range)
}
Expand Down Expand Up @@ -594,7 +594,7 @@ struct TextFindAllResult {

// highlight in client
if let layoutManager = client.layoutManager {
let wholeRange = textFind.string.nsRange
let wholeRange = textFind.string.range
layoutManager.groupTemporaryAttributesUpdate(in: wholeRange) {
layoutManager.removeTemporaryAttribute(.backgroundColor, forCharacterRange: wholeRange)
for highlight in highlights {
Expand Down Expand Up @@ -693,6 +693,6 @@ extension NSTextView {

@IBAction final func unhighlight(_ sender: Any?) {

self.layoutManager?.removeTemporaryAttribute(.backgroundColor, forCharacterRange: self.string.nsRange)
self.layoutManager?.removeTemporaryAttribute(.backgroundColor, forCharacterRange: self.string.range)
}
}
4 changes: 2 additions & 2 deletions CotEditor/Sources/Text Finder/Views/FindPanelFieldView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ final class FindPanelFieldViewController: NSViewController, NSTextViewDelegate {
guard
let string = sender?.representedObject as? String,
let textView = self.findTextView,
textView.shouldChangeText(in: textView.string.nsRange, replacementString: string)
textView.shouldChangeText(in: textView.string.range, replacementString: string)
else { return }

textView.string = string
Expand All @@ -209,7 +209,7 @@ final class FindPanelFieldViewController: NSViewController, NSTextViewDelegate {
guard
let string = sender?.representedObject as? String,
let textView = self.replacementTextView,
textView.shouldChangeText(in: textView.string.nsRange, replacementString: string)
textView.shouldChangeText(in: textView.string.range, replacementString: string)
else { return }

textView.string = string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public struct RegularExpressionSortPattern: SortPattern, Equatable, Sendable {

guard
let regex = try? self.regex,
let match = regex.firstMatch(in: line, range: line.nsRange)
let match = regex.firstMatch(in: line, range: line.range)
else { return nil }

if self.usesCaptureGroup {
Expand Down
7 changes: 0 additions & 7 deletions Packages/EditorCore/Sources/StringUtils/NSString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ public extension String {

public extension StringProtocol {

/// Whole range in NSRange.
var nsRange: NSRange {

NSRange(location: 0, length: self.length)
}


/// The `NSRange` like length.
var length: Int {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public extension String {
/// - Returns: The number of lines.
func numberOfLines(in range: NSRange? = nil, includesLastBreak: Bool = false) -> Int {

let range = range ?? self.nsRange
let range = range ?? self.range

if self.isEmpty || range.isEmpty { return 0 }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,6 @@ extension String {
let pattern = ignoringEmptyLines ? "(?<!^|[ \\t])[ \\t]++$" : "[ \\t]++$"
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)

return regex.matches(in: self, range: self.nsRange).map(\.range)
return regex.matches(in: self, range: self.range).map(\.range)
}
}
2 changes: 1 addition & 1 deletion Packages/EditorCore/Sources/TextFind/MultipleReplace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ extension MultipleReplace {

for replacement in self.replacements where replacement.isEnabled {
let mode = replacement.mode(settings: self.settings)
let findRanges = result.selectedRanges ?? [result.string.nsRange]
let findRanges = result.selectedRanges ?? [result.string.range]

// -> Invalid replacement rules will just be ignored.
guard let textFind = try? TextFind(for: result.string, findString: replacement.findString, mode: mode, inSelection: inSelection, selectedRanges: findRanges) else { continue }
Expand Down
2 changes: 1 addition & 1 deletion Packages/EditorCore/Sources/TextFind/TextFind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public struct TextFind: Equatable, Sendable {
self.inSelection = inSelection
self.string = string
self.selectedRanges = selectedRanges
self.scopeRanges = inSelection ? selectedRanges : [string.nsRange]
self.scopeRanges = inSelection ? selectedRanges : [string.range]
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ struct StringCommentingTests {
//
// foo bar
"""
let editor = Editor(string: string, selectedRanges: [string.nsRange])
let editor = Editor(string: string, selectedRanges: [string.range])

#expect(editor.canUncomment(partly: false))
#expect(editor.canUncomment(partly: true))
Expand All @@ -151,7 +151,7 @@ struct StringCommentingTests {
// foo bar
"""
let editor = Editor(string: string, selectedRanges: [string.nsRange])
let editor = Editor(string: string, selectedRanges: [string.range])

#expect(!editor.canUncomment(partly: false))
#expect(editor.canUncomment(partly: true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ struct StringLineProcessingTests {

#expect(string.sortLinesAscending(in: NSRange(4, 1)) == nil)

context = try #require(string.sortLinesAscending(in: string.nsRange))
context = try #require(string.sortLinesAscending(in: string.range))
#expect(context.strings == ["aa\nbbbb\nccc"])
#expect(context.ranges == [NSRange(0, 11)])
#expect(context.selectedRanges == [NSRange(0, 11)])
Expand All @@ -124,7 +124,7 @@ struct StringLineProcessingTests {

#expect(string.reverseLines(in: NSRange(4, 1)) == nil)

context = try #require(string.reverseLines(in: string.nsRange))
context = try #require(string.reverseLines(in: string.range))
#expect(context.strings == ["ccc\nbbbb\naa"])
#expect(context.ranges == [NSRange(0, 11)])
#expect(context.selectedRanges == [NSRange(0, 11)])
Expand All @@ -149,7 +149,7 @@ struct StringLineProcessingTests {

#expect(string.deleteDuplicateLine(in: [NSRange(4, 1)]) == nil)

context = try #require(string.deleteDuplicateLine(in: [string.nsRange]))
context = try #require(string.deleteDuplicateLine(in: [string.range]))
#expect(context.strings == ["", ""])
#expect(context.ranges == [NSRange(12, 4), NSRange(16, 4)])
#expect(context.selectedRanges == nil)
Expand Down
2 changes: 1 addition & 1 deletion Tests/Sources/Models/SyntaxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ actor SyntaxTests {
let source = try String(contentsOf: sourceURL, encoding: .utf8)

let outlineItems = try syntax.outlineExtractors
.flatMap { try $0.items(in: source, range: source.nsRange) }
.flatMap { try $0.items(in: source, range: source.range) }
.sorted(using: SortDescriptor(\.range.location))


Expand Down

0 comments on commit 83e50f0

Please sign in to comment.