1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-24 03:25:03 +03:00

Use the custom API for getting buffer info

This commit is contained in:
Tae Won Ha 2017-12-08 18:37:17 +01:00
parent c2415f8ee3
commit 52c0b7ec51
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
9 changed files with 123 additions and 30 deletions

View File

@ -56,7 +56,7 @@ public class NvimApi {
return "\(Swift.type(of: self))(type: \(self.type), message: \"\(self.message)\")" return "\(Swift.type(of: self))(type: \(self.type), message: \"\(self.message)\")"
} }
init(_ message: String) { public init(_ message: String) {
self.type = .unknown self.type = .unknown
self.message = message self.message = message
} }

View File

@ -2222,7 +2222,7 @@ extension NvimApi.Tabpage {
} }
} }
private func msgPackDictToSwift(_ dict: Dictionary<NvimApi.Value, NvimApi.Value>?) -> Dictionary<String, NvimApi.Value>? { fileprivate func msgPackDictToSwift(_ dict: Dictionary<NvimApi.Value, NvimApi.Value>?) -> Dictionary<String, NvimApi.Value>? {
return dict?.flatMapToDict { k, v in return dict?.flatMapToDict { k, v in
guard let strKey = k.stringValue else { guard let strKey = k.stringValue else {
return nil return nil
@ -2232,29 +2232,29 @@ private func msgPackDictToSwift(_ dict: Dictionary<NvimApi.Value, NvimApi.Value>
} }
} }
private func msgPackArrayDictToSwift(_ array: [NvimApi.Value]?) -> [Dictionary<String, NvimApi.Value>]? { fileprivate func msgPackArrayDictToSwift(_ array: [NvimApi.Value]?) -> [Dictionary<String, NvimApi.Value>]? {
return array? return array?
.flatMap { v in v.dictionaryValue } .flatMap { v in v.dictionaryValue }
.flatMap { d in msgPackDictToSwift(d) } .flatMap { d in msgPackDictToSwift(d) }
} }
extension Dictionary { extension Dictionary {
func mapToDict<K, V>(_ transform: ((key: Key, value: Value)) throws -> (K, V)) rethrows -> Dictionary<K, V> { fileprivate func mapToDict<K, V>(_ transform: ((key: Key, value: Value)) throws -> (K, V)) rethrows -> Dictionary<K, V> {
let array = try self.map(transform) let array = try self.map(transform)
return tuplesToDict(array) return tuplesToDict(array)
} }
func flatMapToDict<K, V>(_ transform: ((key: Key, value: Value)) throws -> (K, V)?) rethrows -> Dictionary<K, V> { fileprivate func flatMapToDict<K, V>(_ transform: ((key: Key, value: Value)) throws -> (K, V)?) rethrows -> Dictionary<K, V> {
let array = try self.flatMap(transform) let array = try self.flatMap(transform)
return tuplesToDict(array) return tuplesToDict(array)
} }
private func tuplesToDict<K:Hashable, V, S:Sequence>(_ sequence: S) fileprivate func tuplesToDict<K:Hashable, V, S:Sequence>(_ sequence: S)
-> Dictionary<K, V> where S.Iterator.Element == (K, V) { -> Dictionary<K, V> where S.Iterator.Element == (K, V) {
var result = Dictionary<K, V>(minimumCapacity: sequence.underestimatedCount) var result = Dictionary<K, V>(minimumCapacity: sequence.underestimatedCount)
for (key, value) in sequence { for (key, value) in sequence {
result[key] = value result[key] = value
} }

View File

@ -8,6 +8,7 @@
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
1929B40A751BDA2882D4FC94 /* NvimViewObjects.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B22A0CAD417EC3790F02 /* NvimViewObjects.swift */; }; 1929B40A751BDA2882D4FC94 /* NvimViewObjects.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929B22A0CAD417EC3790F02 /* NvimViewObjects.swift */; };
1929B86897DAEFDBABAB1C14 /* NvimApiExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1929BBD7F88AE4F01E626691 /* NvimApiExtension.swift */; };
4B2016EE1FD45EED0038528A /* NvimAutoCommandEvent.generated.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B2016EC1FD45EED0038528A /* NvimAutoCommandEvent.generated.m */; }; 4B2016EE1FD45EED0038528A /* NvimAutoCommandEvent.generated.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B2016EC1FD45EED0038528A /* NvimAutoCommandEvent.generated.m */; };
4B2016EF1FD45EED0038528A /* NvimAutoCommandEvent.generated.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B2016ED1FD45EED0038528A /* NvimAutoCommandEvent.generated.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B2016EF1FD45EED0038528A /* NvimAutoCommandEvent.generated.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B2016ED1FD45EED0038528A /* NvimAutoCommandEvent.generated.h */; settings = {ATTRIBUTES = (Public, ); }; };
4B90F02D1FD2AFAE008A39E0 /* NvimUiBridgeProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B90F00F1FD2AFAC008A39E0 /* NvimUiBridgeProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; 4B90F02D1FD2AFAE008A39E0 /* NvimUiBridgeProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B90F00F1FD2AFAC008A39E0 /* NvimUiBridgeProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; };
@ -84,6 +85,7 @@
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
1929B22A0CAD417EC3790F02 /* NvimViewObjects.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NvimViewObjects.swift; sourceTree = "<group>"; }; 1929B22A0CAD417EC3790F02 /* NvimViewObjects.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NvimViewObjects.swift; sourceTree = "<group>"; };
1929BBD7F88AE4F01E626691 /* NvimApiExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NvimApiExtension.swift; sourceTree = "<group>"; };
4B2016EC1FD45EED0038528A /* NvimAutoCommandEvent.generated.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NvimAutoCommandEvent.generated.m; sourceTree = "<group>"; }; 4B2016EC1FD45EED0038528A /* NvimAutoCommandEvent.generated.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NvimAutoCommandEvent.generated.m; sourceTree = "<group>"; };
4B2016ED1FD45EED0038528A /* NvimAutoCommandEvent.generated.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NvimAutoCommandEvent.generated.h; sourceTree = "<group>"; }; 4B2016ED1FD45EED0038528A /* NvimAutoCommandEvent.generated.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NvimAutoCommandEvent.generated.h; sourceTree = "<group>"; };
4B90F0041FD2AF59008A39E0 /* NvimView.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = NvimView.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 4B90F0041FD2AF59008A39E0 /* NvimView.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = NvimView.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@ -215,6 +217,7 @@
4B90F0221FD2AFAD008A39E0 /* TextDrawer.m */, 4B90F0221FD2AFAD008A39E0 /* TextDrawer.m */,
4B90F0771FD2BA7B008A39E0 /* Logger.h */, 4B90F0771FD2BA7B008A39E0 /* Logger.h */,
1929B22A0CAD417EC3790F02 /* NvimViewObjects.swift */, 1929B22A0CAD417EC3790F02 /* NvimViewObjects.swift */,
1929BBD7F88AE4F01E626691 /* NvimApiExtension.swift */,
); );
path = NvimView; path = NvimView;
sourceTree = "<group>"; sourceTree = "<group>";
@ -410,6 +413,7 @@
4B90F03A1FD2AFAE008A39E0 /* NvimView+Key.swift in Sources */, 4B90F03A1FD2AFAE008A39E0 /* NvimView+Key.swift in Sources */,
4B90F0361FD2AFAE008A39E0 /* NvimViewDelegate.swift in Sources */, 4B90F0361FD2AFAE008A39E0 /* NvimViewDelegate.swift in Sources */,
1929B40A751BDA2882D4FC94 /* NvimViewObjects.swift in Sources */, 1929B40A751BDA2882D4FC94 /* NvimViewObjects.swift in Sources */,
1929B86897DAEFDBABAB1C14 /* NvimApiExtension.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@ -0,0 +1,71 @@
//
// Created by Tae Won Ha on 08.12.17.
// Copyright (c) 2017 Tae Won Ha. All rights reserved.
//
import Foundation
import NvimMsgPack
extension NvimApi {
public func getBufGetInfo(
buffer: NvimApi.Buffer,
checkBlocked: Bool = true
) -> NvimApi.Response<Dictionary<String, NvimApi.Value>> {
if checkBlocked && self.getMode().value?["blocking"]?.boolValue == true {
return .failure(NvimApi.Error(type: .blocked, message: "Nvim is currently blocked"))
}
let params: [NvimApi.Value] = [
.int(Int64(buffer.handle)),
]
let response = self.rpc(method: "nvim_buf_get_info", params: params, expectsReturnValue: true)
guard let value = response.value else {
return .failure(response.error!)
}
guard let result = (msgPackDictToSwift(value.dictionaryValue)) else {
return .failure(NvimApi.Error("Error converting result to \(Dictionary<String, NvimApi.Value>.self)"))
}
return .success(result)
}
}
func msgPackDictToSwift(_ dict: Dictionary<NvimApi.Value, NvimApi.Value>?) -> Dictionary<String, NvimApi.Value>? {
return dict?.flatMapToDict { k, v in
guard let strKey = k.stringValue else {
return nil
}
return (strKey, v)
}
}
fileprivate func msgPackArrayDictToSwift(_ array: [NvimApi.Value]?) -> [Dictionary<String, NvimApi.Value>]? {
return array?
.flatMap { v in v.dictionaryValue }
.flatMap { d in msgPackDictToSwift(d) }
}
extension Dictionary {
fileprivate func flatMapToDict<K, V>(_ transform: ((key: Key, value: Value)) throws -> (K, V)?) rethrows -> Dictionary<K, V> {
let array = try self.flatMap(transform)
return tuplesToDict(array)
}
fileprivate func tuplesToDict<K:Hashable, V, S:Sequence>(_ sequence: S)
-> Dictionary<K, V> where S.Iterator.Element == (K, V) {
var result = Dictionary<K, V>(minimumCapacity: sequence.underestimatedCount)
for (key, value) in sequence {
result[key] = value
}
return result
}
}

View File

@ -189,18 +189,27 @@ extension NvimView {
} }
private func neoVimBuffer(for buf: NvimApi.Buffer, currentBuffer: NvimApi.Buffer?) -> NvimView.Buffer? { private func neoVimBuffer(for buf: NvimApi.Buffer, currentBuffer: NvimApi.Buffer?) -> NvimView.Buffer? {
guard let path = self.nvim.bufGetName(buffer: buf).value else { guard let info = self.nvim.getBufGetInfo(buffer: buf).value else {
return nil
}
guard let dirty = self.nvim.bufGetOption(buffer: buf, name: "mod").value?.boolValue else {
return nil return nil
} }
let current = buf == currentBuffer let current = buf == currentBuffer
guard let path = info["filename"]?.stringValue,
let dirty = info["modified"]?.boolValue,
let buftype = info["buftype"]?.stringValue,
let listed = info["buflisted"]?.boolValue
else {
return nil
}
guard listed else {
return nil
}
let url = path == "" || buftype != "" ? nil : URL(fileURLWithPath: path)
return NvimView.Buffer(apiBuffer: buf, return NvimView.Buffer(apiBuffer: buf,
url: URL(fileURLWithPath: path), url: url,
type: buftype,
isDirty: dirty, isDirty: dirty,
isCurrent: current) isCurrent: current)
} }

View File

@ -11,11 +11,17 @@ extension NvimView {
public struct Buffer: Equatable { public struct Buffer: Equatable {
public static func ==(lhs: Buffer, rhs: Buffer) -> Bool { public static func ==(lhs: Buffer, rhs: Buffer) -> Bool {
return lhs.handle == rhs.handle guard lhs.handle == rhs.handle else {
return false
}
// Transient buffer active -> open a file -> the resulting buffer has the same handle, but different URL
return lhs.url == rhs.url
} }
public let apiBuffer: NvimApi.Buffer public let apiBuffer: NvimApi.Buffer
public let url: URL? public let url: URL?
public let type: String
public let isDirty: Bool public let isDirty: Bool
public let isCurrent: Bool public let isCurrent: Bool
@ -33,6 +39,10 @@ extension NvimView {
} }
public var name: String? { public var name: String? {
if self.type == "quickfix" {
return "Quickfix"
}
return self.url?.lastPathComponent return self.url?.lastPathComponent
} }

@ -1 +1 @@
Subproject commit 9589942270b0e58bca6288c87b304ac7105f10bf Subproject commit 741a6f026fbc3a3d1235e358331bfaf0d1cc6799

View File

@ -54,13 +54,12 @@ class BuffersList: NSView,
self.usesTheme = state.appearance.usesTheme self.usesTheme = state.appearance.usesTheme
let buffers = state.buffers.removingDuplicatesPreservingFromBeginning() if self.buffers == state.buffers && !themeChanged && self.showsFileIcon == state.appearance.showsFileIcon {
if self.buffers == buffers && !themeChanged && self.showsFileIcon == state.appearance.showsFileIcon {
return return
} }
self.showsFileIcon = state.appearance.showsFileIcon self.showsFileIcon = state.appearance.showsFileIcon
self.buffers = buffers self.buffers = state.buffers
self.bufferList.reloadData() self.bufferList.reloadData()
self.adjustFileViewWidth() self.adjustFileViewWidth()
}) })

View File

@ -157,7 +157,7 @@ extension NvimApi.Tabpage {
} }
} }
private func msgPackDictToSwift(_ dict: Dictionary<NvimApi.Value, NvimApi.Value>?) -> Dictionary<String, NvimApi.Value>? { fileprivate func msgPackDictToSwift(_ dict: Dictionary<NvimApi.Value, NvimApi.Value>?) -> Dictionary<String, NvimApi.Value>? {
return dict?.flatMapToDict { k, v in return dict?.flatMapToDict { k, v in
guard let strKey = k.stringValue else { guard let strKey = k.stringValue else {
return nil return nil
@ -167,29 +167,29 @@ private func msgPackDictToSwift(_ dict: Dictionary<NvimApi.Value, NvimApi.Value>
} }
} }
private func msgPackArrayDictToSwift(_ array: [NvimApi.Value]?) -> [Dictionary<String, NvimApi.Value>]? { fileprivate func msgPackArrayDictToSwift(_ array: [NvimApi.Value]?) -> [Dictionary<String, NvimApi.Value>]? {
return array? return array?
.flatMap { v in v.dictionaryValue } .flatMap { v in v.dictionaryValue }
.flatMap { d in msgPackDictToSwift(d) } .flatMap { d in msgPackDictToSwift(d) }
} }
extension Dictionary { extension Dictionary {
func mapToDict<K, V>(_ transform: ((key: Key, value: Value)) throws -> (K, V)) rethrows -> Dictionary<K, V> { fileprivate func mapToDict<K, V>(_ transform: ((key: Key, value: Value)) throws -> (K, V)) rethrows -> Dictionary<K, V> {
let array = try self.map(transform) let array = try self.map(transform)
return tuplesToDict(array) return tuplesToDict(array)
} }
func flatMapToDict<K, V>(_ transform: ((key: Key, value: Value)) throws -> (K, V)?) rethrows -> Dictionary<K, V> { fileprivate func flatMapToDict<K, V>(_ transform: ((key: Key, value: Value)) throws -> (K, V)?) rethrows -> Dictionary<K, V> {
let array = try self.flatMap(transform) let array = try self.flatMap(transform)
return tuplesToDict(array) return tuplesToDict(array)
} }
private func tuplesToDict<K:Hashable, V, S:Sequence>(_ sequence: S) fileprivate func tuplesToDict<K:Hashable, V, S:Sequence>(_ sequence: S)
-> Dictionary<K, V> where S.Iterator.Element == (K, V) { -> Dictionary<K, V> where S.Iterator.Element == (K, V) {
var result = Dictionary<K, V>(minimumCapacity: sequence.underestimatedCount) var result = Dictionary<K, V>(minimumCapacity: sequence.underestimatedCount)
for (key, value) in sequence { for (key, value) in sequence {
result[key] = value result[key] = value
} }