Skip to content

Commit

Permalink
Strict null check more quick open files
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz committed Feb 5, 2019
1 parent 34dbc71 commit 8446e3f
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 42 deletions.
4 changes: 4 additions & 0 deletions src/tsconfig.strictNullChecks.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@
"./vs/editor/standalone/browser/iPadShowKeyboard/iPadShowKeyboard.ts",
"./vs/editor/standalone/browser/inspectTokens/inspectTokens.ts",
"./vs/editor/standalone/browser/quickOpen/editorQuickOpen.ts",
"./vs/editor/standalone/browser/quickOpen/gotoLine.ts",
"./vs/editor/standalone/browser/quickOpen/quickCommand.ts",
"./vs/editor/standalone/browser/quickOpen/quickOpenEditorWidget.ts",
"./vs/editor/standalone/browser/quickOpen/quickOutline.ts",
Expand Down Expand Up @@ -434,6 +435,7 @@
"./vs/workbench/browser/parts/editor/breadcrumbsModel.ts",
"./vs/workbench/browser/parts/editor/editor.ts",
"./vs/workbench/browser/parts/editor/editorControl.ts",
"./vs/workbench/browser/parts/editor/editorPicker.ts",
"./vs/workbench/browser/parts/editor/editorWidgets.ts",
"./vs/workbench/browser/parts/editor/rangeDecorations.ts",
"./vs/workbench/browser/parts/editor/resourceViewer.ts",
Expand Down Expand Up @@ -567,6 +569,7 @@
"./vs/workbench/parts/preferences/test/common/smartSnippetInserter.test.ts",
"./vs/workbench/parts/quickopen/browser/commandsHandler.ts",
"./vs/workbench/parts/quickopen/browser/gotoLineHandler.ts",
"./vs/workbench/parts/quickopen/browser/gotoSymbolHandler.ts",
"./vs/workbench/parts/quickopen/browser/helpHandler.ts",
"./vs/workbench/parts/quickopen/browser/viewPickerHandler.ts",
"./vs/workbench/parts/relauncher/electron-browser/relauncher.contribution.ts",
Expand All @@ -575,6 +578,7 @@
"./vs/workbench/parts/scm/electron-browser/scmActivity.ts",
"./vs/workbench/parts/scm/electron-browser/scmMenus.ts",
"./vs/workbench/parts/scm/electron-browser/scmUtil.ts",
"./vs/workbench/parts/search/browser/openAnythingHandler.ts",
"./vs/workbench/parts/search/browser/openFileHandler.ts",
"./vs/workbench/parts/search/browser/openSymbolHandler.ts",
"./vs/workbench/parts/search/browser/patternInputWidget.ts",
Expand Down
23 changes: 12 additions & 11 deletions src/vs/editor/standalone/browser/quickOpen/gotoLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import 'vs/css!./gotoLine';
import * as nls from 'vs/nls';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { IContext, QuickOpenEntry, QuickOpenModel } from 'vs/base/parts/quickopen/browser/quickOpenModel';
import { IAutoFocus, Mode } from 'vs/base/parts/quickopen/common/quickOpen';
import { QuickOpenEntry, QuickOpenModel } from 'vs/base/parts/quickopen/browser/quickOpenModel';
import { IAutoFocus, Mode, IEntryRunContext } from 'vs/base/parts/quickopen/common/quickOpen';
import { ICodeEditor, IDiffEditor, isCodeEditor } from 'vs/editor/browser/editorBrowser';
import { ServicesAccessor, registerEditorAction } from 'vs/editor/browser/editorExtensions';
import { Position } from 'vs/editor/common/core/position';
Expand Down Expand Up @@ -49,14 +49,15 @@ export class GotoLineEntry extends QuickOpenEntry {
position = new Position(numbers[0], numbers[1]);
}

let model: ITextModel;
let model: ITextModel | null;
if (isCodeEditor(this.editor)) {
model = this.editor.getModel();
} else {
model = (<IDiffEditor>this.editor).getModel().modified;
const diffModel = (<IDiffEditor>this.editor).getModel();
model = diffModel ? diffModel.modified : null;
}

