How to centre the scroll to a coordinate?

Aim:

To scroll an active editor to a specific coordinate by user code.


Possible solution:

Use the API command scrollToBufferPosition() as documented …
https://flight-manual.atom.io/api/v1.41.0/TextEditor/#instance-scrollToBufferPosition


Assume the following code as base:

te = atom.workspace.getActiveTextEditor();
te.scrollToBufferPosition([100,0],{center:true});

Has the option (center) been used correctly?


Testing:

Preliminary testing seems to show the centre of the active editor is not achieved.
(See picture. Result is the same even without clip)


Alternative

Trigger the built-in go-to-line:toggle command.


Questions

Please:

  • Has the API command been used correctly?
  • What is the task of the center option (if understood incorrectly)?

I have no idea what the screenshot is supposed to illustrate. I guess that buffer positions near the start and the end of the source code cannot be centered, because Atom doesn’t allow scrolling past the top/bottom. At least that’s what the “if possible” bit in documentation implies.

There are third-party packages that enable that kind of scrolling, maybe it works with them in use.

@idleberg:
Thank you for taking an interest and making a reply.
I will rework my question.

SUMMERY

atom.workspace.getActiveTextEditor().scrollToBufferPosition(position,{center:true})
  1. Is the center option coded correctly?
  2. What should we expect the center option to do?
  3. Does this center option work as expected?


DETAILED

My aim is to code a functionality for the init file.
Core to the functionality is the ability to scroll to a position,
without effecting the cursor.

A base principle is tested as part of the conceptual phase.
The code being tested in the development console is:

te = atom.workspace.getActiveTextEditor();
position = [100,0];
te.scrollToBufferPosition(position,{center:true});

Assume the code can be executed in the development console.

The focus is on line 3, the scrollToBufferPosition() function,
specifically the options object:

  1. Is the center option coded correctly?
  2. What should we expect the center option to do?
  3. Does this center option work as expected?

I expect the text is centred around position.
Actual behaviour does not seem to show any difference
with or without the option object in the function call.

I ask that someone confirms the working
of the scrollToBufferPosition() API function.

[1] google search pattern “Atom NEAR scroll and preserve cursor position”

[2] one possibility from search - scroll-with-keeping-cursor-pos

Let me rephrase my previous answer with an example (and this time I could test it in Atom):

Your editor height is set to display 50 lines of code, while your code consists of 100 lines. Now, when you call scrollToBufferPosition method with center: true, it only has an effect for lines >=25 and >=75. The editor.scrollPastEnd setting doesn’t influence this behaviour (it probably should.)

However, and this might be a bug, the line will never be centered if it’s already in the current view. So in our example, scrolling to 1-50 will not change the buffer position unless you scroll further down before calling the method.

1 Like

@idleberg, @d_l:
Thank you for your time in taking part in this conversation.

@d_l:
Scrolling to a specific position was what I aimed for.
It is a one part of a bigger functionality.

@idleberg:

I have concluded as you are saying.
Thank you for the confirmation.


Conclusion

I have evaluated the code in
https://github.com/atom/atom/blob/master/packages/go-to-line/lib/go-to-line-view.js

Answering my own questions:

  1. Is the center option coded correctly?
    yes
  2. What should we expect the center option to do?
    Centring only happens when requested point is off page
  3. Does this center option work as expected?
    See (2). The problem is that a line is barely visible and still centring action does not take place.

The cheat method would be something like:

function goRow(row) {
  var editor = atom.workspace.getActiveTextEditor();
  editor.scrollToBufferPosition([1,1]);

  setTimeout( () => {
    editor.scrollToBufferPosition([row,1],{center: true});
  }, 10);
}
  • Execute the above code in the developers console
  • Test by …
    • Opening a file
    • Executing in the console the function goRow(x) where x is a row number.

Executing below (as example) one after another, scrolls as to keep the set line numbers centred.
goRow(100)
goRow(105)
goRow(107)
goRow(120)