Skip to content

Commit

Permalink
Remove IState.getModeId()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Dec 21, 2016
1 parent 589173a commit 4439d01
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 111 deletions.
1 change: 0 additions & 1 deletion src/vs/editor/common/modes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import Event, { Emitter } from 'vs/base/common/event';
export interface IState {
clone(): IState;
equals(other: IState): boolean;
getModeId(): string;
}

/**
Expand Down
118 changes: 68 additions & 50 deletions src/vs/editor/common/modes/monarch/monarchLexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ import * as modes from 'vs/editor/common/modes';
import * as monarchCommon from 'vs/editor/common/modes/monarch/monarchCommon';
import { IModeService } from 'vs/editor/common/services/modeService';
import { Token } from 'vs/editor/common/core/token';
import { NullState, nullTokenize, NULL_MODE_ID } from 'vs/editor/common/modes/nullMode';
import { NULL_STATE, nullTokenize, NULL_MODE_ID } from 'vs/editor/common/modes/nullMode';
import { ModeTransition } from 'vs/editor/common/core/modeTransition';

const CACHE_STACK_DEPTH = 5;

/**
* Reuse the same stack elements up to a certain depth.
*/
export class MonarchStackElementFactory {

private static _INSTANCE = new MonarchStackElementFactory(5);
private static _INSTANCE = new MonarchStackElementFactory(CACHE_STACK_DEPTH);
public static create(parent: MonarchStackElement, state: string): MonarchStackElement {
return this._INSTANCE.create(parent, state);
}
Expand Down Expand Up @@ -108,20 +110,6 @@ export class MonarchStackElement {
return this.parent;
}

public print(): void {
console.log(this._print());
// console.log('PRINTING MY STACK');
// console.log(this);
}

private _print(): string {
if (this.parent) {
return this.state + ' -> ' + this.parent._print();
} else {
return this.state + ' -|';
}
}

public popall(): MonarchStackElement {
let result: MonarchStackElement = this;
while (result.parent) {
Expand All @@ -135,41 +123,68 @@ export class MonarchStackElement {
}
}

export class EmbeddedModeData {
public readonly modeId: string;
public readonly state: modes.IState;

constructor(modeId: string, state: modes.IState) {
this.modeId = modeId;
this.state = state;
}

public equals(other: EmbeddedModeData): boolean {
return (
this.modeId === other.modeId
&& this.state.equals(other.state)
);
}

public clone(): EmbeddedModeData {
let stateClone = this.state.clone();
// save an object
if (stateClone === this.state) {
return this;
}
return new EmbeddedModeData(this.modeId, this.state);
}
}

export class MonarchLineState implements modes.IState {

private readonly _modeId: string;
public readonly stack: MonarchStackElement;
public readonly embeddedModeState: modes.IState;
public readonly embeddedModeData: EmbeddedModeData;

constructor(
modeId: string,
stack: MonarchStackElement,
embeddedModeState: modes.IState
embeddedModeData: EmbeddedModeData
) {
if (!stack) {
console.log('CREATING LINE STATE WITHOUT STACK!!!'); process.exit(0);
}
this._modeId = modeId;
this.stack = stack;
this.embeddedModeState = embeddedModeState;
this.embeddedModeData = embeddedModeData;
}

public clone(): modes.IState {
return this;
let embeddedModeDataClone = this.embeddedModeData ? this.embeddedModeData.clone() : null;
// save an object
if (embeddedModeDataClone === this.embeddedModeData) {
return this;
}
return new MonarchLineState(this.stack, this.embeddedModeData);
}

public equals(other: modes.IState): boolean {
if (!(other instanceof MonarchLineState)) {
return false;
}
if (this._modeId !== other._modeId) {
if (!this.stack.equals(other.stack)) {
return false;
}
return this.stack.equals(other.stack);
}

public getModeId(): string {
return this._modeId;
if (this.embeddedModeData === null && other.embeddedModeData === null) {
return true;
}
if (this.embeddedModeData === null || other.embeddedModeData === null) {
return false;
}
return this.embeddedModeData.equals(other.embeddedModeData);
}
}

Expand Down Expand Up @@ -210,12 +225,12 @@ export class MonarchTokenizer implements modes.ITokenizationSupport {

public getInitialState(): modes.IState {
let rootState = MonarchStackElementFactory.create(null, this._lexer.start);
return new MonarchLineState(this._modeId, rootState, null);
return new MonarchLineState(rootState, null);
}

public tokenize(line: string, _lineState: modes.IState, offsetDelta: number): modes.ILineTokens {
let lineState = (<MonarchLineState>_lineState);
if (lineState.embeddedModeState) {
if (lineState.embeddedModeData) {
return this._nestedTokenize(line, lineState, offsetDelta, [], []);
} else {
return this._myTokenize(line, lineState, offsetDelta, [], []);
Expand Down Expand Up @@ -267,8 +282,9 @@ export class MonarchTokenizer implements modes.ITokenizationSupport {
return popOffset;
}

private _safeNestedModeTokenize(embeddedModeLine: string, embeddedModeState: modes.IState, offsetDelta: number): modes.ILineTokens {
const nestedModeId = embeddedModeState.getModeId();
private _safeNestedModeTokenize(embeddedModeLine: string, embeddedModeData: EmbeddedModeData, offsetDelta: number): modes.ILineTokens {
const nestedModeId = embeddedModeData.modeId;
const embeddedModeState = embeddedModeData.state;

const nestedModeTokenizationSupport = modes.TokenizationRegistry.get(nestedModeId);
if (nestedModeTokenizationSupport) {
Expand All @@ -286,20 +302,20 @@ export class MonarchTokenizer implements modes.ITokenizationSupport {

if (popOffset === -1) {
// tokenization will not leave nested mode
let nestedModeLineTokens = this._safeNestedModeTokenize(line, lineState.embeddedModeState, offsetDelta);
let nestedModeLineTokens = this._safeNestedModeTokenize(line, lineState.embeddedModeData, offsetDelta);
// Prepend nested mode's result to our result
return {
tokens: prependTokens.concat(nestedModeLineTokens.tokens),
actualStopOffset: nestedModeLineTokens.actualStopOffset,
modeTransitions: prependModeTransitions.concat(nestedModeLineTokens.modeTransitions),
endState: new MonarchLineState(this._modeId, lineState.stack, nestedModeLineTokens.endState)
endState: new MonarchLineState(lineState.stack, new EmbeddedModeData(lineState.embeddedModeData.modeId, nestedModeLineTokens.endState))
};
}

let nestedModeLine = line.substring(0, popOffset);
if (nestedModeLine.length > 0) {
// tokenize with the nested mode
let nestedModeLineTokens = this._safeNestedModeTokenize(nestedModeLine, lineState.embeddedModeState, offsetDelta);
let nestedModeLineTokens = this._safeNestedModeTokenize(nestedModeLine, lineState.embeddedModeData, offsetDelta);
// Prepend nested mode's result to our result
prependTokens = prependTokens.concat(nestedModeLineTokens.tokens);
prependModeTransitions = prependModeTransitions.concat(nestedModeLineTokens.modeTransitions);
Expand All @@ -318,7 +334,7 @@ export class MonarchTokenizer implements modes.ITokenizationSupport {

const lineLength = line.length;

let embeddedModeState = lineState.embeddedModeState;
let embeddedModeData = lineState.embeddedModeData;
let stack = lineState.stack;
let pos = 0;

Expand Down Expand Up @@ -431,11 +447,11 @@ export class MonarchTokenizer implements modes.ITokenizationSupport {
// enter embedded mode?
if (action.nextEmbedded) {
if (action.nextEmbedded === '@pop') {
if (!embeddedModeState) {
if (!embeddedModeData) {
monarchCommon.throwError(this._lexer, 'cannot pop embedded mode if not inside one');
}
embeddedModeState = null;
} else if (embeddedModeState) {
embeddedModeData = null;
} else if (embeddedModeData) {
monarchCommon.throwError(this._lexer, 'cannot enter embedded mode from within an embedded mode');
} else {
enteringEmbeddedMode = monarchCommon.substituteMatches(this._lexer, action.nextEmbedded, matched, matches, state);
Expand Down Expand Up @@ -570,16 +586,16 @@ export class MonarchTokenizer implements modes.ITokenizationSupport {
enteringEmbeddedMode = enteringEmbeddedModeId;
}

let embeddedModeState = this._getNestedEmbeddedModeState(enteringEmbeddedMode);
let embeddedModeData = this._getNestedEmbeddedModeData(enteringEmbeddedMode);

if (pos < lineLength) {
// there is content from the embedded mode on this line
let restOfLine = line.substr(pos);
return this._nestedTokenize(restOfLine, new MonarchLineState(this._modeId, stack, embeddedModeState), offsetDelta + pos, prependTokens, prependModeTransitions);
return this._nestedTokenize(restOfLine, new MonarchLineState(stack, embeddedModeData), offsetDelta + pos, prependTokens, prependModeTransitions);
} else {
return {
tokens: prependTokens,
endState: new MonarchLineState(this._modeId, stack, embeddedModeState),
endState: new MonarchLineState(stack, embeddedModeData),
actualStopOffset: offsetDelta + line.length,
modeTransitions: prependModeTransitions
};
Expand All @@ -589,21 +605,23 @@ export class MonarchTokenizer implements modes.ITokenizationSupport {

return {
tokens: prependTokens,
endState: new MonarchLineState(this._modeId, stack, embeddedModeState),
endState: new MonarchLineState(stack, embeddedModeData),
actualStopOffset: offsetDelta + line.length,
modeTransitions: prependModeTransitions
};
}

private _getNestedEmbeddedModeState(mimetypeOrModeId: string): modes.IState {
private _getNestedEmbeddedModeData(mimetypeOrModeId: string): EmbeddedModeData {
let nestedMode = this._locateMode(mimetypeOrModeId);
if (nestedMode) {
let tokenizationSupport = modes.TokenizationRegistry.get(nestedMode.getId());
if (tokenizationSupport) {
return tokenizationSupport.getInitialState();
return new EmbeddedModeData(nestedMode.getId(), tokenizationSupport.getInitialState());
}
}
return new NullState(nestedMode ? nestedMode.getId() : NULL_MODE_ID);

let nestedModeId = nestedMode ? nestedMode.getId() : NULL_MODE_ID;
return new EmbeddedModeData(nestedModeId, NULL_STATE);
}

private _locateMode(mimetypeOrModeId: string): modes.IMode {
Expand Down
19 changes: 4 additions & 15 deletions src/vs/editor/common/modes/nullMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,19 @@ import { IState, ILineTokens } from 'vs/editor/common/modes';
import { ModeTransition } from 'vs/editor/common/core/modeTransition';
import { Token } from 'vs/editor/common/core/token';

export class NullState implements IState {

private readonly _modeId: string;

constructor(modeId: string) {
this._modeId = modeId;
}
class NullStateImpl implements IState {

public clone(): IState {
return this;
}

public equals(other: IState): boolean {
return (
other instanceof NullState
&& this._modeId === other._modeId
);
}

public getModeId(): string {
return this._modeId;
return (this === other);
}
}

export const NULL_STATE: IState = new NullStateImpl();

export const NULL_MODE_ID = 'vs.editor.nullMode';

export function nullTokenize(modeId: string, buffer: string, state: IState, deltaOffset: number = 0, stopAtOffset?: number): ILineTokens {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/editor/common/modes/textToHtmlTokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { IHTMLContentElement } from 'vs/base/common/htmlContent';
import * as strings from 'vs/base/common/strings';
import { IState, ITokenizationSupport, TokenizationRegistry } from 'vs/editor/common/modes';
import { NullState, nullTokenize } from 'vs/editor/common/modes/nullMode';
import { NULL_STATE, nullTokenize } from 'vs/editor/common/modes/nullMode';

export function tokenizeToHtmlContent(text: string, languageId: string): IHTMLContentElement {
return _tokenizeToHtmlContent(text, _getSafeTokenizationSupport(languageId));
Expand All @@ -23,7 +23,7 @@ function _getSafeTokenizationSupport(languageId: string): ITokenizationSupport {
return tokenizationSupport;
}
return {
getInitialState: () => new NullState(null),
getInitialState: () => NULL_STATE,
tokenize: (buffer: string, state: IState, deltaOffset: number = 0, stopAtOffset?: number) => nullTokenize(null, buffer, state, deltaOffset, stopAtOffset)
};
}
Expand Down
17 changes: 5 additions & 12 deletions src/vs/editor/common/services/modeServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,9 @@ export class ModeServiceImpl implements IModeService {

export class TokenizationState2Adapter implements modes.IState {

private readonly _modeId: string;
public readonly actual: modes.IState2;

constructor(modeId: string, actual: modes.IState2) {
this._modeId = modeId;
constructor(actual: modes.IState2) {
this.actual = actual;
}

Expand All @@ -320,20 +318,15 @@ export class TokenizationState2Adapter implements modes.IState {
if (actualClone === this.actual) {
return this;
}
return new TokenizationState2Adapter(this._modeId, actualClone);
return new TokenizationState2Adapter(actualClone);
}

public equals(other: modes.IState): boolean {
return (
other instanceof TokenizationState2Adapter
&& this._modeId === other._modeId
&& this.actual.equals(other.actual)
);
}

public getModeId(): string {
return this._modeId;
}
}

export class TokenizationSupport2Adapter implements modes.ITokenizationSupport {
Expand All @@ -347,7 +340,7 @@ export class TokenizationSupport2Adapter implements modes.ITokenizationSupport {
}

public getInitialState(): modes.IState {
return new TokenizationState2Adapter(this._modeId, this._actual.getInitialState());
return new TokenizationState2Adapter(this._actual.getInitialState());
}

public tokenize(line: string, state: modes.IState, offsetDelta: number = 0, stopAtOffset?: number): modes.ILineTokens {
Expand All @@ -372,14 +365,14 @@ export class TokenizationSupport2Adapter implements modes.ITokenizationSupport {
if (actualResult.endState.equals(state.actual)) {
endState = state;
} else {
endState = new TokenizationState2Adapter(state.getModeId(), actualResult.endState);
endState = new TokenizationState2Adapter(actualResult.endState);
}

return {
tokens: tokens,
actualStopOffset: offsetDelta + line.length,
endState: endState,
modeTransitions: [new ModeTransition(offsetDelta, state.getModeId())],
modeTransitions: [new ModeTransition(offsetDelta, this._modeId)],
};
}
}
Expand Down
Loading

0 comments on commit 4439d01

Please sign in to comment.