mirror of
https://github.com/coteditor/CotEditor.git
synced 2024-09-11 11:25:57 +03:00
Improve error messages in Scripting
This commit is contained in:
parent
bbf3cbd2dd
commit
f2a2085f73
@ -9,6 +9,7 @@ Change Log
|
||||
|
||||
- Highlight named capture where highlight regular expression patterns such as in the find panel.
|
||||
- Add “.erb” extension to Ruby syntax style.
|
||||
- Improve error messages for OSA Scripting.
|
||||
- [trivial] Tweak the help layout.
|
||||
- [dev][non-AppStore ver.] Migrate Sparkle from submodule to SwiftPM.
|
||||
- [dev][non-AppStore ver.] Use sparkle:channel element in appcast.xml for prerelease check instead of appcast-beta.xml.
|
||||
|
@ -9,7 +9,7 @@
|
||||
// ---------------------------------------------------------------------------
|
||||
//
|
||||
// © 2004-2007 nakamuxu
|
||||
// © 2014-2020 1024jp
|
||||
// © 2014-2021 1024jp
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@ -221,7 +221,11 @@ extension Document {
|
||||
let arguments = command.evaluatedArguments,
|
||||
let encodingName = arguments["newEncoding"] as? String,
|
||||
let encoding = EncodingManager.shared.encoding(name: encodingName)
|
||||
else { return false }
|
||||
else {
|
||||
command.scriptErrorNumber = OSAParameterMismatch
|
||||
command.scriptErrorString = "Invalid encoding name."
|
||||
return false
|
||||
}
|
||||
|
||||
if encoding == self.fileEncoding.encoding {
|
||||
return true
|
||||
@ -233,6 +237,8 @@ extension Document {
|
||||
do {
|
||||
try self.changeEncoding(to: fileEncoding, lossy: lossy)
|
||||
} catch {
|
||||
command.scriptErrorNumber = errOSAGeneralError
|
||||
command.scriptErrorString = error.localizedDescription
|
||||
return false
|
||||
}
|
||||
|
||||
@ -246,11 +252,18 @@ extension Document {
|
||||
guard
|
||||
let arguments = command.evaluatedArguments,
|
||||
let encodingName = arguments["newEncoding"] as? String,
|
||||
let encoding = EncodingManager.shared.encoding(name: encodingName) else { return false }
|
||||
let encoding = EncodingManager.shared.encoding(name: encodingName)
|
||||
else {
|
||||
command.scriptErrorNumber = OSAParameterMismatch
|
||||
command.scriptErrorString = "Invalid encoding name."
|
||||
return false
|
||||
}
|
||||
|
||||
do {
|
||||
try self.reinterpret(encoding: encoding)
|
||||
} catch {
|
||||
command.scriptErrorNumber = errOSAGeneralError
|
||||
command.scriptErrorString = error.localizedDescription
|
||||
return false
|
||||
}
|
||||
|
||||
@ -304,7 +317,11 @@ extension Document {
|
||||
let count: Int
|
||||
if options.contains(.regularExpression) {
|
||||
let regexOptions: NSRegularExpression.Options = options.contains(.caseInsensitive) ? [.caseInsensitive] : []
|
||||
guard let regex = try? NSRegularExpression(pattern: searchString, options: regexOptions.union(.anchorsMatchLines)) else { return 0 }
|
||||
guard let regex = try? NSRegularExpression(pattern: searchString, options: regexOptions.union(.anchorsMatchLines)) else {
|
||||
command.scriptErrorNumber = errOSAGeneralError
|
||||
command.scriptErrorString = "Invalid regular expression."
|
||||
return 0
|
||||
}
|
||||
|
||||
count = regex.replaceMatches(in: mutableString, range: string.nsRange, withTemplate: replacementString)
|
||||
} else {
|
||||
@ -326,10 +343,13 @@ extension Document {
|
||||
let replacedString: String
|
||||
if options.contains(.regularExpression) {
|
||||
let regexOptions: NSRegularExpression.Options = options.contains(.caseInsensitive) ? .caseInsensitive : []
|
||||
guard
|
||||
let regex = try? NSRegularExpression(pattern: searchString, options: regexOptions.union(.anchorsMatchLines)),
|
||||
let match = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: foundRange)
|
||||
else { return 0 }
|
||||
guard let regex = try? NSRegularExpression(pattern: searchString, options: regexOptions.union(.anchorsMatchLines)) else {
|
||||
command.scriptErrorNumber = errOSAGeneralError
|
||||
command.scriptErrorString = "Invalid regular expression."
|
||||
return 0
|
||||
}
|
||||
|
||||
guard let match = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: foundRange) else { return 0 }
|
||||
|
||||
replacedString = regex.replacementString(for: match, in: string, offset: 0, template: replacementString)
|
||||
} else {
|
||||
@ -357,11 +377,19 @@ extension Document {
|
||||
guard
|
||||
let arguments = command.evaluatedArguments,
|
||||
let rangeArray = arguments["range"] as? [Int], rangeArray.count == 2
|
||||
else { return nil }
|
||||
else {
|
||||
command.scriptErrorNumber = OSAParameterMismatch
|
||||
command.scriptErrorString = "The range paramator must be a list of {location, length}."
|
||||
return nil
|
||||
}
|
||||
|
||||
let fuzzyRange = FuzzyRange(location: rangeArray[0], length: max(rangeArray[1], 1))
|
||||
|
||||
guard let range = string.range(in: fuzzyRange) else { return nil }
|
||||
guard let range = string.range(in: fuzzyRange) else {
|
||||
command.scriptErrorNumber = OSAParameterMismatch
|
||||
command.scriptErrorString = "Out of the range."
|
||||
return nil
|
||||
}
|
||||
|
||||
return (self.string as NSString).substring(with: range)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user