Get cursor's pixel position relatively to editor's viewport


#1

Hi! In my package I want to show custom tooltip over last cursor. I need to get exact coordinates of current cursor. I tried to use following code:

if (this.editor = atom.workspace.getActiveTextEditor()) {
      this.editorView = atom.views.getView(this.editor);
      this.cursor = this.editor.getLastCursor();
      const pos = this.editorView.pixelPositionForBufferPosition(this.cursor.getBufferPosition());
      // pos returns { top: number, left: number }
}

But pixelPositionForBufferPosition() returns position without editor’s scroll position. In other words, for lines that are not in start viewport (scrollTop = 0) it returns wrong coordinates. Proper result should look like that: pos.top - this.editorView.scrollTop and pos.left - this.editorView.scrollLeft or something like that.

How can I get cursor position in pixel, relatively to viewport.


#2

I didn’t know that there was a pixelPositionForBufferPosition() method and I can’t find anything about it in the API. Where did you find it?


#3

For example here - Get DOM element under cursor. Is there any other way to get cursor position and show custom tooltip using top and left properties?


#4

It looks like this may be relevant: https://github.com/atom/atom/issues/13541#issuecomment-272032035


#5

Unfortunately it’s not what I was looking for. I found getScrollTop() and getScrollLeft() which resolves my problem. Those methods need to be used on Editor’s View instance.

I think that topic can be resolved.