Git Product home page Git Product logo

Comments (1)

adityastomar67 avatar adityastomar67 commented on June 3, 2024

You cannot do it with neovim options but by creating a custom function for checking the current line which is already commented is of certain length. And if it exceeds that you can split it from there and comment the next line. Then you can create a autocomand for checking the it whenever trying to coment any line.

Code can be something like this

function splitLineComment(length_limit)
  -- Get the current line
  local line_text = vim.api.nvim_get_current_line()

  -- Check if the line is a line comment
  if line_text:match('^%s*//%s*') then
    -- Check if the comment exceeds the length limit
    local comment_text = line_text:gsub('^%s*//%s*', '')
    if #comment_text > length_limit then
      -- Split the comment onto a new line
      local split_text = comment_text:sub(1, length_limit - 1)
      local remaining_text = comment_text:sub(length_limit)
      local new_line_text = '// ' .. split_text
      local next_line_number = vim.api.nvim_win_get_cursor(0)[1] + 1

      -- Insert the new line with the split comment
      vim.api.nvim_buf_set_lines(0, next_line_number - 1, next_line_number - 1, false, {new_line_text})

      -- Remove the split portion from the original line
      vim.api.nvim_set_current_line(line_text:gsub(comment_text, remaining_text))
    end
  end
end

-- Map the function to a key combination
vim.api.nvim_set_keymap('n', '<Leader>s', ':lua splitLineComment(80)<CR>', {noremap = true})

some minor changes and autocommand and you're good to go.

from nvstar.

Related Issues (4)

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.