Why can't I get selectLine to work?


I’m trying to create a couple of commands that will modify the current line, but I can’t get past the first step of selecting that line so I can get its contents, modify them, and update them. For example, I want to create a command that will delete the first and last tags on a line (e.g., deleting HTML header tags). According to the documentation there is a command called selectLine. But when I try using it:

atom.commands.add 'atom-text-editor', 'custom:delete-tags', ->
  return unless editor = atom.workspace.getActiveTextEditor()

I get the error:

Uncaught TypeError: undefined is not a function

It sounds like it’s saying the function doesn’t exist; am I misreading the documentation?

In contrast, I don’t have any trouble using getLastSelection from the sample command in the documentation. This works just fine:

atom.commands.add 'atom-text-editor', 'markdown:paste-as-link', ->
  return unless editor = atom.workspace.getActiveTextEditor()
  selection = editor.getLastSelection()
  clipboardText = atom.clipboard.read()

Any idea what I’m doing wrong?

Also, is there a better way to do what I want than selecting the line?


I don’t see a TextEditor.selectLine() function:

There is, however, a TextEditor.selectLinesContainingCursors() function:


That works! Thanks, @leedohm.

Looked at my notes, and it turns out the documentation I was looking at was for Selection:


Selects an entire line in the buffer.
Argument	Description
row	        The line Number to select (default: the row of the cursor). 

Sounds like part of the problem was that I don’t yet understand Atom’s object model, so I didn’t grok the line at the top of the Selection documentation that says it “represents the selection in the TextEditor.” Out of curiosity, how would you end up with a Selection object that could call selectLine (not that I need to, I just want to understand what’s going on)? I went through a bunch of the text editor selection methods, and none of them seem to return a Selection. Is Selection an internal class that we aren’t expected to call directly?


There’s really no need to create a Selection object because typically if you need something selected you call TextEditor.setSelectedBufferRange() or its ilk:

Even when working with Decorations, you end up marking a buffer range and then decorating that.

This is why Selection is in the “Extended Classes” section, because it is less common to much less common to use those classes, whereas the “Essential Classes” are used very commonly.


Thanks so much, @leedohm!