提交 628d2a46 编写于 作者: A Alex Dima

Simplify WordHelper

上级 429be924
......@@ -17,7 +17,6 @@ import { IDisposable } from 'vs/base/common/lifecycle';
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
import { Selection } from 'vs/editor/common/core/selection';
import { ModeTransition } from 'vs/editor/common/core/modeTransition';
import { IndentRange } from 'vs/editor/common/model/indentRanges';
import { ICommandHandlerDescription } from 'vs/platform/commands/common/commands';
import { ContextKeyExpr, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
......@@ -1870,11 +1869,6 @@ export interface ITokenizedModel extends ITextModel {
*/
getLineContext(lineNumber: number): ILineContext;
/**
* @internal
*/
_getLineModeTransitions(lineNumber: number): ModeTransition[];
/**
* Get the current language mode associated with the model.
*/
......
......@@ -500,16 +500,21 @@ export class TextModelWithTokens extends TextModel implements editorCommon.IToke
// Having tokens allows implementing additional helper methods
_lineIsTokenized(lineNumber: number): boolean {
return this._invalidLineStartIndex > lineNumber - 1;
}
protected _getWordDefinition(): RegExp {
return WordHelper.massageWordDefinitionOf(this.getModeId());
}
public getWordAtPosition(position: editorCommon.IPosition): editorCommon.IWordAtPosition {
return WordHelper.getWordAtPosition(this, this.validatePosition(position));
public getWordAtPosition(_position: editorCommon.IPosition): editorCommon.IWordAtPosition {
let position = this.validatePosition(_position);
let lineContent = this.getLineContent(position.lineNumber);
if (this._invalidLineStartIndex <= position.lineNumber) {
// this line is not tokenized
return WordHelper.getWordAtPosition(lineContent, position.column, this.getModeId(), null);
}
let modeTransitions = this._getLineModeTransitions(position.lineNumber);
return WordHelper.getWordAtPosition(lineContent, position.column, this.getModeId(), modeTransitions);
}
public getWordUntilPosition(position: editorCommon.IPosition): editorCommon.IWordAtPosition {
......
......@@ -4,30 +4,16 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import { IPosition, IWordAtPosition } from 'vs/editor/common/editorCommon';
import { IWordAtPosition } from 'vs/editor/common/editorCommon';
import { ModeTransition } from 'vs/editor/common/core/modeTransition';
import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry';
import { getWordAtText, ensureValidWordDefinition } from 'vs/editor/common/model/wordHelper';
export interface ITextSource {
_lineIsTokenized(lineNumber: number): boolean;
getLineContent(lineNumber: number): string;
getModeId(): string;
_getLineModeTransitions(lineNumber: number): ModeTransition[];
}
export class WordHelper {
private static _safeGetWordDefinition(modeId: string): RegExp {
return LanguageConfigurationRegistry.getWordDefinition(modeId);
}
public static massageWordDefinitionOf(modeId: string): RegExp {
return ensureValidWordDefinition(WordHelper._safeGetWordDefinition(modeId));
let wordDefinition = LanguageConfigurationRegistry.getWordDefinition(modeId);
return ensureValidWordDefinition(wordDefinition);
}
private static _getWordAtColumn(txt: string, column: number, modeIndex: number, modeTransitions: ModeTransition[]): IWordAtPosition {
......@@ -41,23 +27,21 @@ export class WordHelper {
);
}
public static getWordAtPosition(textSource: ITextSource, position: IPosition): IWordAtPosition {
public static getWordAtPosition(lineContent: string, column: number, topModeId: string, modeTransitions: ModeTransition[]): IWordAtPosition {
if (!textSource._lineIsTokenized(position.lineNumber)) {
return getWordAtText(position.column, WordHelper.massageWordDefinitionOf(textSource.getModeId()), textSource.getLineContent(position.lineNumber), 0);
if (!modeTransitions || modeTransitions.length === 0) {
return getWordAtText(column, WordHelper.massageWordDefinitionOf(topModeId), lineContent, 0);
}
let result: IWordAtPosition = null;
let txt = textSource.getLineContent(position.lineNumber);
let modeTransitions = textSource._getLineModeTransitions(position.lineNumber);
let columnIndex = position.column - 1;
let columnIndex = column - 1;
let modeIndex = ModeTransition.findIndexInSegmentsArray(modeTransitions, columnIndex);
result = WordHelper._getWordAtColumn(txt, position.column, modeIndex, modeTransitions);
result = WordHelper._getWordAtColumn(lineContent, column, modeIndex, modeTransitions);
if (!result && modeIndex > 0 && modeTransitions[modeIndex].startIndex === columnIndex) {
// The position is right at the beginning of `modeIndex`, so try looking at `modeIndex` - 1 too
result = WordHelper._getWordAtColumn(txt, position.column, modeIndex - 1, modeTransitions);
result = WordHelper._getWordAtColumn(lineContent, column, modeIndex - 1, modeTransitions);
}
return result;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册