mirror of
https://github.com/qvacua/vimr.git
synced 2024-11-24 11:37:32 +03:00
Use the custom API for getting buffer info
This commit is contained in:
parent
c2415f8ee3
commit
52c0b7ec51
@ -56,7 +56,7 @@ public class NvimApi {
|
||||
return "\(Swift.type(of: self))(type: \(self.type), message: \"\(self.message)\")"
|
||||
}
|
||||
|
||||
init(_ message: String) {
|
||||
public init(_ message: String) {
|
||||
self.type = .unknown
|
||||
self.message = message
|
||||
}
|
||||
|
@ -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
|
||||
guard let strKey = k.stringValue else {
|
||||
return nil
|
||||
@ -2232,7 +2232,7 @@ 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?
|
||||
.flatMap { v in v.dictionaryValue }
|
||||
.flatMap { d in msgPackDictToSwift(d) }
|
||||
@ -2240,17 +2240,17 @@ private func msgPackArrayDictToSwift(_ array: [NvimApi.Value]?) -> [Dictionary<S
|
||||
|
||||
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)
|
||||
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)
|
||||
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) {
|
||||
|
||||
var result = Dictionary<K, V>(minimumCapacity: sequence.underestimatedCount)
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
/* Begin PBXBuildFile section */
|
||||
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 */; };
|
||||
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, ); }; };
|
||||
@ -84,6 +85,7 @@
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
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>"; };
|
||||
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; };
|
||||
@ -215,6 +217,7 @@
|
||||
4B90F0221FD2AFAD008A39E0 /* TextDrawer.m */,
|
||||
4B90F0771FD2BA7B008A39E0 /* Logger.h */,
|
||||
1929B22A0CAD417EC3790F02 /* NvimViewObjects.swift */,
|
||||
1929BBD7F88AE4F01E626691 /* NvimApiExtension.swift */,
|
||||
);
|
||||
path = NvimView;
|
||||
sourceTree = "<group>";
|
||||
@ -410,6 +413,7 @@
|
||||
4B90F03A1FD2AFAE008A39E0 /* NvimView+Key.swift in Sources */,
|
||||
4B90F0361FD2AFAE008A39E0 /* NvimViewDelegate.swift in Sources */,
|
||||
1929B40A751BDA2882D4FC94 /* NvimViewObjects.swift in Sources */,
|
||||
1929B86897DAEFDBABAB1C14 /* NvimApiExtension.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
71
NvimView/NvimView/NvimApiExtension.swift
Normal file
71
NvimView/NvimView/NvimApiExtension.swift
Normal 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
|
||||
}
|
||||
}
|
@ -189,18 +189,27 @@ extension NvimView {
|
||||
}
|
||||
|
||||
private func neoVimBuffer(for buf: NvimApi.Buffer, currentBuffer: NvimApi.Buffer?) -> NvimView.Buffer? {
|
||||
guard let path = self.nvim.bufGetName(buffer: buf).value else {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard let dirty = self.nvim.bufGetOption(buffer: buf, name: "mod").value?.boolValue else {
|
||||
guard let info = self.nvim.getBufGetInfo(buffer: buf).value else {
|
||||
return nil
|
||||
}
|
||||
|
||||
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,
|
||||
url: URL(fileURLWithPath: path),
|
||||
url: url,
|
||||
type: buftype,
|
||||
isDirty: dirty,
|
||||
isCurrent: current)
|
||||
}
|
||||
|
@ -11,11 +11,17 @@ extension NvimView {
|
||||
public struct Buffer: Equatable {
|
||||
|
||||
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 url: URL?
|
||||
public let type: String
|
||||
|
||||
public let isDirty: Bool
|
||||
public let isCurrent: Bool
|
||||
@ -33,6 +39,10 @@ extension NvimView {
|
||||
}
|
||||
|
||||
public var name: String? {
|
||||
if self.type == "quickfix" {
|
||||
return "Quickfix"
|
||||
}
|
||||
|
||||
return self.url?.lastPathComponent
|
||||
}
|
||||
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit 9589942270b0e58bca6288c87b304ac7105f10bf
|
||||
Subproject commit 741a6f026fbc3a3d1235e358331bfaf0d1cc6799
|
@ -54,13 +54,12 @@ class BuffersList: NSView,
|
||||
|
||||
self.usesTheme = state.appearance.usesTheme
|
||||
|
||||
let buffers = state.buffers.removingDuplicatesPreservingFromBeginning()
|
||||
if self.buffers == buffers && !themeChanged && self.showsFileIcon == state.appearance.showsFileIcon {
|
||||
if self.buffers == state.buffers && !themeChanged && self.showsFileIcon == state.appearance.showsFileIcon {
|
||||
return
|
||||
}
|
||||
|
||||
self.showsFileIcon = state.appearance.showsFileIcon
|
||||
self.buffers = buffers
|
||||
self.buffers = state.buffers
|
||||
self.bufferList.reloadData()
|
||||
self.adjustFileViewWidth()
|
||||
})
|
||||
|
@ -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
|
||||
guard let strKey = k.stringValue else {
|
||||
return nil
|
||||
@ -167,7 +167,7 @@ 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?
|
||||
.flatMap { v in v.dictionaryValue }
|
||||
.flatMap { d in msgPackDictToSwift(d) }
|
||||
@ -175,17 +175,17 @@ private func msgPackArrayDictToSwift(_ array: [NvimApi.Value]?) -> [Dictionary<S
|
||||
|
||||
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)
|
||||
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)
|
||||
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) {
|
||||
|
||||
var result = Dictionary<K, V>(minimumCapacity: sequence.underestimatedCount)
|
||||
|
Loading…
Reference in New Issue
Block a user