1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-18 11:11:34 +03:00

GH-292 Make open quickly a bit better..

This commit is contained in:
Tae Won Ha 2016-12-17 09:35:07 +01:00
parent aaf03362fa
commit 7b1ed4d421
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
5 changed files with 45 additions and 73 deletions

View File

@ -1439,13 +1439,14 @@ extension NeoVimView {
} }
public func ipcBecameInvalid(_ reason: String) { public func ipcBecameInvalid(_ reason: String) {
NSLog("ERROR \(#function): force-quitting")
DispatchUtils.gui { DispatchUtils.gui {
if self.agent.neoVimIsQuitting { if self.agent.neoVimIsQuitting {
return return
} }
self.delegate?.ipcBecameInvalid(reason: reason) self.delegate?.ipcBecameInvalid(reason: reason)
NSLog("ERROR \(#function): force-quitting")
self.agent.quit() self.agent.quit()
} }
} }

View File

@ -20,6 +20,7 @@ class Matcher {
static func exactMatchIgnoringCase(_ target: String, pattern: String) -> ExactMatchResult { static func exactMatchIgnoringCase(_ target: String, pattern: String) -> ExactMatchResult {
let ltarget = target.lowercased() let ltarget = target.lowercased()
let lpattern = pattern.lowercased() let lpattern = pattern.lowercased()
if ltarget == lpattern { if ltarget == lpattern {
return .exact return .exact
} }
@ -40,7 +41,7 @@ class Matcher {
} }
static func numberOfUppercaseMatches(_ target: String, pattern: String) -> Int { static func numberOfUppercaseMatches(_ target: String, pattern: String) -> Int {
var tscalars = target.unicodeScalars.filter { uppercaseCharSet.contains(UnicodeScalar($0.value)!) } var tscalars = target.unicodeScalars.filter { self.uppercaseCharSet.contains($0) }
let count = tscalars.count let count = tscalars.count
guard count > 0 else { guard count > 0 else {
@ -49,8 +50,8 @@ class Matcher {
let pscalars = pattern.uppercased().unicodeScalars let pscalars = pattern.uppercased().unicodeScalars
pscalars.forEach { scalar in pscalars.forEach {
if let idx = tscalars.index(of: scalar) { if let idx = tscalars.index(of: $0) {
tscalars.remove(at: idx) tscalars.remove(at: idx)
} }
} }
@ -59,56 +60,28 @@ class Matcher {
} }
/// Matches `pattern` to `target` in a fuzzy way. /// Matches `pattern` to `target` in a fuzzy way.
/// - returns: `Array` of `Range<String.UnicodeScalarIndex>` /// - returns: number of matched characters where first character match gets a bonus of 5
static func fuzzyIgnoringCase(_ target: String, static func fuzzyIgnoringCase(_ target: String, pattern: String) -> Int {
pattern: String) -> (matches: Int, ranges: [CountableClosedRange<Int>]) {
let tlower = target.lowercased() let tlower = target.lowercased()
let plower = pattern.lowercased() let plower = pattern.lowercased()
let tchars = tlower.unicodeScalars let tchars = tlower.unicodeScalars
let pchars = plower.unicodeScalars let pchars = plower.unicodeScalars
var flags = Array(repeating: false, count: tchars.count) var result = 0
var pidx = pchars.startIndex var pidx = pchars.startIndex
for (i, tchar) in tchars.enumerated() { for tchar in tchars {
if pchars[pidx] == tchar { if pchars[pidx] == tchar {
flags[i] = true result += 1
pidx = pchars.index(after: pidx) pidx = pchars.index(after: pidx)
} }
} }
var ranges: [CountableClosedRange<Int>] = [] if tchars.first == pchars.first {
var matches = 0 result += 5
var lastTrue = -1
var curTrue = -1
for (i, isTrue) in flags.enumerated() {
if isTrue {
matches = matches &+ 1
if lastTrue == -1 {
lastTrue = i
}
curTrue = i
if i == flags.count &- 1 {
if lastTrue > -1 && curTrue > -1 {
ranges.append(lastTrue...curTrue)
lastTrue = -1
curTrue = -1
}
}
} else {
if lastTrue > -1 && curTrue > -1 {
ranges.append(lastTrue...curTrue)
lastTrue = -1
curTrue = -1
}
}
} }
return (matches, ranges) return result
} }
/// Wagner-Fischer algorithm. /// Wagner-Fischer algorithm.
@ -125,7 +98,7 @@ class Matcher {
var prevRow = Array(repeating: 0, count: m &+ 1) var prevRow = Array(repeating: 0, count: m &+ 1)
var curRow = Array(repeating: 0, count: m &+ 1) var curRow = Array(repeating: 0, count: m &+ 1)
for i in 0...m { for i in 0 ... m {
prevRow[i] = i prevRow[i] = i
} }

View File

@ -8,26 +8,24 @@ import Foundation
class Scorer { class Scorer {
static func score(_ target: String, pattern: String) -> Int { static func score(_ target: String, pattern: String) -> Int {
let wf = Matcher.wagnerFisherDistance(target, pattern: pattern)
let fuzzy = Matcher.fuzzyIgnoringCase(target, pattern: pattern) let fuzzy = Matcher.fuzzyIgnoringCase(target, pattern: pattern)
let upper = Matcher.numberOfUppercaseMatches(target, pattern: pattern) let upper = Matcher.numberOfUppercaseMatches(target, pattern: pattern)
let exactMatch = Matcher.exactMatchIgnoringCase(target, pattern: pattern) let exactMatch = Matcher.exactMatchIgnoringCase(target, pattern: pattern)
let wf = Matcher.wagnerFisherDistance(target, pattern: pattern)
let exactScore: Int let exactScore: Int
switch exactMatch { switch exactMatch {
case .none: case .none:
exactScore = 0 exactScore = 0
case .exact: case .exact:
return 100 return 1000
case .prefix, .contains, .suffix: case .prefix, .contains, .suffix:
exactScore = 5 exactScore = 5
} }
let wfScore = 0 - (wf / 10)
return exactScore return exactScore
+ wfScore + 10 * fuzzy
+ fuzzy.matches + 5 * upper
+ 2 * upper - wf
} }
} }

View File

@ -31,8 +31,8 @@ class MatcherTest: XCTestCase {
} }
func testFuzzyMatcher() { func testFuzzyMatcher() {
expect(Matcher.fuzzyIgnoringCase(self.target, pattern: "ucotft").matches).to(equal(6)) expect(Matcher.fuzzyIgnoringCase(self.target, pattern: "ucotft")).to(equal(6 + 5))
expect(Matcher.fuzzyIgnoringCase(self.target, pattern: "uco-tft").matches).to(equal(3)) expect(Matcher.fuzzyIgnoringCase(self.target, pattern: "uco-tft")).to(equal(3 + 5))
} }
func testWagerFischerAlgo() { func testWagerFischerAlgo() {

View File

@ -24,7 +24,7 @@ class ScorerTest: XCTestCase {
let targets = [ let targets = [
"NeoVimView.swift", "NeoVimView.swift",
"NeoVimViewDelegate.swift", "NeoVimViewDelegate.swift",
"NeoVimServer", "NeoVimAgent",
] ]
expect(Scorer.score(targets[0], pattern: pattern)).to(beGreaterThan(Scorer.score(targets[1], pattern: pattern))) expect(Scorer.score(targets[0], pattern: pattern)).to(beGreaterThan(Scorer.score(targets[1], pattern: pattern)))