const isValid = model.validatePosition(position).equals(position);
const isValid = model ? model.validatePosition(position).equals(position) : false;
let label: string;

if (isValid) {
Expand All @@ -65,10 +66,10 @@ export class GotoLineEntry extends QuickOpenEntry {
} else {
label = nls.localize('gotoLineLabelValidLine', "Go to line {0}", position.lineNumber, position.column);
}
} else if (position.lineNumber < 1 || position.lineNumber > model.getLineCount()) {
label = nls.localize('gotoLineLabelEmptyWithLineLimit', "Type a line number between 1 and {0} to navigate to", model.getLineCount());
} else if (position.lineNumber < 1 || position.lineNumber > (model ? model.getLineCount() : 0)) {
label = nls.localize('gotoLineLabelEmptyWithLineLimit', "Type a line number between 1 and {0} to navigate to", model ? model.getLineCount() : 0);
} else {
label = nls.localize('gotoLineLabelEmptyWithLineAndColumnLimit', "Type a character between 1 and {0} to navigate to", model.getLineMaxColumn(position.lineNumber));
label = nls.localize('gotoLineLabelEmptyWithLineAndColumnLimit', "Type a character between 1 and {0} to navigate to", model ? model.getLineMaxColumn(position.lineNumber) : 0);
}

