From 791a3c3f881c7ab69db1c71e159b9b454a6b0dd2 Mon Sep 17 00:00:00 2001 From: RedCMD Date: Mon, 6 May 2024 22:15:00 +1200 Subject: [PATCH] Support `injectTo` #10 --- src/TextMate.ts | 101 ++++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 37 deletions(-) diff --git a/src/TextMate.ts b/src/TextMate.ts index 116ff24..c9014c8 100644 --- a/src/TextMate.ts +++ b/src/TextMate.ts @@ -19,16 +19,16 @@ type ScopeName = string; type ScopeStack = { readonly parent: ScopeStack | null, - readonly scopeName: ScopeName -} + readonly scopeName: ScopeName; +}; type EncodedTokenAttributes = number; type AttributedScopeStack = { readonly parent: AttributedScopeStack | null, readonly scopePath: ScopeStack, - readonly tokenAttributes: EncodedTokenAttributes -} + readonly tokenAttributes: EncodedTokenAttributes; +}; export type StateStackImpl = vscodeTextmate.StateStack & { readonly _stackElementBrand: void; @@ -81,8 +81,8 @@ export type StateStackImpl = vscodeTextmate.StateStack & { * The list of scopes containing the "contentName" (besides "name") for this state. * This list **must** contain as an element `scopeName`. */ - readonly contentNameScopesList: AttributedScopeStack | null -} + readonly contentNameScopesList: AttributedScopeStack | null; +}; interface IRegExpSourceAnchorCache { readonly A0_G0: string; @@ -98,28 +98,28 @@ type RegExpSource = { readonly hasAnchor: boolean; readonly hasBackReferences: boolean; readonly _anchorCache: IRegExpSourceAnchorCache | null; -} +}; type CaptureRule = Rule & { readonly retokenizeCapturedWithRuleId: RuleId | 0; -} +}; type IRegExpSourceListAnchorCache = { readonly A0_G0: CompiledRule | null, readonly A0_G1: CompiledRule | null, readonly A1_G0: CompiledRule | null, readonly A1_G1: CompiledRule | null, -} +}; export type CompiledRule = { readonly scanner: vscodeOniguruma.OnigScanner; -} +}; export type RegExpSourceList = { readonly _items: RegExpSource[], readonly _hasAnchors: boolean, readonly _cached: CompiledRule | null, readonly _anchorCache: IRegExpSourceListAnchorCache, -} +}; export type Rule = { readonly $location: undefined; @@ -149,7 +149,7 @@ export type Rule = { readonly _while: RegExpSource, readonly whileHasBackReferences: boolean, _cachedCompiledWhilePatterns: RegExpSourceList | null, -} +}; interface IOnigCaptureIndex { readonly start: number; @@ -161,19 +161,19 @@ export type IMatchResult = { readonly matchedRuleId: RuleId | typeof endRuleId; readonly time: number; readonly anchorPosition: number; -} +}; type Matcher = { (matcherInput: T): boolean; -} +}; type BalancedBracketSelectors = { readonly balancedBracketScopes: Matcher[]; readonly unbalancedBracketScopes: Matcher[]; readonly allowAny: boolean; -} +}; type IGrammarRepository = { lookup(scopeName: ScopeName): vscodeTextmate.IRawGrammar | undefined; injections(scopeName: ScopeName): ScopeName[]; -} +}; const enum FontStyle { NotSet = -1, None = 0, @@ -186,12 +186,12 @@ type OrMask = number; type StyleAttributes = { readonly fontStyle: OrMask, readonly foregroundId: number, - readonly backgroundId: number -} + readonly backgroundId: number; +}; type IThemeProvider = { themeMatch(scopePath: ScopeStack): StyleAttributes | null; getDefaults(): StyleAttributes; -} +}; const enum OptionalStandardTokenType { Other = 0, Comment = 1, @@ -202,25 +202,25 @@ const enum OptionalStandardTokenType { } type BasicScopeAttributes = { readonly languageId: number, - readonly tokenType: OptionalStandardTokenType -} + readonly tokenType: OptionalStandardTokenType; +}; type ScopeMatcher = { readonly values: ReadonlyMap | null; readonly scopesRegExp: RegExp | null; -} +}; type BasicScopeAttributesProvider = { readonly _defaultAttributes: BasicScopeAttributes; readonly _embeddedLanguagesMatcher: ScopeMatcher; -} +}; type Injection = { readonly debugSelector: string; readonly matcher: Matcher; readonly priority: -1 | 0 | 1; // 0 is the default. -1 for 'L' and 1 for 'R' readonly ruleId: RuleId; readonly grammar: vscodeTextmate.IRawGrammar; -} +}; const enum StandardTokenType { Other = 0, @@ -248,7 +248,7 @@ export type IGrammar = vscodeTextmate.IGrammar & { readonly _rootId: RuleId | -1, readonly _lastRuleId: number, readonly _ruleId2desc: Rule[], - readonly _includedGrammars: { [scopeName: string]: vscodeTextmate.IRawGrammar }, + readonly _includedGrammars: { [scopeName: string]: vscodeTextmate.IRawGrammar; }, readonly _grammarRepository: IGrammarRepository & IThemeProvider, readonly _grammar: vscodeTextmate.IRawGrammar, readonly _injections: Injection[] | null, @@ -262,10 +262,10 @@ type ILocation = { readonly filename: string; readonly line: number; readonly char: number; -} +}; type ILocatable = { readonly $vscodeTextmateLocation?: ILocation; -} +}; type IncludeString = string; type RegExpString = string; @@ -274,13 +274,13 @@ type IRawCaptures = IRawCapturesMap & ILocatable; type IRawCapturesMap = { [captureId: string]: IRawRule; -} +}; type IRawRepositoryMap = { [name: string]: IRawRule; $self: IRawRule; $base: IRawRule; -} +}; type IRawRepository = IRawRepositoryMap & ILocatable; @@ -305,20 +305,20 @@ export type IRawRule = ILocatable & { readonly repository?: IRawRepository; readonly applyEndPatternLast?: boolean; -} +}; export type IRawGrammar = ILocatable & { repository: IRawRepository; readonly scopeName: ScopeName; readonly patterns: IRawRule[]; - readonly injections?: { [expression: string]: IRawRule }; + readonly injections?: { [expression: string]: IRawRule; }; readonly injectionSelector?: string; readonly fileTypes?: string[]; readonly name?: string; readonly firstLineMatch?: string; -} +}; async function onigLibInterface() { @@ -357,16 +357,43 @@ async function loadGrammar(scopeName: string): Promise