Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: cursor move events only redraw when changed #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions rplugin/python3/magma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def _initialize(self) -> None:

def _set_autocommands(self) -> None:
self.nvim.command("augroup magma")
self.nvim.command(" autocmd CursorMoved * call MagmaUpdateInterface()")
self.nvim.command(" autocmd CursorMovedI * call MagmaUpdateInterface()")
self.nvim.command(" autocmd WinScrolled * call MagmaUpdateInterface()")
self.nvim.command(" autocmd CursorMoved * call MagmaOnCursorMoved()")
self.nvim.command(" autocmd CursorMovedI * call MagmaOnCursorMoved()")
self.nvim.command(" autocmd WinScrolled * call MagmaOnWinScrolled()")
self.nvim.command(" autocmd BufEnter * call MagmaUpdateInterface()")
self.nvim.command(" autocmd BufLeave * call MagmaClearInterface()")
self.nvim.command(" autocmd BufUnload * call MagmaOnBufferUnload()")
Expand Down Expand Up @@ -100,6 +100,16 @@ def _update_interface(self) -> None:

magma.update_interface()

def _on_cursor_moved(self, scrolled=False) -> None:
if not self.initialized:
return

magma = self._get_magma(False)
if magma is None:
return

magma.on_cursor_moved(scrolled)

def _ask_for_choice(self, preface: str, options: List[str]) -> str:
index = self.nvim.funcs.inputlist(
[preface]
Expand Down Expand Up @@ -344,6 +354,16 @@ def function_magma_tick(self, _) -> None:
def function_update_interface(self, _) -> None:
self._update_interface()

@pynvim.function("MagmaOnCursorMoved", sync=True)
@nvimui
def function_on_cursor_moved(self, _) -> None:
self._on_cursor_moved()

@pynvim.function("MagmaOnWinScrolled", sync=True)
@nvimui
def function_on_win_scrolled(self, _) -> None:
self._on_cursor_moved(scrolled=True)

@pynvim.function("MagmaOperatorfunc", sync=True)
@nvimui
def function_magma_operatorfunc(self, args) -> None:
Expand Down
15 changes: 15 additions & 0 deletions rplugin/python3/magma/magmabuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,21 @@ def update_interface(self) -> None:

self.updating_interface = False

def on_cursor_moved(self, scrolled=False) -> None:
selected_cell = self._get_selected_span()

if self.selected_cell == selected_cell and selected_cell is not None:
if selected_cell.end.lineno < self.nvim.funcs.line("w$") and self.should_open_display_window and scrolled:
Copy link
Owner

@dccsillag dccsillag Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the purpose of this conditional, and it doesn't seem right to me.

self.nvim.funcs.line("w$") will give the line number of the last visible line in the window (note: this is a Vim API and line numbers are different in neovim APIs; that's why it's < and not <=, I imagine). But what if part of the cell is visible but its end is past the end of the buffer?

Furthermore, why is this done:

                if self.display_window is not None: # and self.nvim.funcs.winbufnr(self.display_window) != -1:
                    self.nvim.funcs.nvim_win_close(self.display_window, True)
                    self.canvas.clear()
                    self.display_window = None
                self._show_outputs(self.outputs[selected_cell], selected_cell.end)
                self.canvas.present()

instead of just

self.clear_interface()
self._show_outputs(self.outputs[selected_cell], selected_cell.end)
self.canvas.present()

?

if self.display_window is not None: # and self.nvim.funcs.winbufnr(self.display_window) != -1:
self.nvim.funcs.nvim_win_close(self.display_window, True)
self.canvas.clear()
self.display_window = None
self._show_outputs(self.outputs[selected_cell], selected_cell.end)
self.canvas.present()
return

self.update_interface()

def _show_selected(self, span: Span) -> None:
if span.begin.lineno == span.end.lineno:
self.nvim.funcs.nvim_buf_add_highlight(
Expand Down