提交 d86c17e7 编写于 作者: A Alex Dima

Add CursorContext

上级 6fdf8a1e
......@@ -4,13 +4,10 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
import { onUnexpectedError } from 'vs/base/common/errors';
import { IModeConfiguration, IOneCursorState, IViewModelHelper, OneCursor } from 'vs/editor/common/controller/oneCursor';
import { IOneCursorState, OneCursor, CursorContext } from 'vs/editor/common/controller/oneCursor';
import { Selection } from 'vs/editor/common/core/selection';
import { IConfiguration, IModel, ISelection } from 'vs/editor/common/editorCommon';
import { IAutoClosingPair } from 'vs/editor/common/modes/languageConfiguration';
import { ISelection } from 'vs/editor/common/editorCommon';
import { Position } from 'vs/editor/common/core/position';
import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry';
import { CursorState } from 'vs/editor/common/controller/cursorCommon';
export interface ICursorCollectionState {
......@@ -20,9 +17,7 @@ export interface ICursorCollectionState {
export class CursorCollection {
private model: IModel;
private configuration: IConfiguration;
private modeConfiguration: IModeConfiguration;
private context: CursorContext;
private primaryCursor: OneCursor;
private secondaryCursors: OneCursor[];
......@@ -30,28 +25,26 @@ export class CursorCollection {
// An index which identifies the last cursor that was added / moved (think Ctrl+drag)
private lastAddedCursorIndex: number;
private viewModelHelper: IViewModelHelper;
constructor(model: IModel, configuration: IConfiguration, viewModelHelper: IViewModelHelper) {
this.model = model;
this.configuration = configuration;
this.viewModelHelper = viewModelHelper;
this.modeConfiguration = this.getModeConfiguration();
this.primaryCursor = new OneCursor(this.model, this.configuration, this.modeConfiguration, this.viewModelHelper);
constructor(context: CursorContext) {
this.context = context;
this.primaryCursor = new OneCursor(context);
this.secondaryCursors = [];
this.lastAddedCursorIndex = 0;
}
public dispose(): void {
this.primaryCursor.dispose();
this.primaryCursor.dispose(this.context);
this.killSecondaryCursors();
}
public updateContext(context: CursorContext): void {
this.context = context;
}
public ensureValidState(): void {
this.primaryCursor.ensureValidState();
this.primaryCursor.ensureValidState(this.context);
for (let i = 0, len = this.secondaryCursors.length; i < len; i++) {
this.secondaryCursors[i].ensureValidState();
this.secondaryCursors[i].ensureValidState(this.context);
}
}
......@@ -63,22 +56,14 @@ export class CursorCollection {
}
public restoreState(state: ICursorCollectionState): void {
this.primaryCursor.restoreState(state.primary);
this.primaryCursor.restoreState(this.context, state.primary);
this.killSecondaryCursors();
for (var i = 0; i < state.secondary.length; i++) {
this.addSecondaryCursor(null);
this.secondaryCursors[i].restoreState(state.secondary[i]);
this.secondaryCursors[i].restoreState(this.context, state.secondary[i]);
}
}
public updateMode(): void {
this.modeConfiguration = this.getModeConfiguration();
this.getAll().forEach((cursor) => {
cursor.updateModeConfiguration(this.modeConfiguration);
});
}
public getAll(): OneCursor[] {
var result: OneCursor[] = [];
result.push(this.primaryCursor);
......@@ -147,7 +132,7 @@ export class CursorCollection {
}
public setSelections(selections: ISelection[], viewSelections?: ISelection[]): void {
this.primaryCursor.setSelection(selections[0], viewSelections ? viewSelections[0] : null);
this.primaryCursor.setSelection(this.context, selections[0], viewSelections ? viewSelections[0] : null);
this._setSecondarySelections(selections.slice(1), viewSelections ? viewSelections.slice(1) : null);
}
......@@ -159,7 +144,7 @@ export class CursorCollection {
if (states === null) {
return;
}
this.primaryCursor.setState(states[0].modelState, states[0].viewState, ensureInEditableRange);
this.primaryCursor.setState(this.context, states[0].modelState, states[0].viewState, ensureInEditableRange);
this._setSecondaryStates(states.slice(1), ensureInEditableRange);
}
......@@ -183,7 +168,7 @@ export class CursorCollection {
}
for (let i = 0; i < secondaryStatesLength; i++) {
this.secondaryCursors[i].setState(secondaryStates[i].modelState, secondaryStates[i].viewState, ensureInEditableRange);
this.secondaryCursors[i].setState(this.context, secondaryStates[i].modelState, secondaryStates[i].viewState, ensureInEditableRange);
}
}
......@@ -196,9 +181,9 @@ export class CursorCollection {
}
public addSecondaryCursor(selection: ISelection): void {
var newCursor = new OneCursor(this.model, this.configuration, this.modeConfiguration, this.viewModelHelper);
var newCursor = new OneCursor(this.context);
if (selection) {
newCursor.setSelection(selection);
newCursor.setSelection(this.context, selection);
}
this.secondaryCursors.push(newCursor);
this.lastAddedCursorIndex = this.secondaryCursors.length;
......@@ -237,7 +222,7 @@ export class CursorCollection {
for (var i = 0; i < secondarySelectionsLength; i++) {
if (secondarySelections[i]) {
this.secondaryCursors[i].setSelection(secondarySelections[i], viewSelections ? viewSelections[i] : null);
this.secondaryCursors[i].setSelection(this.context, secondarySelections[i], viewSelections ? viewSelections[i] : null);
}
}
......@@ -248,7 +233,7 @@ export class CursorCollection {
if (this.lastAddedCursorIndex >= removeIndex + 1) {
this.lastAddedCursorIndex--;
}
this.secondaryCursors[removeIndex].dispose();
this.secondaryCursors[removeIndex].dispose(this.context);
this.secondaryCursors.splice(removeIndex, 1);
}
......@@ -317,7 +302,7 @@ export class CursorCollection {
}
sortedCursors[winnerSortedCursorIndex].selection = resultingSelection;
cursors[winnerIndex].setSelection(resultingSelection);
cursors[winnerIndex].setSelection(this.context, resultingSelection);
}
for (var j = 0; j < sortedCursors.length; j++) {
......@@ -334,58 +319,4 @@ export class CursorCollection {
}
}
}
private getModeConfiguration(): IModeConfiguration {
let i: number;
let result: IModeConfiguration = {
electricChars: {},
autoClosingPairsOpen: {},
autoClosingPairsClose: {},
surroundingPairs: {}
};
let electricChars: string[] = null;
try {
electricChars = LanguageConfigurationRegistry.getElectricCharacters(this.model.getLanguageIdentifier().id);
} catch (e) {
onUnexpectedError(e);
electricChars = null;
}
if (electricChars) {
for (i = 0; i < electricChars.length; i++) {
result.electricChars[electricChars[i]] = true;
}
}
let autoClosingPairs: IAutoClosingPair[];
try {
autoClosingPairs = LanguageConfigurationRegistry.getAutoClosingPairs(this.model.getLanguageIdentifier().id);
} catch (e) {
onUnexpectedError(e);
autoClosingPairs = null;
}
if (autoClosingPairs) {
for (i = 0; i < autoClosingPairs.length; i++) {
result.autoClosingPairsOpen[autoClosingPairs[i].open] = autoClosingPairs[i].close;
result.autoClosingPairsClose[autoClosingPairs[i].close] = autoClosingPairs[i].open;
}
}
let surroundingPairs: IAutoClosingPair[];
try {
surroundingPairs = LanguageConfigurationRegistry.getSurroundingPairs(this.model.getLanguageIdentifier().id);
} catch (e) {
onUnexpectedError(e);
surroundingPairs = null;
}
if (surroundingPairs) {
for (i = 0; i < surroundingPairs.length; i++) {
result.surroundingPairs[surroundingPairs[i].open] = surroundingPairs[i].close;
}
}
return result;
}
}
......@@ -7,11 +7,14 @@
import { Position } from 'vs/editor/common/core/position';
import { CharCode } from 'vs/base/common/charCode';
import * as strings from 'vs/base/common/strings';
import { IModeConfiguration } from 'vs/editor/common/controller/oneCursor';
import { ICommand, IConfigurationChangedEvent, TextModelResolvedOptions, IConfiguration } from 'vs/editor/common/editorCommon';
import { TextModel } from 'vs/editor/common/model/textModel';
import { Selection } from 'vs/editor/common/core/selection';
import { Range } from 'vs/editor/common/core/range';
import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry';
import { onUnexpectedError } from 'vs/base/common/errors';
import { LanguageIdentifier } from 'vs/editor/common/modes';
import { IAutoClosingPair } from 'vs/editor/common/modes/languageConfiguration';
export interface CharacterMap {
[char: string]: string;
......@@ -42,10 +45,10 @@ export class CursorConfiguration {
}
constructor(
languageIdentifier: LanguageIdentifier,
oneIndent: string,
modelOptions: TextModelResolvedOptions,
configuration: IConfiguration,
modeConfiguration: IModeConfiguration
configuration: IConfiguration
) {
let c = configuration.editor;
......@@ -56,15 +59,65 @@ export class CursorConfiguration {
this.useTabStops = c.useTabStops;
this.wordSeparators = c.wordSeparators;
this.autoClosingBrackets = c.autoClosingBrackets;
this.autoClosingPairsOpen = modeConfiguration.autoClosingPairsOpen;
this.autoClosingPairsClose = modeConfiguration.autoClosingPairsClose;
this.surroundingPairs = modeConfiguration.surroundingPairs;
this.electricChars = modeConfiguration.electricChars;
this.autoClosingPairsOpen = {};
this.autoClosingPairsClose = {};
this.surroundingPairs = {};
this.electricChars = {};
let electricChars = CursorConfiguration._getElectricCharacters(languageIdentifier);
if (electricChars) {
for (let i = 0; i < electricChars.length; i++) {
this.electricChars[electricChars[i]] = true;
}
}
let autoClosingPairs = CursorConfiguration._getAutoClosingPairs(languageIdentifier);
if (autoClosingPairs) {
for (let i = 0; i < autoClosingPairs.length; i++) {
this.autoClosingPairsOpen[autoClosingPairs[i].open] = autoClosingPairs[i].close;
this.autoClosingPairsClose[autoClosingPairs[i].close] = autoClosingPairs[i].open;
}
}
let surroundingPairs = CursorConfiguration._getSurroundingPairs(languageIdentifier);
if (surroundingPairs) {
for (let i = 0; i < surroundingPairs.length; i++) {
this.surroundingPairs[surroundingPairs[i].open] = surroundingPairs[i].close;
}
}
}
public normalizeIndentation(str: string): string {
return TextModel.normalizeIndentation(str, this.tabSize, this.insertSpaces);
}
private static _getElectricCharacters(languageIdentifier: LanguageIdentifier): string[] {
try {
return LanguageConfigurationRegistry.getElectricCharacters(languageIdentifier.id);
} catch (e) {
onUnexpectedError(e);
return null;
}
}
private static _getAutoClosingPairs(languageIdentifier: LanguageIdentifier): IAutoClosingPair[] {
try {
return LanguageConfigurationRegistry.getAutoClosingPairs(languageIdentifier.id);
} catch (e) {
onUnexpectedError(e);
return null;
}
}
private static _getSurroundingPairs(languageIdentifier: LanguageIdentifier): IAutoClosingPair[] {
try {
return LanguageConfigurationRegistry.getSurroundingPairs(languageIdentifier.id);
} catch (e) {
onUnexpectedError(e);
return null;
}
}
}
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册