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

More strict null checks (#60565)

上级 e1e4225a
......@@ -188,6 +188,7 @@
"./vs/editor/contrib/comment/comment.ts",
"./vs/editor/contrib/comment/lineCommentCommand.ts",
"./vs/editor/contrib/comment/test/blockCommentCommand.test.ts",
"./vs/editor/contrib/contextmenu/contextmenu.ts",
"./vs/editor/contrib/cursorUndo/cursorUndo.ts",
"./vs/editor/contrib/dnd/dnd.ts",
"./vs/editor/contrib/dnd/dragAndDropCommand.ts",
......@@ -225,8 +226,10 @@
"./vs/editor/contrib/gotoError/gotoError.ts",
"./vs/editor/contrib/gotoError/gotoErrorWidget.ts",
"./vs/editor/contrib/hover/getHover.ts",
"./vs/editor/contrib/hover/hover.ts",
"./vs/editor/contrib/hover/hoverOperation.ts",
"./vs/editor/contrib/hover/hoverWidgets.ts",
"./vs/editor/contrib/hover/modesContentHover.ts",
"./vs/editor/contrib/hover/modesGlyphHover.ts",
"./vs/editor/contrib/inPlaceReplace/inPlaceReplace.ts",
"./vs/editor/contrib/inPlaceReplace/inPlaceReplaceCommand.ts",
......@@ -245,6 +248,7 @@
"./vs/editor/contrib/links/links.ts",
"./vs/editor/contrib/markdown/markdownRenderer.ts",
"./vs/editor/contrib/message/messageController.ts",
"./vs/editor/contrib/multicursor/multicursor.ts",
"./vs/editor/contrib/parameterHints/parameterHints.ts",
"./vs/editor/contrib/parameterHints/parameterHintsWidget.ts",
"./vs/editor/contrib/parameterHints/provideSignatureHelp.ts",
......@@ -269,10 +273,12 @@
"./vs/editor/contrib/wordPartOperations/wordPartOperations.ts",
"./vs/editor/contrib/zoneWidget/zoneWidget.ts",
"./vs/editor/editor.worker.ts",
"./vs/editor/standalone/browser/accessibilityHelp/accessibilityHelp.ts",
"./vs/editor/standalone/browser/colorizer.ts",
"./vs/editor/standalone/browser/iPadShowKeyboard/iPadShowKeyboard.ts",
"./vs/editor/standalone/browser/inspectTokens/inspectTokens.ts",
"./vs/editor/standalone/browser/simpleServices.ts",
"./vs/editor/standalone/browser/standaloneCodeEditor.ts",
"./vs/editor/standalone/browser/standaloneCodeServiceImpl.ts",
"./vs/editor/standalone/browser/standaloneThemeServiceImpl.ts",
"./vs/editor/standalone/browser/toggleHighContrast/toggleHighContrast.ts",
......
......@@ -228,7 +228,7 @@ export class Separator extends Action {
export interface IActionItemOptions extends IBaseActionItemOptions {
icon?: boolean;
label?: boolean;
keybinding?: string;
keybinding?: string | null;
}
export class ActionItem extends BaseActionItem {
......
......@@ -20,6 +20,7 @@ import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService, IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { ITextModel } from 'vs/editor/common/model';
export class ContextMenuController implements IEditorContribution {
......@@ -60,6 +61,10 @@ export class ContextMenuController implements IEditorContribution {
}
private _onContextMenu(e: IEditorMouseEvent): void {
if (!this._editor.hasModel()) {
return;
}
if (!this._editor.getConfiguration().contribInfo.contextmenu) {
this._editor.focus();
// Ensure the cursor is at the position of the mouse click
......@@ -88,7 +93,7 @@ export class ContextMenuController implements IEditorContribution {
}
// Unless the user triggerd the context menu through Shift+F10, use the mouse position as menu position
let anchor: IAnchor;
let anchor: IAnchor | null = null;
if (e.target.type !== MouseTargetType.TEXTAREA) {
anchor = { x: e.event.posx - 1, width: 2, y: e.event.posy - 1, height: 2 };
}
......@@ -97,10 +102,13 @@ export class ContextMenuController implements IEditorContribution {
this.showContextMenu(anchor);
}
public showContextMenu(anchor?: IAnchor): void {
public showContextMenu(anchor?: IAnchor | null): void {
if (!this._editor.getConfiguration().contribInfo.contextmenu) {
return; // Context menu is turned off through configuration
}
if (!this._editor.hasModel()) {
return;
}
if (!this._contextMenuService) {
this._editor.focus();
......@@ -108,7 +116,7 @@ export class ContextMenuController implements IEditorContribution {
}
// Find actions available for menu
const menuActions = this._getMenuActions();
const menuActions = this._getMenuActions(this._editor.getModel());
// Show menu if we have actions to show
if (menuActions.length > 0) {
......@@ -116,11 +124,11 @@ export class ContextMenuController implements IEditorContribution {
}
}
private _getMenuActions(): IAction[] {
private _getMenuActions(model: ITextModel): IAction[] {
const result: IAction[] = [];
let contextMenu = this._menuService.createMenu(MenuId.EditorContext, this._contextKeyService);
const groups = contextMenu.getActions({ arg: this._editor.getModel().uri });
const groups = contextMenu.getActions({ arg: model.uri });
contextMenu.dispose();
for (let group of groups) {
......@@ -133,6 +141,9 @@ export class ContextMenuController implements IEditorContribution {
}
private _doShowContextMenu(actions: IAction[], anchor: IAnchor | null = null): void {
if (!this._editor.hasModel()) {
return;
}
// Disable hover
const oldHoverSetting = this._editor.getConfiguration().contribInfo.hover;
......@@ -160,7 +171,7 @@ export class ContextMenuController implements IEditorContribution {
// Show menu
this._contextMenuIsBeingShownCount++;
this._contextMenuService.showContextMenu({
getAnchor: () => anchor,
getAnchor: () => anchor!,
getActions: () => actions,
......@@ -178,7 +189,7 @@ export class ContextMenuController implements IEditorContribution {
return new ActionItem(action, action, { icon: true, label: true, isMenu: true });
},
getKeyBinding: (action): ResolvedKeybinding => {
getKeyBinding: (action): ResolvedKeybinding | null => {
return this._keybindingFor(action);
},
......@@ -192,7 +203,7 @@ export class ContextMenuController implements IEditorContribution {
});
}
private _keybindingFor(action: IAction): ResolvedKeybinding {
private _keybindingFor(action: IAction): ResolvedKeybinding | null {
return this._keybindingService.lookupKeybinding(action.id);
}
......
......@@ -174,13 +174,13 @@ export class ModesHoverController implements IEditorContribution {
if (targetType === MouseTargetType.CONTENT_TEXT) {
this.glyphWidget.hide();
if (this._isHoverEnabled) {
if (this._isHoverEnabled && mouseEvent.target.range) {
this.contentWidget.startShowingAt(mouseEvent.target.range, HoverStartMode.Delayed, false);
}
} else if (targetType === MouseTargetType.GUTTER_GLYPH_MARGIN) {
this.contentWidget.hide();
if (this._isHoverEnabled) {
if (this._isHoverEnabled && mouseEvent.target.position) {
this.glyphWidget.startShowingAt(mouseEvent.target.position.lineNumber);
}
} else {
......@@ -224,11 +224,9 @@ export class ModesHoverController implements IEditorContribution {
if (this._glyphWidget) {
this._glyphWidget.dispose();
this._glyphWidget = null;
}
if (this._contentWidget) {
this._contentWidget.dispose();
this._contentWidget = null;
}
}
}
......@@ -256,6 +254,9 @@ class ShowHoverAction extends EditorAction {
}
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
if (!editor.hasModel()) {
return;
}
let controller = ModesHoverController.get(editor);
if (!controller) {
return;
......
......@@ -84,7 +84,7 @@ export class ContentHoverWidget extends Widget implements editorBrowser.IContent
return this._containerDomNode;
}
public showAt(position: Position, range: Range, focus: boolean): void {
public showAt(position: Position, range: Range | null, focus: boolean): void {
// Position has changed
this._showAtPosition = position;
this._showAtRange = range;
......
......@@ -314,7 +314,7 @@ export class ModesContentHoverWidget extends ContentHoverWidget {
// update column from which to show
let renderColumn = Number.MAX_VALUE;
let highlightRange = Range.lift(messages[0].range);
let highlightRange: Range | null = messages[0].range ? Range.lift(messages[0].range) : null;
let fragment = document.createDocumentFragment();
let isEmptyHoverContent = true;
......@@ -326,7 +326,7 @@ export class ModesContentHoverWidget extends ContentHoverWidget {
}
renderColumn = Math.min(renderColumn, msg.range.startColumn);
highlightRange = Range.plusRange(highlightRange, msg.range);
highlightRange = highlightRange ? Range.plusRange(highlightRange, msg.range) : Range.lift(msg.range);
if (!(msg instanceof ColorHover)) {
msg.contents
......@@ -357,7 +357,11 @@ export class ModesContentHoverWidget extends ContentHoverWidget {
const widget = new ColorPickerWidget(fragment, model, this._editor.getConfiguration().pixelRatio, this._themeService);
getColorPresentations(editorModel, colorInfo, msg.provider, CancellationToken.None).then(colorPresentations => {
model.colorPresentations = colorPresentations;
model.colorPresentations = colorPresentations || [];
if (!this._editor.hasModel()) {
// gone...
return;
}
const originalText = this._editor.getModel().getValueInRange(msg.range);
model.guessColorPresentation(color, originalText);
......@@ -400,7 +404,7 @@ export class ModesContentHoverWidget extends ContentHoverWidget {
alpha: color.rgba.a
}
}, msg.provider, CancellationToken.None).then((colorPresentations) => {
model.colorPresentations = colorPresentations;
model.colorPresentations = colorPresentations || [];
});
};
......@@ -427,10 +431,10 @@ export class ModesContentHoverWidget extends ContentHoverWidget {
}
this._isChangingDecorations = true;
this._highlightDecorations = this._editor.deltaDecorations(this._highlightDecorations, [{
this._highlightDecorations = this._editor.deltaDecorations(this._highlightDecorations, highlightRange ? [{
range: highlightRange,
options: ModesContentHoverWidget._DECORATION_OPTIONS
}]);
}] : []);
this._isChangingDecorations = false;
}
......
......@@ -54,6 +54,10 @@ export class InsertCursorAbove extends EditorAction {
}
public run(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void {
if (!editor.hasModel()) {
return;
}
const useLogicalLine = (args && args.logicalLine === true);
const cursors = editor._getCursors();
const context = cursors.context;
......@@ -99,6 +103,10 @@ export class InsertCursorBelow extends EditorAction {
}
public run(accessor: ServicesAccessor, editor: ICodeEditor, args: any): void {
if (!editor.hasModel()) {
return;
}
const useLogicalLine = (args && args.logicalLine === true);
const cursors = editor._getCursors();
const context = cursors.context;
......@@ -154,6 +162,10 @@ class InsertCursorAtEndOfEachLineSelected extends EditorAction {
}
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
if (!editor.hasModel()) {
return;
}
const model = editor.getModel();
const selections = editor.getSelections();
let newSelections: Selection[] = [];
......@@ -177,10 +189,14 @@ class InsertCursorAtEndOfLineSelected extends EditorAction {
}
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
if (!editor.hasModel()) {
return;
}
const selections = editor.getSelections();
const lineCount = editor.getModel().getLineCount();
let newSelections = [];
let newSelections: Selection[] = [];
for (let i = selections[0].startLineNumber; i <= lineCount; i++) {
newSelections.push(new Selection(i, selections[0].startColumn, i, selections[0].endColumn));
}
......@@ -203,9 +219,13 @@ class InsertCursorAtTopOfLineSelected extends EditorAction {
}
public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
if (!editor.hasModel()) {
return;
}
const selections = editor.getSelections();
let newSelections = [];
let newSelections: Selection[] = [];
for (let i = selections[0].startLineNumber; i >= 1; i--) {
newSelections.push(new Selection(i, selections[0].startColumn, i, selections[0].endColumn));
}
......@@ -226,7 +246,10 @@ export class MultiCursorSessionResult {
export class MultiCursorSession {
public static create(editor: ICodeEditor, findController: CommonFindController): MultiCursorSession {
public static create(editor: ICodeEditor, findController: CommonFindController): MultiCursorSession | null {
if (!editor.hasModel()) {
return null;
}
const findState = findController.getState();
// Find widget owns entirely what we search for if:
......@@ -281,10 +304,14 @@ export class MultiCursorSession {
public readonly searchText: string,
public readonly wholeWord: boolean,
public readonly matchCase: boolean,
public currentMatch: Selection
public currentMatch: Selection | null
) { }
public addSelectionToNextFindMatch(): MultiCursorSessionResult {
public addSelectionToNextFindMatch(): MultiCursorSessionResult | null {
if (!this._editor.hasModel()) {
return null;
}
const nextMatch = this._getNextMatch();
if (!nextMatch) {
return null;
......@@ -294,7 +321,11 @@ export class MultiCursorSession {
return new MultiCursorSessionResult(allSelections.concat(nextMatch), nextMatch, ScrollType.Smooth);
}
public moveSelectionToNextFindMatch(): MultiCursorSessionResult {
public moveSelectionToNextFindMatch(): MultiCursorSessionResult | null {
if (!this._editor.hasModel()) {
return null;
}
const nextMatch = this._getNextMatch();
if (!nextMatch) {
return null;
......@@ -304,7 +335,11 @@ export class MultiCursorSession {
return new MultiCursorSessionResult(allSelections.slice(0, allSelections.length - 1).concat(nextMatch), nextMatch, ScrollType.Smooth);
}
private _getNextMatch(): Selection {
private _getNextMatch(): Selection | null {
if (!this._editor.hasModel()) {
return null;
}
if (this.currentMatch) {
const result = this.currentMatch;
this.currentMatch = null;
......@@ -323,7 +358,11 @@ export class MultiCursorSession {
return new Selection(nextMatch.range.startLineNumber, nextMatch.range.startColumn, nextMatch.range.endLineNumber, nextMatch.range.endColumn);
}
public addSelectionToPreviousFindMatch(): MultiCursorSessionResult {
public addSelectionToPreviousFindMatch(): MultiCursorSessionResult | null {
if (!this._editor.hasModel()) {
return null;
}
const previousMatch = this._getPreviousMatch();
if (!previousMatch) {
return null;
......@@ -333,7 +372,11 @@ export class MultiCursorSession {
return new MultiCursorSessionResult(allSelections.concat(previousMatch), previousMatch, ScrollType.Smooth);
}
public moveSelectionToPreviousFindMatch(): MultiCursorSessionResult {
public moveSelectionToPreviousFindMatch(): MultiCursorSessionResult | null {
if (!this._editor.hasModel()) {
return null;
}
const previousMatch = this._getPreviousMatch();
if (!previousMatch) {
return null;
......@@ -343,7 +386,11 @@ export class MultiCursorSession {
return new MultiCursorSessionResult(allSelections.slice(0, allSelections.length - 1).concat(previousMatch), previousMatch, ScrollType.Smooth);
}
private _getPreviousMatch(): Selection {
private _getPreviousMatch(): Selection | null {
if (!this._editor.hasModel()) {
return null;
}
if (this.currentMatch) {
const result = this.currentMatch;
this.currentMatch = null;
......@@ -363,6 +410,10 @@ export class MultiCursorSession {
}
public selectAll(): FindMatch[] {
if (!this._editor.hasModel()) {
return [];
}
this.findController.highlightFindOptions();
return this._editor.getModel().findMatches(this.searchText, true, false, this.matchCase, this.wholeWord ? this._editor.getConfiguration().wordSeparators : null, false, Constants.MAX_SAFE_SMALL_INTEGER);
......@@ -375,7 +426,7 @@ export class MultiCursorSelectionController extends Disposable implements IEdito
private readonly _editor: ICodeEditor;
private _ignoreSelectionChange: boolean;
private _session: MultiCursorSession;
private _session: MultiCursorSession | null;
private _sessionDispose: IDisposable[];
public static get(editor: ICodeEditor): MultiCursorSelectionController {
......@@ -466,7 +517,7 @@ export class MultiCursorSelectionController extends Disposable implements IEdito
return new Selection(selection.startLineNumber, word.startColumn, selection.startLineNumber, word.endColumn);
}
private _applySessionResult(result: MultiCursorSessionResult): void {
private _applySessionResult(result: MultiCursorSessionResult | null): void {
if (!result) {
return;
}
......@@ -476,11 +527,14 @@ export class MultiCursorSelectionController extends Disposable implements IEdito
}
}
public getSession(findController: CommonFindController): MultiCursorSession {
public getSession(findController: CommonFindController): MultiCursorSession | null {
return this._session;
}
public addSelectionToNextFindMatch(findController: CommonFindController): void {
if (!this._editor.hasModel()) {
return;
}
if (!this._session) {
// If there are multiple cursors, handle the case where they do not all select the same text.
const allSelections = this._editor.getSelections();
......@@ -527,6 +581,10 @@ export class MultiCursorSelectionController extends Disposable implements IEdito
}
public selectAll(findController: CommonFindController): void {
if (!this._editor.hasModel()) {
return;
}
let matches: FindMatch[] | null = null;
const findState = findController.getState();
......@@ -578,7 +636,7 @@ export abstract class MultiCursorSelectionControllerAction extends EditorAction
}
const findController = CommonFindController.get(editor);
if (!findController) {
return null;
return;
}
this._run(multiCursorController, findController);
}
......@@ -715,9 +773,9 @@ export class CompatChangeAll extends MultiCursorSelectionControllerAction {
class SelectionHighlighterState {
public readonly searchText: string;
public readonly matchCase: boolean;
public readonly wordSeparators: string;
public readonly wordSeparators: string | null;
constructor(searchText: string, matchCase: boolean, wordSeparators: string) {
constructor(searchText: string, matchCase: boolean, wordSeparators: string | null) {
this.searchText = searchText;
this.matchCase = matchCase;
this.wordSeparators = wordSeparators;
......@@ -726,7 +784,7 @@ class SelectionHighlighterState {
/**
* Everything equals except for `lastWordUnderCursor`
*/
public static softEquals(a: SelectionHighlighterState, b: SelectionHighlighterState): boolean {
public static softEquals(a: SelectionHighlighterState | null, b: SelectionHighlighterState | null): boolean {
if (!a && !b) {
return true;
}
......@@ -748,7 +806,7 @@ export class SelectionHighlighter extends Disposable implements IEditorContribut
private _isEnabled: boolean;
private decorations: string[];
private updateSoon: RunOnceScheduler;
private state: SelectionHighlighterState;
private state: SelectionHighlighterState | null;
constructor(editor: ICodeEditor) {
super();
......@@ -800,12 +858,11 @@ export class SelectionHighlighter extends Disposable implements IEditorContribut
this._setState(SelectionHighlighter._createState(this._isEnabled, this.editor));
}
private static _createState(isEnabled: boolean, editor: ICodeEditor): SelectionHighlighterState {
private static _createState(isEnabled: boolean, editor: ICodeEditor): SelectionHighlighterState | null {
if (!isEnabled) {
return null;
}
const model = editor.getModel();
if (!model) {
if (!editor.hasModel()) {
return null;
}
const s = editor.getSelection();
......@@ -877,7 +934,7 @@ export class SelectionHighlighter extends Disposable implements IEditorContribut
return new SelectionHighlighterState(r.searchText, r.matchCase, r.wholeWord ? editor.getConfiguration().wordSeparators : null);
}
private _setState(state: SelectionHighlighterState): void {
private _setState(state: SelectionHighlighterState | null): void {
if (SelectionHighlighterState.softEquals(this.state, state)) {
this.state = state;
return;
......@@ -889,6 +946,10 @@ export class SelectionHighlighter extends Disposable implements IEditorContribut
return;
}
if (!this.editor.hasModel()) {
return;
}
const model = this.editor.getModel();
if (model.isTooLargeForTokenization()) {
// the file is too large, so searching word under cursor in the whole document takes is blocking the UI.
......
......@@ -77,7 +77,7 @@ const nlsSingleSelection = nls.localize("singleSelection", "Line {0}, Column {1}
const nlsMultiSelectionRange = nls.localize("multiSelectionRange", "{0} selections ({1} characters selected)");
const nlsMultiSelection = nls.localize("multiSelection", "{0} selections");
function getSelectionLabel(selections: Selection[] | null, charactersSelected: number): string | null {
function getSelectionLabel(selections: Selection[] | null, charactersSelected: number): string {
if (!selections || selections.length === 0) {
return nlsNoSelection;
}
......@@ -98,7 +98,7 @@ function getSelectionLabel(selections: Selection[] | null, charactersSelected: n
return strings.format(nlsMultiSelection, selections.length);
}
return null;
return '';
}
class AccessibilityHelpWidget extends Widget implements IOverlayWidget {
......
......@@ -360,7 +360,7 @@ export class StandaloneDiffEditor extends DiffEditorWidget implements IStandalon
constructor(
domElement: HTMLElement,
options: IDiffEditorConstructionOptions,
options: IDiffEditorConstructionOptions | undefined,
toDispose: IDisposable,
@IInstantiationService instantiationService: IInstantiationService,
@IContextKeyService contextKeyService: IContextKeyService,
......
......@@ -100,7 +100,7 @@ export function onDidCreateEditor(listener: (codeEditor: ICodeEditor) => void):
* The editor will read the size of `domElement`.
*/
export function createDiffEditor(domElement: HTMLElement, options?: IDiffEditorConstructionOptions, override?: IEditorOverrideServices): IStandaloneDiffEditor {
return withAllStandaloneServices(domElement, override, (services) => {
return withAllStandaloneServices(domElement, override || {}, (services) => {
return new StandaloneDiffEditor(
domElement,
options,
......
......@@ -53,12 +53,12 @@ export module StaticServices {
export class LazyStaticService<T> {
private _serviceId: ServiceIdentifier<T>;
private _factory: (overrides: IEditorOverrideServices) => T;
private _value: T;
private _factory: (overrides?: IEditorOverrideServices) => T;
private _value: T | null;
public get id() { return this._serviceId; }
constructor(serviceId: ServiceIdentifier<T>, factory: (overrides: IEditorOverrideServices) => T) {
constructor(serviceId: ServiceIdentifier<T>, factory: (overrides?: IEditorOverrideServices) => T) {
this._serviceId = serviceId;
this._factory = factory;
this._value = null;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册