-
Notifications
You must be signed in to change notification settings - Fork 28.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Monaco] Add monaco.editor.registerEditorOpener
method to be able to intercept editor open operations
#177064
Conversation
956c4f1
to
9216747
Compare
@hediet Could this be considered for one of the next Monaco versions? Thank you! |
@@ -456,6 +459,36 @@ export function registerLinkOpener(opener: ILinkOpener): IDisposable { | |||
}); | |||
} | |||
|
|||
export interface ICodeEditorOpener { | |||
openCodeEditor(source: ICodeEditor, resource: URI, selectionOrPosition?: IRange | IPosition): boolean | Promise<boolean>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some doc comments explaining when this method is called (copying the comment from the register call).
…ept editor open operations
Addressed review and rebased onto main. |
Thanks!
Generally, please don't rebase in PRs, as that resets the review. |
This seems stuck, do I need to do something? |
Fixes microsoft/monaco-editor#2000
By default the "go to defintion" action in the Monaco editor does nothing if the definition is in another model, or opens a peek window when multiple definitions are found. This default is a good thing because just changing the underlying model would be very detrimental when embedding the editor. However, there are use-cases where you maybe want to control what happens when you go to a definition. For example, when we embed the Monaco editor we would like to open a new tab to the source code containing the definition for certain models but not all of them.
This PR adds a new function
monaco.editor.registerEditorOpener
that lets the embedder intercept such open operations and handle those themselves. By using a boolean return value the embedder can indicate if the resource was handled or the editor should fallback to the default. The function returns anIDisposable
instance that can be used to unregister the handler again. This interception can be tested in the code below by e.g. Ctrl-clicking on thealert
function. By intercepting the open operation you can also choose to change the underlying editor model yourself if that makes sense in your application (see linked issue).Edit: I decided to remove a second commit because it didn't work properly in some cases. This reduces the PR to just the new method to intercept the editor open operation.
Monaco Playground Code: