-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Goto definition (Ctrl + click) works only if definition is in same model #2000
Comments
I think it would not be a good idea to implement this feature generally; or at least it should be configurable to keep the current behavior. I would assume most use cases use a single editable model (the one that's shown), accompanied by zero or more additional models. For example: We add several models to the editor (all .js, or .d.ts files) to provide code completion for complex scenarios, but would never ever ever want a user to navigate there in the same editor instance by invoking "Go to definition". Because that would mean we are now in a state that is absolutely meaningless to the user, and additionally there is no way for the user to get back to the old model. However, I see a need to provide functionality like this - even in our use case. We would like for the user to "Go to definition" for specific models. Not in the same editor instance, but rather in a new tab. Today we accomplish that by hijacking the Playground code: monaco.editor.createModel("const foo = 1;", "javascript", monaco.Uri.file("foo.js"));
const editor = monaco.editor.create(document.getElementById("container"), {
value: `function hello() {
alert('Hello world!');
}
hello();
foo`,
language: "javascript"
});
const editorService = editor._codeEditorService;
const openEditorBase = editorService.openCodeEditor.bind(editorService);
editorService.openCodeEditor = async (input, source) => {
const result = await openEditorBase(input, source);
if (result === null) {
alert("intercepted")
console.log("Open definition for:", input);
console.log("Corresponding model:", monaco.editor.getModel(input.resource));
}
return result; // always return the base result
}; By adjusting the code a bit I would assume you could use the same technique to open the found editor model in the same editor instance (the The downside is that this is not an official API. So the question is could there be an official API endpoint that enables registering some kind of provider/handler that kicks in when the standard implementation does not find a model (i.e. the Thoughts? |
Dear Mr. Pahnke,
So an official API endpoint that enables registering some kind of provider/handler that kicks in when the standard implementation does not find a model would be a good solution. |
@alexdima can we have a Single(default) and Multifile option on the editor options to mitigate this issue? This way the editor will stay compatible with current use cases but also widely open the doors to VSCode-like browser editors, which support multiple files. |
How to switch to Python language, it seems that it can't be used |
Hello @spahnke , Has the implementation of the editor changed recently in a way that your solution above stopped working? I even get an error running it in the playground. Any help is much appreciated. Kind Regards, |
I believe since version 0.35.x the Monaco project started to minify/mangle every internal API, so that it has become impossible to hook into any internal objects/methods. I don't have a way forward myself yet 😕 |
Thank you for your help. I was able to get it working using your example. The following threads also helped a lot: #2407 #291 (comment) |
This will hopefully be fixed soon for the monaco editor. |
monaco-editor version: 0.20.0
Browser: Chrome
OS: Windows
Reproduction steps:
Go to playground
Paste code below
Run
ctrl + MouseOver on IdentifierA in line 1
=> Identifier A is blue and underlined; small widget pops up showing reference "another IdentifierA" => OK!
ctrl left click on IdentifierA in line 1
=> IdentifierA in line 3 blinks and Cursor jumps to line 3 => OK!
ctrl + MouseOver on IdentifierB in line 2
=> Identifier B is underlined; small widget pops up showing reference "another IdentifierB" => OK!
ctrl left click on IdentifierB in line 2
=> Nothing happens. Editor should display model2 and Curser should jump to line 1 => Bug!
I was able to fix this bug:
a) insert
editor.setModel(model);
in standaloneCodeServiceImpl.ts after line 43
b) replace
return null;
whith
return monaco.editor.getModels().find(model => model.uri.toString() === resource.toString());
in Method
StandaloneCodeEditorServiceImpl.findModel
Instead of b) it would certainly be better to use
SimpleEditorModelResolverService
, but i don't know how to get a reference of it so i better refrain from making a pull request on base of my amateur work.By the way:
I'm writing an online-IDE with compiler for my students. Without monaco editor this would not be possible:
Monaco editor is an awesome library, thank you!
Editor Playground html:
Editor Playground css:
none
Editor Playground js:
The text was updated successfully, but these errors were encountered: