Skip to content

Commit

Permalink
Refactor Snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Aug 1, 2024
1 parent eb2472b commit 55ade0e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
5 changes: 2 additions & 3 deletions CotEditor/Sources/NSTextView+Snippet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ extension NSTextView {

guard let ranges = self.rangesForUserTextChange?.map(\.rangeValue) else { return }

let (strings, selectedRanges) = snippet.insertions(for: self.string, ranges: ranges)
let context = snippet.insertions(for: self.string, ranges: ranges)

self.replace(with: strings, ranges: ranges, selectedRanges: selectedRanges,
actionName: String(localized: "Insert Snippet", table: "MainMenu"))
self.edit(with: context, actionName: String(localized: "Insert Snippet", table: "MainMenu"))
self.centerSelectionInVisibleArea(self)
}
}
7 changes: 4 additions & 3 deletions CotEditor/Sources/Snippet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import Foundation.NSString
import Shortcut
import TextEditing

struct Snippet: Equatable, Identifiable {

Expand Down Expand Up @@ -103,8 +104,7 @@ extension Snippet {
/// - Parameters:
/// - string: The whole content string where to insert the snippet.
/// - ranges: The current selected ranges.
/// - Returns: Strings to insert and the content-based selected ranges.
func insertions(for string: String, ranges: [NSRange]) -> (strings: [String], selectedRanges: [NSRange]?) {
func insertions(for string: String, ranges: [NSRange]) -> EditingContext {

var offset = 0
let insertions = ranges.map { range in
Expand All @@ -120,7 +120,8 @@ extension Snippet {
}
let selectedRanges = insertions.flatMap(\.ranges)

return (insertions.map(\.string), selectedRanges.isEmpty ? nil : selectedRanges)
return EditingContext(strings: insertions.map(\.string), ranges: ranges,
selectedRanges: selectedRanges.isEmpty ? nil : selectedRanges)
}


Expand Down
16 changes: 8 additions & 8 deletions Tests/SnippetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,21 @@ struct SnippetTests {
bbcc
"""
let snippet = Snippet(name: "", format: "<li><<<SELECTION>>><<<CURSOR>>></li>")
let (strings, selections) = snippet.insertions(for: string, ranges: [
let context = snippet.insertions(for: string, ranges: [
NSRange(location: 4, length: 3),
NSRange(location: 8, length: 0),
NSRange(location: 9, length: 2),
])

let expectedStrings = [
#expect(context.strings == [
"<li>aaa</li>",
"<li></li>",
"<li>bb</li>",
]
let expectedSelections = [NSRange(location: 11, length: 0),
NSRange(location: 21, length: 0),
NSRange(location: 33, length: 0)]
#expect(strings == expectedStrings)
#expect(selections == expectedSelections)
])
#expect(context.selectedRanges == [
NSRange(location: 11, length: 0),
NSRange(location: 21, length: 0),
NSRange(location: 33, length: 0),
])
}
}

0 comments on commit 55ade0e

Please sign in to comment.