return {
Expand All @@ -83,12 +84,12 @@ export class GotoLineEntry extends QuickOpenEntry {
}

getAriaLabel(): string {
const currentLine = this.editor.getPosition().lineNumber;

const position = this.editor.getPosition();
const currentLine = position ? position.lineNumber : 0;
return nls.localize('gotoLineAriaLabel', "Current Line: {0}. Go to line {0}.", currentLine, this.parseResult.label);
}

run(mode: Mode, context: IContext): boolean {
run(mode: Mode, _context: IEntryRunContext): boolean {
if (mode === Mode.OPEN) {
return this.runOpen();
}
Expand Down
11 changes: 5 additions & 6 deletions src/vs/workbench/browser/parts/editor/editorPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import 'vs/css!./media/editorpicker';
import * as nls from 'vs/nls';
import { URI } from 'vs/base/common/uri';
import { IIconLabelValueOptions } from 'vs/base/browser/ui/iconLabel/iconLabel';
import { IAutoFocus, Mode, IEntryRunContext, IQuickNavigateConfiguration, IModel } from 'vs/base/parts/quickopen/common/quickOpen';
import { QuickOpenModel, QuickOpenEntry, QuickOpenEntryGroup, QuickOpenItemAccessor } from 'vs/base/parts/quickopen/browser/quickOpenModel';
Expand Down Expand Up @@ -33,12 +32,12 @@ export class EditorPickerEntry extends QuickOpenEntryGroup {

getLabelOptions(): IIconLabelValueOptions {
return {
extraClasses: getIconClasses(this.modelService, this.modeService, this.getResource()),
extraClasses: getIconClasses(this.modelService, this.modeService, this.getResource() || undefined),
italic: !this._group.isPinned(this.editor)
};
}

getLabel(): string {
getLabel() {
return this.editor.getName();
}

Expand All @@ -50,15 +49,15 @@ export class EditorPickerEntry extends QuickOpenEntryGroup {
return this._group;
}

getResource(): URI {
getResource() {
return toResource(this.editor, { supportSideBySide: true });
}

getAriaLabel(): string {
return nls.localize('entryAriaLabel', "{0}, editor group picker", this.getLabel());
}

getDescription(): string {
getDescription() {
return this.editor.getDescription();
}

Expand Down Expand Up @@ -109,7 +108,7 @@ export abstract class BaseEditorPicker extends QuickOpenHandler {
return false;
}

e.setHighlights(itemScore.labelMatch, itemScore.descriptionMatch);
e.setHighlights(itemScore.labelMatch || [], itemScore.descriptionMatch);

return true;
});
Expand Down
45 changes: 25 additions & 20 deletions src/vs/workbench/parts/quickopen/browser/gotoSymbolHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class OutlineModel extends QuickOpenModel {

// Update previous result with count
if (currentResult) {
currentResult.setGroupLabel(this.renderGroupLabel(currentType, typeCounter));
currentResult.setGroupLabel(typeof currentType === 'number' ? this.renderGroupLabel(currentType, typeCounter) : undefined);
}

currentType = result.getKind();
Expand All @@ -146,7 +146,7 @@ class OutlineModel extends QuickOpenModel {

// Update previous result with count
if (currentResult) {
currentResult.setGroupLabel(this.renderGroupLabel(currentType, typeCounter));
currentResult.setGroupLabel(typeof currentType === 'number' ? this.renderGroupLabel(currentType, typeCounter) : undefined);
}
}

Expand Down Expand Up @@ -338,7 +338,7 @@ class SymbolEntry extends EditorQuickOpenEntryGroup {

// Decorate if possible
if (types.isFunction(activeTextEditorWidget.changeDecorations)) {
this.handler.decorateOutline(this.range, range, activeTextEditorWidget, this.editorService.activeControl.group);
this.handler.decorateOutline(this.range, range, activeTextEditorWidget, this.editorService.activeControl.group!);
}
}

Expand All @@ -365,11 +365,11 @@ export class GotoSymbolHandler extends QuickOpenHandler {

static readonly ID = 'workbench.picker.filesymbols';

private rangeHighlightDecorationId: IEditorLineDecoration;
private lastKnownEditorViewState: IEditorViewState;
private rangeHighlightDecorationId?: IEditorLineDecoration;
private lastKnownEditorViewState: IEditorViewState | null;

private cachedOutlineRequest: Promise<OutlineModel>;
private pendingOutlineRequest: CancellationTokenSource;
private cachedOutlineRequest?: Promise<OutlineModel | null>;
private pendingOutlineRequest?: CancellationTokenSource;

constructor(
@IEditorService private readonly editorService: IEditorService
Expand All @@ -386,11 +386,11 @@ export class GotoSymbolHandler extends QuickOpenHandler {
private onDidActiveEditorChange(): void {
this.clearOutlineRequest();

this.lastKnownEditorViewState = undefined;
this.lastKnownEditorViewState = null;
this.rangeHighlightDecorationId = undefined;
}

getResults(searchValue: string, token: CancellationToken): Promise<QuickOpenModel> {
getResults(searchValue: string, token: CancellationToken): Promise<QuickOpenModel | null> {
searchValue = searchValue.trim();

// Support to cancel pending outline requests
Expand All @@ -406,6 +406,10 @@ export class GotoSymbolHandler extends QuickOpenHandler {

// Resolve Outline Model
return this.getOutline().then(outline => {
if (!outline) {
return outline;
}

if (token.isCancellationRequested) {
return outline;
}
Expand Down Expand Up @@ -469,20 +473,20 @@ export class GotoSymbolHandler extends QuickOpenHandler {
const label = strings.trim(element.name);

// Show parent scope as description
const description: string = element.containerName;
const description = element.containerName || '';
const icon = symbolKindToCssClass(element.kind);

// Add
results.push(new SymbolEntry(i,
label, element.kind, description, `symbol-icon ${icon}`,
element.range, element.selectionRange, null, this.editorService, this
element.range, element.selectionRange, [], this.editorService, this
));
}

return results;
}

private getOutline(): Promise<OutlineModel> {
private getOutline(): Promise<OutlineModel | null> {
if (!this.cachedOutlineRequest) {
this.cachedOutlineRequest = this.doGetActiveOutline();
}
Expand All @@ -499,7 +503,7 @@ export class GotoSymbolHandler extends QuickOpenHandler {
}

if (model && types.isFunction((<ITextModel>model).getLanguageIdentifier)) {
return Promise.resolve(asPromise(() => getDocumentSymbols(<ITextModel>model, true, this.pendingOutlineRequest.token)).then(entries => {
return Promise.resolve(asPromise(() => getDocumentSymbols(<ITextModel>model, true, this.pendingOutlineRequest!.token)).then(entries => {
return new OutlineModel(this.toQuickOpenEntries(entries));
}));
}
Expand All @@ -515,7 +519,7 @@ export class GotoSymbolHandler extends QuickOpenHandler {
if (this.rangeHighlightDecorationId) {
deleteDecorations.push(this.rangeHighlightDecorationId.lineDecorationId);
deleteDecorations.push(this.rangeHighlightDecorationId.rangeHighlightId);
this.rangeHighlightDecorationId = null;
this.rangeHighlightDecorationId = undefined;
}

const newDecorations: IModelDeltaDecoration[] = [
Expand Down Expand Up @@ -555,20 +559,21 @@ export class GotoSymbolHandler extends QuickOpenHandler {
}

private clearDecorations(): void {
if (this.rangeHighlightDecorationId) {
const rangeHighlightDecorationId = this.rangeHighlightDecorationId;
if (rangeHighlightDecorationId) {
this.editorService.visibleControls.forEach(editor => {
if (editor.group.id === this.rangeHighlightDecorationId.groupId) {
if (editor.group && editor.group.id === rangeHighlightDecorationId.groupId) {
const editorControl = <IEditor>editor.getControl();
editorControl.changeDecorations((changeAccessor: IModelDecorationsChangeAccessor) => {
changeAccessor.deltaDecorations([
this.rangeHighlightDecorationId.lineDecorationId,
this.rangeHighlightDecorationId.rangeHighlightId
rangeHighlightDecorationId.lineDecorationId,
rangeHighlightDecorationId.rangeHighlightId
], []);
});
}
});

this.rangeHighlightDecorationId = null;
this.rangeHighlightDecorationId = undefined;
}
}

Expand Down Expand Up @@ -598,6 +603,6 @@ export class GotoSymbolHandler extends QuickOpenHandler {
this.pendingOutlineRequest = undefined;
}

this.cachedOutlineRequest = null;
this.cachedOutlineRequest = undefined;
}
}
6 changes: 3 additions & 3 deletions src/vs/workbench/parts/search/browser/openAnythingHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class OpenAnythingHandler extends QuickOpenHandler {
entry.setRange(searchWithRange ? searchWithRange.range : null);

const itemScore = scoreItem(entry, query, true, QuickOpenItemAccessor, this.scorerCache);
entry.setHighlights(itemScore.labelMatch, itemScore.descriptionMatch);
entry.setHighlights(itemScore.labelMatch || [], itemScore.descriptionMatch);
}
});

Expand Down Expand Up @@ -165,7 +165,7 @@ export class OpenAnythingHandler extends QuickOpenHandler {
return this.openFileHandler.hasShortResponseTime() && this.openSymbolHandler.hasShortResponseTime();
}

private extractRange(value: string): ISearchWithRange {
private extractRange(value: string): ISearchWithRange | null {
if (!value) {
return null;
}
Expand Down Expand Up @@ -211,7 +211,7 @@ export class OpenAnythingHandler extends QuickOpenHandler {
}
}

if (range) {
if (patternMatch && range) {
return {
search: value.substr(0, patternMatch.index), // clear range suffix from search value
range: range
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/parts/search/browser/openFileHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class FileQuickOpenModel extends QuickOpenModel {
}

export class FileEntry extends EditorQuickOpenEntry {
private range: IRange;
private range: IRange | null;

constructor(
private resource: URI,
Expand Down Expand Up @@ -85,7 +85,7 @@ export class FileEntry extends EditorQuickOpenEntry {
return this.resource;
}

setRange(range: IRange): void {
setRange(range: IRange | null): void {
this.range = range;
}

Expand Down

0 comments on commit 8446e3f

Please sign in to comment.