Skip to content

Commit

Permalink
feat: Cover Empty Lines (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
TillerBurr authored Feb 23, 2024
1 parent 21d766c commit 2f50650
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ variable, their values, and a brief description.
| `g:molten_auto_init_behavior` | `"raise"` \| (`"init"`) | When set to "raise" commands which would otherwise ask for a kernel when they're run without a running kernel will instead raise an exception. Useful for other plugins that want to use `pcall` and do their own error handling |
| `g:molten_auto_open_html_in_browser` | `true` \| (`false`) | Automatically open HTML outputs in a browser. related: `molten_open_cmd` |
| `g:molten_auto_open_output` | (`true`) \| `false` | Automatically open the floating output window when your cursor moves into a cell |
| `g:molten_cover_empty_lines` | `true` \| (`false`) | The output window and virtual text will be shown just below the last line of code in the cell.|
| `g:molten_cover_lines_starting_with` | (`{}`) \| array of str | When `cover_empty_lines` is true, also covers lines starting with these strings |
| `g:molten_copy_output` | `true` \| (`false`) | Copy evaluation output to clipboard automatically (requires [`pyperclip`](#requirements))|
| `g:molten_enter_output_behavior` | (`"open_then_enter"`) \| `"open_and_enter"` \| `"no_open"` | The behavior of [MoltenEnterOutput](#moltenenteroutput) |
| `g:molten_image_provider` | (`"none"`) \| `"image.nvim"` | How images are displayed |
Expand Down Expand Up @@ -400,4 +402,3 @@ that isn't currently supported, feel free to open an issue and/or PR!

- @dccsillag and everyone who has contributed to [magma-nvim](https://github.com/dccsillag/magma-nvim)
- @3rd and everyone who has contributed to [image.nvim](https://github.com/3rd/image.nvim)

4 changes: 4 additions & 0 deletions rplugin/python3/molten/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class MoltenOptions:
auto_init_behavior: str
auto_open_html_in_browser: bool
auto_open_output: bool
cover_empty_lines: bool
cover_lines_starting_with: List[str]
copy_output: bool
enter_output_behavior: str
image_provider: str
Expand Down Expand Up @@ -67,6 +69,8 @@ def __init__(self, nvim: Nvim):
("molten_auto_init_behavior", "init"), # "raise" or "init"
("molten_auto_open_html_in_browser", False),
("molten_auto_open_output", True),
("molten_cover_empty_lines", False),
("molten_cover_lines_starting_with", []),
("molten_copy_output", False),
("molten_enter_output_behavior", "open_then_enter"),
("molten_image_provider", "none"),
Expand Down
42 changes: 37 additions & 5 deletions rplugin/python3/molten/outputbuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def build_output_text(self, shape, buf: int, virtual: bool) -> Tuple[List[str],
def show_virtual_output(self, anchor: Position) -> None:
if self.displayed_status == OutputStatus.DONE and self.virt_text_id is not None:
return

offset = self.calculate_offset(anchor) if self.options.cover_empty_lines else 0
self.displayed_status = self.output.status

buf = self.nvim.buffers[anchor.bufno]
Expand All @@ -186,7 +186,7 @@ def show_virtual_output(self, anchor: Position) -> None:
win = self.nvim.current.window
win_info = self.nvim.funcs.getwininfo(win.handle)[0]
win_col = win_info["wincol"]
win_row = anchor.lineno
win_row = anchor.lineno + offset
win_width = win_info["width"] - win_info["textoff"]
win_height = win_info["height"]

Expand Down Expand Up @@ -218,11 +218,41 @@ def show_virtual_output(self, anchor: Position) -> None:
)
self.canvas.present()

def calculate_offset(self, anchor: Position) -> int:
offset = 0
lineno = anchor.lineno
while lineno > 0:
current_line = self.nvim.funcs.nvim_buf_get_lines(
anchor.bufno,
lineno,
lineno + 1,
False,
)[0]
is_comment = False
for x in self.options.cover_lines_starting_with:
if current_line.startswith(x):
is_comment = True
break
if current_line != "" and not is_comment:
return offset
else:
lineno -= 1
offset -= 1
# Only get here if current_pos.lineno == 0
return 0

def show_floating_win(self, anchor: Position) -> None:
win = self.nvim.current.window
win_col = win.col
win_row = self._buffer_to_window_lineno(anchor.lineno + 1)
if win_row == 0: # anchor position is off screen
if self.options.cover_empty_lines:
offset = self.calculate_offset(anchor)
win_row = (
self._buffer_to_window_lineno(anchor.lineno + offset) + 1
)
else:
win_row = self._buffer_to_window_lineno(anchor.lineno + 1)

if win_row <= 0: # anchor position is off screen
return
win_width = win.width
win_height = win.height
Expand Down Expand Up @@ -324,8 +354,10 @@ def show_floating_win(self, anchor: Position) -> None:
if self.display_virt_lines is not None:
del self.display_virt_lines

if self.options.output_virt_lines:
if self.options.output_virt_lines or self.options.cover_empty_lines:
virt_lines_y = anchor.lineno
if self.options.cover_empty_lines:
virt_lines_y += offset
virt_lines_height = max_height + border_h
if self.options.virt_lines_off_by_1:
virt_lines_y += 1
Expand Down

0 comments on commit 2f50650

Please sign in to comment.