numToStr / FTerm.nvim

:fire: No-nonsense floating terminal plugin for neovim :fire:
MIT License
721 stars 24 forks source link

Scratch Terminal issue #75

Open delminskii opened 1 year ago

delminskii commented 1 year ago

I noticed there's no option to save current buffer before executing the scratch terminal in this plugin so I did that myself by calling vim.cmd. Here's a part of my config for that (executing a code inside of buffer 0):

local fterm = require('FTerm')
local fterm_dimensions = {
  height = 0.35,
  width = 1.0,
  y = 1.0
}
fterm.setup({ dimensions = fterm_dimensions })
local runners = {
  python = 'python3.7',
  sh = 'bash'
}
map('n', '<Leader>e', function()
  vim.cmd('write')
  local buf = vim.api.nvim_buf_get_name(0)
  local ftype = vim.filetype.match({ filename = buf })
  local exec = runners[ftype]
  if exec ~= nil then
    fterm.scratch({
      cmd = { exec, buf },
      dimensions = fterm_dimensions
    })
  end
  end
)

Environment: OS: debian10 NVIM version: the latest 0.9.0 dev

The problem is that if I do execute that action over unsaved buffer too fast. the contents of it displays in the terminal buffer. How to reproduce that with the config above:

  1. Edit some code (do not save that buffer);
  2. Type <Leader>e hotkey.

All the actions above must be performed as quick as you can. And whatever you do in the main buffer (buffer 0) is mirrored to the terminal buffer.

This's a little video showing that behaviour (starting from 0:56 sec) https://user-images.githubusercontent.com/6321554/199979073-ecd3b7b5-d949-4d11-afd9-ff1f96fb10a6.mp4

numToStr commented 1 year ago

Hmmm... interesting. It looks like a race condition where buffer is written on the terminal window before the window itself is destroyed. If this is the case, then I have no idea on how to fix this.