1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-25 23:02:35 +03:00

GH-571 Add NvimMsgPack in SwiftNeoVim

This commit is contained in:
Tae Won Ha 2017-11-28 13:16:10 +01:00
parent 9aea94df41
commit 0833cffa69
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
16 changed files with 217 additions and 50 deletions

View File

@ -236,7 +236,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.12;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
@ -290,7 +290,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.12;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";

View File

@ -32,7 +32,6 @@ public class Connection {
self.session = session
session.dataCallback = { data in (try? unpackAll(data))?.forEach(self.processResponse) }
session.run()
}
public convenience init?(unixDomainSocketPath path: String) {
@ -43,6 +42,10 @@ public class Connection {
self.init(with: session)
}
public func run() {
self.session.run()
}
@discardableResult
public func request(type: Int = 0,
msgid: UInt32,

View File

@ -12,6 +12,8 @@
@property CFTimeInterval timeout;
@property (nonatomic, copy, nullable) void (^dataCallback)(NSData * __nonnull);
@property (readonly, getter=isRunning) bool running;
- (void)run;
- (CFSocketError)writeData:(NSData * __nonnull)data;

View File

@ -45,12 +45,18 @@ static void socket_call_back(
int _native_socket;
struct sockaddr_un _sockaddr;
NSString *_path;
CFRunLoopRef _run_loop;
CFRunLoopSourceRef _run_loop_source;
NSThread *_thread;
}
- (bool)isRunning {
return [_thread isExecuting];
}
- (instancetype)initWithPath:(NSString *)path {
self = [super init];
if (self == nil) {
@ -58,40 +64,7 @@ static void socket_call_back(
}
_timeout = 5;
if ((_native_socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
NSLog(@"Error: Unix domain socket NULL!");
return nil;
}
_sockaddr.sun_family = AF_UNIX;
strcpy(_sockaddr.sun_path, [path cStringUsingEncoding:NSUTF8StringEncoding]);
if (connect(_native_socket, (struct sockaddr *) &_sockaddr, (socklen_t) SUN_LEN(&_sockaddr)) == -1) {
NSLog(@"Error: Could not connect to the socket!");
close(_native_socket);
return nil;
}
CFSocketContext context;
context.copyDescription = NULL;
context.release = NULL;
context.retain = NULL;
context.version = 0;
context.info = (__bridge void *) self;
_socket = CFSocketCreateWithNative(
NULL,
_native_socket,
kCFSocketConnectCallBack | kCFSocketDataCallBack,
socket_call_back,
&context
);
if (_socket == nil) {
NSLog(@"Error: CFSocket is NULL!");
close(_native_socket);
return nil;
}
_path = path;
return self;
}
@ -113,15 +86,51 @@ static void socket_call_back(
}
- (void)run {
if ((_native_socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
NSLog(@"Error: Unix domain socket NULL!");
[NSException raise:@"UnixDomainSocketConnectionException" format:@"Unix domain socket NULL!"];
}
_sockaddr.sun_family = AF_UNIX;
strcpy(_sockaddr.sun_path, [_path cStringUsingEncoding:NSUTF8StringEncoding]);
if (connect(_native_socket, (struct sockaddr *) &_sockaddr, (socklen_t) SUN_LEN(&_sockaddr)) == -1) {
NSLog(@"Error: Could not connect to the socket!");
close(_native_socket);
[NSException raise:@"UnixDomainSocketConnectionException" format:@"Could not connect to socket!"];
}
CFSocketContext context;
context.copyDescription = NULL;
context.release = NULL;
context.retain = NULL;
context.version = 0;
context.info = (__bridge void *) self;
_socket = CFSocketCreateWithNative(
NULL,
_native_socket,
kCFSocketConnectCallBack | kCFSocketDataCallBack,
socket_call_back,
&context
);
if (_socket == nil) {
NSLog(@"Error: CFSocket is NULL!");
close(_native_socket);
[NSException raise:@"UnixDomainSocketConnectionException" format:@"CFSocket is NULL!"];
}
_run_loop_source = CFSocketCreateRunLoopSource(NULL, _socket, 0);
_thread = [[NSThread alloc] initWithBlock:^{
_run_loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(_run_loop, _run_loop_source, kCFRunLoopDefaultMode);
CFRunLoopRun();
}];
_thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadMain) object:nil];
[_thread start];
}
- (void)threadMain {
_run_loop = CFRunLoopGetCurrent();
CFRunLoopAddSource(_run_loop, _run_loop_source, kCFRunLoopDefaultMode);
CFRunLoopRun();
}
- (CFSocketError)writeData:(NSData *)data {
return CFSocketSendData(_socket, NULL, (__bridge CFDataRef) data, _timeout);
}

View File

@ -12,3 +12,5 @@ guard let nvim = Nvim(at: "/tmp/nvim.sock") else {
print(nvim.listRuntimePaths())
let bufs = nvim.listBufs().value
print(String(describing: bufs?.map({ nvim.bufGetName(buffer: $0) })))
let buf = bufs![0]
nvim.bufGetChangedtick(buffer: buf)

View File

@ -232,7 +232,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.12;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
@ -286,7 +286,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.12;
MACOSX_DEPLOYMENT_TARGET = 10.10;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";

View File

@ -6,6 +6,20 @@
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4B9E333E1FCB457800E0C4BC"
BuildableName = "MsgPackRpc.framework"
BlueprintName = "MsgPackRpc"
ReferencedContainer = "container:../MsgPackRpc/MsgPackRpc.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
@ -30,6 +44,15 @@
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4B9E336B1FCB475600E0C4BC"
BuildableName = "NvimMsgPack.framework"
BlueprintName = "NvimMsgPack"
ReferencedContainer = "container:NvimMsgPack.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>

View File

@ -134,6 +134,10 @@ public class Nvim {
self.connection = connection
}
public func run() {
self.connection.run()
}
public func rpc(method: String,
params: [MsgPackRpc.Value],
expectsReturnValue: Bool) -> Result<MsgPackRpc.Value, Nvim.Error> {
@ -181,6 +185,10 @@ public class Nvim {
self.session = session
}
public func connect() {
self.session.run()
}
public func rpc(method: String,
params: [Nvim.Value],
expectsReturnValue: Bool = true) -> Nvim.Response<Nvim.Value> {

View File

@ -3,7 +3,7 @@
import MsgPackRpc
extension Nvim.Error {
public extension Nvim.Error {
public enum ErrorType: Int {
@ -13,7 +13,7 @@ extension Nvim.Error {
}
}
extension Nvim {
public extension Nvim {
public func bufLineCount(
buffer: Nvim.Buffer,

View File

@ -210,7 +210,8 @@ static CFDataRef local_server_callback(CFMessagePortRef local __unused, SInt32 m
_neoVimServerTask.arguments = shellArgs;
[_neoVimServerTask launch];
NSString *cmd = [NSString stringWithFormat:@"exec \"%@\" '%@' '%@'",
NSString *cmd = [NSString stringWithFormat:@"NVIM_LISTEN_ADDRESS=%@ exec \"%@\" '%@' '%@'",
[NSString stringWithFormat:@"/tmp/vimr_%@.sock", _uuid],
[self neoVimServerExecutablePath],
[self localServerName],
[self remoteServerName]];

View File

@ -57,6 +57,8 @@ extension NeoVimView {
self.logger.info("=== Starting neovim...")
let noErrorDuringInitialization = self.agent.runLocalServerAndNeoVim(withWidth: size.width, height: size.height)
self.nvim.connect()
self.agent.vimCommand("set mouse=a")
self.agent.vimCommand("set title")
self.agent.vimCommand("set termguicolors")

View File

@ -4,6 +4,7 @@
*/
import Cocoa
import NvimMsgPack
public class NeoVimView: NSView,
NeoVimUiBridgeProtocol,
@ -151,6 +152,11 @@ public class NeoVimView: NSView,
public init(frame rect: NSRect, config: Config) {
self.drawer = TextDrawer(font: self._font)
self.agent = NeoVimAgent(uuid: self.uuid)
guard let nvim = Nvim(at: "/tmp/vimr_\(self.uuid).sock") else {
preconditionFailure("Nvim could not be instantiated")
}
self.nvim = nvim
super.init(frame: .zero)
self.registerForDraggedTypes([NSPasteboard.PasteboardType(String(kUTTypeFileURL))])
@ -199,6 +205,7 @@ public class NeoVimView: NSView,
with: URL(fileURLWithPath: "/tmp/nvv-bridge.log"),
shouldLogDebug: nil)
let agent: NeoVimAgent
let nvim: Nvim
let grid = Grid()
let drawer: TextDrawer
@ -235,7 +242,7 @@ public class NeoVimView: NSView,
var _cwd = URL(fileURLWithPath: NSHomeDirectory())
var shouldDrawCursor = false
var isInitialResize = true
// cache the tabs for Touch Bar use
var tabsCache = [NeoVimTab]()

View File

@ -165,6 +165,10 @@
4B854A1D1D31447C00E08DE1 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B854A1C1D31447C00E08DE1 /* main.m */; };
4B8AC0441DBCB3A2007CCC9B /* NeoVimObjectsExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B8AC0431DBCB3A1007CCC9B /* NeoVimObjectsExtensions.swift */; };
4B8C7C761F05A04400F25BE8 /* CocoaMarkdown.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4B183E0D1E06E2940079E8A8 /* CocoaMarkdown.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
4B90EFC91FCD6CE2008A39E0 /* MsgPackRpc.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B90EFCA1FCD6CE2008A39E0 /* MsgPackRpc.framework */; };
4B90EFCB1FCD6CE5008A39E0 /* NvimMsgPack.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B90EFCC1FCD6CE5008A39E0 /* NvimMsgPack.framework */; };
4B90EFCD1FCD6CEB008A39E0 /* MsgPackRpc.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B90EFCA1FCD6CE2008A39E0 /* MsgPackRpc.framework */; };
4B90EFCE1FCD6CEE008A39E0 /* NvimMsgPack.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4B90EFCC1FCD6CE5008A39E0 /* NvimMsgPack.framework */; };
4B91FFF41DEB772200447068 /* CocoaFontAwesome.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 4B337FBA1DEB76F20020ADD2 /* CocoaFontAwesome.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
4B91FFF61DEB772B00447068 /* CocoaFontAwesome.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 4B337FBA1DEB76F20020ADD2 /* CocoaFontAwesome.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
4B96384C1ED9793B001C556F /* NeoVimAutoCommandEvent.generated.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B96384B1ED9793B001C556F /* NeoVimAutoCommandEvent.generated.m */; };
@ -512,6 +516,8 @@
4B854A1A1D31447C00E08DE1 /* NeoVimServer */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = NeoVimServer; sourceTree = BUILT_PRODUCTS_DIR; };
4B854A1C1D31447C00E08DE1 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
4B8AC0431DBCB3A1007CCC9B /* NeoVimObjectsExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NeoVimObjectsExtensions.swift; sourceTree = "<group>"; };
4B90EFCA1FCD6CE2008A39E0 /* MsgPackRpc.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = MsgPackRpc.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4B90EFCC1FCD6CE5008A39E0 /* NvimMsgPack.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = NvimMsgPack.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4B96384B1ED9793B001C556F /* NeoVimAutoCommandEvent.generated.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NeoVimAutoCommandEvent.generated.m; sourceTree = "<group>"; };
4B96384D1ED9797F001C556F /* NeoVimUiBridgeProtocol.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NeoVimUiBridgeProtocol.m; sourceTree = "<group>"; };
4B96384F1ED979BD001C556F /* Logger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
@ -566,6 +572,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
4B90EFCE1FCD6CEE008A39E0 /* NvimMsgPack.framework in Frameworks */,
4B90EFCD1FCD6CEB008A39E0 /* MsgPackRpc.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -608,6 +616,8 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
4B90EFCB1FCD6CE5008A39E0 /* NvimMsgPack.framework in Frameworks */,
4B90EFC91FCD6CE2008A39E0 /* MsgPackRpc.framework in Frameworks */,
4B2A2BFE1D0351810074CE9A /* SwiftNeoVim.framework in Frameworks */,
4BDD056A1DB0CAB700D1B405 /* Sparkle.framework in Frameworks */,
4B337FBB1DEB76F20020ADD2 /* CocoaFontAwesome.framework in Frameworks */,
@ -834,6 +844,8 @@
4B5012001EBA791000F76C46 /* Frameworks */ = {
isa = PBXGroup;
children = (
4B90EFCC1FCD6CE5008A39E0 /* NvimMsgPack.framework */,
4B90EFCA1FCD6CE2008A39E0 /* MsgPackRpc.framework */,
4BB7F3911F9D260B00624F61 /* Swifter.framework */,
4B5011F71EBA67EB00F76C46 /* RxTest.framework */,
4B183E0D1E06E2940079E8A8 /* CocoaMarkdown.framework */,

View File

@ -6,6 +6,34 @@
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4B9E333E1FCB457800E0C4BC"
BuildableName = "MsgPackRpc.framework"
BlueprintName = "MsgPackRpc"
ReferencedContainer = "container:MsgPackRpc/MsgPackRpc.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4B9E336B1FCB475600E0C4BC"
BuildableName = "NvimMsgPack.framework"
BlueprintName = "NvimMsgPack"
ReferencedContainer = "container:NvimMsgPack/NvimMsgPack.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
@ -20,6 +48,20 @@
ReferencedContainer = "container:VimR.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4B854A191D31447C00E08DE1"
BuildableName = "NeoVimServer"
BlueprintName = "NeoVimServer"
ReferencedContainer = "container:VimR.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction

View File

@ -20,6 +20,62 @@
ReferencedContainer = "container:VimR.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4B9E333E1FCB457800E0C4BC"
BuildableName = "MsgPackRpc.framework"
BlueprintName = "MsgPackRpc"
ReferencedContainer = "container:MsgPackRpc/MsgPackRpc.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4B9E336B1FCB475600E0C4BC"
BuildableName = "NvimMsgPack.framework"
BlueprintName = "NvimMsgPack"
ReferencedContainer = "container:NvimMsgPack/NvimMsgPack.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4B854A191D31447C00E08DE1"
BuildableName = "NeoVimServer"
BlueprintName = "NeoVimServer"
ReferencedContainer = "container:VimR.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "4B2A2BF61D0351810074CE9A"
BuildableName = "SwiftNeoVim.framework"
BlueprintName = "SwiftNeoVim"
ReferencedContainer = "container:VimR.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction

View File

@ -54,7 +54,7 @@ extension_template = Template('''\
import MsgPackRpc
extension Nvim.Error {
public extension Nvim.Error {
public enum ErrorType: Int {
@ -63,7 +63,7 @@ extension Nvim.Error {
}
}
extension Nvim {
public extension Nvim {
$body
}