Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ let package = Package(
),
],
dependencies: [
.package(url: "https://github.com/codefiesta/VimKit", from: .init(0, 4, 3))
.package(url: "https://github.com/codefiesta/VimKit", from: .init(0, 4, 7))
],
targets: [
.target(
name: "VimAssistant",
dependencies: ["VimKit"],
resources: [.process("Resources/")],
linkerSettings: [
.linkedFramework("AVFoundation"),
.linkedFramework("CoreML"),
Expand Down
22 changes: 22 additions & 0 deletions Sources/VimAssistant/Extensions/Array+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Array+Extensions.swift
// VimAssistant
//
// Created by Kevin McKee
//

import Foundation

extension Array where Element: Comparable {

/// Returns the largest indices of the array elements
/// - Parameter count: the total elements to sort
/// - Returns: the indices of the largest elements
func largestIndices(_ count: Int = 10) -> [Int] {
let count = Swift.min(count, self.count)
let sorted = enumerated().sorted{ $0.element > $1.element }
let elements = sorted[0..<count]
let indices = elements.map { (tuple) in tuple.offset }
return indices
}
}
22 changes: 22 additions & 0 deletions Sources/VimAssistant/Extensions/AttributedString+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// AttributedString+Extensions.swift
// VimAssistant
//
// Created by Kevin McKee
//

import Foundation

extension AttributedString {

/// Replaces a range of the string with the new elements.
/// - Parameters:
/// - bounds: the range bounds
/// - newElements: the new elements to replace with
public mutating func replaceSubrange(bounds: Range<Int>, with s: some AttributedStringProtocol) {
let start = index(startIndex, offsetByCharacters: bounds.lowerBound)
let end = index(startIndex, offsetByCharacters: bounds.upperBound)
let subrange: Range<AttributedString.Index> = start..<end
replaceSubrange(subrange, with: s)
}
}
22 changes: 22 additions & 0 deletions Sources/VimAssistant/Extensions/String+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// String+Extensions.swift
// VimAssistant
//
// Created by Kevin McKee
//

import Foundation

extension String {

/// Replaces a range of the string with the new elements.
/// - Parameters:
/// - bounds: the range bounds
/// - newElements: the new elements to replace with
public mutating func replaceSubrange<C>(bounds: Range<Int>, with newElements: C) where C: Collection, C.Element == Character {
let start = index(startIndex, offsetBy: bounds.lowerBound)
let end = index(startIndex, offsetBy: bounds.upperBound)
let subrange: Range<String.Index> = start..<end
replaceSubrange(subrange, with: newElements)
}
}
138 changes: 0 additions & 138 deletions Sources/VimAssistant/Model/TokenizedString.swift

This file was deleted.

70 changes: 70 additions & 0 deletions Sources/VimAssistant/Model/VimAssistant+Handler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// VimAssistant+Handler.swift
// VimAssistant
//
// Created by Kevin McKee
//

import Foundation
import SwiftData
import VimKit

public extension VimAssistant {

struct Handler {

/// Handles the received
/// - Parameters:
/// - vim: the vim object to use
/// - prediction: the prediction
func handle(vim: Vim, prediction: VimPrediction?) {
guard let prediction, let bestPrediction = prediction.bestPrediction, bestPrediction.confidence >= 0.85 else { return }
guard prediction.entities.isNotEmpty else { return }

let action = bestPrediction.action

print("❤️", bestPrediction)

for entity in prediction.entities {
if entity.label == "CON-BIM-CATG" {
print("🚀", entity.value)
perform(vim: vim, action: action, category: entity.value)
} else if entity.label == "CON-BIM-FAML" {

} else if entity.label == "CON-BIM-TYPE" {

}
}
}

private func perform(vim: Vim, action: VimPrediction.Action, category: String) {

guard let db = vim.db else { return }
let modelContext = ModelContext(db.modelContainer)

let orderedSame = ComparisonResult.orderedSame
let predicate = #Predicate<Database.Node>{
if let element = $0.element, let cat = element.category {
return cat.name.caseInsensitiveCompare(category) == orderedSame
} else {
return false
}
}

let descriptor = FetchDescriptor<Database.Node>(predicate: predicate, sortBy: [SortDescriptor(\.index)])
guard let results = try? modelContext.fetch(descriptor), results.isNotEmpty else { return }
let ids = results.compactMap{ Int($0.index) }
Task {
switch action {
case .hide:
await vim.hide(ids: ids)
case .isolate:
await vim.isolate(ids: ids)
case .quantify:
break
}
}
}

}
}
Loading