How to scroll to cursor position in embedded texteditorview


#1

I have a ScrollView which contains a TextEditorView as a subview, as follows:

@content: ->
  # ...
  @subview 'editorView', new TextEditorView

I want to have the view scroll to the cursor position when the cursor within the texteditorview goes off the edge of the scrollview. I have tried subscribing to the onDidChangeCursorBufferPosition of the texteditorview, as follows:

@editorView.getModel().onDidChangeCursorBufferPosition() (cursorEvent) =>
  # ...
  atom.workspaceView.trigger 'editor:scroll-to-cursor' # doesn't work
  @editorView.getModel().scrollToCursorPosition() # doesn't work
  @editorView.element.scrollToCursorPosition() # undefined

Since calling 'editor:scroll-to-cursor' manually from the command palette does what I want, I thought this would work, even if it is a bit of a hack. Didn’t scroll, and neither did the TextEditor class’s scrollToCursorPosition. I grepped through the source and found in text-editor-element.coffee:

text-editor-element.coffee:297:  'editor:scroll-to-cursor': -> @scrollToCursorPosition()

But as shown, the text editor element does not appear to have a scrollToCursorPosition() method defined.

What is next? How might I get the cursor to stay in view in the scrollview? I’m somewhat stumped. code here, if interested.


Scrolling the text editor in bottom pane and getting line height
#2

I’m running into a similar problem. I create an atom-text-editor element like this:

class MyView
  constructor: ->
    @item = document.createElement 'div'
    editorView = document.createElement 'atom-text-editor'
    @item.appendChild editorView
    @editor = editorView.getModel()

  someMethod: ->
    @editor.scrollToScreenPosition(...)

and then put the MyView instance in a bottom panel, but the editor just won’t scroll, no matter what I try. I found out that the .editor-contents--private element somehow gets its height set to something greater than the height of the editor, might be related. You can find the code I use here.


#3

Ok I was able to solve this. The problem was indeed that .editor-contents--private's height messed things up. But when I call @editor.displayBuffer.setHeight(editorView.clientHeight), the height gets the correct value, and programmatically scrolling works again :smiley:. I’ll post an issue about this over at the atom repo.


#4

My previous solution still failed sometimes. I just found a solution that works always: set position: absolute; on the atom-text-editor element. Under the hood, Atom handles the text editor height differently when it is absolutely positioned.