Skip to content
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

Browser freezes when highlighting a lot of compressed code #27

Open
keaiwkaw opened this issue Sep 13, 2023 · 5 comments
Open

Browser freezes when highlighting a lot of compressed code #27

keaiwkaw opened this issue Sep 13, 2023 · 5 comments

Comments

@keaiwkaw
Copy link

The browser crashed while I was writing 60k compressed js code into the monaco-editor. When I commented out the highlighted logic, the compressed code appeared normally

@xueerli
Copy link

xueerli commented Apr 11, 2024

@keaiwkaw Hi, have you solved this problem?

@kachurun
Copy link

kachurun commented Sep 13, 2024

@xueerli
@keaiwkaw

I solved it by patching method wireTmGrammars from here.
I just don't allow the tokenizer to tokenize lines that are longer than 300 symbols.

Easy peasy:

/**
 * Wires up monaco-editor with monaco-textmate
 */
export function wireTmGrammars(monaco: MonacoEditor, registry: Registry, languages: Map<string, string>, editor?: MonacoEditorInstance) {
    const maxLineLength = 300;

    return Promise.all(
        Array.from(languages.keys())
            .map(async(languageId) => {
                const grammar = await registry.loadGrammar(languages.get(languageId));
                monaco.languages.setTokensProvider(languageId, {
                    getInitialState: () => new TokenizerState(INITIAL),
                    tokenize: (line: string, state: TokenizerState) => {
                        // Return plaintext for long lines
                        if (line.length > maxLineLength) {
                            return {
                                endState: state,
                                tokens: [{ startIndex: 0, scopes: 'plaintext' }]
                            };
                        }

                        const res = grammar.tokenizeLine(line, state.ruleStack);

                        editor ??= monaco.editor.getEditors()[0] as MonacoEditorInstance;

                        return {
                            endState: new TokenizerState(res.ruleStack),
                            tokens: res.tokens.map(token => ({
                                ...token,
                                scopes: editor ? TMToMonacoToken(editor, token.scopes) : token.scopes.at(-1)
                            })),
                        };
                    }
                });
            })
    );
}

@XiayidanAlimu
Copy link

@xueerli @keaiwkaw

I solved it by patching method wireTmGrammars from here. I just don't allow the tokenizer to tokenize lines that are longer than 300 symbols.

Easy peasy:

/**
 * Wires up monaco-editor with monaco-textmate
 */
export function wireTmGrammars(monaco: MonacoEditor, registry: Registry, languages: Map<string, string>, editor?: MonacoEditorInstance) {
    const maxLineLength = 300;

    return Promise.all(
        Array.from(languages.keys())
            .map(async(languageId) => {
                const grammar = await registry.loadGrammar(languages.get(languageId));
                monaco.languages.setTokensProvider(languageId, {
                    getInitialState: () => new TokenizerState(INITIAL),
                    tokenize: (line: string, state: TokenizerState) => {
                        // Return plaintext for long lines
                        if (line.length > maxLineLength) {
                            return {
                                endState: state,
                                tokens: [{ startIndex: 0, scopes: 'plaintext' }]
                            };
                        }

                        const res = grammar.tokenizeLine(line, state.ruleStack);

                        editor ??= monaco.editor.getEditors()[0] as MonacoEditorInstance;

                        return {
                            endState: new TokenizerState(res.ruleStack),
                            tokens: res.tokens.map(token => ({
                                ...token,
                                scopes: editor ? TMToMonacoToken(editor, token.scopes) : token.scopes.at(-1)
                            })),
                        };
                    }
                });
            })
    );
}

I am inspired by your solution, but I don't know where should I wrote this code in my project, as the function wireTmGrammars that you overwrite is in monaco-editor-textmate repository. can you help me ?

@kachurun
Copy link

@XiayidanAlimu This repository contains only two files. Just add it to your project and patch the function, or use patch-package for this.

@XiayidanAlimu
Copy link

Thank you for your kindly response. patch-package is a useful tool, which I haven't used before. At present, I have encountered some dependencies issues with my project folder node_modules, but I am hopefully expecting that your solution could work. have a nice day

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants