-
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
A guide for forking and extending monaco-typescript is needed, OR options to add code wrapper #1661
Comments
For my game editor ct.js, I ended up with tons of hacks that establish hidden and uneditable lines around the code: https://github.com/ct-js/ct-js/blob/develop/src/js/codeEditorHelpers.js#L74 The hacks block direct editing from a keyboard, manage cursors and selectors, handle "Replace all" command, and hide the first and the last lines. Some constants are hard-coded, so you will need to improve the code for multiple hidden numbers. The known issue is that you can view and delete the hidden code in the peek view, as it creates additional editors. |
This might be easier to do with microsoft/monaco-typescript#65 |
I have the same issue (I'd like my editor to have a "function body" context with predefined arguments and an expected return type), was there any improvements in this area? |
@guillaume86 Yes, there have been improvements if you don't require to be able to see that function stub in the editor. Building off of https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-configure-javascript-defaults you can set to ignore the diagnostic that reports top-level return statements as an error and then just provide the declaration for your function parameters as an extra lib to get code completion and types for them. The diagnostic code to ignore can be inferred from the error message in the editor. monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
noSemanticValidation: false,
noSyntaxValidation: false,
diagnosticCodesToIgnore: [/* top-level return */ 1108]
});
monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
target: monaco.languages.typescript.ScriptTarget.ESNext,
allowNonTsExtensions: true
});
var libSource = `
/** The first parameter */
declare const param1: number;
/** The second parameter */
declare const param2: number;
`
var libUri = 'signature.d.ts';
monaco.languages.typescript.javascriptDefaults.addExtraLib(libSource, libUri);
// When resolving definitions and references, the editor will try to use created models.
// Creating a model for the library allows "peek definition/references" commands to work with the library.
monaco.editor.createModel(libSource, 'typescript', monaco.Uri.parse(libUri));
monaco.editor.create(document.getElementById('container'), {
value: "return param1 + param2",
language: 'javascript'
}); |
Sadly this does nothing about the passed arguments, or expected types, or of what |
Solution: use https://www.npmjs.com/package/constrained-editor-plugin to define the function body and combine it with editor.setHiddenAreas([{
startLineNumber: 1,
endLineNumber: 1
}, {
...
}]) (You don't need to track those hidden areas afterwards.) To shift the line number count, https://microsoft.github.io/monaco-editor/docs.html#interfaces/editor.IEditorOptions.html#lineNumbers The method setHiddenAreas does exist on IStandaloneCodeEditor despite errors being shown. Might make a demo later when I get my hands on updating my editor's code. Quacking congratulations for leaving this and many linked issues open for all those years. And thanks to @Pranomvignesh for making a solution for uneditable ranges. |
When formatting, the code disappears and you can see its hidden code. |
Closely related: #1426
monaco-editor version: 0.18.1
So there is a good amount of issues that require more peculiar additions to
monaco-typescript
, mainly wrapping code in a function, in a JS object, for changing typing context, and for injected, user-defined code. In these cases,monaco.languages.typescript.*Defaults.addExtraLib
is not enough, as seen in #1452, #45, #1439, #170.Particular use cases include:
this
does not point toglobalThis
, e.g. in game editors for in-game entities, HTTP request editors, for any other code that comes from an end-user of a framework;As an end-user, it seems impossible to make such changes to monaco-typescript (due to not having required competence in monaco/vscode/typescript development). The expected result is that a version of monaco-vscode wraps input code in another code, returning correct positions for markers and such, and successfully embeds into monaco-editor while preserving syntax highlighting (this is pointed out separately as monarch tokenizer is not a part of monaco-typescript, and bringing the tokenizer to a new language mode is not obvious).
All the issues above surely have a solution, but end-users require docs to solve them.
Alternative: adding options for
monaco.languages.typescript.*Defaults
that will allow wrapping input code in a predefined structure.The text was updated successfully, but these errors were encountered: