提交 1d5456ec 编写于 作者: M Michel Kaporin

Merge branch 'master' of github.com:Microsoft/vscode

......@@ -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;
}
}
}
/**
......
......@@ -71,7 +71,7 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor
private defineKeybindingWidget: DefineKeybindingWidget;
private keybindingsListContainer: HTMLElement;
private keybindingItemToReveal: IKeybindingItemEntry;
private unAssignedKeybindingItemToRevealAndFocus: IKeybindingItemEntry;
private listEntries: IListEntry[];
private keybindingsList: List<IListEntry>;
......@@ -147,7 +147,7 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor
focus(): void {
const activeKeybindingEntry = this.activeKeybindingEntry;
if (activeKeybindingEntry) {
this.focusEntry(activeKeybindingEntry, false);
this.focusEntry(activeKeybindingEntry);
} else {
this.searchWidget.focus();
}
......@@ -159,24 +159,24 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor
}
defineKeybinding(keybindingEntry: IKeybindingItemEntry): TPromise<any> {
this.overlayContainer.style.display = 'block';
this.showOverlayContainer();
return this.defineKeybindingWidget.define().then(key => {
if (key) {
return this.keybindingEditingService.editKeybinding(key, keybindingEntry.keybindingItem.keybindingItem)
.then(() => {
if (!keybindingEntry.keybindingItem.keybinding) { // reveal only if keybinding was added because the entry will be placed in different position after rendering
this.keybindingItemToReveal = keybindingEntry;
if (!keybindingEntry.keybindingItem.keybinding) { // reveal only if keybinding was added to unassinged. Because the entry will be placed in different position after rendering
this.unAssignedKeybindingItemToRevealAndFocus = keybindingEntry;
}
});
}
return null;
}).then(() => {
this.hideOverlayContainer();
this.focusEntry(keybindingEntry, false);
this.focusEntry(keybindingEntry);
}, error => {
this.hideOverlayContainer();
this.onKeybindingEditingError(error);
this.focusEntry(keybindingEntry, false);
this.focusEntry(keybindingEntry);
return error;
});
}
......@@ -194,7 +194,7 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor
.then(() => this.focus(),
error => {
this.onKeybindingEditingError(error);
this.focusEntry(keybindingEntry, false);
this.focusEntry(keybindingEntry);
});
}
return TPromise.as(null);
......@@ -213,7 +213,7 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor
.then(() => this.focus(),
error => {
this.onKeybindingEditingError(error);
this.focusEntry(keybindingEntry, false);
this.focusEntry(keybindingEntry);
});
}
return TPromise.as(null);
......@@ -243,6 +243,10 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor
this.hideOverlayContainer();
}
private showOverlayContainer() {
this.overlayContainer.style.display = 'block';
}
private hideOverlayContainer() {
this.overlayContainer.style.display = 'none';
}
......@@ -274,7 +278,7 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor
this.keybindingsList = this._register(new List<IListEntry>(this.keybindingsListContainer, new Delegate(), [new KeybindingHeaderRenderer(), new KeybindingItemRenderer(this, this.keybindingsService)], { identityProvider: e => e.id }));
this._register(this.keybindingsList.onContextMenu(e => this.onContextMenu(e)));
this._register(this.keybindingsList.onFocusChange(e => this.onFocusChange(e)));
this._register(this.keybindingsList.onDOMFocus(() => this.keybindingsList.focusNext()));
this._register(this.keybindingsList.onDOMFocus(() => this.onKeybindingsListDOMFocus()));
this._register(this.keybindingsList.onDOMBlur(() => this.keybindingFocusContextKey.reset()));
this._register(this.listService.register(this.keybindingsList));
......@@ -290,13 +294,20 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor
}
private renderKeybindingsEntries(keybindingsEntries: IKeybindingItemEntry[]): void {
const currentFocussedIndices = this.keybindingsList.getFocus();
this.listEntries = [{ id: 'keybinding-header-entry', templateId: KEYBINDING_HEADER_TEMPLATE_ID }, ...keybindingsEntries];
this.keybindingsList.splice(0, this.keybindingsList.length, this.listEntries);
this.layoutKebindingsList();
if (this.keybindingItemToReveal) {
this.focusEntry(this.keybindingItemToReveal, true);
this.keybindingItemToReveal = null;
if (this.unAssignedKeybindingItemToRevealAndFocus) {
const index = this.getNewIndexOfUnassignedKeybinding(this.unAssignedKeybindingItemToRevealAndFocus);
if (index !== -1) {
this.keybindingsList.reveal(index, 0.2);
this.keybindingsList.setFocus([index]);
}
this.unAssignedKeybindingItemToRevealAndFocus = null;
} else {
this.keybindingsList.setFocus(currentFocussedIndices);
}
}
......@@ -306,26 +317,35 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor
this.keybindingsList.layout(listHeight);
}
private focusEntry(keybindingItemEntry: IKeybindingItemEntry, reveal: boolean): void {
let index = -1;
for (let i = 0; i < this.listEntries.length; i++) {
const entry = this.listEntries[i];
if (entry.id === keybindingItemEntry.id) {
index = i;
break;
private getIndexOf(listEntry: IListEntry): number {
const index = this.listEntries.indexOf(listEntry);
if (index === -1) {
for (let i = 0; i < this.listEntries.length; i++) {
if (this.listEntries[i].id === listEntry.id) {
return i;
}
}
}
return index;
}
private getNewIndexOfUnassignedKeybinding(unassignedKeybinding: IKeybindingItemEntry): number {
for (let index = 0; index < this.listEntries.length; index++) {
const entry = this.listEntries[index];
if (entry.templateId === KEYBINDING_ENTRY_TEMPLATE_ID) {
if ((<IKeybindingItemEntry>entry).keybindingItem.command === keybindingItemEntry.keybindingItem.command) {
index = i;
break;
const keybindingItemEntry = (<IKeybindingItemEntry>entry);
if (keybindingItemEntry.keybindingItem.command === unassignedKeybinding.keybindingItem.command) {
return index;
}
}
}
return -1;
}
private focusEntry(keybindingItemEntry: IKeybindingItemEntry): void {
const index = this.getIndexOf(keybindingItemEntry);
if (index !== -1) {
this.keybindingsList.getHTMLElement().focus();
if (reveal) {
this.keybindingsList.reveal(index, 0.2);
}
this.keybindingsList.setFocus([index]);
}
}
......@@ -362,6 +382,12 @@ export class KeybindingsEditor extends BaseEditor implements IKeybindingsEditor
}
}
private onKeybindingsListDOMFocus(): void {
if (!this.keybindingsList.getFocusedElements().length) {
this.keybindingsList.focusNext();
}
}
private createRemoveAction(keybindingItem: IKeybindingItemEntry): IAction {
return <IAction>{
label: localize('removeLabel', "Remove Keybinding"),
......
......@@ -142,7 +142,7 @@ export class KeybindingsEditorModel extends EditorModel {
const commandDefaultLabel = workbenchAction && language !== LANGUAGE_DEFAULT ? workbenchActionsRegistry.getAlias(workbenchAction.id) : null;
return <IKeybindingItem>{
keybinding: keybindingItem ? keybindingItem.resolvedKeybinding : null,
keybindingItem,
keybindingItem: keybindingItem ? keybindingItem : new ResolvedKeybindingItem(null, command, null, null, true),
command,
commandLabel: editorAction ? editorAction.label : workbenchAction ? workbenchAction.label : '',
commandDefaultLabel,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册