mirror of
https://github.com/qvacua/vimr.git
synced 2024-11-24 11:37:32 +03:00
GH-309 Select the neovim window when the file is already open
This commit is contained in:
parent
3f7c51cdcb
commit
3769633d24
@ -51,6 +51,7 @@ typedef NS_ENUM(NSUInteger, NeoVimAgentMsgId) {
|
||||
NeoVimAgentMsgIdInputMarked,
|
||||
NeoVimAgentMsgIdDelete,
|
||||
NeoVimAgentMsgIdResize,
|
||||
NeoVimAgentMsgIdSelectWindow,
|
||||
NeoVimAgentMsgIdQuit,
|
||||
|
||||
NeoVimAgentMsgIdGetDirtyDocs,
|
||||
|
@ -216,6 +216,12 @@ static CFDataRef local_server_callback(CFMessagePortRef local, SInt32 msgid, CFD
|
||||
return nil;
|
||||
}
|
||||
|
||||
case NeoVimAgentMsgIdSelectWindow: {
|
||||
int *values = data_to_int_array(data, 1);
|
||||
server_select_win(values[0]);
|
||||
return nil;
|
||||
}
|
||||
|
||||
case NeoVimAgentMsgIdQuit:
|
||||
// exit() after returning the response such that the agent can get the response and so does not log a warning.
|
||||
[self performSelector:@selector(quit) onThread:_localServerThread withObject:nil waitUntilDone:NO];
|
||||
|
@ -21,4 +21,5 @@ extern bool server_has_dirty_docs();
|
||||
extern NSString *server_escaped_filename(NSString *filename);
|
||||
extern NSArray *server_buffers();
|
||||
extern NSArray *server_tabs();
|
||||
extern void server_select_win(int window_handle);
|
||||
extern void server_quit();
|
||||
|
@ -471,6 +471,18 @@ static void neovim_input(void **argv) {
|
||||
}
|
||||
}
|
||||
|
||||
static void neovim_select_window(void **argv) {
|
||||
win_T *window = (win_T *) argv[0];
|
||||
|
||||
Error err;
|
||||
nvim_set_current_win(window->handle, &err);
|
||||
// TODO: handle error
|
||||
WLOG("Error selecting window with handle %d: %s", window->handle, err.msg);
|
||||
|
||||
// nvim_set_current_win() does not seem to trigger a redraw.
|
||||
ui_schedule_refresh();
|
||||
}
|
||||
|
||||
static void send_dirty_status() {
|
||||
bool new_dirty_status = server_has_dirty_docs();
|
||||
DLOG("dirty status: %d vs. %d", _dirty, new_dirty_status);
|
||||
@ -782,6 +794,14 @@ NSArray *server_tabs() {
|
||||
return tabs;
|
||||
}
|
||||
|
||||
void server_select_win(int window_handle) {
|
||||
FOR_ALL_TAB_WINDOWS(tab, win) {
|
||||
if (win->handle == window_handle) {
|
||||
loop_schedule(&main_loop, event_create(1, neovim_select_window, 1, win));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void server_quit() {
|
||||
DLOG("NeoVimServer exiting...");
|
||||
exit(0);
|
||||
|
@ -9,6 +9,7 @@
|
||||
@protocol NeoVimUiBridgeProtocol;
|
||||
@class NeoVimBuffer;
|
||||
@class NeoVimTab;
|
||||
@class NeoVimWindow;
|
||||
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
@ -37,6 +38,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
- (NSArray<NSString *> *)escapedFileNames:(NSArray<NSString *> *)fileNames;
|
||||
- (NSArray<NeoVimBuffer *> *)buffers;
|
||||
- (NSArray<NeoVimTab*> *)tabs;
|
||||
- (void)selectWindow:(NeoVimWindow *)window;
|
||||
|
||||
@end
|
||||
|
||||
|
@ -225,6 +225,12 @@ static CFDataRef local_server_callback(CFMessagePortRef local, SInt32 msgid, CFD
|
||||
return [self escapedFileNames:@[ fileName ]][0];
|
||||
}
|
||||
|
||||
- (void)selectWindow:(NeoVimWindow *)window {
|
||||
int values[] = { (int) window.handle };
|
||||
NSData *data = [[NSData alloc] initWithBytes:values length:sizeof(int)];
|
||||
[self sendMessageWithId:NeoVimAgentMsgIdSelectWindow data:data expectsReply:NO];
|
||||
}
|
||||
|
||||
- (NSArray <NSString *>*)escapedFileNames:(NSArray <NSString *>*)fileNames {
|
||||
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:fileNames];
|
||||
NSData *response = [self sendMessageWithId:NeoVimAgentMsgIdGetEscapeFileNames data:data expectsReply:YES];
|
||||
|
@ -222,9 +222,23 @@ extension NeoVimView {
|
||||
}
|
||||
|
||||
public func open(urls: [URL]) {
|
||||
let currentBufferIsTransient = self.agent.buffers().filter { $0.isCurrent }.first?.isTransient ?? false
|
||||
let tabs = self.agent.tabs()
|
||||
let buffers = tabs.map { $0.allBuffers() }.flatMap { $0 }
|
||||
let currentBufferIsTransient = buffers.filter { $0.isCurrent }.first?.isTransient ?? false
|
||||
|
||||
urls.enumerated().forEach { (idx, url) in
|
||||
let path = url.path
|
||||
if buffers.filter({ $0.fileName == path }).first != nil {
|
||||
for window in tabs.map({ $0.windows }).flatMap({ $0 }) {
|
||||
|
||||
if window.buffer.fileName == path {
|
||||
Swift.print(window)
|
||||
self.agent.select(window)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if idx == 0 && currentBufferIsTransient {
|
||||
self.open(url, cmd: "e")
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user