diff --git a/CHANGELOG.md b/CHANGELOG.md index 29d2217c3..35a97449d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,11 @@ develop - Enable activate “Show Invisibles” action even if all of invisible characters were set as not shown when the document was opened. +### Fixes + +- Fix an issue where some of script APIs returned always string with LF line endings. + + 2.5.7-alpha -------------------------- diff --git a/CotEditor/Sources/Editable.swift b/CotEditor/Sources/Editable.swift index d08daed3c..69cd5348c 100644 --- a/CotEditor/Sources/Editable.swift +++ b/CotEditor/Sources/Editable.swift @@ -38,19 +38,24 @@ protocol Editable: class { extension Editable { - /// return string in text view (LF fix) + /// line ending applied document string var string: String { + + guard let string = self.textView?.string else { return "" } - return self.textView?.string ?? "" + return string.replacingLineEndings(with: self.lineEnding) } - /// return current selection - var substringWithSelection: String? { + /// line ending applied current selection + var selectedString: String { - guard let selectedRange = self.textView?.selectedRange else { return nil } + guard let textView = self.textView, + let string = textView.string else { return "" } - return (self.string as NSString).substring(with: selectedRange) + let substring = (string as NSString).substring(with: textView.selectedRange) + + return substring.replacingLineEndings(with: self.lineEnding) } diff --git a/CotEditor/Sources/ScriptManager.swift b/CotEditor/Sources/ScriptManager.swift index c7496e1f3..20fa2219c 100644 --- a/CotEditor/Sources/ScriptManager.swift +++ b/CotEditor/Sources/ScriptManager.swift @@ -225,7 +225,7 @@ final class ScriptManager: NSObject { switch type { case .selection: - return editor.substringWithSelection ?? "" + return editor.selectedString case .allText: return editor.string diff --git a/CotEditor/Sources/TextSelection.swift b/CotEditor/Sources/TextSelection.swift index 103dd5f59..dc57f803d 100644 --- a/CotEditor/Sources/TextSelection.swift +++ b/CotEditor/Sources/TextSelection.swift @@ -102,12 +102,7 @@ final class TextSelection: NSObject { /// string of the selection (Unicode text) var contents: Any? { get { - guard - let document = self.document, - var string = document.editor?.substringWithSelection else { return nil } - - // apply line endings - string = string.replacingLineEndings(with: document.lineEnding) + guard let string = self.document?.editor?.selectedString else { return nil } return NSTextStorage(string: string) }