michaelb / sniprun

A neovim plugin to run lines/blocs of code (independently of the rest of the file), supporting multiples languages
MIT License
1.43k stars 46 forks source link

How to run the entire current file? #276

Closed Wangch29 closed 6 months ago

Wangch29 commented 7 months ago

Besides of %SnipRun

michaelb commented 7 months ago

Besides :%SnipRun, aka the intended way ?? Not much, except the API (see API.md, run_range) while hacking a little for the end of the range, maybe putting -1 or just a large number will work, I haven't tested

tom-gora commented 6 months ago

I think you might appreciate my wrappers:

function SnipRunFlowSnippet()
  -- set up state to track if user cancels or accepts
  local canceled = false
  local accepted = false

  -- prompt for selection
  print "Select code to run and press ESC to cancel or Return to execute"

  -- enter visual lines
  vim.cmd "normal! V"
  vim.cmd "redraw"

  while not canceled and not accepted do
    -- loop to do the selection allowing enter, esc or v-block up and down
    local char = vim.fn.getchar()
    if char == 27 then -- escape
      canceled = true
    elseif char == 13 then -- return
      accepted = true
    elseif char == 106 then -- j
      vim.cmd "normal! j"
      vim.cmd "redraw"
    elseif char == 107 then -- k
      vim.cmd "normal! k"
      vim.cmd "redraw"
    end
  end

  if accepted then
    -- HACK: exit visual first to get selection to marks
    local esc = vim.api.nvim_replace_termcodes("<esc>", true, false, true)
    vim.api.nvim_feedkeys(esc, "x", false)
    -- run the snippet
    vim.cmd ":'<,'>SnipRun"
  end

  -- exit visual and confirm cancellation
  if canceled then
    print "Operation canceled"
    local esc = vim.api.nvim_replace_termcodes("<esc>", true, false, true)
    vim.api.nvim_feedkeys(esc, "x", false)
  end
end

function SnipRunFlowFile()
  vim.cmd "normal! ggVG$"
  local esc = vim.api.nvim_replace_termcodes("<esc>", true, false, true)
  vim.api.nvim_feedkeys(esc, "x", false)
  vim.cmd ":'<,'>SnipRun"
end

setting them from bindings in normal 1. in a table:


      "<cmd>lua SnipRunFlowSnippet()<CR>",
      "Execute SNIPPET with SnipRun",
      general_opts,
    },

    ["<leader>cX"] = {
      "<cmd>lua SnipRunFlowFile()<CR>",
      "Execute FILE with SnipRun",
      general_opts,
    },```

2. or native:  
```vim.api.nvim_set_keymap('n', '<leader>cx', ':lua SnipRunFlowSnippet()<CR>', general_opts)
vim.api.nvim_set_keymap('n', '<leader>cX', ':lua SnipRunFlowFile()<CR>', general_opts)```
michaelb commented 6 months ago

@tom-gora , I don't know if you were addressing me or Wangch29.

I personally don't see very much added value (plus, you might want your script to listen to up/down arrows as well as j/k, this caused me 30s of confusion). If you prefer "command then select" workflow, maybe the "operator mode" (see the friendly manual ) for vim motions will be of interest

Thanks for sharing your script

tom-gora commented 6 months ago

@michaelb Sorry, I was intending to reply to the op, as I came up with the solution and earlier I've seen this issue while researching. I agree the added value is small. I barely wrote this to be able to quickly trigger sniprun with leader based mappings without needing to type commands. This works for me and by all means I'm not a pro, I just wanted to help the person asking the question.