Hi Rahul,

Am Samstag, den 30.01.2021, 09:51 +0530 schrieb Rahul Khandelwal:
> I am trying to create a netbeans plugin for TOML file support.
> I have followed the lexer/parser tutorial on apache netbeans site and few
> other tutorials.
> I have got the basics working.
> 
> But when I edit the file, lexer seems to encounter some issues due to which
> the coloring get messed up.
> 
> Please check the below video link to take a look at the issue.
> https://github.com/Rahul-khandelwal/nb-for-toml/blob/master/example/text-color-messed-up.mov
> 
> I am also including the link to the repository.
> https://github.com/Rahul-khandelwal/nb-for-toml/

your issue is, that you don't implement the Lexer#state method. The
NetBeans language infrastructure does a partitial lexing of the file
and to do that, the state is saved with the tokens. On change the lexer
is started at the editing position and needs to be initialized with the
state at the previous token.

This is my suggestion:

    static class TokenSupplier {

        private final TomlLexer lexer;
        // tunnel editor Input to antlr
        TokenSupplier(LexerRestartInfo<TomlTokenId> lri) {
            CharStream stream = new TomlCharStream(lri.input());
            lexer = new TomlLexer(stream);
            if(lri.state() != null) {
                Object[] state = (Object[]) lri.state();
                lexer._mode = (int) state[0];
                lexer._modeStack.clear();
                lexer._modeStack.addAll((int[]) state[1]);
            }
        }

        public org.antlr.v4.runtime.Token get() {
            return lexer.nextToken();
        }

        public Object getState() {
            return new Object[] {
                lexer._mode,
                lexer._modeStack.toArray()
            };
        }
    }

com.rahul.nbfortoml.lexer.TomlEditorLexer.state() then delegates to
com.rahul.nbfortoml.lexer.TomlEditorLexer.TokenSupplier.getState().

As you can see the mode of the lexer and its modestack are saved and
restored when the supplier is created.

I think this minimal state might mess the reporting of lexer errors
(wrong lines/offsets), but you should critically check if you really
need that information - if so, the saved state needs to be expanded.

HTH

Matthias


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@netbeans.apache.org
For additional commands, e-mail: dev-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists



Reply via email to