From ca8c7fedb6329ad2dcfdb0beeac786203fada62d Mon Sep 17 00:00:00 2001 From: Tae Won Ha Date: Mon, 11 Dec 2023 08:31:25 +0100 Subject: [PATCH] Reorder Neovim launch init sequence Colorscheme change notification still not working --- .../Sources/NvimView/NvimView+Resize.swift | 170 +++++++++--------- 1 file changed, 83 insertions(+), 87 deletions(-) diff --git a/NvimView/Sources/NvimView/NvimView+Resize.swift b/NvimView/Sources/NvimView/NvimView+Resize.swift index cea39171..3b735971 100644 --- a/NvimView/Sources/NvimView/NvimView+Resize.swift +++ b/NvimView/Sources/NvimView/NvimView+Resize.swift @@ -93,101 +93,97 @@ extension NvimView { // We wait here, since the user of NvimView cannot subscribe // on the Completable. We could demand that the user call launchNeoVim() // by themselves, but... - var c: Int32 = 0 // FIXME - try? - self.api.run(at: sockPath, inPipe: inPipe, outPipe: outPipe, errorPipe: errorPipe) - .andThen(self.api.getApiInfo(errWhenBlocked: false).map { value in - guard let info = value.arrayValue, - info.count == 2, - let channel = info[0].int32Value, - let dict = info[1].dictionaryValue, - let version = dict["version"]?.dictionaryValue, - let major = version["major"]?.intValue, - let minor = version["minor"]?.intValue - else { - throw RxNeovimApi.Error.exception(message: "Could not convert values to api info.") - } - - guard (major >= kMinAlphaVersion && minor >= kMinMinorVersion) || major >= - kMinMajorVersion - else { - self.eventsSubject.onNext(.ipcBecameInvalid( - "Incompatible neovim version \(major).\(minor)" - )) - throw RxNeovimApi.Error - .exception(message: "Incompatible neovim version.") - } - - c = channel - - return channel - }.asCompletable()) + try? self.api.run(at: sockPath, inPipe: inPipe, outPipe: outPipe, errorPipe: errorPipe) .andThen( - self.api.command(command: "let g:gui_vimr = 1", expectsReturnValue: false) + self.api.getApiInfo(errWhenBlocked: false) + .map { value in + guard let info = value.arrayValue, + info.count == 2, + let channel = info[0].int32Value, + let dict = info[1].dictionaryValue, + let version = dict["version"]?.dictionaryValue, + let major = version["major"]?.intValue, + let minor = version["minor"]?.intValue + else { + throw RxNeovimApi.Error.exception(message: "Could not convert values to api info.") + } + + guard (major >= kMinAlphaVersion && minor >= kMinMinorVersion) || major >= + kMinMajorVersion + else { + self.eventsSubject.onNext(.ipcBecameInvalid( + "Incompatible neovim version \(major).\(minor)" + )) + throw RxNeovimApi.Error.exception(message: "Incompatible neovim version.") + } + + return channel + } + .map { (channel: Int32) in + self.api.exec2(src: """ + ":augroup vimr + ":augroup! + :autocmd VimEnter * call rpcnotify(\(channel), 'autocommand', 'vimenter') + :autocmd BufWinEnter * call rpcnotify(\( + channel + ), 'autocommand', 'bufwinenter', str2nr(expand(''))) + :autocmd BufWinEnter * call rpcnotify(\( + channel + ), 'autocommand', 'bufwinleave', str2nr(expand(''))) + :autocmd TabEnter * call rpcnotify(\( + channel + ), 'autocommand', 'tabenter', str2nr(expand(''))) + :autocmd BufWritePost * call rpcnotify(\( + channel + ), 'autocommand', 'bufwritepost', str2nr(expand(''))) + :autocmd BufEnter * call rpcnotify(\( + channel + ), 'autocommand', 'bufenter', str2nr(expand(''))) + :autocmd DirChanged * call rpcnotify(\( + channel + ), 'autocommand', 'dirchanged', expand('')) + :autocmd ColorScheme * call rpcnotify(\(channel), 'autocommand', 'colorscheme', \ + get(nvim_get_hl(0, {'id': hlID('Normal')}), 'fg', -1), \ + get(nvim_get_hl(0, {'id': hlID('Normal')}), 'bg', -1), \ + get(nvim_get_hl(0, {'id': hlID('Visual')}), 'fg', -1), \ + get(nvim_get_hl(0, {'id': hlID('Visual')}), 'bg', -1), \ + get(nvim_get_hl(0, {'id': hlID('Directory')}), 'fg', -1)) + :autocmd ExitPre * call rpcnotify(\(channel), 'autocommand', 'exitpre') + :autocmd BufModifiedSet * call rpcnotify(\( + channel + ), 'autocommand', 'bufmodifiedset', \ + str2nr(expand('')), getbufinfo(str2nr(expand('')))[0].changed) + ":augroup END + """, opts: [:], errWhenBlocked: false) + } + .asCompletable() ) - .andThen(self.api.uiAttach(width: size.width, height: size.height, options: [ - "ext_linegrid": true, - "ext_multigrid": false, - "ext_tabline": MessagePackValue(self.usesCustomTabBar), - "rgb": true, - ])) + .andThen(self.api.command(command: "let g:gui_vimr = 1", expectsReturnValue: false)) .andThen( - self.api.exec2(src: """ - ":augroup vimr - ":augroup! - :autocmd VimEnter * call rpcnotify(\(c), 'autocommand', 'vimenter') - :autocmd BufWinEnter * call rpcnotify(\( - c - ), 'autocommand', 'bufwinenter', str2nr(expand(''))) - :autocmd BufWinEnter * call rpcnotify(\( - c - ), 'autocommand', 'bufwinleave', str2nr(expand(''))) - :autocmd TabEnter * call rpcnotify(\( - c - ), 'autocommand', 'tabenter', str2nr(expand(''))) - :autocmd BufWritePost * call rpcnotify(\( - c - ), 'autocommand', 'bufwritepost', str2nr(expand(''))) - :autocmd BufEnter * call rpcnotify(\( - c - ), 'autocommand', 'bufenter', str2nr(expand(''))) - :autocmd DirChanged * call rpcnotify(\( - c - ), 'autocommand', 'dirchanged', expand('')) - :autocmd ColorScheme * call rpcnotify(\(c), 'autocommand', 'colorscheme', \ - get(nvim_get_hl(0, {'id': hlID('Normal')}), 'fg', -1), \ - get(nvim_get_hl(0, {'id': hlID('Normal')}), 'bg', -1), \ - get(nvim_get_hl(0, {'id': hlID('Visual')}), 'fg', -1), \ - get(nvim_get_hl(0, {'id': hlID('Visual')}), 'bg', -1), \ - get(nvim_get_hl(0, {'id': hlID('Directory')}), 'fg', -1)) - :autocmd ExitPre * call rpcnotify(\(c), 'autocommand', 'exitpre') - :autocmd BufModifiedSet * call rpcnotify(\( - c - ), 'autocommand', 'bufmodifiedset', \ - str2nr(expand('')), getbufinfo(str2nr(expand('')))[0].changed) - ":augroup END - """, opts: [:], errWhenBlocked: false).asCompletable() + self.api.uiAttach(width: size.width, height: size.height, options: [ + "ext_linegrid": true, + "ext_multigrid": false, + "ext_tabline": MessagePackValue(self.usesCustomTabBar), + "rgb": true, + ]) ) .andThen( - self.sourceFileUrls.reduce(Completable.empty()) { prev, url in - prev - .andThen( - self.api.exec2(src: "source \(url.shellEscapedPath)", opts: ["output": true]) - .map { - retval in - guard let output_value = retval["output"] ?? retval["output"], - let output = output_value.stringValue - else { - throw RxNeovimApi.Error - .exception(message: "Could not convert values to output.") - } - return output + self.sourceFileUrls.reduce(.empty()) { prev, url in + prev.andThen( + self.api.exec2(src: "source \(url.shellEscapedPath)", opts: ["output": true]) + .map { retval in + guard let output = retval["output"]?.stringValue else { + throw RxNeovimApi.Error + .exception(message: "Could not convert values to output.") } - .asCompletable() - ) + return output + } + .asCompletable() + ) } ) - .andThen(self.api.subscribe(event: NvimView.rpcEventName)).wait() + .andThen(self.api.subscribe(event: NvimView.rpcEventName)) + .wait() } private func randomEmoji() -> String {