1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-18 03:01:38 +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) {
NSLog("ERROR \(#function): force-quitting")
DispatchUtils.gui {
if self.agent.neoVimIsQuitting {
return
}
self.delegate?.ipcBecameInvalid(reason: reason)
NSLog("ERROR \(#function): force-quitting")
self.agent.quit()
}
}

View File

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

View File

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

View File

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

View File

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