This commit is contained in:
Johannes 2022-12-16 09:12:23 +01:00
parent 1e1da2aed3
commit 6aba22da1a
No known key found for this signature in database
GPG key ID: 6DEF802A22264FCA

View file

@ -63,12 +63,13 @@ export class GotoDefinitionAtPositionEditorContribution implements IEditorContri
this.toUnhook.add(linkGesture.onExecute((mouseEvent: ClickLinkMouseEvent) => {
if (this.isEnabled(mouseEvent)) {
this.gotoDefinition(mouseEvent.target.position!, mouseEvent.hasSideBySideModifier).then(() => {
this.removeLinkDecorations();
}, (error: Error) => {
this.removeLinkDecorations();
onUnexpectedError(error);
});
this.gotoDefinition(mouseEvent.target.position!, mouseEvent.hasSideBySideModifier)
.catch((error: Error) => {
onUnexpectedError(error);
})
.finally(() => {
this.removeLinkDecorations();
});
}
}));
@ -82,33 +83,30 @@ export class GotoDefinitionAtPositionEditorContribution implements IEditorContri
return editor.getContribution<GotoDefinitionAtPositionEditorContribution>(GotoDefinitionAtPositionEditorContribution.ID);
}
startFindDefinitionFromCursor(position: Position) {
async startFindDefinitionFromCursor(position: Position) {
// For issue: https://github.com/microsoft/vscode/issues/46257
// equivalent to mouse move with meta/ctrl key
// First find the definition and add decorations
// to the editor to be shown with the content hover widget
return this.startFindDefinition(position).then(() => {
// Add listeners for editor cursor move and key down events
// Dismiss the "extended" editor decorations when the user hides
// the hover widget. There is no event for the widget itself so these
// serve as a best effort. After removing the link decorations, the hover
// widget is clean and will only show declarations per next request.
this.toUnhookForKeyboard.add(this.editor.onDidChangeCursorPosition(() => {
await this.startFindDefinition(position);
// Add listeners for editor cursor move and key down events
// Dismiss the "extended" editor decorations when the user hides
// the hover widget. There is no event for the widget itself so these
// serve as a best effort. After removing the link decorations, the hover
// widget is clean and will only show declarations per next request.
this.toUnhookForKeyboard.add(this.editor.onDidChangeCursorPosition(() => {
this.currentWordAtPosition = null;
this.removeLinkDecorations();
this.toUnhookForKeyboard.clear();
}));
this.toUnhookForKeyboard.add(this.editor.onKeyDown((e: IKeyboardEvent) => {
if (e) {
this.currentWordAtPosition = null;
this.removeLinkDecorations();
this.toUnhookForKeyboard.clear();
}));
this.toUnhookForKeyboard.add(this.editor.onKeyDown((e: IKeyboardEvent) => {
if (e) {
this.currentWordAtPosition = null;
this.removeLinkDecorations();
this.toUnhookForKeyboard.clear();
}
}));
});
}
}));
}
private startFindDefinitionFromMouse(mouseEvent: ClickLinkMouseEvent, withKey?: ClickLinkKeyboardEvent): void {
@ -129,7 +127,7 @@ export class GotoDefinitionAtPositionEditorContribution implements IEditorContri
this.startFindDefinition(position);
}
private startFindDefinition(position: Position): Promise<number | undefined> {
private async startFindDefinition(position: Position): Promise<void> {
// Dispose listeners for updating decorations when using keyboard to show definition hover
this.toUnhookForKeyboard.clear();
@ -139,12 +137,12 @@ export class GotoDefinitionAtPositionEditorContribution implements IEditorContri
if (!word) {
this.currentWordAtPosition = null;
this.removeLinkDecorations();
return Promise.resolve(0);
return;
}
// Return early if word at position is still the same
if (this.currentWordAtPosition && this.currentWordAtPosition.startColumn === word.startColumn && this.currentWordAtPosition.endColumn === word.endColumn && this.currentWordAtPosition.word === word.word) {
return Promise.resolve(0);
return;
}
this.currentWordAtPosition = word;
@ -159,66 +157,71 @@ export class GotoDefinitionAtPositionEditorContribution implements IEditorContri
this.previousPromise = createCancelablePromise(token => this.findDefinition(position, token));
return this.previousPromise.then(results => {
if (!results || !results.length || !state.validate(this.editor)) {
this.removeLinkDecorations();
let results: LocationLink[] | null;
try {
results = await this.previousPromise;
} catch (error) {
onUnexpectedError(error);
return;
}
if (!results || !results.length || !state.validate(this.editor)) {
this.removeLinkDecorations();
return;
}
const linkRange = results[0].originSelectionRange
? Range.lift(results[0].originSelectionRange)
: new Range(position.lineNumber, word.startColumn, position.lineNumber, word.endColumn);
// Multiple results
if (results.length > 1) {
let combinedRange = linkRange;
for (const { originSelectionRange } of results) {
if (originSelectionRange) {
combinedRange = Range.plusRange(combinedRange, originSelectionRange);
}
}
this.addDecoration(
combinedRange,
new MarkdownString().appendText(nls.localize('multipleResults', "Click to show {0} definitions.", results.length))
);
} else {
// Single result
const result = results[0];
if (!result.uri) {
return;
}
const linkRange = results[0].originSelectionRange
? Range.lift(results[0].originSelectionRange)
: new Range(position.lineNumber, word.startColumn, position.lineNumber, word.endColumn);
this.textModelResolverService.createModelReference(result.uri).then(ref => {
// Multiple results
if (results.length > 1) {
let combinedRange = linkRange;
for (const { originSelectionRange } of results) {
if (originSelectionRange) {
combinedRange = Range.plusRange(combinedRange, originSelectionRange);
}
}
this.addDecoration(
combinedRange,
new MarkdownString().appendText(nls.localize('multipleResults', "Click to show {0} definitions.", results.length))
);
}
// Single result
else {
const result = results[0];
if (!result.uri) {
if (!ref.object || !ref.object.textEditorModel) {
ref.dispose();
return;
}
this.textModelResolverService.createModelReference(result.uri).then(ref => {
const { object: { textEditorModel } } = ref;
const { startLineNumber } = result.range;
if (!ref.object || !ref.object.textEditorModel) {
ref.dispose();
return;
}
const { object: { textEditorModel } } = ref;
const { startLineNumber } = result.range;
if (startLineNumber < 1 || startLineNumber > textEditorModel.getLineCount()) {
// invalid range
ref.dispose();
return;
}
const previewValue = this.getPreviewValue(textEditorModel, startLineNumber, result);
const languageId = this.languageService.guessLanguageIdByFilepathOrFirstLine(textEditorModel.uri);
this.addDecoration(
linkRange,
previewValue ? new MarkdownString().appendCodeblock(languageId ? languageId : '', previewValue) : undefined
);
if (startLineNumber < 1 || startLineNumber > textEditorModel.getLineCount()) {
// invalid range
ref.dispose();
});
}
}).then(undefined, onUnexpectedError);
return;
}
const previewValue = this.getPreviewValue(textEditorModel, startLineNumber, result);
const languageId = this.languageService.guessLanguageIdByFilepathOrFirstLine(textEditorModel.uri);
this.addDecoration(
linkRange,
previewValue ? new MarkdownString().appendCodeblock(languageId ? languageId : '', previewValue) : undefined
);
ref.dispose();
});
}
}
private getPreviewValue(textEditorModel: ITextModel, startLineNumber: number, result: LocationLink) {
@ -314,6 +317,7 @@ export class GotoDefinitionAtPositionEditorContribution implements IEditorContri
public dispose(): void {
this.toUnhook.dispose();
this.toUnhookForKeyboard.dispose();
}
}