From 491eaa73d414ea901bff0b4ae4aa0c14cd41de40 Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Sat, 7 Jan 2017 15:53:32 +0100 Subject: [PATCH] GH-339 Push the current line and col to the UI --- NeoVimServer/server_ui.m | 30 ++++++++++++++++++---------- SwiftNeoVim/NeoVimAgent.m | 8 ++++++-- SwiftNeoVim/NeoVimUiBridgeProtocol.h | 2 +- SwiftNeoVim/NeoVimView.swift | 12 ++++++----- VimR/NeoVimViewExtension.swift | 6 ++---- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/NeoVimServer/server_ui.m b/NeoVimServer/server_ui.m index fdfbf0f6..6f1d8300 100644 --- a/NeoVimServer/server_ui.m +++ b/NeoVimServer/server_ui.m @@ -190,9 +190,15 @@ static void server_ui_cursor_goto(UI *ui __unused, int row, int col) { _put_row = row; _put_column = col; - int values[] = {row, col, screen_cursor_row(), screen_cursor_column()}; - DLOG("%d:%d - %d:%d", values[0], values[1], values[2], values[3]); - NSData *data = [[NSData alloc] initWithBytes:values length:(4 * sizeof(int))]; + int values[] = { + row, col, + screen_cursor_row(), screen_cursor_column(), + (int) curwin->w_cursor.lnum, curwin->w_cursor.col + 1 + }; + + DLOG("%d:%d - %d:%d - %d:%d", values[0], values[1], values[2], values[3], values[4], values[5]); + + NSData *data = [[NSData alloc] initWithBytes:values length:(6 * sizeof(int))]; [_neovim_server sendMessageWithId:NeoVimServerMsgIdSetPosition data:data]; [data release]; } @@ -639,15 +645,17 @@ void neovim_vim_command_output(void **argv) { // We don't know why nvim_command_output does not work when the optimization level is set to -Os. // If set to -O0, nvim_command_output works fine... -_- - // String commandOutput = nvim_command_output((String) { - // .data = (char *) input.cstr, - // .size = [input lengthOfBytesUsingEncoding:NSUTF8StringEncoding] - // }, &err); - do_cmdline_cmd("redir => v:command_output"); - nvim_command(vim_string_from(input), &err); - do_cmdline_cmd("redir END"); + String commandOutput = nvim_command_output((String) { + .data = (char *) input.cstr, + .size = [input lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + }, &err); +// do_cmdline_cmd("redir => v:command_output"); +// nvim_command(vim_string_from(input), &err); +// do_cmdline_cmd("redir END"); +// +// char_u *output = get_vim_var_str(VV_COMMAND_OUTPUT); - char_u *output = get_vim_var_str(VV_COMMAND_OUTPUT); + char_u *output = (char_u *) commandOutput.data; // FIXME: handle err.set == true NSString *result = nil; diff --git a/SwiftNeoVim/NeoVimAgent.m b/SwiftNeoVim/NeoVimAgent.m index 10cd2e51..495db135 100644 --- a/SwiftNeoVim/NeoVimAgent.m +++ b/SwiftNeoVim/NeoVimAgent.m @@ -355,6 +355,9 @@ static CFDataRef local_server_callback(CFMessagePortRef local, SInt32 msgid, CFD } - (NSData *)sendMessageWithId:(NeoVimAgentMsgId)msgid data:(NSData *)data expectsReply:(bool)expectsReply { + if (!NSThread.isMainThread) { + NSLog(@"THREAD: %@\t msgid: %d\t data: %@", NSThread.currentThread, msgid, data); + } if (_neoVimIsQuitting == 1 && msgid != NeoVimAgentMsgIdQuit) { // This happens often, e.g. when exiting full screen by closing all buffers. We try to resize the window after // the message port has been closed. This is a quick-and-dirty fix. @@ -450,9 +453,10 @@ static CFDataRef local_server_callback(CFMessagePortRef local, SInt32 msgid, CFD return; case NeoVimServerMsgIdSetPosition: { - int *values = data_to_int_array(data, 4); + int *values = data_to_int_array(data, 6); [_bridge gotoPosition:(Position) { .row = values[0], .column = values[1] } - screenCursor:(Position) { .row = values[2], .column = values[3] }]; + screenCursor:(Position) { .row = values[2], .column = values[3] } + currentPosition:(Position) { .row = values[4], .column = values[5] }]; return; } diff --git a/SwiftNeoVim/NeoVimUiBridgeProtocol.h b/SwiftNeoVim/NeoVimUiBridgeProtocol.h index 1d83f7d9..43882459 100644 --- a/SwiftNeoVim/NeoVimUiBridgeProtocol.h +++ b/SwiftNeoVim/NeoVimUiBridgeProtocol.h @@ -69,7 +69,7 @@ NS_ASSUME_NONNULL_BEGIN * 2. NeoVim wants to put the cursor at (row, column). * In case of 1. NeoVim will put in subsequent call. In case of 2. NeoVim seems to flush twice in a row. */ -- (void)gotoPosition:(Position)position screenCursor:(Position)screenCursor; +- (void)gotoPosition:(Position)position screenCursor:(Position)screenCursor currentPosition:(Position)currentPosition; - (void)updateMenu; - (void)busyStart; diff --git a/SwiftNeoVim/NeoVimView.swift b/SwiftNeoVim/NeoVimView.swift index 0b8a924a..4e07954b 100644 --- a/SwiftNeoVim/NeoVimView.swift +++ b/SwiftNeoVim/NeoVimView.swift @@ -107,6 +107,8 @@ public class NeoVimView: NSView, NeoVimUiBridgeProtocol, NSUserInterfaceValidati return true } + public fileprivate(set) var currentPosition = Position(row: 1, column: 1) + fileprivate static let emojis: [UInt32] = [ 0x1F600...0x1F64F, 0x1F910...0x1F918, @@ -1252,8 +1254,9 @@ extension NeoVimView { } } - public func gotoPosition(_ position: Position, screenCursor: Position) { + public func gotoPosition(_ position: Position, screenCursor: Position, currentPosition: Position) { DispatchUtils.gui { + self.currentPosition = currentPosition // NSLog("\(#function): \(position), \(screenCursor)") let curScreenCursor = self.grid.screenCursor @@ -1316,10 +1319,9 @@ extension NeoVimView { DispatchUtils.gui { self.grid.scroll(Int(count)) self.setNeedsDisplay(region: self.grid.region) - DispatchUtils.gui { - // We do this in the next tick since we could (and do) call the agent again in the delegate method. - self.delegate?.scroll() - } + // Do not send msgs to agent -> neovim in the delegate method. It causes spinning when you're opening a file with + // existing swap file. + self.delegate?.scroll() } } diff --git a/VimR/NeoVimViewExtension.swift b/VimR/NeoVimViewExtension.swift index 978bf598..63fe3a22 100644 --- a/VimR/NeoVimViewExtension.swift +++ b/VimR/NeoVimViewExtension.swift @@ -15,13 +15,11 @@ protocol NeoVimInfoProvider: class { extension NeoVimView: NeoVimInfoProvider { func currentLine() -> Int { - let output = self.vimOutput(of: "echo line('.')") - return Int(output) ?? 0 + return self.currentPosition.row } func currentColumn() -> Int { - let output = self.vimOutput(of: "echo virtcol('.')") - return Int(output) ?? 0 + return self.currentPosition.column } }