Fix line ending of string via script

This commit is contained in:
1024jp 2016-08-20 21:50:29 +09:00
parent cd50d6550f
commit b621442068
4 changed files with 18 additions and 13 deletions

View File

@ -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
--------------------------

View File

@ -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)
}

View File

@ -225,7 +225,7 @@ final class ScriptManager: NSObject {
switch type {
case .selection:
return editor.substringWithSelection ?? ""
return editor.selectedString
case .allText:
return editor.string

View File

@ -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)
}