From 46d1426b7e9c333cec7e726f01ec17fedcfddb93 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Fri, 12 Oct 2018 08:45:49 +0200 Subject: [PATCH] Strict null checks (#60565) --- src/tsconfig.strictNullChecks.json | 18 ++- src/vs/base/browser/dom.ts | 43 +++---- src/vs/base/common/types.ts | 4 +- .../editor/browser/config/charWidthReader.ts | 14 +-- .../editor/browser/controller/coreCommands.ts | 29 +++-- .../browser/controller/textAreaState.ts | 21 ++-- src/vs/editor/common/controller/cursor.ts | 6 +- .../common/controller/cursorCollection.ts | 6 +- .../editor/common/controller/cursorCommon.ts | 46 +++++-- .../common/controller/cursorMoveCommands.ts | 114 +++++++++--------- .../common/controller/cursorWordOperations.ts | 54 +++++---- src/vs/editor/common/controller/oneCursor.ts | 7 +- src/vs/editor/common/core/rgba.ts | 2 + src/vs/editor/common/diff/diffComputer.ts | 10 +- src/vs/editor/common/editorCommon.ts | 4 +- src/vs/editor/common/model.ts | 2 +- src/vs/editor/common/model/textModel.ts | 2 +- src/vs/editor/common/modes.ts | 2 +- .../modes/supports/inplaceReplaceSupport.ts | 12 +- .../common/modes/supports/tokenization.ts | 12 +- .../common/modes/textToHtmlTokenizer.ts | 2 +- .../editor/common/view/minimapCharRenderer.ts | 4 +- .../common/view/runtimeMinimapCharRenderer.ts | 8 +- src/vs/editor/common/view/viewEvents.ts | 8 +- src/vs/monaco.d.ts | 4 +- src/vs/platform/commands/common/commands.ts | 10 +- .../contextkey/browser/contextKeyService.ts | 6 +- .../platform/contextkey/common/contextkey.ts | 6 +- 28 files changed, 248 insertions(+), 208 deletions(-) diff --git a/src/tsconfig.strictNullChecks.json b/src/tsconfig.strictNullChecks.json index 033ac9e46e9..43c3c987864 100644 --- a/src/tsconfig.strictNullChecks.json +++ b/src/tsconfig.strictNullChecks.json @@ -7,6 +7,7 @@ "include": [ "./typings", "./vs/base/browser/browser.ts", + "./vs/base/browser/dom.ts", "./vs/base/browser/event.ts", "./vs/base/browser/history.ts", "./vs/base/browser/iframe.ts", @@ -92,8 +93,12 @@ "./vs/code/electron-main/keyboard.ts", "./vs/code/electron-main/theme.ts", "./vs/code/node/shellEnv.ts", + "./vs/editor/browser/config/charWidthReader.ts", "./vs/editor/browser/config/elementSizeObserver.ts", + "./vs/editor/browser/controller/textAreaState.ts", + "./vs/editor/common/commands/replaceCommand.ts", "./vs/editor/common/commands/surroundSelectionCommand.ts", + "./vs/editor/common/commands/trimTrailingWhitespaceCommand.ts", "./vs/editor/common/config/editorOptions.ts", "./vs/editor/common/config/editorZoom.ts", "./vs/editor/common/config/fontInfo.ts", @@ -109,6 +114,7 @@ "./vs/editor/common/core/stringBuilder.ts", "./vs/editor/common/core/token.ts", "./vs/editor/common/core/uint.ts", + "./vs/editor/common/diff/diffComputer.ts", "./vs/editor/common/editorAction.ts", "./vs/editor/common/editorCommon.ts", "./vs/editor/common/editorContextKeys.ts", @@ -138,15 +144,21 @@ "./vs/editor/common/modes/supports/characterPair.ts", "./vs/editor/common/modes/supports/electricCharacter.ts", "./vs/editor/common/modes/supports/indentRules.ts", + "./vs/editor/common/modes/supports/inplaceReplaceSupport.ts", "./vs/editor/common/modes/supports/onEnter.ts", "./vs/editor/common/modes/supports/richEditBrackets.ts", + "./vs/editor/common/modes/supports/tokenization.ts", + "./vs/editor/common/modes/textToHtmlTokenizer.ts", "./vs/editor/common/modes/tokenizationRegistry.ts", "./vs/editor/common/services/editorWorkerService.ts", - "./vs/editor/common/services/modeService.ts", "./vs/editor/common/services/modelService.ts", + "./vs/editor/common/services/modeService.ts", "./vs/editor/common/services/resolverService.ts", "./vs/editor/common/standalone/standaloneBase.ts", + "./vs/editor/common/view/minimapCharRenderer.ts", "./vs/editor/common/view/overviewZoneManager.ts", + "./vs/editor/common/view/runtimeMinimapCharRenderer.ts", + "./vs/editor/common/view/viewEvents.ts", "./vs/editor/common/viewLayout/whitespaceComputer.ts", "./vs/editor/common/viewModel/prefixSumComputer.ts", "./vs/editor/contrib/codeAction/codeActionTrigger.ts", @@ -154,8 +166,8 @@ "./vs/editor/contrib/find/findState.ts", "./vs/editor/contrib/find/replaceAllCommand.ts", "./vs/editor/contrib/find/replacePattern.ts", - "./vs/editor/contrib/inPlaceReplace/inPlaceReplaceCommand.ts", "./vs/editor/contrib/indentation/indentUtils.ts", + "./vs/editor/contrib/inPlaceReplace/inPlaceReplaceCommand.ts", "./vs/editor/contrib/linesOperations/copyLinesCommand.ts", "./vs/editor/standalone/common/monarch/monarchCommon.ts", "./vs/editor/standalone/common/monarch/monarchCompile.ts", @@ -202,8 +214,8 @@ "./vs/platform/opener/common/opener.ts", "./vs/platform/output/node/outputAppender.ts", "./vs/platform/progress/common/progress.ts", - "./vs/platform/quickOpen/common/quickOpen.ts", "./vs/platform/quickinput/common/quickInput.ts", + "./vs/platform/quickOpen/common/quickOpen.ts", "./vs/platform/registry/common/platform.ts", "./vs/platform/search/common/search.ts", "./vs/platform/state/common/state.ts", diff --git a/src/vs/base/browser/dom.ts b/src/vs/base/browser/dom.ts index b396fbd5f11..4c7c9ae2240 100644 --- a/src/vs/base/browser/dom.ts +++ b/src/vs/base/browser/dom.ts @@ -20,7 +20,7 @@ export function clearNode(node: HTMLElement): void { } } -export function isInDOM(node: Node): boolean { +export function isInDOM(node: Node | null): boolean { while (node) { if (node === document.body) { return true; @@ -151,7 +151,7 @@ const _manualClassList = new class implements IDomClassList { const _nativeClassList = new class implements IDomClassList { hasClass(node: HTMLElement, className: string): boolean { - return className && node.classList && node.classList.contains(className); + return Boolean(className) && node.classList && node.classList.contains(className); } addClasses(node: HTMLElement, ...classNames: string[]): void { @@ -198,7 +198,7 @@ class DomListener implements IDisposable { private readonly _type: string; private readonly _useCapture: boolean; - constructor(node: Element | Window | Document, type: string, handler: (e: any) => void, useCapture: boolean) { + constructor(node: Element | Window | Document, type: string, handler: (e: any) => void, useCapture?: boolean) { this._node = node; this._type = type; this._handler = handler; @@ -215,8 +215,8 @@ class DomListener implements IDisposable { this._node.removeEventListener(this._type, this._handler, this._useCapture); // Prevent leakers from holding on to the dom or handler func - this._node = null; - this._handler = null; + this._node = null!; + this._handler = null!; } } @@ -257,7 +257,7 @@ export let addStandardDisposableListener: IAddStandardDisposableListenerSignatur export function addDisposableNonBubblingMouseOutListener(node: Element, handler: (event: MouseEvent) => void): IDisposable { return addDisposableListener(node, 'mouseout', (e: MouseEvent) => { // Mouse out bubbles, so this is an attempt to ignore faux mouse outs coming from children elements - let toElement = (e.relatedTarget || e.toElement); + let toElement: Node | null = (e.relatedTarget || e.toElement); while (toElement && toElement !== node) { toElement = toElement.parentNode; } @@ -311,7 +311,7 @@ class AnimationFrameQueueItem implements IDisposable { public priority: number; private _canceled: boolean; - constructor(runner: () => void, priority: number) { + constructor(runner: () => void, priority: number = 0) { this._runner = runner; this.priority = priority; this._canceled = false; @@ -366,7 +366,7 @@ class AnimationFrameQueueItem implements IDisposable { inAnimationFrameRunner = true; while (CURRENT_QUEUE.length > 0) { CURRENT_QUEUE.sort(AnimationFrameQueueItem.sort); - let top = CURRENT_QUEUE.shift(); + let top = CURRENT_QUEUE.shift()!; top.execute(); } inAnimationFrameRunner = false; @@ -387,7 +387,7 @@ class AnimationFrameQueueItem implements IDisposable { runAtThisOrScheduleAtNextAnimationFrame = (runner: () => void, priority?: number) => { if (inAnimationFrameRunner) { let item = new AnimationFrameQueueItem(runner, priority); - CURRENT_QUEUE.push(item); + CURRENT_QUEUE!.push(item); return item; } else { return scheduleAtNextAnimationFrame(runner, priority); @@ -407,7 +407,7 @@ export function modify(callback: () => void): IDisposable { * Add a throttled listener. `handler` is fired at most every 16ms or with the next animation frame (if browser supports it). */ export interface IEventMerger { - (lastEvent: R, currentEvent: E): R; + (lastEvent: R | null, currentEvent: E): R; } export interface DOMEvent { @@ -431,7 +431,7 @@ class TimeoutThrottledDomListener extends Disposable { let invokeHandler = () => { lastHandlerTime = (new Date()).getTime(); - handler(lastEvent); + handler(lastEvent); lastEvent = null; }; @@ -455,7 +455,7 @@ export function addDisposableThrottledListener } export function getComputedStyle(el: HTMLElement): CSSStyleDeclaration { - return document.defaultView.getComputedStyle(el, null); + return document.defaultView!.getComputedStyle(el, null); } // Adapted from WinJS @@ -660,7 +660,7 @@ export const StandardWindow: IStandardWindow = new class implements IStandardWin // modern browsers return window.scrollX; } else { - return document.body.scrollLeft + document.documentElement.scrollLeft; + return document.body.scrollLeft + document.documentElement!.scrollLeft; } } @@ -669,7 +669,7 @@ export const StandardWindow: IStandardWindow = new class implements IStandardWin // modern browsers return window.scrollY; } else { - return document.body.scrollTop + document.documentElement.scrollTop; + return document.body.scrollTop + document.documentElement!.scrollTop; } } }; @@ -728,7 +728,7 @@ export function getLargestChildWidth(parent: HTMLElement, children: HTMLElement[ // ---------------------------------------------------------------------------------------- -export function isAncestor(testChild: Node, testAncestor: Node): boolean { +export function isAncestor(testChild: Node | null, testAncestor: Node): boolean { while (testChild) { if (testChild === testAncestor) { return true; @@ -739,7 +739,7 @@ export function isAncestor(testChild: Node, testAncestor: Node): boolean { return false; } -export function findParentWithClass(node: HTMLElement, clazz: string, stopAtClazzOrNode?: string | HTMLElement): HTMLElement { +export function findParentWithClass(node: HTMLElement, clazz: string, stopAtClazzOrNode?: string | HTMLElement): HTMLElement | null { while (node) { if (hasClass(node, clazz)) { return node; @@ -1002,17 +1002,18 @@ export function $(description: string, attrs?: { [key: st result.className = match[4].replace(/\./g, ' ').trim(); } - Object.keys(attrs || {}).forEach(name => { + attrs = attrs || {}; + Object.keys(attrs).forEach(name => { + const value = attrs![name]; if (/^on\w+$/.test(name)) { - (result)[name] = attrs[name]; + (result)[name] = value; } else if (name === 'selected') { - const value = attrs[name]; if (value) { result.setAttribute(name, 'true'); } } else { - result.setAttribute(name, attrs[name]); + result.setAttribute(name, value); } }); @@ -1061,7 +1062,7 @@ export function hide(...elements: HTMLElement[]): void { } } -function findParentWithAttribute(node: Node, attribute: string): HTMLElement { +function findParentWithAttribute(node: Node | null, attribute: string): HTMLElement | null { while (node) { if (node instanceof HTMLElement && node.hasAttribute(attribute)) { return node; diff --git a/src/vs/base/common/types.ts b/src/vs/base/common/types.ts index 865c7f0e0bf..31591c49df1 100644 --- a/src/vs/base/common/types.ts +++ b/src/vs/base/common/types.ts @@ -129,14 +129,14 @@ export function areFunctions(...objects: any[]): boolean { export type TypeConstraint = string | Function; -export function validateConstraints(args: any[], constraints: TypeConstraint[]): void { +export function validateConstraints(args: any[], constraints: (TypeConstraint | undefined)[]): void { const len = Math.min(args.length, constraints.length); for (let i = 0; i < len; i++) { validateConstraint(args[i], constraints[i]); } } -export function validateConstraint(arg: any, constraint: TypeConstraint): void { +export function validateConstraint(arg: any, constraint: TypeConstraint | undefined): void { if (isString(constraint)) { if (typeof arg !== constraint) { diff --git a/src/vs/editor/browser/config/charWidthReader.ts b/src/vs/editor/browser/config/charWidthReader.ts index 3d92f7a4ba3..9f35141df60 100644 --- a/src/vs/editor/browser/config/charWidthReader.ts +++ b/src/vs/editor/browser/config/charWidthReader.ts @@ -33,8 +33,8 @@ class DomCharWidthReader { private readonly _bareFontInfo: BareFontInfo; private readonly _requests: CharWidthRequest[]; - private _container: HTMLElement; - private _testElements: HTMLSpanElement[]; + private _container: HTMLElement | null; + private _testElements: HTMLSpanElement[] | null; constructor(bareFontInfo: BareFontInfo, requests: CharWidthRequest[]) { this._bareFontInfo = bareFontInfo; @@ -49,13 +49,13 @@ class DomCharWidthReader { this._createDomElements(); // Add the container to the DOM - document.body.appendChild(this._container); + document.body.appendChild(this._container!); // Read character widths this._readFromDomElements(); // Remove the container from the DOM - document.body.removeChild(this._container); + document.body.removeChild(this._container!); this._container = null; this._testElements = null; @@ -107,11 +107,11 @@ class DomCharWidthReader { parent = italicDomNode; } - parent.appendChild(document.createElement('br')); + parent!.appendChild(document.createElement('br')); let testElement = document.createElement('span'); DomCharWidthReader._render(testElement, request); - parent.appendChild(testElement); + parent!.appendChild(testElement); testElements[i] = testElement; } @@ -141,7 +141,7 @@ class DomCharWidthReader { private _readFromDomElements(): void { for (let i = 0, len = this._requests.length; i < len; i++) { const request = this._requests[i]; - const testElement = this._testElements[i]; + const testElement = this._testElements![i]; request.fulfill(testElement.offsetWidth / 256); } diff --git a/src/vs/editor/browser/controller/coreCommands.ts b/src/vs/editor/browser/controller/coreCommands.ts index 9a6b68ffe5d..dd0be15c2dd 100644 --- a/src/vs/editor/browser/controller/coreCommands.ts +++ b/src/vs/editor/browser/controller/coreCommands.ts @@ -7,7 +7,7 @@ import * as nls from 'vs/nls'; import { Position } from 'vs/editor/common/core/position'; import { Range } from 'vs/editor/common/core/range'; import * as editorCommon from 'vs/editor/common/editorCommon'; -import { CursorState, ICursors, RevealTarget, IColumnSelectData, CursorContext, EditOperationType } from 'vs/editor/common/controller/cursorCommon'; +import { CursorState, ICursors, RevealTarget, IColumnSelectData, CursorContext, EditOperationType, PartialCursorState } from 'vs/editor/common/controller/cursorCommon'; import { CursorChangeReason } from 'vs/editor/common/controller/cursorEvents'; import { CursorMoveCommands, CursorMove as CursorMove_ } from 'vs/editor/common/controller/cursorMoveCommands'; import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; @@ -703,14 +703,14 @@ export namespace CoreNavigationCommands { public runCoreEditorCommand(cursors: ICursors, args: any): void { const context = cursors.context; - let newState: CursorState; + let newState: PartialCursorState; if (args.wholeLine) { newState = CursorMoveCommands.line(context, cursors.getPrimaryCursor(), false, args.position, args.viewPosition); } else { newState = CursorMoveCommands.moveTo(context, cursors.getPrimaryCursor(), false, args.position, args.viewPosition); } - const states = cursors.getAll(); + const states: PartialCursorState[] = cursors.getAll(); // Check if we should remove a cursor (sort of like a toggle) if (states.length > 1) { @@ -766,8 +766,9 @@ export namespace CoreNavigationCommands { const lastAddedCursorIndex = cursors.getLastAddedCursorIndex(); - let newStates = cursors.getAll().slice(0); - newStates[lastAddedCursorIndex] = CursorMoveCommands.moveTo(context, newStates[lastAddedCursorIndex], true, args.position, args.viewPosition); + const states = cursors.getAll(); + let newStates: PartialCursorState[] = states.slice(0); + newStates[lastAddedCursorIndex] = CursorMoveCommands.moveTo(context, states[lastAddedCursorIndex], true, args.position, args.viewPosition); cursors.context.model.pushStackElement(); cursors.setStates( @@ -846,8 +847,8 @@ export namespace CoreNavigationCommands { cursors.reveal(true, RevealTarget.Primary, editorCommon.ScrollType.Smooth); } - private _exec(context: CursorContext, cursors: CursorState[]): CursorState[] { - let result: CursorState[] = []; + private _exec(context: CursorContext, cursors: CursorState[]): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; const lineNumber = cursor.modelState.position.lineNumber; @@ -925,8 +926,8 @@ export namespace CoreNavigationCommands { cursors.reveal(true, RevealTarget.Primary, editorCommon.ScrollType.Smooth); } - private _exec(context: CursorContext, cursors: CursorState[]): CursorState[] { - let result: CursorState[] = []; + private _exec(context: CursorContext, cursors: CursorState[]): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; const lineNumber = cursor.modelState.position.lineNumber; @@ -1245,8 +1246,9 @@ export namespace CoreNavigationCommands { const lastAddedCursorIndex = cursors.getLastAddedCursorIndex(); - let newStates = cursors.getAll().slice(0); - let lastAddedState = newStates[lastAddedCursorIndex]; + const states = cursors.getAll(); + let newStates: PartialCursorState[] = states.slice(0); + let lastAddedState = states[lastAddedCursorIndex]; newStates[lastAddedCursorIndex] = CursorMoveCommands.word(context, lastAddedState, lastAddedState.modelState.hasSelection(), args.position); context.model.pushStackElement(); @@ -1302,8 +1304,9 @@ export namespace CoreNavigationCommands { public runCoreEditorCommand(cursors: ICursors, args: any): void { const lastAddedCursorIndex = cursors.getLastAddedCursorIndex(); - let newStates = cursors.getAll().slice(0); - newStates[lastAddedCursorIndex] = CursorMoveCommands.line(cursors.context, newStates[lastAddedCursorIndex], this._inSelectionMode, args.position, args.viewPosition); + const states = cursors.getAll(); + let newStates: PartialCursorState[] = states.slice(0); + newStates[lastAddedCursorIndex] = CursorMoveCommands.line(cursors.context, states[lastAddedCursorIndex], this._inSelectionMode, args.position, args.viewPosition); cursors.context.model.pushStackElement(); cursors.setStates( diff --git a/src/vs/editor/browser/controller/textAreaState.ts b/src/vs/editor/browser/controller/textAreaState.ts index 350b9a93f5f..4f1dc67db86 100644 --- a/src/vs/editor/browser/controller/textAreaState.ts +++ b/src/vs/editor/browser/controller/textAreaState.ts @@ -35,10 +35,10 @@ export class TextAreaState { public readonly value: string; public readonly selectionStart: number; public readonly selectionEnd: number; - public readonly selectionStartPosition: Position; - public readonly selectionEndPosition: Position; + public readonly selectionStartPosition: Position | null; + public readonly selectionEndPosition: Position | null; - constructor(value: string, selectionStart: number, selectionEnd: number, selectionStartPosition: Position, selectionEndPosition: Position) { + constructor(value: string, selectionStart: number, selectionEnd: number, selectionStartPosition: Position | null, selectionEndPosition: Position | null) { this.value = value; this.selectionStart = selectionStart; this.selectionEnd = selectionEnd; @@ -66,7 +66,7 @@ export class TextAreaState { } } - public deduceEditorPosition(offset: number): [Position, number, number] { + public deduceEditorPosition(offset: number): [Position | null, number, number] { if (offset <= this.selectionStart) { const str = this.value.substring(offset, this.selectionStart); return this._finishDeduceEditorPosition(this.selectionStartPosition, str, -1); @@ -83,7 +83,7 @@ export class TextAreaState { return this._finishDeduceEditorPosition(this.selectionEndPosition, str2, -1); } - private _finishDeduceEditorPosition(anchor: Position, deltaText: string, signum: number): [Position, number, number] { + private _finishDeduceEditorPosition(anchor: Position | null, deltaText: string, signum: number): [Position | null, number, number] { let lineFeedCnt = 0; let lastLineFeedIndex = -1; while ((lastLineFeedIndex = deltaText.indexOf('\n', lastLineFeedIndex + 1)) !== -1) { @@ -247,21 +247,22 @@ export class PagedScreenReaderStrategy { let selectionEndPage = PagedScreenReaderStrategy._getPageOfLine(selection.endLineNumber); let selectionEndPageRange = PagedScreenReaderStrategy._getRangeForPage(selectionEndPage); - let pretextRange = selectionStartPageRange.intersectRanges(new Range(1, 1, selection.startLineNumber, selection.startColumn)); + let pretextRange = selectionStartPageRange.intersectRanges(new Range(1, 1, selection.startLineNumber, selection.startColumn))!; let pretext = model.getValueInRange(pretextRange, EndOfLinePreference.LF); let lastLine = model.getLineCount(); let lastLineMaxColumn = model.getLineMaxColumn(lastLine); - let posttextRange = selectionEndPageRange.intersectRanges(new Range(selection.endLineNumber, selection.endColumn, lastLine, lastLineMaxColumn)); + let posttextRange = selectionEndPageRange.intersectRanges(new Range(selection.endLineNumber, selection.endColumn, lastLine, lastLineMaxColumn))!; let posttext = model.getValueInRange(posttextRange, EndOfLinePreference.LF); - let text: string | null = null; + + let text: string; if (selectionStartPage === selectionEndPage || selectionStartPage + 1 === selectionEndPage) { // take full selection text = model.getValueInRange(selection, EndOfLinePreference.LF); } else { - let selectionRange1 = selectionStartPageRange.intersectRanges(selection); - let selectionRange2 = selectionEndPageRange.intersectRanges(selection); + let selectionRange1 = selectionStartPageRange.intersectRanges(selection)!; + let selectionRange2 = selectionEndPageRange.intersectRanges(selection)!; text = ( model.getValueInRange(selectionRange1, EndOfLinePreference.LF) + String.fromCharCode(8230) diff --git a/src/vs/editor/common/controller/cursor.ts b/src/vs/editor/common/controller/cursor.ts index 3aaa6ac8a87..629a737d5e2 100644 --- a/src/vs/editor/common/controller/cursor.ts +++ b/src/vs/editor/common/controller/cursor.ts @@ -11,7 +11,7 @@ import { Position } from 'vs/editor/common/core/position'; import { Range } from 'vs/editor/common/core/range'; import { Selection, SelectionDirection, ISelection } from 'vs/editor/common/core/selection'; import * as editorCommon from 'vs/editor/common/editorCommon'; -import { CursorColumns, CursorConfiguration, EditOperationResult, CursorContext, CursorState, RevealTarget, IColumnSelectData, ICursors, EditOperationType } from 'vs/editor/common/controller/cursorCommon'; +import { CursorColumns, CursorConfiguration, EditOperationResult, CursorContext, CursorState, RevealTarget, IColumnSelectData, ICursors, EditOperationType, PartialCursorState } from 'vs/editor/common/controller/cursorCommon'; import { DeleteOperations } from 'vs/editor/common/controller/cursorDeleteOperations'; import { TypeOperations } from 'vs/editor/common/controller/cursorTypeOperations'; import { RawContentChangedType } from 'vs/editor/common/model/textModelEvents'; @@ -191,7 +191,7 @@ export class Cursor extends viewEvents.ViewEventEmitter implements ICursors { return this._cursors.getAll(); } - public setStates(source: string, reason: CursorChangeReason, states: CursorState[]): void { + public setStates(source: string, reason: CursorChangeReason, states: PartialCursorState[]): void { if (states.length > Cursor.MAX_CURSOR_COUNT) { states = states.slice(0, Cursor.MAX_CURSOR_COUNT); this._onDidReachMaxCursorCount.fire(void 0); @@ -764,7 +764,7 @@ class CommandExecutor { let operations: IIdentifiedSingleEditOperation[] = []; let operationMinor = 0; - const addEditOperation = (selection: Range, text: string) => { + const addEditOperation = (selection: Range, text: string | null) => { if (selection.isEmpty() && text === '') { // This command wants to add a no-op => no thank you return; diff --git a/src/vs/editor/common/controller/cursorCollection.ts b/src/vs/editor/common/controller/cursorCollection.ts index e9a95685fa6..68b5ef1c279 100644 --- a/src/vs/editor/common/controller/cursorCollection.ts +++ b/src/vs/editor/common/controller/cursorCollection.ts @@ -6,7 +6,7 @@ import { OneCursor } from 'vs/editor/common/controller/oneCursor'; import { Selection, ISelection } from 'vs/editor/common/core/selection'; import { Position } from 'vs/editor/common/core/position'; -import { CursorState, CursorContext } from 'vs/editor/common/controller/cursorCommon'; +import { CursorState, CursorContext, PartialCursorState } from 'vs/editor/common/controller/cursorCommon'; export class CursorCollection { @@ -108,7 +108,7 @@ export class CursorCollection { return this.primaryCursor.asCursorState(); } - public setStates(states: CursorState[]): void { + public setStates(states: PartialCursorState[]): void { if (states === null) { return; } @@ -119,7 +119,7 @@ export class CursorCollection { /** * Creates or disposes secondary cursors as necessary to match the number of `secondarySelections`. */ - private _setSecondaryStates(secondaryStates: CursorState[]): void { + private _setSecondaryStates(secondaryStates: PartialCursorState[]): void { const secondaryCursorsLength = this.secondaryCursors.length; const secondaryStatesLength = secondaryStates.length; diff --git a/src/vs/editor/common/controller/cursorCommon.ts b/src/vs/editor/common/controller/cursorCommon.ts index 6761ce120b0..c324bf27a4f 100644 --- a/src/vs/editor/common/controller/cursorCommon.ts +++ b/src/vs/editor/common/controller/cursorCommon.ts @@ -51,7 +51,7 @@ export interface ICursors { getColumnSelectData(): IColumnSelectData; setColumnSelectData(columnSelectData: IColumnSelectData): void; - setStates(source: string, reason: CursorChangeReason, states: CursorState[]): void; + setStates(source: string, reason: CursorChangeReason, states: PartialCursorState[]): void; reveal(horizontal: boolean, target: RevealTarget, scrollType: ScrollType): void; revealRange(revealHorizontal: boolean, viewRange: Range, verticalType: VerticalRevealType, scrollType: ScrollType): void; @@ -93,7 +93,7 @@ export class CursorConfiguration { public readonly shouldAutoCloseBefore: { quote: (ch: string) => boolean, bracket: (ch: string) => boolean }; private readonly _languageIdentifier: LanguageIdentifier; - private _electricChars: { [key: string]: boolean; }; + private _electricChars: { [key: string]: boolean; } | null; public static shouldRecreate(e: IConfigurationChangedEvent): boolean { return ( @@ -179,7 +179,7 @@ export class CursorConfiguration { return TextModel.normalizeIndentation(str, this.tabSize, this.insertSpaces); } - private static _getElectricCharacters(languageIdentifier: LanguageIdentifier): string[] { + private static _getElectricCharacters(languageIdentifier: LanguageIdentifier): string[] | null { try { return LanguageConfigurationRegistry.getElectricCharacters(languageIdentifier.id); } catch (e) { @@ -188,7 +188,7 @@ export class CursorConfiguration { } } - private static _getAutoClosingPairs(languageIdentifier: LanguageIdentifier): IAutoClosingPair[] { + private static _getAutoClosingPairs(languageIdentifier: LanguageIdentifier): IAutoClosingPair[] | null { try { return LanguageConfigurationRegistry.getAutoClosingPairs(languageIdentifier.id); } catch (e) { @@ -220,7 +220,7 @@ export class CursorConfiguration { } } - private static _getSurroundingPairs(languageIdentifier: LanguageIdentifier): IAutoClosingPair[] { + private static _getSurroundingPairs(languageIdentifier: LanguageIdentifier): IAutoClosingPair[] | null { try { return LanguageConfigurationRegistry.getSurroundingPairs(languageIdentifier.id); } catch (e) { @@ -394,18 +394,40 @@ export class CursorContext { } } +export class PartialModelCursorState { + readonly modelState: SingleCursorState; + readonly viewState: null; + + constructor(modelState: SingleCursorState) { + this.modelState = modelState; + this.viewState = null; + } +} + +export class PartialViewCursorState { + readonly modelState: null; + readonly viewState: SingleCursorState; + + constructor(viewState: SingleCursorState) { + this.modelState = null; + this.viewState = viewState; + } +} + +export type PartialCursorState = CursorState | PartialModelCursorState | PartialViewCursorState; + export class CursorState { _cursorStateBrand: void; - public static fromModelState(modelState: SingleCursorState): CursorState { - return new CursorState(modelState, null); + public static fromModelState(modelState: SingleCursorState): PartialModelCursorState { + return new PartialModelCursorState(modelState); } - public static fromViewState(viewState: SingleCursorState): CursorState { - return new CursorState(null, viewState); + public static fromViewState(viewState: SingleCursorState): PartialViewCursorState { + return new PartialViewCursorState(viewState); } - public static fromModelSelection(modelSelection: ISelection): CursorState { + public static fromModelSelection(modelSelection: ISelection): PartialModelCursorState { const selectionStartLineNumber = modelSelection.selectionStartLineNumber; const selectionStartColumn = modelSelection.selectionStartColumn; const positionLineNumber = modelSelection.positionLineNumber; @@ -417,8 +439,8 @@ export class CursorState { return CursorState.fromModelState(modelState); } - public static fromModelSelections(modelSelections: ISelection[]): CursorState[] { - let states: CursorState[] = []; + public static fromModelSelections(modelSelections: ISelection[]): PartialModelCursorState[] { + let states: PartialModelCursorState[] = []; for (let i = 0, len = modelSelections.length; i < len; i++) { states[i] = this.fromModelSelection(modelSelections[i]); } diff --git a/src/vs/editor/common/controller/cursorMoveCommands.ts b/src/vs/editor/common/controller/cursorMoveCommands.ts index fac40969243..23c088e3193 100644 --- a/src/vs/editor/common/controller/cursorMoveCommands.ts +++ b/src/vs/editor/common/controller/cursorMoveCommands.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { SingleCursorState, ICursorSimpleModel, CursorState, CursorContext } from 'vs/editor/common/controller/cursorCommon'; +import { SingleCursorState, ICursorSimpleModel, CursorState, CursorContext, PartialCursorState } from 'vs/editor/common/controller/cursorCommon'; import { Position, IPosition } from 'vs/editor/common/core/position'; import { Range } from 'vs/editor/common/core/range'; import { MoveOperations } from 'vs/editor/common/controller/cursorMoveOperations'; @@ -13,8 +13,8 @@ import { ICommandHandlerDescription } from 'vs/platform/commands/common/commands export class CursorMoveCommands { - public static addCursorDown(context: CursorContext, cursors: CursorState[], useLogicalLine: boolean): CursorState[] { - let result: CursorState[] = [], resultLen = 0; + public static addCursorDown(context: CursorContext, cursors: CursorState[], useLogicalLine: boolean): PartialCursorState[] { + let result: PartialCursorState[] = [], resultLen = 0; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; result[resultLen++] = new CursorState(cursor.modelState, cursor.viewState); @@ -27,8 +27,8 @@ export class CursorMoveCommands { return result; } - public static addCursorUp(context: CursorContext, cursors: CursorState[], useLogicalLine: boolean): CursorState[] { - let result: CursorState[] = [], resultLen = 0; + public static addCursorUp(context: CursorContext, cursors: CursorState[], useLogicalLine: boolean): PartialCursorState[] { + let result: PartialCursorState[] = [], resultLen = 0; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; result[resultLen++] = new CursorState(cursor.modelState, cursor.viewState); @@ -41,8 +41,8 @@ export class CursorMoveCommands { return result; } - public static moveToBeginningOfLine(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): CursorState[] { - let result: CursorState[] = []; + public static moveToBeginningOfLine(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; result[i] = this._moveToLineStart(context, cursor, inSelectionMode); @@ -51,7 +51,7 @@ export class CursorMoveCommands { return result; } - private static _moveToLineStart(context: CursorContext, cursor: CursorState, inSelectionMode: boolean): CursorState { + private static _moveToLineStart(context: CursorContext, cursor: CursorState, inSelectionMode: boolean): PartialCursorState { const currentViewStateColumn = cursor.viewState.position.column; const currentModelStateColumn = cursor.modelState.position.column; const isFirstLineOfWrappedLine = currentViewStateColumn === currentModelStateColumn; @@ -67,20 +67,20 @@ export class CursorMoveCommands { } } - private static _moveToLineStartByView(context: CursorContext, cursor: CursorState, inSelectionMode: boolean): CursorState { + private static _moveToLineStartByView(context: CursorContext, cursor: CursorState, inSelectionMode: boolean): PartialCursorState { return CursorState.fromViewState( MoveOperations.moveToBeginningOfLine(context.config, context.viewModel, cursor.viewState, inSelectionMode) ); } - private static _moveToLineStartByModel(context: CursorContext, cursor: CursorState, inSelectionMode: boolean): CursorState { + private static _moveToLineStartByModel(context: CursorContext, cursor: CursorState, inSelectionMode: boolean): PartialCursorState { return CursorState.fromModelState( MoveOperations.moveToBeginningOfLine(context.config, context.model, cursor.modelState, inSelectionMode) ); } - public static moveToEndOfLine(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): CursorState[] { - let result: CursorState[] = []; + public static moveToEndOfLine(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; result[i] = this._moveToLineEnd(context, cursor, inSelectionMode); @@ -89,7 +89,7 @@ export class CursorMoveCommands { return result; } - private static _moveToLineEnd(context: CursorContext, cursor: CursorState, inSelectionMode: boolean): CursorState { + private static _moveToLineEnd(context: CursorContext, cursor: CursorState, inSelectionMode: boolean): PartialCursorState { const viewStatePosition = cursor.viewState.position; const viewModelMaxColumn = context.viewModel.getLineMaxColumn(viewStatePosition.lineNumber); const isEndOfViewLine = viewStatePosition.column === viewModelMaxColumn; @@ -105,20 +105,20 @@ export class CursorMoveCommands { } } - private static _moveToLineEndByView(context: CursorContext, cursor: CursorState, inSelectionMode: boolean): CursorState { + private static _moveToLineEndByView(context: CursorContext, cursor: CursorState, inSelectionMode: boolean): PartialCursorState { return CursorState.fromViewState( MoveOperations.moveToEndOfLine(context.config, context.viewModel, cursor.viewState, inSelectionMode) ); } - private static _moveToLineEndByModel(context: CursorContext, cursor: CursorState, inSelectionMode: boolean): CursorState { + private static _moveToLineEndByModel(context: CursorContext, cursor: CursorState, inSelectionMode: boolean): PartialCursorState { return CursorState.fromModelState( MoveOperations.moveToEndOfLine(context.config, context.model, cursor.modelState, inSelectionMode) ); } - public static expandLineSelection(context: CursorContext, cursors: CursorState[]): CursorState[] { - let result: CursorState[] = []; + public static expandLineSelection(context: CursorContext, cursors: CursorState[]): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; @@ -143,8 +143,8 @@ export class CursorMoveCommands { return result; } - public static moveToBeginningOfBuffer(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): CursorState[] { - let result: CursorState[] = []; + public static moveToBeginningOfBuffer(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; result[i] = CursorState.fromModelState(MoveOperations.moveToBeginningOfBuffer(context.config, context.model, cursor.modelState, inSelectionMode)); @@ -152,8 +152,8 @@ export class CursorMoveCommands { return result; } - public static moveToEndOfBuffer(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): CursorState[] { - let result: CursorState[] = []; + public static moveToEndOfBuffer(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; result[i] = CursorState.fromModelState(MoveOperations.moveToEndOfBuffer(context.config, context.model, cursor.modelState, inSelectionMode)); @@ -161,7 +161,7 @@ export class CursorMoveCommands { return result; } - public static selectAll(context: CursorContext, cursor: CursorState): CursorState { + public static selectAll(context: CursorContext, cursor: CursorState): PartialCursorState { const lineCount = context.model.getLineCount(); const maxColumn = context.model.getLineMaxColumn(lineCount); @@ -171,7 +171,7 @@ export class CursorMoveCommands { )); } - public static line(context: CursorContext, cursor: CursorState, inSelectionMode: boolean, _position: IPosition, _viewPosition: IPosition): CursorState { + public static line(context: CursorContext, cursor: CursorState, inSelectionMode: boolean, _position: IPosition, _viewPosition: IPosition): PartialCursorState { const position = context.model.validatePosition(_position); const viewPosition = ( _viewPosition @@ -230,12 +230,12 @@ export class CursorMoveCommands { } } - public static word(context: CursorContext, cursor: CursorState, inSelectionMode: boolean, _position: IPosition): CursorState { + public static word(context: CursorContext, cursor: CursorState, inSelectionMode: boolean, _position: IPosition): PartialCursorState { const position = context.model.validatePosition(_position); return CursorState.fromModelState(WordOperations.word(context.config, context.model, cursor.modelState, inSelectionMode, position)); } - public static cancelSelection(context: CursorContext, cursor: CursorState): CursorState { + public static cancelSelection(context: CursorContext, cursor: CursorState): PartialCursorState { if (!cursor.modelState.hasSelection()) { return new CursorState(cursor.modelState, cursor.viewState); } @@ -249,7 +249,7 @@ export class CursorMoveCommands { )); } - public static moveTo(context: CursorContext, cursor: CursorState, inSelectionMode: boolean, _position: IPosition, _viewPosition: IPosition): CursorState { + public static moveTo(context: CursorContext, cursor: CursorState, inSelectionMode: boolean, _position: IPosition, _viewPosition: IPosition): PartialCursorState { const position = context.model.validatePosition(_position); const viewPosition = ( _viewPosition @@ -259,7 +259,7 @@ export class CursorMoveCommands { return CursorState.fromViewState(cursor.viewState.move(inSelectionMode, viewPosition.lineNumber, viewPosition.column, 0)); } - public static move(context: CursorContext, cursors: CursorState[], args: CursorMove.ParsedArguments): CursorState[] { + public static move(context: CursorContext, cursors: CursorState[], args: CursorMove.ParsedArguments): PartialCursorState[] { const inSelectionMode = args.select; const value = args.value; @@ -347,7 +347,7 @@ export class CursorMoveCommands { case CursorMove.Direction.ViewPortIfOutside: { // Move to a position inside the viewport const visibleViewRange = context.getCompletelyVisibleViewRange(); - let result: CursorState[] = []; + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; result[i] = this.findPositionInViewportIfOutside(context, cursor, visibleViewRange, inSelectionMode); @@ -360,7 +360,7 @@ export class CursorMoveCommands { } - public static findPositionInViewportIfOutside(context: CursorContext, cursor: CursorState, visibleViewRange: Range, inSelectionMode: boolean): CursorState { + public static findPositionInViewportIfOutside(context: CursorContext, cursor: CursorState, visibleViewRange: Range, inSelectionMode: boolean): PartialCursorState { let viewLineNumber = cursor.viewState.position.lineNumber; if (visibleViewRange.startLineNumber <= viewLineNumber && viewLineNumber <= visibleViewRange.endLineNumber - 1) { @@ -405,8 +405,8 @@ export class CursorMoveCommands { return Math.max(startLineNumber, range.endLineNumber - count + 1); } - private static _moveLeft(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean, noOfColumns: number): CursorState[] { - let result: CursorState[] = []; + private static _moveLeft(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean, noOfColumns: number): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; @@ -426,8 +426,8 @@ export class CursorMoveCommands { return result; } - private static _moveHalfLineLeft(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): CursorState[] { - let result: CursorState[] = []; + private static _moveHalfLineLeft(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; const viewLineNumber = cursor.viewState.position.lineNumber; @@ -437,8 +437,8 @@ export class CursorMoveCommands { return result; } - private static _moveRight(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean, noOfColumns: number): CursorState[] { - let result: CursorState[] = []; + private static _moveRight(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean, noOfColumns: number): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; let newViewState = MoveOperations.moveRight(context.config, context.viewModel, cursor.viewState, inSelectionMode, noOfColumns); @@ -457,8 +457,8 @@ export class CursorMoveCommands { return result; } - private static _moveHalfLineRight(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): CursorState[] { - let result: CursorState[] = []; + private static _moveHalfLineRight(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; const viewLineNumber = cursor.viewState.position.lineNumber; @@ -468,8 +468,8 @@ export class CursorMoveCommands { return result; } - private static _moveDownByViewLines(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean, linesCount: number): CursorState[] { - let result: CursorState[] = []; + private static _moveDownByViewLines(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean, linesCount: number): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; result[i] = CursorState.fromViewState(MoveOperations.moveDown(context.config, context.viewModel, cursor.viewState, inSelectionMode, linesCount)); @@ -477,8 +477,8 @@ export class CursorMoveCommands { return result; } - private static _moveDownByModelLines(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean, linesCount: number): CursorState[] { - let result: CursorState[] = []; + private static _moveDownByModelLines(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean, linesCount: number): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; result[i] = CursorState.fromModelState(MoveOperations.moveDown(context.config, context.model, cursor.modelState, inSelectionMode, linesCount)); @@ -486,8 +486,8 @@ export class CursorMoveCommands { return result; } - private static _moveUpByViewLines(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean, linesCount: number): CursorState[] { - let result: CursorState[] = []; + private static _moveUpByViewLines(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean, linesCount: number): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; result[i] = CursorState.fromViewState(MoveOperations.moveUp(context.config, context.viewModel, cursor.viewState, inSelectionMode, linesCount)); @@ -495,8 +495,8 @@ export class CursorMoveCommands { return result; } - private static _moveUpByModelLines(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean, linesCount: number): CursorState[] { - let result: CursorState[] = []; + private static _moveUpByModelLines(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean, linesCount: number): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; result[i] = CursorState.fromModelState(MoveOperations.moveUp(context.config, context.model, cursor.modelState, inSelectionMode, linesCount)); @@ -504,16 +504,16 @@ export class CursorMoveCommands { return result; } - private static _moveToViewPosition(context: CursorContext, cursor: CursorState, inSelectionMode: boolean, toViewLineNumber: number, toViewColumn: number): CursorState { + private static _moveToViewPosition(context: CursorContext, cursor: CursorState, inSelectionMode: boolean, toViewLineNumber: number, toViewColumn: number): PartialCursorState { return CursorState.fromViewState(cursor.viewState.move(inSelectionMode, toViewLineNumber, toViewColumn, 0)); } - private static _moveToModelPosition(context: CursorContext, cursor: CursorState, inSelectionMode: boolean, toModelLineNumber: number, toModelColumn: number): CursorState { + private static _moveToModelPosition(context: CursorContext, cursor: CursorState, inSelectionMode: boolean, toModelLineNumber: number, toModelColumn: number): PartialCursorState { return CursorState.fromModelState(cursor.modelState.move(inSelectionMode, toModelLineNumber, toModelColumn, 0)); } - private static _moveToViewMinColumn(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): CursorState[] { - let result: CursorState[] = []; + private static _moveToViewMinColumn(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; const viewLineNumber = cursor.viewState.position.lineNumber; @@ -523,8 +523,8 @@ export class CursorMoveCommands { return result; } - private static _moveToViewFirstNonWhitespaceColumn(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): CursorState[] { - let result: CursorState[] = []; + private static _moveToViewFirstNonWhitespaceColumn(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; const viewLineNumber = cursor.viewState.position.lineNumber; @@ -534,8 +534,8 @@ export class CursorMoveCommands { return result; } - private static _moveToViewCenterColumn(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): CursorState[] { - let result: CursorState[] = []; + private static _moveToViewCenterColumn(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; const viewLineNumber = cursor.viewState.position.lineNumber; @@ -545,8 +545,8 @@ export class CursorMoveCommands { return result; } - private static _moveToViewMaxColumn(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): CursorState[] { - let result: CursorState[] = []; + private static _moveToViewMaxColumn(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; const viewLineNumber = cursor.viewState.position.lineNumber; @@ -556,8 +556,8 @@ export class CursorMoveCommands { return result; } - private static _moveToViewLastNonWhitespaceColumn(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): CursorState[] { - let result: CursorState[] = []; + private static _moveToViewLastNonWhitespaceColumn(context: CursorContext, cursors: CursorState[], inSelectionMode: boolean): PartialCursorState[] { + let result: PartialCursorState[] = []; for (let i = 0, len = cursors.length; i < len; i++) { const cursor = cursors[i]; const viewLineNumber = cursor.viewState.position.lineNumber; diff --git a/src/vs/editor/common/controller/cursorWordOperations.ts b/src/vs/editor/common/controller/cursorWordOperations.ts index c6969fd6937..ccda783109d 100644 --- a/src/vs/editor/common/controller/cursorWordOperations.ts +++ b/src/vs/editor/common/controller/cursorWordOperations.ts @@ -49,12 +49,12 @@ export class WordOperations { return { start: start, end: end, wordType: wordType, nextCharClass: nextCharClass }; } - private static _findPreviousWordOnLine(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, position: Position): IFindWordResult { + private static _findPreviousWordOnLine(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, position: Position): IFindWordResult | null { let lineContent = model.getLineContent(position.lineNumber); return this._doFindPreviousWordOnLine(lineContent, wordSeparators, position); } - private static _doFindPreviousWordOnLine(lineContent: string, wordSeparators: WordCharacterClassifier, position: Position): IFindWordResult { + private static _doFindPreviousWordOnLine(lineContent: string, wordSeparators: WordCharacterClassifier, position: Position): IFindWordResult | null { let wordType = WordType.None; for (let chIndex = position.column - 2; chIndex >= 0; chIndex--) { let chCode = lineContent.charCodeAt(chIndex); @@ -103,12 +103,12 @@ export class WordOperations { return len; } - private static _findNextWordOnLine(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, position: Position): IFindWordResult { + private static _findNextWordOnLine(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, position: Position): IFindWordResult | null { let lineContent = model.getLineContent(position.lineNumber); return this._doFindNextWordOnLine(lineContent, wordSeparators, position); } - private static _doFindNextWordOnLine(lineContent: string, wordSeparators: WordCharacterClassifier, position: Position): IFindWordResult { + private static _doFindNextWordOnLine(lineContent: string, wordSeparators: WordCharacterClassifier, position: Position): IFindWordResult | null { let wordType = WordType.None; let len = lineContent.length; @@ -324,7 +324,7 @@ export class WordOperations { return new Position(lineNumber, maxColumn); } - protected static _deleteWordLeftWhitespace(model: ICursorSimpleModel, position: Position): Range { + protected static _deleteWordLeftWhitespace(model: ICursorSimpleModel, position: Position): Range | null { const lineContent = model.getLineContent(position.lineNumber); const startIndex = position.column - 2; const lastNonWhitespace = strings.lastNonWhitespaceIndex(lineContent, startIndex); @@ -334,7 +334,7 @@ export class WordOperations { return null; } - public static deleteWordLeft(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, selection: Selection, whitespaceHeuristics: boolean, wordNavigationType: WordNavigationType): Range { + public static deleteWordLeft(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, selection: Selection, whitespaceHeuristics: boolean, wordNavigationType: WordNavigationType): Range | null { if (!selection.isEmpty()) { return selection; } @@ -409,7 +409,7 @@ export class WordOperations { return len; } - protected static _deleteWordRightWhitespace(model: ICursorSimpleModel, position: Position): Range { + protected static _deleteWordRightWhitespace(model: ICursorSimpleModel, position: Position): Range | null { const lineContent = model.getLineContent(position.lineNumber); const startIndex = position.column - 1; const firstNonWhitespace = this._findFirstNonWhitespaceChar(lineContent, startIndex); @@ -420,7 +420,7 @@ export class WordOperations { return null; } - public static deleteWordRight(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, selection: Selection, whitespaceHeuristics: boolean, wordNavigationType: WordNavigationType): Range { + public static deleteWordRight(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, selection: Selection, whitespaceHeuristics: boolean, wordNavigationType: WordNavigationType): Range | null { if (!selection.isEmpty()) { return selection; } @@ -503,16 +503,15 @@ export class WordOperations { if (!inSelectionMode) { // Entering word selection for the first time - const isTouchingPrevWord = (prevWord && prevWord.wordType === WordType.Regular && prevWord.start <= position.column - 1 && position.column - 1 <= prevWord.end); - const isTouchingNextWord = (nextWord && nextWord.wordType === WordType.Regular && nextWord.start <= position.column - 1 && position.column - 1 <= nextWord.end); - let startColumn: number; let endColumn: number; - if (isTouchingPrevWord) { + if (prevWord && prevWord.wordType === WordType.Regular && prevWord.start <= position.column - 1 && position.column - 1 <= prevWord.end) { + // isTouchingPrevWord startColumn = prevWord.start + 1; endColumn = prevWord.end + 1; - } else if (isTouchingNextWord) { + } else if (nextWord && nextWord.wordType === WordType.Regular && nextWord.start <= position.column - 1 && position.column - 1 <= nextWord.end) { + // isTouchingNextWord startColumn = nextWord.start + 1; endColumn = nextWord.end + 1; } else { @@ -534,16 +533,15 @@ export class WordOperations { ); } - const isInsidePrevWord = (prevWord && prevWord.wordType === WordType.Regular && prevWord.start < position.column - 1 && position.column - 1 < prevWord.end); - const isInsideNextWord = (nextWord && nextWord.wordType === WordType.Regular && nextWord.start < position.column - 1 && position.column - 1 < nextWord.end); - let startColumn: number; let endColumn: number; - if (isInsidePrevWord) { + if (prevWord && prevWord.wordType === WordType.Regular && prevWord.start < position.column - 1 && position.column - 1 < prevWord.end) { + // isInsidePrevWord startColumn = prevWord.start + 1; endColumn = prevWord.end + 1; - } else if (isInsideNextWord) { + } else if (nextWord && nextWord.wordType === WordType.Regular && nextWord.start < position.column - 1 && position.column - 1 < nextWord.end) { + // isInsideNextWord startColumn = nextWord.start + 1; endColumn = nextWord.end + 1; } else { @@ -575,42 +573,46 @@ export class WordOperations { export class WordPartOperations extends WordOperations { public static deleteWordPartLeft(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, selection: Selection, whitespaceHeuristics: boolean): Range { - const candidates = [ + const candidates = enforceDefined([ WordOperations.deleteWordLeft(wordSeparators, model, selection, whitespaceHeuristics, WordNavigationType.WordStart), WordOperations.deleteWordLeft(wordSeparators, model, selection, whitespaceHeuristics, WordNavigationType.WordEnd), WordOperations._deleteWordPartLeft(model, selection) - ]; + ]); candidates.sort(Range.compareRangesUsingEnds); return candidates[2]; } public static deleteWordPartRight(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, selection: Selection, whitespaceHeuristics: boolean): Range { - const candidates = [ + const candidates = enforceDefined([ WordOperations.deleteWordRight(wordSeparators, model, selection, whitespaceHeuristics, WordNavigationType.WordStart), WordOperations.deleteWordRight(wordSeparators, model, selection, whitespaceHeuristics, WordNavigationType.WordEnd), WordOperations._deleteWordPartRight(model, selection) - ]; + ]); candidates.sort(Range.compareRangesUsingStarts); return candidates[0]; } public static moveWordPartLeft(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, position: Position): Position { - const candidates = [ + const candidates = enforceDefined([ WordOperations.moveWordLeft(wordSeparators, model, position, WordNavigationType.WordStart), WordOperations.moveWordLeft(wordSeparators, model, position, WordNavigationType.WordEnd), WordOperations._moveWordPartLeft(model, position) - ]; + ]); candidates.sort(Position.compare); return candidates[2]; } public static moveWordPartRight(wordSeparators: WordCharacterClassifier, model: ICursorSimpleModel, position: Position): Position { - const candidates = [ + const candidates = enforceDefined([ WordOperations.moveWordRight(wordSeparators, model, position, WordNavigationType.WordStart), WordOperations.moveWordRight(wordSeparators, model, position, WordNavigationType.WordEnd), WordOperations._moveWordPartRight(model, position) - ]; + ]); candidates.sort(Position.compare); return candidates[0]; } } + +function enforceDefined(arr: (T | undefined | null)[]): T[] { + return arr.filter(el => Boolean(el)); +} \ No newline at end of file diff --git a/src/vs/editor/common/controller/oneCursor.ts b/src/vs/editor/common/controller/oneCursor.ts index bd5a79933d7..0a75e44cd4a 100644 --- a/src/vs/editor/common/controller/oneCursor.ts +++ b/src/vs/editor/common/controller/oneCursor.ts @@ -14,13 +14,10 @@ export class OneCursor { public modelState: SingleCursorState; public viewState: SingleCursorState; - private _selTrackedRange: string; + private _selTrackedRange: string | null; private _trackSelection: boolean; constructor(context: CursorContext) { - this.modelState = null; - this.viewState = null; - this._selTrackedRange = null; this._trackSelection = true; @@ -62,7 +59,7 @@ export class OneCursor { } public readSelectionFromMarkers(context: CursorContext): Selection { - const range = context.model._getTrackedRange(this._selTrackedRange); + const range = context.model._getTrackedRange(this._selTrackedRange!)!; if (this.modelState.selection.getDirection() === SelectionDirection.LTR) { return new Selection(range.startLineNumber, range.startColumn, range.endLineNumber, range.endColumn); } diff --git a/src/vs/editor/common/core/rgba.ts b/src/vs/editor/common/core/rgba.ts index 07c5edcb3bd..0ce94359f31 100644 --- a/src/vs/editor/common/core/rgba.ts +++ b/src/vs/editor/common/core/rgba.ts @@ -10,6 +10,8 @@ export class RGBA8 { _rgba8Brand: void; + static Empty = new RGBA8(0, 0, 0, 0); + /** * Red: integer in [0-255] */ diff --git a/src/vs/editor/common/diff/diffComputer.ts b/src/vs/editor/common/diff/diffComputer.ts index 0ad0f1dfd75..be2d727328a 100644 --- a/src/vs/editor/common/diff/diffComputer.ts +++ b/src/vs/editor/common/diff/diffComputer.ts @@ -238,14 +238,14 @@ class LineChange implements ILineChange { public originalEndLineNumber: number; public modifiedStartLineNumber: number; public modifiedEndLineNumber: number; - public charChanges: CharChange[]; + public charChanges: CharChange[] | undefined; constructor( originalStartLineNumber: number, originalEndLineNumber: number, modifiedStartLineNumber: number, modifiedEndLineNumber: number, - charChanges: CharChange[] + charChanges: CharChange[] | undefined ) { this.originalStartLineNumber = originalStartLineNumber; this.originalEndLineNumber = originalEndLineNumber; @@ -259,7 +259,7 @@ class LineChange implements ILineChange { let originalEndLineNumber: number; let modifiedStartLineNumber: number; let modifiedEndLineNumber: number; - let charChanges: CharChange[]; + let charChanges: CharChange[] | undefined = undefined; if (diffChange.originalLength === 0) { originalStartLineNumber = originalLineSequence.getStartLineNumber(diffChange.originalStart) - 1; @@ -477,7 +477,7 @@ export class DiffComputer { return; } - let charChanges: CharChange[]; + let charChanges: CharChange[] | undefined = undefined; if (this.shouldComputeCharChanges) { charChanges = [new CharChange( originalLineNumber, originalStartColumn, originalLineNumber, originalEndColumn, @@ -512,7 +512,7 @@ export class DiffComputer { prevChange.originalEndLineNumber = originalLineNumber; prevChange.modifiedEndLineNumber = modifiedLineNumber; if (this.shouldComputeCharChanges) { - prevChange.charChanges.push(new CharChange( + prevChange.charChanges!.push(new CharChange( originalLineNumber, originalStartColumn, originalLineNumber, originalEndColumn, modifiedLineNumber, modifiedStartColumn, modifiedLineNumber, modifiedEndColumn )); diff --git a/src/vs/editor/common/editorCommon.ts b/src/vs/editor/common/editorCommon.ts index 78829d1fe2b..8beff0159df 100644 --- a/src/vs/editor/common/editorCommon.ts +++ b/src/vs/editor/common/editorCommon.ts @@ -22,7 +22,7 @@ export interface IEditOperationBuilder { * @param range The range to replace (delete). May be empty to represent a simple insert. * @param text The text to replace with. May be null to represent a simple delete. */ - addEditOperation(range: Range, text: string): void; + addEditOperation(range: Range, text: string | null): void; /** * Add a new edit operation (a replace operation). @@ -142,7 +142,7 @@ export interface ICharChange extends IChange { * A line change */ export interface ILineChange extends IChange { - readonly charChanges: ICharChange[]; + readonly charChanges: ICharChange[] | undefined; } /** diff --git a/src/vs/editor/common/model.ts b/src/vs/editor/common/model.ts index 440bd75453c..f38ef53e4cb 100644 --- a/src/vs/editor/common/model.ts +++ b/src/vs/editor/common/model.ts @@ -934,7 +934,7 @@ export interface ITextModel { /** * @internal */ - _setTrackedRange(id: string, newRange: Range, newStickiness: TrackedRangeStickiness): string | null; + _setTrackedRange(id: string | null, newRange: Range | null, newStickiness: TrackedRangeStickiness): string | null; /** * Normalize a string containing whitespace according to indentation rules (converts to spaces or to tabs). diff --git a/src/vs/editor/common/model/textModel.ts b/src/vs/editor/common/model/textModel.ts index fb1973a199e..1d82601e095 100644 --- a/src/vs/editor/common/model/textModel.ts +++ b/src/vs/editor/common/model/textModel.ts @@ -1552,7 +1552,7 @@ export class TextModel extends Disposable implements model.ITextModel { return this.getDecorationRange(id); } - _setTrackedRange(id: string, newRange: Range, newStickiness: model.TrackedRangeStickiness): string | null { + _setTrackedRange(id: string | null, newRange: Range | null, newStickiness: model.TrackedRangeStickiness): string | null { const node = (id ? this._decorations[id] : null); if (!node) { diff --git a/src/vs/editor/common/modes.ts b/src/vs/editor/common/modes.ts index 2e1428422c8..7c62ab125e5 100644 --- a/src/vs/editor/common/modes.ts +++ b/src/vs/editor/common/modes.ts @@ -200,7 +200,7 @@ export interface ITokenizationSupport { getInitialState(): IState; // add offsetDelta to each of the returned indices - tokenize(line: string, state: IState, offsetDelta: number): TokenizationResult; + tokenize?(line: string, state: IState, offsetDelta: number): TokenizationResult; tokenize2(line: string, state: IState, offsetDelta: number): TokenizationResult2; } diff --git a/src/vs/editor/common/modes/supports/inplaceReplaceSupport.ts b/src/vs/editor/common/modes/supports/inplaceReplaceSupport.ts index 800568652d2..e4e069b9376 100644 --- a/src/vs/editor/common/modes/supports/inplaceReplaceSupport.ts +++ b/src/vs/editor/common/modes/supports/inplaceReplaceSupport.ts @@ -10,7 +10,7 @@ export class BasicInplaceReplace { public static readonly INSTANCE = new BasicInplaceReplace(); - public navigateValueSet(range1: IRange, text1: string, range2: IRange, text2: string, up: boolean): IInplaceReplaceSupportResult { + public navigateValueSet(range1: IRange, text1: string, range2: IRange, text2: string, up: boolean): IInplaceReplaceSupportResult | null { if (range1 && text1) { let result = this.doNavigateValueSet(text1, up); @@ -35,7 +35,7 @@ export class BasicInplaceReplace { return null; } - private doNavigateValueSet(text: string, up: boolean): string { + private doNavigateValueSet(text: string, up: boolean): string | null { let numberResult = this.numberReplace(text, up); if (numberResult !== null) { return numberResult; @@ -43,7 +43,7 @@ export class BasicInplaceReplace { return this.textReplace(text, up); } - private numberReplace(value: string, up: boolean): string { + private numberReplace(value: string, up: boolean): string | null { let precision = Math.pow(10, value.length - (value.lastIndexOf('.') + 1)); let n1 = Number(value); let n2 = parseFloat(value); @@ -71,11 +71,11 @@ export class BasicInplaceReplace { ['public', 'protected', 'private'], ]; - private textReplace(value: string, up: boolean): string { + private textReplace(value: string, up: boolean): string | null { return this.valueSetsReplace(this._defaultValueSet, value, up); } - private valueSetsReplace(valueSets: string[][], value: string, up: boolean): string { + private valueSetsReplace(valueSets: string[][], value: string, up: boolean): string | null { let result: string | null = null; for (let i = 0, len = valueSets.length; result === null && i < len; i++) { result = this.valueSetReplace(valueSets[i], value, up); @@ -83,7 +83,7 @@ export class BasicInplaceReplace { return result; } - private valueSetReplace(valueSet: string[], value: string, up: boolean): string { + private valueSetReplace(valueSet: string[], value: string, up: boolean): string | null { let idx = valueSet.indexOf(value); if (idx >= 0) { idx += up ? +1 : -1; diff --git a/src/vs/editor/common/modes/supports/tokenization.ts b/src/vs/editor/common/modes/supports/tokenization.ts index c82bb3460bf..bcc7c93cbb7 100644 --- a/src/vs/editor/common/modes/supports/tokenization.ts +++ b/src/vs/editor/common/modes/supports/tokenization.ts @@ -23,15 +23,15 @@ export class ParsedTokenThemeRule { * -1 if not set. An or mask of `FontStyle` otherwise. */ readonly fontStyle: FontStyle; - readonly foreground: string; - readonly background: string; + readonly foreground: string | null; + readonly background: string | null; constructor( token: string, index: number, fontStyle: number, - foreground: string, - background: string, + foreground: string | null, + background: string | null, ) { this.token = token; this.index = index; @@ -114,7 +114,7 @@ function resolveParsedTokenThemeRules(parsedThemeRules: ParsedTokenThemeRule[], let defaultForeground = '000000'; let defaultBackground = 'ffffff'; while (parsedThemeRules.length >= 1 && parsedThemeRules[0].token === '') { - let incomingDefaults = parsedThemeRules.shift(); + let incomingDefaults = parsedThemeRules.shift()!; if (incomingDefaults.fontStyle !== FontStyle.NotSet) { defaultFontStyle = incomingDefaults.fontStyle; } @@ -160,7 +160,7 @@ export class ColorMap { this._color2id = new Map(); } - public getId(color: string): ColorId { + public getId(color: string | null): ColorId { if (color === null) { return 0; } diff --git a/src/vs/editor/common/modes/textToHtmlTokenizer.ts b/src/vs/editor/common/modes/textToHtmlTokenizer.ts index 80f82b78c32..d932f8d0e44 100644 --- a/src/vs/editor/common/modes/textToHtmlTokenizer.ts +++ b/src/vs/editor/common/modes/textToHtmlTokenizer.ts @@ -9,7 +9,7 @@ import { LineTokens, IViewLineTokens } from 'vs/editor/common/core/lineTokens'; import { CharCode } from 'vs/base/common/charCode'; import { NULL_STATE, nullTokenize2 } from 'vs/editor/common/modes/nullMode'; -const fallback = { +const fallback: ITokenizationSupport = { getInitialState: () => NULL_STATE, tokenize: undefined, tokenize2: (buffer: string, state: IState, deltaOffset: number) => nullTokenize2(LanguageId.Null, buffer, state, deltaOffset) diff --git a/src/vs/editor/common/view/minimapCharRenderer.ts b/src/vs/editor/common/view/minimapCharRenderer.ts index 49d6afab3d9..b641f623708 100644 --- a/src/vs/editor/common/view/minimapCharRenderer.ts +++ b/src/vs/editor/common/view/minimapCharRenderer.ts @@ -34,11 +34,11 @@ export class MinimapTokensColorTracker { private _updateColorMap(): void { const colorMap = TokenizationRegistry.getColorMap(); if (!colorMap) { - this._colors = [null]; + this._colors = [RGBA8.Empty]; this._backgroundIsLight = true; return; } - this._colors = [null]; + this._colors = [RGBA8.Empty]; for (let colorId = 1; colorId < colorMap.length; colorId++) { const source = colorMap[colorId].rgba; // Use a VM friendly data-type diff --git a/src/vs/editor/common/view/runtimeMinimapCharRenderer.ts b/src/vs/editor/common/view/runtimeMinimapCharRenderer.ts index b8896f635b6..21f84e3fe98 100644 --- a/src/vs/editor/common/view/runtimeMinimapCharRenderer.ts +++ b/src/vs/editor/common/view/runtimeMinimapCharRenderer.ts @@ -16,17 +16,17 @@ function toUint8ClampedArrat(arr: number[]): Uint8ClampedArray { let minimapCharRenderer: MinimapCharRenderer | null = null; export function getOrCreateMinimapCharRenderer(): MinimapCharRenderer { if (!minimapCharRenderer) { - let _x1Data = toUint8ClampedArrat(x1Data); + let _x1Data = toUint8ClampedArrat(x1Data!); x1Data = null; - let _x2Data = toUint8ClampedArrat(x2Data); + let _x2Data = toUint8ClampedArrat(x2Data!); x2Data = null; minimapCharRenderer = new MinimapCharRenderer(_x2Data, _x1Data); } return minimapCharRenderer; } -let x2Data = [ +let x2Data: number[] | null = [ // 0, 0, @@ -600,7 +600,7 @@ let x2Data = [ ]; -let x1Data = [ +let x1Data: number[] | null = [ // 0, diff --git a/src/vs/editor/common/view/viewEvents.ts b/src/vs/editor/common/view/viewEvents.ts index 54035eea267..d8ccfc42c80 100644 --- a/src/vs/editor/common/view/viewEvents.ts +++ b/src/vs/editor/common/view/viewEvents.ts @@ -312,7 +312,7 @@ export interface IViewEventListener { export class ViewEventEmitter extends Disposable { private _listeners: IViewEventListener[]; - private _collector: ViewEventsCollector; + private _collector: ViewEventsCollector | null; private _collectorCnt: number; constructor() { @@ -332,13 +332,13 @@ export class ViewEventEmitter extends Disposable { if (this._collectorCnt === 1) { this._collector = new ViewEventsCollector(); } - return this._collector; + return this._collector!; } protected _endEmit(): void { this._collectorCnt--; if (this._collectorCnt === 0) { - const events = this._collector.finalize(); + const events = this._collector!.finalize(); this._collector = null; if (events.length > 0) { this._emit(events); @@ -383,7 +383,7 @@ export class ViewEventsCollector { public finalize(): ViewEvent[] { let result = this._events; - this._events = null; + this._events = []; return result; } diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index 38c82ebded3..df8acc21d1c 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -1825,7 +1825,7 @@ declare namespace monaco.editor { * @param range The range to replace (delete). May be empty to represent a simple insert. * @param text The text to replace with. May be null to represent a simple delete. */ - addEditOperation(range: Range, text: string): void; + addEditOperation(range: Range, text: string | null): void; /** * Add a new edit operation (a replace operation). * The inverse edits will be accessible in `ICursorStateComputerData.getInverseEditOperations()` @@ -1937,7 +1937,7 @@ declare namespace monaco.editor { * A line change */ export interface ILineChange extends IChange { - readonly charChanges: ICharChange[]; + readonly charChanges: ICharChange[] | undefined; } export interface INewScrollPosition { diff --git a/src/vs/platform/commands/common/commands.ts b/src/vs/platform/commands/common/commands.ts index 9158572cbb1..1a6f04b2363 100644 --- a/src/vs/platform/commands/common/commands.ts +++ b/src/vs/platform/commands/common/commands.ts @@ -18,7 +18,7 @@ export interface ICommandEvent { export interface ICommandService { _serviceBrand: any; onWillExecuteCommand: Event; - executeCommand(commandId: string, ...args: any[]): Promise; + executeCommand(commandId: string, ...args: any[]): Promise; } export interface ICommandsMap { @@ -45,7 +45,7 @@ export interface ICommandRegistry { registerCommand(id: string, command: ICommandHandler): IDisposable; registerCommand(command: ICommand): IDisposable; registerCommandAlias(oldId: string, newId: string): IDisposable; - getCommand(id: string): ICommand; + getCommand(id: string): ICommand | undefined; getCommands(): ICommandsMap; } @@ -68,7 +68,7 @@ export const CommandsRegistry: ICommandRegistry = new class implements ICommandR // add argument validation if rich command metadata is provided if (idOrCommand.description) { - const constraints: TypeConstraint[] = []; + const constraints: (TypeConstraint | undefined)[] = []; for (let arg of idOrCommand.description.args) { constraints.push(arg.constraint); } @@ -104,7 +104,7 @@ export const CommandsRegistry: ICommandRegistry = new class implements ICommandR }); } - getCommand(id: string): ICommand { + getCommand(id: string): ICommand | undefined { const list = this._commands.get(id); if (!list || list.isEmpty()) { return undefined; @@ -115,7 +115,7 @@ export const CommandsRegistry: ICommandRegistry = new class implements ICommandR getCommands(): ICommandsMap { const result: ICommandsMap = Object.create(null); this._commands.forEach((value, key) => { - result[key] = this.getCommand(key); + result[key] = this.getCommand(key)!; }); return result; } diff --git a/src/vs/platform/contextkey/browser/contextKeyService.ts b/src/vs/platform/contextkey/browser/contextKeyService.ts index 31b0b7f49f8..d3f6a029386 100644 --- a/src/vs/platform/contextkey/browser/contextKeyService.ts +++ b/src/vs/platform/contextkey/browser/contextKeyService.ts @@ -142,9 +142,9 @@ class ContextKey implements IContextKey { private _parent: AbstractContextKeyService; private _key: string; - private _defaultValue: T; + private _defaultValue: T | undefined; - constructor(parent: AbstractContextKeyService, key: string, defaultValue: T) { + constructor(parent: AbstractContextKeyService, key: string, defaultValue: T | undefined) { this._parent = parent; this._key = key; this._defaultValue = defaultValue; @@ -200,7 +200,7 @@ export abstract class AbstractContextKeyService implements IContextKeyService { abstract dispose(): void; - public createKey(key: string, defaultValue: T): IContextKey { + public createKey(key: string, defaultValue: T | undefined): IContextKey { return new ContextKey(this, key, defaultValue); } diff --git a/src/vs/platform/contextkey/common/contextkey.ts b/src/vs/platform/contextkey/common/contextkey.ts index 77f7de8a67e..eb9acf5392d 100644 --- a/src/vs/platform/contextkey/common/contextkey.ts +++ b/src/vs/platform/contextkey/common/contextkey.ts @@ -514,9 +514,9 @@ export class ContextKeyAndExpr implements ContextKeyExpr { export class RawContextKey extends ContextKeyDefinedExpr { - private _defaultValue: T; + private _defaultValue: T | undefined; - constructor(key: string, defaultValue: T) { + constructor(key: string, defaultValue: T | undefined) { super(key); this._defaultValue = defaultValue; } @@ -575,7 +575,7 @@ export interface IContextKeyService { dispose(): void; onDidChangeContext: Event; - createKey(key: string, defaultValue: T): IContextKey; + createKey(key: string, defaultValue: T | undefined): IContextKey; contextMatchesRules(rules: ContextKeyExpr): boolean; getContextKeyValue(key: string): T;