提交 e82d8bb6 编写于 作者: M Matt Bierner

Strict null checking goToDefinition

上级 8446e3fb
......@@ -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);
......
......@@ -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);
......@@ -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> {
......@@ -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;
}
......@@ -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) : '';
}
}
......@@ -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) : '';
}
}
......@@ -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) : '';
}
}
......@@ -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) : '';
}
}
......
......@@ -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,
......@@ -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) => {
......@@ -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) {
......@@ -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();
});
......@@ -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());
}
......@@ -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));
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册