Giter Site home page Giter Site logo

Comments (4)

tom-gora avatar tom-gora commented on May 24, 2024 1

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

from sniprun.

michaelb avatar michaelb commented on May 24, 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

from sniprun.

tom-gora avatar tom-gora commented on May 24, 2024

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)```

from sniprun.

michaelb avatar michaelb commented on May 24, 2024

@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

from sniprun.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.