提交 89a8ab2a 编写于 作者: B Benjamin Pasero

code cleanup

上级 194fa300
......@@ -8,7 +8,7 @@
import 'vs/css!./media/editorstatus';
import nls = require('vs/nls');
import {TPromise} from 'vs/base/common/winjs.base';
import { $, append, runAtThisOrScheduleAtNextAnimationFrame } from 'vs/base/browser/dom';
import {$, append, runAtThisOrScheduleAtNextAnimationFrame} from 'vs/base/browser/dom';
import strings = require('vs/base/common/strings');
import paths = require('vs/base/common/paths');
import types = require('vs/base/common/types');
......@@ -59,7 +59,7 @@ function getTextModel(editorWidget: IEditor): ITextModel {
let textModel: ITextModel;
// Support for diff
let model = editorWidget.getModel();
const model = editorWidget.getModel();
if (model && !!(<IDiffEditorModel>model).modified) {
textModel = (<IDiffEditorModel>model).modified;
}
......@@ -151,7 +151,7 @@ class State {
}
public update(update: StateDelta): StateChange {
let e = new StateChange();
const e = new StateChange();
let somethingChanged = false;
if (typeof update.selectionStatus !== 'undefined') {
......@@ -303,7 +303,7 @@ export class EditorStatus implements IStatusbarItem {
}
private updateState(update: StateDelta): void {
let changed = this.state.update(update);
const changed = this.state.update(update);
if (!changed) {
// Nothing really changed
return;
......@@ -313,7 +313,7 @@ export class EditorStatus implements IStatusbarItem {
this.toRender = changed;
this.delayedRender = runAtThisOrScheduleAtNextAnimationFrame(() => {
this.delayedRender = null;
let toRender = this.toRender;
const toRender = this.toRender;
this.toRender = null;
this._renderNow(toRender);
});
......@@ -391,14 +391,14 @@ export class EditorStatus implements IStatusbarItem {
} else {
if (info.charactersSelected) {
return strings.format(nlsMultiSelectionRange, info.selections.length, info.charactersSelected);
} else {
} else if (info.selections.length > 0) {
return strings.format(nlsMultiSelection, info.selections.length);
}
}
}
private onModeClick(): void {
let action = this.instantiationService.createInstance(ChangeModeAction, ChangeModeAction.ID, ChangeModeAction.LABEL);
const action = this.instantiationService.createInstance(ChangeModeAction, ChangeModeAction.ID, ChangeModeAction.LABEL);
action.run().done(null, errors.onUnexpectedError);
action.dispose();
......@@ -415,14 +415,14 @@ export class EditorStatus implements IStatusbarItem {
}
private onEOLClick(): void {
let action = this.instantiationService.createInstance(ChangeEOLAction, ChangeEOLAction.ID, ChangeEOLAction.LABEL);
const action = this.instantiationService.createInstance(ChangeEOLAction, ChangeEOLAction.ID, ChangeEOLAction.LABEL);
action.run().done(null, errors.onUnexpectedError);
action.dispose();
}
private onEncodingClick(): void {
let action = this.instantiationService.createInstance(ChangeEncodingAction, ChangeEncodingAction.ID, ChangeEncodingAction.LABEL);
const action = this.instantiationService.createInstance(ChangeEncodingAction, ChangeEncodingAction.ID, ChangeEncodingAction.LABEL);
action.run().done(null, errors.onUnexpectedError);
action.dispose();
......@@ -480,11 +480,11 @@ export class EditorStatus implements IStatusbarItem {
// We only support text based editors
if (editorWidget) {
let textModel = getTextModel(editorWidget);
const textModel = getTextModel(editorWidget);
if (textModel) {
// Compute mode
if (!!(<ITokenizedModel>textModel).getMode) {
let mode = (<ITokenizedModel>textModel).getMode();
const mode = (<ITokenizedModel>textModel).getMode();
if (mode) {
info = { mode: this.modeService.getLanguageName(mode.getId()) };
}
......@@ -518,7 +518,7 @@ export class EditorStatus implements IStatusbarItem {
}
private onSelectionChange(editorWidget?: IEditor): void {
let info: IEditorSelectionStatus = {};
const info: IEditorSelectionStatus = {};
// We only support text based editors
if (editorWidget) {
......@@ -528,7 +528,7 @@ export class EditorStatus implements IStatusbarItem {
// Compute selection length
info.charactersSelected = 0;
let textModel = getTextModel(editorWidget);
const textModel = getTextModel(editorWidget);
if (textModel) {
info.selections.forEach((selection) => {
info.charactersSelected += textModel.getValueLengthInRange(selection);
......@@ -537,9 +537,9 @@ export class EditorStatus implements IStatusbarItem {
// Compute the visible column for one selection. This will properly handle tabs and their configured widths
if (info.selections.length === 1) {
let visibleColumn = editorWidget.getVisibleColumnFromPosition(editorWidget.getPosition());
const visibleColumn = editorWidget.getVisibleColumnFromPosition(editorWidget.getPosition());
let selectionClone = info.selections[0].clone(); // do not modify the original position we got from the editor
const selectionClone = info.selections[0].clone(); // do not modify the original position we got from the editor
selectionClone.positionColumn = visibleColumn;
info.selections[0] = selectionClone;
......@@ -550,11 +550,11 @@ export class EditorStatus implements IStatusbarItem {
}
private onEOLChange(editorWidget?: IEditor): void {
let info: StateDelta = { EOL: null };
const info: StateDelta = { EOL: null };
let codeEditor = getCodeEditor(editorWidget);
const codeEditor = getCodeEditor(editorWidget);
if (codeEditor && !codeEditor.getConfiguration().readOnly) {
let codeEditorModel = codeEditor.getModel();
const codeEditorModel = codeEditor.getModel();
if (codeEditorModel) {
info.EOL = codeEditorModel.getEOL();
}
......@@ -568,14 +568,14 @@ export class EditorStatus implements IStatusbarItem {
return;
}
let info: StateDelta = { encoding: null };
const info: StateDelta = { encoding: null };
// We only support text based editors
if (e instanceof BaseTextEditor) {
let encodingSupport: IEncodingSupport = <any>asFileOrUntitledEditorInput(e.input);
const encodingSupport: IEncodingSupport = <any>asFileOrUntitledEditorInput(e.input);
if (encodingSupport && types.isFunction(encodingSupport.getEncoding)) {
let rawEncoding = encodingSupport.getEncoding();
let encodingInfo = SUPPORTED_ENCODINGS[rawEncoding];
const rawEncoding = encodingSupport.getEncoding();
const encodingInfo = SUPPORTED_ENCODINGS[rawEncoding];
if (encodingInfo) {
info.encoding = encodingInfo.labelShort; // if we have a label, take it from there
} else {
......@@ -588,9 +588,9 @@ export class EditorStatus implements IStatusbarItem {
}
private onResourceEncodingChange(resource: uri): void {
let activeEditor = this.editorService.getActiveEditor();
const activeEditor = this.editorService.getActiveEditor();
if (activeEditor) {
let activeResource = getUntitledOrFileResource(activeEditor.input, true);
const activeResource = getUntitledOrFileResource(activeEditor.input, true);
if (activeResource && activeResource.toString() === resource.toString()) {
return this.onEncodingChange(<IBaseEditor>activeEditor); // only update if the encoding changed for the active resource
}
......@@ -598,13 +598,13 @@ export class EditorStatus implements IStatusbarItem {
}
private onTabFocusModeChange(): void {
let info: StateDelta = { tabFocusMode: TabFocus.getTabFocusMode() };
const info: StateDelta = { tabFocusMode: TabFocus.getTabFocusMode() };
this.updateState(info);
}
private isActiveEditor(e: IBaseEditor): boolean {
let activeEditor = this.editorService.getActiveEditor();
const activeEditor = this.editorService.getActiveEditor();
return activeEditor && e && activeEditor === e;
}
......@@ -638,27 +638,27 @@ export class ChangeModeAction extends Action {
}
public run(): TPromise<any> {
let languages = this.modeService.getRegisteredLanguageNames();
const languages = this.modeService.getRegisteredLanguageNames();
let activeEditor = this.editorService.getActiveEditor();
if (!(activeEditor instanceof BaseTextEditor)) {
return this.quickOpenService.pick([{ label: nls.localize('noEditor', "No text editor active at this time") }]);
}
let editorWidget = (<BaseTextEditor>activeEditor).getControl();
let textModel = getTextModel(editorWidget);
let fileinput = asFileEditorInput(activeEditor.input, true);
const editorWidget = (<BaseTextEditor>activeEditor).getControl();
const textModel = getTextModel(editorWidget);
const fileinput = asFileEditorInput(activeEditor.input, true);
// Compute mode
let currentModeId: string;
if (!!(<ITokenizedModel>textModel).getMode) {
let mode = (<ITokenizedModel>textModel).getMode();
const mode = (<ITokenizedModel>textModel).getMode();
if (mode) {
currentModeId = this.modeService.getLanguageName(mode.getId());
}
}
// All languages are valid picks
let picks: IPickOpenEntry[] = languages.sort().map((lang, index) => {
const picks: IPickOpenEntry[] = languages.sort().map((lang, index) => {
return {
label: lang,
description: currentModeId === lang ? nls.localize('configuredLanguage', "Configured Language") : void 0
......@@ -676,13 +676,13 @@ export class ChangeModeAction extends Action {
}
}
let configureModeAssociations: IPickOpenEntry = {
const configureModeAssociations: IPickOpenEntry = {
label: configureLabel
};
picks.unshift(configureModeAssociations);
// Offer to "Auto Detect"
let autoDetectMode: IPickOpenEntry = {
const autoDetectMode: IPickOpenEntry = {
label: nls.localize('autoDetect', "Auto Detect")
};
if (fileinput) {
......@@ -693,14 +693,14 @@ export class ChangeModeAction extends Action {
if (language) {
activeEditor = this.editorService.getActiveEditor();
if (activeEditor instanceof BaseTextEditor) {
let editorWidget = activeEditor.getControl();
let models: ITextModel[] = [];
const editorWidget = activeEditor.getControl();
const models: ITextModel[] = [];
let textModel = getTextModel(editorWidget);
const textModel = getTextModel(editorWidget);
models.push(textModel);
// Support for original side of diff
let model = editorWidget.getModel();
const model = editorWidget.getModel();
if (model && !!(<IDiffEditorModel>model).original) {
models.push((<IDiffEditorModel>model).original);
}
......@@ -799,7 +799,6 @@ export class ChangeEOLAction extends Action {
}
public run(): TPromise<any> {
let activeEditor = this.editorService.getActiveEditor();
if (!(activeEditor instanceof BaseTextEditor)) {
return this.quickOpenService.pick([{ label: nls.localize('noEditor', "No text editor active at this time") }]);
......@@ -809,22 +808,22 @@ export class ChangeEOLAction extends Action {
return this.quickOpenService.pick([{ label: nls.localize('noWritableCodeEditor', "The active code editor is read-only.") }]);
}
let editorWidget = (<BaseTextEditor>activeEditor).getControl();
let textModel = getTextModel(editorWidget);
const editorWidget = (<BaseTextEditor>activeEditor).getControl();
const textModel = getTextModel(editorWidget);
let EOLOptions: IChangeEOLEntry[] = [
const EOLOptions: IChangeEOLEntry[] = [
{ label: nlsEOLLF, eol: EndOfLineSequence.LF },
{ label: nlsEOLCRLF, eol: EndOfLineSequence.CRLF },
];
let selectedIndex = (textModel.getEOL() === '\n') ? 0 : 1;
const selectedIndex = (textModel.getEOL() === '\n') ? 0 : 1;
return this.quickOpenService.pick(EOLOptions, { placeHolder: nls.localize('pickEndOfLine', "Select End of Line Sequence"), autoFocus: { autoFocusIndex: selectedIndex } }).then((eol) => {
if (eol) {
activeEditor = this.editorService.getActiveEditor();
if (activeEditor instanceof BaseTextEditor && isWritableCodeEditor(activeEditor)) {
let editorWidget = activeEditor.getControl();
let textModel = getTextModel(editorWidget);
const editorWidget = activeEditor.getControl();
const textModel = getTextModel(editorWidget);
textModel.setEOL(eol.eol);
}
}
......@@ -886,13 +885,13 @@ export class ChangeEncodingAction extends Action {
return TPromise.timeout(50 /* quick open is sensitive to being opened so soon after another */).then(() => {
const configuration = this.configurationService.getConfiguration<IFilesConfiguration>();
let isReopenWithEncoding = (action === reopenWithEncodingPick);
let configuredEncoding = configuration && configuration.files && configuration.files.encoding;
const isReopenWithEncoding = (action === reopenWithEncodingPick);
const configuredEncoding = configuration && configuration.files && configuration.files.encoding;
let directMatchIndex: number;
let aliasMatchIndex: number;
// All encodings are valid picks
let picks: IPickOpenEntry[] = Object.keys(SUPPORTED_ENCODINGS)
const picks: IPickOpenEntry[] = Object.keys(SUPPORTED_ENCODINGS)
.sort((k1, k2) => {
if (k1 === configuredEncoding) {
return -1;
......
......@@ -122,10 +122,14 @@ export class TextFileEditor extends BaseTextEditor {
// Check Model state
const textFileModel = <TextFileEditorModel>resolvedModel;
const hasInput = !!this.getInput();
const modelDisposed = textFileModel.isDisposed();
const inputChanged = (<FileEditorInput>this.getInput()).getResource().toString() !== textFileModel.getResource().toString();
if (
!this.getInput() || // editor got hidden meanwhile
textFileModel.isDisposed() || // input got disposed meanwhile
(<FileEditorInput>this.getInput()).getResource().toString() !== textFileModel.getResource().toString() // a different input was set meanwhile
!hasInput || // editor got hidden meanwhile
modelDisposed || // input got disposed meanwhile
inputChanged // a different input was set meanwhile
) {
return null;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册