-
-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
418 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { StateStackImpl } from '../vendor/vscode-textmate/src/grammar' | ||
import { ShikiError } from './error' | ||
import type { StateStack } from './textmate' | ||
|
||
/** | ||
* GrammarState is a special reference object that holds the state of a grammar. | ||
* | ||
* It's used to highlight code snippets that are part of the target language. | ||
*/ | ||
export class GrammarState { | ||
constructor( | ||
private _stack: StateStack, | ||
public lang: string, | ||
public theme: string, | ||
) {} | ||
|
||
get scopes() { | ||
return getScopes(this._stack as StateStackImpl) | ||
} | ||
|
||
toJSON() { | ||
return { | ||
lang: this.lang, | ||
theme: this.theme, | ||
scopes: this.scopes, | ||
} | ||
} | ||
} | ||
|
||
function getScopes(stack: StateStackImpl) { | ||
const scopes: string[] = [] | ||
const visited = new Set<StateStackImpl>() | ||
|
||
function pushScope(stack: StateStackImpl) { | ||
if (visited.has(stack)) | ||
return | ||
visited.add(stack) | ||
const name = stack?.nameScopesList?.scopeName | ||
if (name) | ||
scopes.push(name) | ||
if (stack.parent) | ||
pushScope(stack.parent) | ||
} | ||
|
||
pushScope(stack) | ||
return scopes | ||
} | ||
|
||
export function getGrammarStack(state: GrammarState) { | ||
if (!(state instanceof GrammarState)) | ||
throw new ShikiError('Invalid grammar state') | ||
// @ts-expect-error _stack is private | ||
return state._stack | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import type { | ||
IGrammar as Grammar, | ||
IGrammar, | ||
IRawGrammar as RawGrammar, | ||
IRawTheme as RawTheme, | ||
IRawThemeSetting as RawThemeSetting, | ||
} from '../textmate' | ||
|
||
export type { | ||
Grammar, | ||
RawGrammar, | ||
RawTheme, | ||
RawThemeSetting, | ||
} | ||
|
||
export interface Grammar extends IGrammar { | ||
name: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.