monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

[Bug] 0.32.0 Regression: setModelLanguage no longer works with mime types

Open Prinzhorn opened this issue 3 years ago • 3 comments

Reproducible in vscode.dev or in VS Code Desktop?

  • [X] Not reproducible in vscode.dev or VS Code Desktop

Reproducible in the monaco editor playground?

Monaco Editor Playground Code

const editor = monaco.editor.create(document.getElementById('container'), {
	value: "function hello() {\n\talert('Hello world!');\n}",
	language: 'plaintext'
});

monaco.editor.setModelLanguage(editor.getModel(), 'text/javascript');

Note that this works and has always worked (using mime as language):

const editor = monaco.editor.create(document.getElementById('container'), {
	value: "function hello() {\n\talert('Hello world!');\n}",
	language: 'text/javascript'
});

Actual Behavior

Language does not change / syntax not highlighted

Expected Behavior

Language changes and syntax highlighting updates

Additional Context

I've wrapped Monaco in a component and use the language prop in the monaco.editor.create call. When the language prop changes I call monaco.editor.setModelLanguage(editor.getModel(), language);. This was working until 0.32.0.

Do I need to use updateOptions? I think I went with this approach because I wasn't trusting updateOptions to only do minimal work (e.g. not create a new model just because I update the language).

Prinzhorn avatar Jun 30 '22 08:06 Prinzhorn

Do I need to use updateOptions? I think I went with this approach because I wasn't trusting updateOptions to only do minimal work (e.g. not create a new model just because I update the language).

NVM, doesn't work either. In fact it seems like updateOptions with a mime type as language didn't even work before 0.32.0. So there's definitely an inconsistency here and something is broken. Maybe that's why I didn't use updateOptions back when I wrote this code.

Prinzhorn avatar Jun 30 '22 08:06 Prinzhorn

I debugged the code and verified that it does not use the language id to lookup in the mime type registry.

I cannot verify the regression though.

@alexdima did something change here?

hediet avatar Jul 19 '22 12:07 hediet

I cannot verify the regression though.

I can easily reproduce this in isolation as well, not just in my project. I followed the instructions to launch the official samples. Then I edited the browser-amd-editor to the follwing:

                        require(['vs/editor/editor.main'], function () {
                                var editor = monaco.editor.create(document.getElementById('container'), {
                                        value: ['function x() {', '\tconsole.log("Hello world!");', '}'].join('\n'),
-                                       language: 'javascript'
+                                       language: 'plaintext'
                                });
+
+                               monaco.editor.setModelLanguage(editor.getModel(), 'text/javascript');
                        });
                </script>
        </body>

There is no syntax highlighting.

Screenshot from 2022-07-22 18-26-52

Then I changed samples/package.json to

                                "file-loader": "^6.2.0",
                                "glob": "^7.2.0",
                                "html-webpack-plugin": "^5.5.0",
-                               "monaco-editor": "^0.32.1",
+                               "monaco-editor": "^0.31.1",
                                "monaco-editor-webpack-plugin": "^7.0.1",
                                "style-loader": "^3.3.1",
                                "terser-webpack-plugin": "^5.3.1",

npm i and restart npm run simpleserver

Screenshot from 2022-07-22 18-26-56

Prinzhorn avatar Jul 22 '22 16:07 Prinzhorn

Thanks for the fix! It's great to see movement again.

Prinzhorn avatar Oct 25 '22 13:10 Prinzhorn

I'm not sure how to verify- is there a way to run the latest code in the monaco playground?

roblourens avatar Oct 27 '22 17:10 roblourens

I'm not sure how to verify- is there a way to run the latest code in the monaco playground?

Unfortunately not yet, @hediet is working on a playground that would allow that. Given this is difficult to verify, I will optimistically mark it verified until our infrastructure in the editor is better.

alexdima avatar Oct 28 '22 08:10 alexdima