How to get visible lines numbers in Atom editor


#1

Hi,

I created a package for Atom and in it I wanted to get which lines of a given text editor are visible. For example if I open an editor at first line, I should lines 1 to 44 depending on the screen resolution. If I scroll down I should see the lines 10 to 54 (also depending on the screen resolution.

Previously I used the properties: editor.presenter.startRow; and editor.presenter.endRow; from the TextEditor, but now presenter no longer exists (it is undefined). Is there another way that I can do this?

My goal here is to get which lines are currently visible and scroll down if I am editing a line that is outside the visible lines.

Here is the actual code:

        /*
	 * Sets the cursor of a text editor to a given range and scrolls to it, if necessary.
	 * Returns the position to which the text was scrolled to, or Point (-1, -1) if no scrolling was done.
	 */
	scrollTextEditor(editor, range)
	{
		// Adjustment of visible rows in the editor, to make sure that we will scroll
		// the editor when towards the end the of the visible page. This constant is also
		// used to offset the scroll range, so we don't 'exactly' center the page on the next
		// '- Untraslated Subtitle -', but we scroll it a bit upwards.
		const visibleRowAdjustment = 8;

		// Get visible rows.
		let startRow = editor.presenter.startRow;
		let endRow = editor.presenter.endRow - visibleRowAdjustment;
		let scrollPosition = new Point(-1, -1);

		// Move the cursor to the replaced text.
		// Note: don't use autoscroll, because it doesn't center the text editor.
		editor.setCursorBufferPosition(range.start, { autoscroll: false });

		// If the range is not visible, scroll the editor to it and center it.
		if (!(range.start.row >= startRow && range.end.row < endRow))
		{
			scrollPosition = new Point(range.start.row + visibleRowAdjustment, 0)
			editor.scrollToBufferPosition(scrollPosition, { center: true });
		}

		return scrollPosition;
	},

#2

Hello.

[No solution just continuing conversation]

Looking into this question (as a beginner), I started in the developer console with:

_editor = atom.workspace.getActiveTextEditor()

Then I start typing in _editor. and have the auto-complete bring up a list. Interesting results in the field of _editor.getScroll... with a nice result when using _editor.getScrollTopRow().

However a not-so-nice result when doing the following:

→  _editor.getScrollBottom
←  function () {
              Grim.deprecate("This is now a view method. Call TextEditorElement::getScrollBottom instead.");
              return this.getElement().getScrollBottom();
            }

That means this method is depreciated, but getScrollTopRow did not have such issue?!

Furthermore _editor.getScrollBottom() (with parenthesis) delivers a character count (unknown: maybe pixel) number… ouch!

The non depreciated method seems to be with the _editor.element. to start with. The most helpful I have found is something like _editor.element.getVisibleRowRange() - but again the character (unknown) numbers are given.

Observations:

  1. Using “scroll” as a search word in the API descriptions, delivers nothing.
    Yet there is something available:
    https://atom.io/docs/api/v1.19.5/TextEditor#instance-scrollToCursorPosition

  2. The API description in https://atom.io/docs/api/... is not clear on how to use and get hold of “TextEditorElement”.

  3. I have not found mention of getScrollTopRow, so that might not be suited for public use.
    https://atom.io/docs/api/search/v1.19.5?utf8=✓&q=getScrollTopRow

  4. When working with scroll actions and what is visible and not - be careful one what is folded. Folded code seem still to be referenced (unsure opinion) when using the screen buffer for example.

So… I am left curious also on how to get the result you asked about.

Regards.


#3

Gear change…
please evaluate the following segment:

_editor = atom.workspace.getActiveTextEditor();
_seeRows = _editor.getRowsPerPage();
_moveRows = 5 * Math.ceil(_seeRows/6);
_editor.pageDown(_moveRows);

One problem with the above code… the page down movement is measured to the cursor position. That needs to be factored in.


#4

FINALLY!!
To the heart of your question try:

editor.element.getFirstVisibleScreenRow()  // editor.presenter.startRow
editor.element.getLastVisibleScreenRow() // editor.presenter.endRow

Do be careful of the folded lines.


#5

I will warn that both those methods are not part of the public API and hence may change/disappear without warning (just as .presenter did).

However, if you think those methods deserve to be part of the public API, feel free to create an issue, or even a PR documenting those methods, on the atom/atom repository.

Edit: Ooh, it’s my cakeday today! Had no idea :slight_smile:


#6

OFF TOPIC

@Wliu: You have been on this forum since 2014 - congratulations!
Many happy returns!

It is most pleasurable to have you come
and contribute to this topic. (sarcasm := void)
Thank you, I welcome your insight. (mood := sincere)

Sadly your answer is at the same time a source a frustration to me.
I cannot speak to @komyg; and do not pretend to.

I am the “new regular” visitor. I have no clue as to what to suggest to add to the public API. Currently I do not know how to use the current documented API effectively. In part, I blame the lack of usage examples in the current API description. It feels like a club to which I am not invited.

A public API does not mean anything if the public (read: less experienced) finds it difficult to use understand. We are not all web designers.

Take the message for example -
"This is now a view method. Call TextEditorElement::getScrollBottom instead.".
What does that mean? Is a “view method” outside the public API?

The above is not aimed to ‘flame’ anyone. Just voicing my individual opinions.
Now my frustration is placed on hold.




ON TOPIC

@Wliu: You or any experienced member:
Ignore what was replied before - please focus on being helpful to the original question. What constructive suggestion(s) can be made to @komyg? Suggestions using the public Atom API would be the most helpful.

I thank you for your consideration.
- Dan Padric


#7

You raise very good points @danPadric. Now that you’re highlighting some examples, it does seem like the API docs expect you to have a deep understanding of Atom (I don’t know what the “view method” is referring to either). I think it means that the method was moved to TextEditorElement, but again, you’re very correct that it’s confusing. I’ve forwarded your feedback to some of the other team members.

I have minimal experience with package development, but at a quick glance I was not able to find any public API (or method combinations) that allows you to find the visible screen rows. Maybe someone with more experience than myself can confirm or refute that.


#8

Hi @danPadric, this was exactly what I needed!

editor.element.getFirstVisibleScreenRow() // editor.presenter.startRow
editor.element.getLastVisibleScreenRow() // editor.presenter.endRow

It worked perfectly.

Thanks for the help!


#9

PS: I will open an issue on the Atom repository to request this in the public API.

And happy birthday @Wliu! :slight_smile:


#10

Hi Felipe.

Thank you for the feedback. Good to hear you have what you need.

Greetings.


#11

@Wliu: Thank you for digging into this case a little deeper.

It would be real nice if this case have some supply some positive momentum
relating to the API as a whole. ++especially the documentation thereof.

Until next time - cheers.


#12

UPDATE: I opened an issue to include the TextEditor.element in the public API: https://github.com/atom/atom/issues/15559.