diff --git a/src/tsconfig.strictNullChecks.json b/src/tsconfig.strictNullChecks.json index 18158fefc1f..87a7602d67a 100644 --- a/src/tsconfig.strictNullChecks.json +++ b/src/tsconfig.strictNullChecks.json @@ -330,6 +330,7 @@ "./vs/editor/contrib/hover/hoverWidgets.ts", "./vs/editor/contrib/hover/modesGlyphHover.ts", "./vs/editor/contrib/inPlaceReplace/inPlaceReplaceCommand.ts", + "./vs/editor/contrib/indentation/indentation.ts", "./vs/editor/contrib/indentation/indentUtils.ts", "./vs/editor/contrib/linesOperations/copyLinesCommand.ts", "./vs/editor/contrib/linesOperations/deleteLinesCommand.ts", diff --git a/src/vs/editor/contrib/indentation/indentation.ts b/src/vs/editor/contrib/indentation/indentation.ts index ca4ac2590ab..85963260837 100644 --- a/src/vs/editor/contrib/indentation/indentation.ts +++ b/src/vs/editor/contrib/indentation/indentation.ts @@ -10,11 +10,11 @@ import { ICodeEditor } from 'vs/editor/browser/editorBrowser'; import { EditorAction, IActionOptions, ServicesAccessor, registerEditorAction, registerEditorContribution } from 'vs/editor/browser/editorExtensions'; import { ShiftCommand } from 'vs/editor/common/commands/shiftCommand'; import { EditOperation } from 'vs/editor/common/core/editOperation'; -import { Range } from 'vs/editor/common/core/range'; +import { Range, IRange } from 'vs/editor/common/core/range'; import { Selection } from 'vs/editor/common/core/selection'; import { ICommand, ICursorStateComputerData, IEditOperationBuilder, IEditorContribution } from 'vs/editor/common/editorCommon'; import { EditorContextKeys } from 'vs/editor/common/editorContextKeys'; -import { IIdentifiedSingleEditOperation, ITextModel } from 'vs/editor/common/model'; +import { IIdentifiedSingleEditOperation, ITextModel, EndOfLineSequence } from 'vs/editor/common/model'; import { TextModel } from 'vs/editor/common/model/textModel'; import { StandardTokenType, TextEdit } from 'vs/editor/common/modes'; import { LanguageConfigurationRegistry } from 'vs/editor/common/modes/languageConfigurationRegistry'; @@ -48,12 +48,12 @@ export function unshiftIndent(tabSize: number, indentation: string, count?: numb export function getReindentEditOperations(model: ITextModel, startLineNumber: number, endLineNumber: number, inheritedIndent?: string): IIdentifiedSingleEditOperation[] { if (model.getLineCount() === 1 && model.getLineMaxColumn(1) === 1) { // Model is empty - return undefined; + return []; } let indentationRules = LanguageConfigurationRegistry.getIndentationRules(model.getLanguageIdentifier().id); if (!indentationRules) { - return undefined; + return []; } endLineNumber = Math.min(endLineNumber, model.getLineCount()); @@ -73,7 +73,7 @@ export function getReindentEditOperations(model: ITextModel, startLineNumber: nu } if (startLineNumber > endLineNumber - 1) { - return undefined; + return []; } let { tabSize, insertSpaces } = model.getOptions(); @@ -167,7 +167,11 @@ export class IndentationToSpacesAction extends EditorAction { return; } let modelOpts = model.getOptions(); - const command = new IndentationToSpacesCommand(editor.getSelection(), modelOpts.tabSize); + let selection = editor.getSelection(); + if (!selection) { + return; + } + const command = new IndentationToSpacesCommand(selection, modelOpts.tabSize); editor.pushUndoStop(); editor.executeCommands(this.id, [command]); @@ -197,7 +201,11 @@ export class IndentationToTabsAction extends EditorAction { return; } let modelOpts = model.getOptions(); - const command = new IndentationToTabsCommand(editor.getSelection(), modelOpts.tabSize); + let selection = editor.getSelection(); + if (!selection) { + return; + } + const command = new IndentationToTabsCommand(selection, modelOpts.tabSize); editor.pushUndoStop(); editor.executeCommands(this.id, [command]); @@ -229,7 +237,7 @@ export class ChangeIndentationSizeAction extends EditorAction { id: n.toString(), label: n.toString(), // add description for tabSize value set in the configuration - description: n === creationOpts.tabSize ? nls.localize('configuredTabSize', "Configured Tab Size") : null + description: n === creationOpts.tabSize ? nls.localize('configuredTabSize', "Configured Tab Size") : undefined })); // auto focus the tabSize set for the current editor @@ -238,10 +246,12 @@ export class ChangeIndentationSizeAction extends EditorAction { setTimeout(() => { quickInputService.pick(picks, { placeHolder: nls.localize({ key: 'selectTabWidth', comment: ['Tab corresponds to the tab key'] }, "Select Tab Size for Current File"), activeItem: picks[autoFocusIndex] }).then(pick => { if (pick) { - model.updateOptions({ - tabSize: parseInt(pick.label, 10), - insertSpaces: this.insertSpaces - }); + if (model && !model.isDisposed()) { + model.updateOptions({ + tabSize: parseInt(pick.label, 10), + insertSpaces: this.insertSpaces + }); + } } }); }, 50/* quick open is sensitive to being opened so soon after another */); @@ -318,7 +328,7 @@ export class ReindentLinesAction extends EditorAction { return; } let edits = getReindentEditOperations(model, 1, model.getLineCount()); - if (edits) { + if (edits.length > 0) { editor.pushUndoStop(); editor.executeEdits(this.id, edits); editor.pushUndoStop(); @@ -342,9 +352,14 @@ export class ReindentSelectedLinesAction extends EditorAction { return; } + let selections = editor.getSelections(); + if (selections === null) { + return; + } + let edits: IIdentifiedSingleEditOperation[] = []; - for (let selection of editor.getSelections()) { + for (let selection of selections) { let startLineNumber = selection.startLineNumber; let endLineNumber = selection.endLineNumber; @@ -360,7 +375,7 @@ export class ReindentSelectedLinesAction extends EditorAction { startLineNumber--; } - let editOperations = getReindentEditOperations(model, startLineNumber, endLineNumber) || []; + let editOperations = getReindentEditOperations(model, startLineNumber, endLineNumber); edits.push(...editOperations); } @@ -374,7 +389,7 @@ export class ReindentSelectedLinesAction extends EditorAction { export class AutoIndentOnPasteCommand implements ICommand { - private _edits: TextEdit[]; + private _edits: { range: IRange; text: string; eol?: EndOfLineSequence; }[]; private _initialSelection: Selection; private _selectionId: string; @@ -385,7 +400,7 @@ export class AutoIndentOnPasteCommand implements ICommand { for (let edit of edits) { if (edit.range && typeof edit.text === 'string') { - this._edits.push(edit); + this._edits.push(edit as { range: IRange; text: string; eol?: EndOfLineSequence; }); } } } @@ -456,11 +471,16 @@ export class AutoIndentOnPaste implements IEditorContribution { } private trigger(range: Range): void { - if (this.editor.getSelections().length > 1) { + let selections = this.editor.getSelections(); + if (selections === null || selections.length > 1) { return; } const model = this.editor.getModel(); + if (!model) { + return; + } + if (!model.isCheapToTokenize(range.getStartPosition().lineNumber)) { return; } @@ -588,7 +608,7 @@ export class AutoIndentOnPaste implements IEditorContribution { } } - let cmd = new AutoIndentOnPasteCommand(textEdits, this.editor.getSelection()); + let cmd = new AutoIndentOnPasteCommand(textEdits, this.editor.getSelection()!); this.editor.executeCommand('autoIndentOnPaste', cmd); this.editor.pushUndoStop(); }