Get current paragraph buffer range is not returning range over code comments


#1

I am working on some features for r-exec and 1 of them needs to get the range of the current paragraph. It mostly works correctly with: atom.workspace.getActiveTextEditor().getCurrentParagraphBufferRange(), but if there is a comment inside of the paragraph range, then it does not skip over the comment to the end of the paragraph. is this a bug in language-r with the way that it is specifying comments or is this an issue in getCurrentParagraphBufferRange()?

For example, only the first line of code would be considered the paragraph (assuming the cursor is on the first line):

print('beginning of the paragraph')
# a comment inside the paragraph
print('end of the paragraph')

thanks!

Harold


#2

So it seems that API documentation is slightly different than what is in the Atom code base:

According to that code, comments are skipped. Somewhat annoying, but I could understand the design decision. Perhaps 1 could add a flag that doesn’t ignore white space?

Also, where does 1 make suggestions for API documentation changes?

Thanks a bunch!


#3

You could simply submit a Pull Request with the documentation change in it. The API documentation is automatically generated from the code comments for anything marked Public, Essential or Extended in classes marked Public, Essential or Extended.


#4

Sorry to dig up this thread, but would a PR adding an “includeComments” flag be accepted ?


#5

Since it isn’t part of the documented Atom API, I would say that it is pretty unlikely unless the added feature is being used inside Atom core somewhere.


#6

Ok :+1:

I made a selectCurrentParagraph that also selects code comments.

It may be useful so I post it here :

selectCurrentParagraph = ->
    editor = atom.workspace.getActiveTextEditor()
    cursor = editor.getLastCursor()

    startRow = endRow = cursor.getBufferRow()

    while editor.lineTextForBufferRow(startRow)
        startRow--

    while editor.lineTextForBufferRow(endRow)
        endRow++

    editor.setSelectedBufferRange({
        start: { row: startRow + 1, column: 0 },
        end: { row: endRow, column: 0 },
    })

To get the range, this should work :

getCurrentParagraphBufferRange = ->
    editor = atom.workspace.getActiveTextEditor()
    cursor = editor.getLastCursor()

    startRow = endRow = cursor.getBufferRow()

    while editor.lineTextForBufferRow(startRow)
        startRow--

    while editor.lineTextForBufferRow(endRow)
        endRow++

    return {
        start: { row: startRow + 1, column: 0 },
        end: { row: endRow, column: 0 },
    }