Coder Social home page Coder Social logo

Comments (4)

JoosepAlviste avatar JoosepAlviste commented on July 18, 2024 1

Hey @RaafatTurki!

It does make sense to have this function in this plugin. I created a PR: #45 Could you try it out and see if it works correctly for your use case?

Also, if you don't mind, could you show your final solution as well? 😄

from nvim-ts-context-commentstring.

RaafatTurki avatar RaafatTurki commented on July 18, 2024 1

It worked pretty well, here's the final Comment.nvim pre_hook I ended up with

pre_hook = function(ctx)
  local comment_utils = require 'Comment.utils'
  local tsctxcs_utils = require 'ts_context_commentstring.utils'
  local tsctxcs_internal = require 'ts_context_commentstring.internal'
  
  local comment_strings = {}
  local cs_type = ctx.ctype == comment_utils.ctype.line and '__default' or '__multiline'

  local calc_cs = function (posision)
    return tsctxcs_internal.calculate_commentstring({
      key = cs_type,
      location = posision
    })
  end

  local abort_cs = function (msg)
    vim.notify(msg)
    return '%s'
  end

  -- visual commenting
  if ctx.cmotion == comment_utils.cmotion.v or ctx.cmotion == comment_utils.cmotion.V then
    table.insert(comment_strings, calc_cs(tsctxcs_utils.get_visual_start_location()))
    table.insert(comment_strings, calc_cs(tsctxcs_utils.get_visual_end_location()))
  -- block commenting
  elseif ctx.ctype == comment_utils.ctype.block then
    table.insert(comment_strings, calc_cs(tsctxcs_utils.get_cursor_location()))
  -- linewise commenting
  else
    table.insert(comment_strings, calc_cs(nil))
  end

  -- compare comment_strings and return one
  local last_cs = nil
  for i, cs in ipairs(comment_strings) do
    if i > 1 and cs ~= last_cs then
      return abort_cs('Commenting aborted due to mismatching comment strings')
    end
    last_cs = cs
  end

  return last_cs
end

One thing I'd like it to do better is not register something in the undo history when aborting but that's a Comment.nvim issue that I'll look further into (will post new snippet here when resolved).

from nvim-ts-context-commentstring.

JoosepAlviste avatar JoosepAlviste commented on July 18, 2024 1

I'm thinking that maybe it would make sense to maintain the pre_hook integration inside this plugin (or perhaps inside Comment.nvim). Then people wouldn't need to copy-paste a lot of unnecessary code into their configuration and we could maintain the integration in one of the plugins, keeping it always up-to-date, adding features, etc.

Then, the users could import it easily:

require('Comment').setup {
  pre_hook = require('ts_context_commentstring.integrations.comment_nvim').pre_hook,
}

Maybe once you're happy with your solution you could make a PR to this plugin or to Comment.nvim?

from nvim-ts-context-commentstring.

RaafatTurki avatar RaafatTurki commented on July 18, 2024

I very much appreciate the care you take in user experience!

In order for this to get implemented correctly an aborting mechanism is needed in the pre_hook (to avoid things like comment string parsing and inserting a change that does nothing in the undo history). Currently there is no such thing in Comment.nvim and its author wouldn't accept a PR to implement it so I'll look into other commenting plugins

Once I come up with a decent solution I'll PR an integration here.

from nvim-ts-context-commentstring.

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.