shikijs / shiki

A beautiful yet powerful syntax highlighter
http://shiki.style/
MIT License
9.93k stars 362 forks source link

Return the grammar state after tokenizing #715

Open golok727 opened 3 months ago

golok727 commented 3 months ago

Clear and concise description of the problem

codeToTokens to return the grammar state after tokenization.

Suggested solution

This could be beneficial for tokenization in the editor I'm currently developing

Alternative

No response

Additional context

No response

Validations

Contributes

antfu commented 3 months ago

What would be the API looks like in your mind?

golok727 commented 3 months ago

What would be the API looks like in your mind?

Including the GrammarState in the TokensResult would be ideal.

Sample usage

class EditorTokensProvider {
// ...
    tokenizeLine(line, state?: GrammarState) {
         const {tokens, grammarState} = highligher.codeToTokens(code, {theme, grammarState: state, lang))

         return {tokens: tokens[0], endState: grammarState }
    }
//...
}

Additionally, could you provide a default static method in the GrammarState to get an initial state?

class GrammarState 
{
 static default(lang, theme) 
{
    return new GrammarState(INITIAL, lang, theme)
}
} 

Thank you for this awesome project and for your time!

antfu commented 2 months ago

That would be a breaking change to the function unfortunately. We would need to find another non-breaking API for that