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

How to run the entire current file? #276

Closed
Wangch29 opened this issue Feb 9, 2024 · 4 comments
Closed

How to run the entire current file? #276

Wangch29 opened this issue Feb 9, 2024 · 4 comments

Comments

@Wangch29
Copy link

Wangch29 commented Feb 9, 2024

Besides of %SnipRun

@michaelb
Copy link
Owner

michaelb commented Feb 9, 2024

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
Copy link

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
Copy link
Owner

@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
Copy link

@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.

@michaelb michaelb closed this as completed Mar 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants