Option to insert comment characters at the beginning of the line, rather than just before the first character?


#1

I when I hit Cmd-/, it inserts the comment character immediately before the first character I have on that line, irrespective of the indentation level.

Instead, I would very much like it to insert the character at the beginning of the line. I use hard tabs, and if it did this, all the indentation would be maintained when commenting.

Is this possible?


#2

Hello.

Perhaps some code will be required? How about the following Coffeescript…

# ****************************************************
#  TOGGLE BETWEEN ADD AND REMOVE OF A COMMENT STRING.
# ****************************************************

atom.commands.add 'atom-text-editor', 'custom:doComment', ->
#   - The original default function set the comment string before the first non-space character in the line.
#   - This module places a comment string at the beginning of the line.
#   - The comment string used is what Atom is preset to use from the code grammar.
#   - Selected text is kept selected and cursor position stay is relative same position.
#   - Empty lines at start and end of selection can be ignored if an internal option is TRUE.
#   - {!!} The action can be undone with a single "undo" command.
#   - The code can be copied into "init.coffee"
#
#  ::SUGGESTION FOR KEY-BINDING "keymap.cson"::
# 'atom-text-editor':
#   'ctrl-/': 'custom:doComment'

  # OPTIONS
  _optionIgnoreEmpty = false  # Do not work with empty lines at front and back of selected range

  if _editor = atom.workspace.getActiveTextEditor()
    # Mark start position for 'undo'
    _checkpoint = _editor.createCheckpoint()

    # Cursor position and selected range
    _origin = _editor.getCursorScreenPosition()
    _selectRange = _editor.getSelectedBufferRange()

    # Toggle to ADD or REMOVE comment for line
    if _editor.isBufferRowCommented(_origin.row)
      # ............REMOVE -> Standard comment line function to remove comment
      _editor.toggleLineCommentsInSelection()
      console.log ("Comment lines removed.")

    else
      # ............ADD -> Custom way to add a comment sign.. at start of the line
      # console.log(_origin, _selectRange.start, _selectDirection) # debug
      _lineStart = _selectRange.start.row;
      _lineEnd = _selectRange.end.row;

      # Get characters used for comment; Change position markers
      _prefix = _editor.getCommentStrings(_editor.getCursorScope()).commentStartString
      _length = _prefix.length
      _origin.column += _length
      _selectRange.start.column += _length
      _selectRange.end.column += _length

      # Ignore empty lines at start and end of selection
      if _optionIgnoreEmpty
        _start = _lineStart
        for i in [_start.._lineEnd] by 1
          if _editor.lineTextForBufferRow(i) != ""
            break
          _lineStart += 1

        _start = _lineEnd
        for i in [_start.._lineStart] by -1
          if _editor.lineTextForBufferRow(i) != ""
            break
          _lineEnd -= 1

      # Add comment string at the start of the lines that was selected
      for i in [_lineStart.._lineEnd] by 1
        _editor.setCursorBufferPosition([i, 0])
        _editor.insertText(_prefix)

      _editor.setCursorBufferPosition(_origin)
      _editor.setSelectedBufferRange(_selectRange)
      console.log ("Comment lines added =  #{_lineEnd - _lineStart + 1}")

      # Mark end position for 'undo'
      _editor.groupChangesSinceCheckpoint(_checkpoint)

  else
    # NO ACTION POSSIBLE
    console.log ("Comment lines added = 0")

This segment can be added into init.coffee and keybinding to be done in "keymap.cson as suggested in the commentary of the code.

Commentary is welcome… please just be gentle with me
- this is my first try for a full code module for Atom. :wink:

Regards.


[SOLVED] How to make a function call in "init.coffee"?
#3

Duuuude, that is fantastic. Thanks so much! I haven’t had the time to learn how to hack on Atom at all yet, so I wouldn’t even know where to start. So, I can’t really comment on the code, but it is working beautifully for me :grinning:

Thanks again! This little nit drives me nuts.


#4

Hi.

Nice to have your feedback. :smile: It was a bit of fun to hack this… and it is actually some function that I wanted too.

There is a flaw in the program though :frowning:
Removal of comments only happens if ALL the lines has comments - in the original functionality. The code I shared does not test for this condition. Mine only test for the selected line having a comment.

So it is a big :bug: that I intend looking at later… but hopefully you can enjoy its use until the bug is fixed.

Cheers.
-Dan Padric