Skip to content

Commit

Permalink
Strict null checking goToDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz committed Feb 5, 2019
1 parent 8446e3f commit e82d8bb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/vs/editor/browser/editorExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ export interface IActionOptions extends ICommandOptions {
}
export abstract class EditorAction extends EditorCommand {

public label: string;
public alias: string;
private menuOpts: IEditorCommandMenuOptions | undefined;
public readonly label: string;
public readonly alias: string;
private readonly menuOpts: IEditorCommandMenuOptions | undefined;

constructor(opts: IActionOptions) {
super(opts);
Expand Down
41 changes: 23 additions & 18 deletions src/vs/editor/contrib/goToDefinition/goToDefinitionCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export class DefinitionAction extends EditorAction {
}

public run(accessor: ServicesAccessor, editor: ICodeEditor): Promise<void> {
if (!editor.hasModel()) {
return Promise.resolve(undefined);
}
const notificationService = accessor.get(INotificationService);
const editorService = accessor.get(ICodeEditorService);
const progressService = accessor.get(IProgressService);
Expand Down Expand Up @@ -112,14 +115,14 @@ export class DefinitionAction extends EditorAction {
return getDefinitionsAtPosition(model, position, token);
}

protected _getNoResultFoundMessage(info?: IWordAtPosition): string {
protected _getNoResultFoundMessage(info: IWordAtPosition | null): string {
return info && info.word
? nls.localize('noResultWord', "No definition found for '{0}'", info.word)
: nls.localize('generic.noResults', "No definition found");
}

protected _getMetaTitle(model: ReferencesModel): string {
return model.references.length > 1 && nls.localize('meta.title', " – {0} definitions", model.references.length);
return model.references.length > 1 ? nls.localize('meta.title', " – {0} definitions", model.references.length) : '';
}

private async _onResult(editorService: ICodeEditorService, editor: ICodeEditor, model: ReferencesModel): Promise<void> {
Expand All @@ -129,21 +132,23 @@ export class DefinitionAction extends EditorAction {

if (this._configuration.openInPeek) {
this._openInPeek(editorService, editor, model);
} else {
} else if (editor.hasModel()) {
const next = model.nearestReference(editor.getModel().uri, editor.getPosition());
const targetEditor = await this._openReference(editor, editorService, next, this._configuration.openToSide);
if (targetEditor && model.references.length > 1) {
this._openInPeek(editorService, targetEditor, model);
} else {
model.dispose();
if (next) {
const targetEditor = await this._openReference(editor, editorService, next, this._configuration.openToSide);
if (targetEditor && model.references.length > 1) {
this._openInPeek(editorService, targetEditor, model);
} else {
model.dispose();
}
}
}
}

private _openReference(editor: ICodeEditor, editorService: ICodeEditorService, reference: Location | LocationLink, sideBySide: boolean): Promise<ICodeEditor> {
private _openReference(editor: ICodeEditor, editorService: ICodeEditorService, reference: Location | LocationLink, sideBySide: boolean): Promise<ICodeEditor | null> {
// range is the target-selection-range when we have one
// and the the fallback is the 'full' range
let range: IRange = undefined;
let range: IRange | undefined = undefined;
if (isLocationLink(reference)) {
range = reference.targetSelectionRange;
}
Expand Down Expand Up @@ -265,14 +270,14 @@ export class DeclarationAction extends DefinitionAction {
return getDeclarationsAtPosition(model, position, token);
}

protected _getNoResultFoundMessage(info?: IWordAtPosition): string {
protected _getNoResultFoundMessage(info: IWordAtPosition | null): string {
return info && info.word
? nls.localize('decl.noResultWord', "No declaration found for '{0}'", info.word)
: nls.localize('decl.generic.noResults', "No declaration found");
}

protected _getMetaTitle(model: ReferencesModel): string {
return model.references.length > 1 && nls.localize('decl.meta.title', " – {0} declarations", model.references.length);
return model.references.length > 1 ? nls.localize('decl.meta.title', " – {0} declarations", model.references.length) : '';
}
}

Expand All @@ -295,14 +300,14 @@ export class GoToDeclarationAction extends DeclarationAction {
});
}

protected _getNoResultFoundMessage(info?: IWordAtPosition): string {
protected _getNoResultFoundMessage(info: IWordAtPosition | null): string {
return info && info.word
? nls.localize('decl.noResultWord', "No declaration found for '{0}'", info.word)
: nls.localize('decl.generic.noResults', "No declaration found");
}

protected _getMetaTitle(model: ReferencesModel): string {
return model.references.length > 1 && nls.localize('decl.meta.title', " – {0} declarations", model.references.length);
return model.references.length > 1 ? nls.localize('decl.meta.title', " – {0} declarations", model.references.length) : '';
}
}

Expand All @@ -329,14 +334,14 @@ export class ImplementationAction extends DefinitionAction {
return getImplementationsAtPosition(model, position, token);
}

protected _getNoResultFoundMessage(info?: IWordAtPosition): string {
protected _getNoResultFoundMessage(info: IWordAtPosition | null): string {
return info && info.word
? nls.localize('goToImplementation.noResultWord', "No implementation found for '{0}'", info.word)
: nls.localize('goToImplementation.generic.noResults', "No implementation found");
}

protected _getMetaTitle(model: ReferencesModel): string {
return model.references.length > 1 && nls.localize('meta.implementations.title', " – {0} implementations", model.references.length);
return model.references.length > 1 ? nls.localize('meta.implementations.title', " – {0} implementations", model.references.length) : '';
}
}

Expand Down Expand Up @@ -387,14 +392,14 @@ export class TypeDefinitionAction extends DefinitionAction {
return getTypeDefinitionsAtPosition(model, position, token);
}

protected _getNoResultFoundMessage(info?: IWordAtPosition): string {
protected _getNoResultFoundMessage(info: IWordAtPosition | null): string {
return info && info.word
? nls.localize('goToTypeDefinition.noResultWord', "No type definition found for '{0}'", info.word)
: nls.localize('goToTypeDefinition.generic.noResults', "No type definition found");
}

protected _getMetaTitle(model: ReferencesModel): string {
return model.references.length > 1 && nls.localize('meta.typeDefinitions.title', " – {0} type definitions", model.references.length);
return model.references.length > 1 ? nls.localize('meta.typeDefinitions.title', " – {0} type definitions", model.references.length) : '';
}
}

Expand Down
23 changes: 12 additions & 11 deletions src/vs/editor/contrib/goToDefinition/goToDefinitionMouse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC
private editor: ICodeEditor;
private toUnhook: IDisposable[];
private decorations: string[];
private currentWordUnderMouse: IWordAtPosition;
private previousPromise: CancelablePromise<LocationLink[]>;
private currentWordUnderMouse: IWordAtPosition | null;
private previousPromise: CancelablePromise<LocationLink[] | null> | null;

constructor(
editor: ICodeEditor,
Expand All @@ -51,7 +51,7 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC
this.toUnhook.push(linkGesture);

this.toUnhook.push(linkGesture.onMouseMoveOrRelevantKeyDown(([mouseEvent, keyboardEvent]) => {
this.startFindDefinition(mouseEvent, keyboardEvent);
this.startFindDefinition(mouseEvent, keyboardEvent || undefined);
}));

this.toUnhook.push(linkGesture.onExecute((mouseEvent: ClickLinkMouseEvent) => {
Expand Down Expand Up @@ -79,20 +79,20 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC
return;
}

if (!this.isEnabled(mouseEvent, withKey)) {
if (!this.editor.hasModel() || !this.isEnabled(mouseEvent, withKey)) {
this.currentWordUnderMouse = null;
this.removeDecorations();
return;
}

// Find word at mouse position
let position = mouseEvent.target.position;
let word = position ? this.editor.getModel().getWordAtPosition(position) : null;
const word = mouseEvent.target.position ? this.editor.getModel().getWordAtPosition(mouseEvent.target.position) : null;
if (!word) {
this.currentWordUnderMouse = null;
this.removeDecorations();
return;
}
const position = mouseEvent.target.position!;

// Return early if word at position is still the same
if (this.currentWordUnderMouse && this.currentWordUnderMouse.startColumn === word.startColumn && this.currentWordUnderMouse.endColumn === word.endColumn && this.currentWordUnderMouse.word === word.word) {
Expand Down Expand Up @@ -158,9 +158,10 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC
wordRange = new Range(position.lineNumber, word.startColumn, position.lineNumber, word.endColumn);
}

const modeId = this.modeService.getModeIdByFilepathOrFirstLine(textEditorModel.uri.fsPath);
this.addDecoration(
wordRange,
new MarkdownString().appendCodeblock(this.modeService.getModeIdByFilepathOrFirstLine(textEditorModel.uri.fsPath), previewValue)
new MarkdownString().appendCodeblock(modeId ? modeId : '', previewValue)
);
ref.dispose();
});
Expand Down Expand Up @@ -274,10 +275,10 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC
}

private isEnabled(mouseEvent: ClickLinkMouseEvent, withKey?: ClickLinkKeyboardEvent): boolean {
return this.editor.getModel() &&
return this.editor.hasModel() &&
mouseEvent.isNoneOrSingleMouseDown &&
(mouseEvent.target.type === MouseTargetType.CONTENT_TEXT) &&
(mouseEvent.hasTriggerModifier || (withKey && withKey.keyCodeIsTriggerKey)) &&
(mouseEvent.hasTriggerModifier || (withKey ? withKey.keyCodeIsTriggerKey : false)) &&
DefinitionProviderRegistry.has(this.editor.getModel());
}

Expand All @@ -287,11 +288,11 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC
return Promise.resolve(null);
}

return getDefinitionsAtPosition(model, target.position, token);
return getDefinitionsAtPosition(model, target.position!, token);
}

private gotoDefinition(target: IMouseTarget, sideBySide: boolean): Promise<any> {
this.editor.setPosition(target.position);
this.editor.setPosition(target.position!);
const action = new DefinitionAction(new DefinitionActionConfig(sideBySide, false, true, false), { alias: undefined, label: undefined, id: undefined, precondition: undefined });
return this.editor.invokeWithinContext(accessor => action.run(accessor, this.editor));
}
Expand Down

0 comments on commit e82d8bb

Please sign in to comment.