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


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: - this.editorView.scrollTop and pos.left - this.editorView.scrollLeft or something like that.

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


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?


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?


It looks like this may be relevant:


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.