Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 55 additions & 22 deletions src/grammar/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ export class Grammar implements IGrammar, IRuleFactoryHelper, IOnigLib {
): ITokenizeLineResult {
const r = this._tokenize(lineText, prevState, false, timeLimit);
return {
tokens: r.lineTokens.getResult(r.ruleStack, r.lineLength),
tokens: r.tokenHandler.getTokenResult(r.ruleStack, r.lineLength),
ruleStack: r.ruleStack,
stoppedEarly: r.stoppedEarly,
fonts: r.lineFonts.getResult()
fonts: r.tokenHandler.getFontResult()
};
}

Expand All @@ -295,10 +295,10 @@ export class Grammar implements IGrammar, IRuleFactoryHelper, IOnigLib {
): ITokenizeLineResult2 {
const r = this._tokenize(lineText, prevState, true, timeLimit);
return {
tokens: r.lineTokens.getBinaryResult(r.ruleStack, r.lineLength),
tokens: r.tokenHandler.getBinaryTokenResult(r.ruleStack, r.lineLength),
ruleStack: r.ruleStack,
stoppedEarly: r.stoppedEarly,
fonts: r.lineFonts.getResult()
fonts: r.tokenHandler.getFontResult()
};
}

Expand All @@ -309,8 +309,7 @@ export class Grammar implements IGrammar, IRuleFactoryHelper, IOnigLib {
timeLimit: number
): {
lineLength: number;
lineTokens: LineTokens;
lineFonts: LineFonts;
tokenHandler: ITokenHandler;
ruleStack: StateStackImpl;
stoppedEarly: boolean;
} {
Expand Down Expand Up @@ -377,21 +376,19 @@ export class Grammar implements IGrammar, IRuleFactoryHelper, IOnigLib {
lineText = lineText + "\n";
const onigLineText = this.createOnigString(lineText);
const lineLength = onigLineText.content.length;
const lineTokens = new LineTokens(
const tokenHandler = new TokenHandler(
emitBinaryTokens,
lineText,
this._tokenTypeMatchers,
this.balancedBracketSelectors
);
const lineFonts = new LineFonts();
const r = _tokenizeString(
this,
onigLineText,
isFirstLine,
0,
prevState,
lineTokens,
lineFonts,
tokenHandler,
true,
timeLimit
);
Expand All @@ -400,8 +397,7 @@ export class Grammar implements IGrammar, IRuleFactoryHelper, IOnigLib {

return {
lineLength: lineLength,
lineTokens: lineTokens,
lineFonts: lineFonts,
tokenHandler: tokenHandler,
ruleStack: r.stack,
stoppedEarly: r.stoppedEarly,
};
Expand Down Expand Up @@ -989,10 +985,6 @@ export class LineTokens {
this._lastTokenEndIndex = 0;
}

public produce(stack: StateStackImpl, endIndex: number): void {
this.produceFromScopes(stack.contentNameScopesList, endIndex);
}

public produceFromScopes(
scopesList: AttributedScopeStack | null,
endIndex: number
Expand Down Expand Up @@ -1089,7 +1081,7 @@ export class LineTokens {

if (this._tokens.length === 0) {
this._lastTokenEndIndex = -1;
this.produce(stack, lineLength);
this.produceFromScopes(stack.contentNameScopesList, lineLength);
this._tokens[this._tokens.length - 1].startIndex = 0;
}

Expand All @@ -1105,7 +1097,7 @@ export class LineTokens {

if (this._binaryTokens.length === 0) {
this._lastTokenEndIndex = -1;
this.produce(stack, lineLength);
this.produceFromScopes(stack.contentNameScopesList, lineLength);
this._binaryTokens[this._binaryTokens.length - 2] = 0;
}

Expand Down Expand Up @@ -1143,10 +1135,6 @@ export class LineFonts {

constructor() { }

public produce(stack: StateStackImpl, endIndex: number): void {
this.produceFromScopes(stack.contentNameScopesList, endIndex);
}

public produceFromScopes(
scopesList: AttributedScopeStack | null,
endIndex: number
Expand Down Expand Up @@ -1183,3 +1171,48 @@ export class LineFonts {
return this._fonts;
}
}

export interface ITokenHandler {
handle(scopes: AttributedScopeStack | null, endIndex: number): void;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ITokenHandler should only have handle!
Use the concerete implementation type for where you need the other methods.

getTokenResult(stack: StateStackImpl, lineLength: number): IToken[];
getBinaryTokenResult(stack: StateStackImpl, lineLength: number): Uint32Array;
getFontResult(): IFontInfo[];
}

export class TokenHandler {

private _lineTokens: LineTokens;
private _lineFonts: LineFonts;

constructor(
emitBinaryTokens: boolean,
lineText: string,
tokenTypeOverrides: TokenTypeMatcher[],
balancedBracketSelectors: BalancedBracketSelectors | null
) {
this._lineTokens = new LineTokens(
emitBinaryTokens,
lineText,
tokenTypeOverrides,
balancedBracketSelectors
);
this._lineFonts = new LineFonts();
}

handle(scopes: AttributedScopeStack | null, endIndex: number): void {
this._lineTokens.produceFromScopes(scopes, endIndex);
this._lineFonts.produceFromScopes(scopes, endIndex);
}

getTokenResult(stack: StateStackImpl, lineLength: number): IToken[] {
return this._lineTokens.getResult(stack, lineLength)
}

getBinaryTokenResult(stack: StateStackImpl, lineLength: number): Uint32Array {
return this._lineTokens.getBinaryResult(stack, lineLength);
}

getFontResult(): IFontInfo[] {
return this._lineFonts.getResult();
}
}
Loading