Plugin script cursor methods

Plugins lack the ability to handle the cursor position while the user is editing a script. This makes it very difficult for plugins to dynamically alter the source without impacting on the user. It would be great to see an API for this.

One implementation could be

LuaSourceContainer Plugin.ActiveScript [read only]
The script which the user is currently editing

Plugin:OpenScript(script, int line, int column=1)
Opens the script at the following cursor position.

int, int Plugin:GetCursorPosition()
Returns the line and column number that the cursor is currently at in the active script.

Plugin:SetCursorPosition(int line, int column)
Sets the position of the cursor in the active script.

Some use cases for this may include

  • Auto-complete for short hand notation
  • Selecting an item in workspace from it’s script reference
  • Automatically appending service references when the user indexes a service name
  • Minify/Beautify a single code block
  • Toggle debug statements (such as prints or timing blocks)
44 Likes

Just came across a need for this. Developing a script macro plugin and need to return cursor to same position after Source has changed – it currently just snaps to the beginning of the line.

3 Likes

This was my main use-case. Best work around I found was to wait until they added a new line and then make the change (since their cursor would move to the start of a new line).

3 Likes

I think if this is implemented then it needs some changes to account for selections. Stuff like “Minify/Beautify a single code block” would work best if you could select the code block first, but the example API has nothing related to selections.


Thinking about selections: Why use line/column? I think it would make sense to do things with character positions from 0/1 to the length of the script. It’s simpler and supports selections better. Helper methods can be given to find what line/column a certain position is given the source of a script.

Another point: using character positions instead of line/column will make it very easy to get the section of the script referred to using string.sub or other string tools that refer to character positions.

Revised API LuaSourceContainer **Plugin.ActiveScript** [read only] The script which the user is currently editing

Plugin:OpenScript(script, int cursorStart, [int cursorEnd])
Opens the script with the given cursor.
If cursorEnd is omitted, it is as if cursorEnd is the same as cursorStart.
If cursorStart and cursorEnd are the same, then the cursor is placed at the position.
If cursorStart is before cursorEnd, then a selection is made from cursorStart to cursorEnd, with the cursor on cursorEnd.
If cursorEnd is before cursorStart, a selection is made from cursorEnd to cursorStart, with the cursor on cursorEnd (the beginning of the selection).

int cursorStart, int cursorEnd Plugin:GetCursorPosition()
Returns beginning and end of the selection. This follows the same rules as OpenScript.

Plugin:SetCursorPosition(int cursorStart, int cursorEnd)
Sets the position of the cursor in the active script. This follows the same rules as OpenScript.


Somewhere reasonable, the following could be implemented for convenience:

int line, int column :CursorPositionToLineAndColumn(string source, int cursorPosition)

int cursorPosition :LineAndColumnToCursorPosition(string source, int line, int column)

At present you can open a script with it’s line number. Therefore you can’t overload that method with your revisions hence why I specified column number. New methods would need to be implemented, but I totally agree it would be more useful your way but both have their own uses.

1 Like

I was about to create a thread about this.

I want to write a plugin to improve my workflow and provide some quality-of-life features that Studio currently does not provide. The plugin I want to write is currently impossible due to the inability to communicate with the currently open script in a non-obstructive way (setting script source resets the cursor position, and the ability to read around the cursor is impossible).

For example:

  • Color3 color preview
    • Need to be able to either read highlighted text, or read text at the cursor position
  • Automatically expand shortcuts into commonly used snippets of code
    • Need to be able to read and write text to the current cursor position
  • Automatically add / remove newlines from the bottom of the script to provide an overscroll buffer
    • Need to be able to get the cursor’s current line and column, and set the current line and column
    • Need to be able to write text to the current cursor position
10 Likes

I wrote a Color3 previewing plugin but because there is insufficient API to sync the plugin with the script editor, users must manually scroll between two different widgets to keep them in sync, or use a “go to line” feature. There is no way for me to intelligently scroll the widget to the same line the user is currently typing on.

All I would have needed for this use-case is the cursor’s line in the currently active script.

3 Likes

Was about to make this request myself and then found it was already requested nearly 4 years ago. Really frustrating to see that this is still a problem after so long.

There are still no APIs relating to the precise cursor position. The most we got is being able to set to the start of a line.

When altering a script’s .Source, the cursor resets to line 1. Because the plugin cannot tell where the cursor was (no API to get it!) it cannot set it back to where it belongs even if we did have more precise set abilities. (Although, I think that setting the .Source should not reset the editor window at all and plugins shouldn’t be the ones to deal with that.)

4 Likes

This would be really useful. It’d allow for plugins like UUID generators, text transformation, etc. Surprised this still isn’t part of the API yet too.

1 Like

Right now, this proposal does not support selection. Is that something you would find useful? I’ve been thinking about updating the proposal to include that.

2 Likes

Absolutely. Creating plugins that can act upon your current selection has countless applications and uses!

5 Likes

This has gotta be a thing. One of the plugins I’m working requires modification of the Script’s source, and whenever the source is changed it shoots the cursor to the very top of the script. This would be a pretty neat feature for anything that requires even the slightest interaction of the Script’s source - right now, without this anything that does so would make scripting godawful. Imagine working on a >10k line script and getting sent to the very top over and over and over again.

Sheesh.

Fair point. If these script editor methods were introduced, I would suggest that they be added to StudioService and then deprecate plugin.OpenScript, based on the fact that they introduced a script-related API (ActiveScript) under StudioService already.

1 Like

Seriously need this. I need to refactor some code but I don’t want to shotgun it. I’d like to be able to register a plugin action that refactors the current line.

2 Likes