Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscribe to navigation events on open files #3649

Merged
merged 1 commit into from Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/extension/analysis/file_tracker_das.ts
@@ -1,5 +1,5 @@
import { CancellationToken, Disposable, TextDocument, Uri, window, workspace } from "vscode";
import { FlutterOutline, FoldingRegion, Occurrences, Outline } from "../../shared/analysis_server_types";
import { AnalysisGetNavigationResponse, AnalysisNavigationNotification, FilePath, FlutterOutline, FoldingRegion, NavigationRegion, Occurrences, Outline } from "../../shared/analysis_server_types";
import { IAmDisposable, Logger } from "../../shared/interfaces";
import { disposeAll } from "../../shared/utils";
import { fsPath } from "../../shared/utils/fs";
Expand All @@ -11,6 +11,7 @@ import { DasAnalyzerClient } from "./analyzer_das";

export class DasFileTracker implements IAmDisposable {
private disposables: Disposable[] = [];
private readonly navigations: { [key: string]: AnalysisNavigationNotification } = {};
private readonly outlines: { [key: string]: Outline } = {};
private readonly flutterOutlines: { [key: string]: FlutterOutline } = {};
private readonly occurrences: { [key: string]: Occurrences[] } = {};
Expand All @@ -30,6 +31,7 @@ export class DasFileTracker implements IAmDisposable {
}));
this.disposables.push(workspace.onDidCloseTextDocument(async (td) => {
const path = fsPath(td.uri);
delete this.navigations[path];
delete this.outlines[path];
delete this.flutterOutlines[path];
delete this.occurrences[path];
Expand All @@ -38,6 +40,7 @@ export class DasFileTracker implements IAmDisposable {
await this.updateSubscriptions();
}));
this.disposables.push(window.onDidChangeVisibleTextEditors((e) => this.updatePriorityFiles()));
this.disposables.push(this.analyzer.registerForAnalysisNavigation((n) => this.navigations[n.file] = n));
this.disposables.push(this.analyzer.registerForAnalysisOutline((o) => this.outlines[o.file] = o.outline));
this.disposables.push(this.analyzer.registerForFlutterOutline((o) => this.flutterOutlines[o.file] = o.outline));
this.disposables.push(this.analyzer.registerForAnalysisOccurrences((o) => this.occurrences[o.file] = o.occurrences));
Expand Down Expand Up @@ -86,6 +89,7 @@ export class DasFileTracker implements IAmDisposable {
subscriptions: {
CLOSING_LABELS: this.analyzer.capabilities.supportsClosingLabels ? openFiles : undefined,
FOLDING: this.wsContext.config.useLsp ? undefined : openFiles,
NAVIGATION: this.wsContext.config.useLsp ? undefined : openFiles,
OCCURRENCES: this.wsContext.config.useLsp ? undefined : openFiles,
OUTLINE: openFiles,
},
Expand Down Expand Up @@ -123,6 +127,22 @@ export class DasFileTracker implements IAmDisposable {
.sort((path1, path2) => path1.localeCompare(path2));
}

public getNavigationTargets(file: FilePath, offset: number): AnalysisGetNavigationResponse | undefined {
// Synthesize an AnalysisGetNavigationResponse based on our existing knowledge about navigation links in the file.
const notification = this.navigations[file];
const region = notification?.regions?.find((region) => this.offsetWithinNavigationRegion(region, offset));
if (!region) return undefined;
return {
files: notification.files,
regions: [region],
targets: notification.targets,
};
}

private offsetWithinNavigationRegion(region: NavigationRegion, offset: number): boolean {
return offset >= region.offset && offset < region.offset + region.length;
}

public getOutlineFor(file: Uri): Outline | undefined {
return this.outlines[fsPath(file)];
}
Expand Down
2 changes: 1 addition & 1 deletion src/extension/extension.ts
Expand Up @@ -332,7 +332,7 @@ export async function activate(context: vs.ExtensionContext, isRestart: boolean
context.subscriptions.push(new LspClosingLabelsDecorations(lspClient));

const completionItemProvider = isUsingLsp || !dasClient ? undefined : new DartCompletionItemProvider(logger, dasClient);
const referenceProvider = isUsingLsp || !dasClient ? undefined : new DartReferenceProvider(dasClient);
const referenceProvider = isUsingLsp || !dasClient || !dasAnalyzer ? undefined : new DartReferenceProvider(dasClient, dasAnalyzer.fileTracker);

const activeFileFilters: vs.DocumentFilter[] = [DART_MODE];

Expand Down
18 changes: 12 additions & 6 deletions src/extension/providers/dart_reference_provider.ts
Expand Up @@ -3,9 +3,10 @@ import { flatMap } from "../../shared/utils";
import { fsPath } from "../../shared/utils/fs";
import { toRange, toRangeOnLine } from "../../shared/vscode/utils";
import { DasAnalyzerClient } from "../analysis/analyzer_das";
import { DasFileTracker } from "../analysis/file_tracker_das";

export class DartReferenceProvider implements ReferenceProvider, DefinitionProvider {
constructor(private readonly analyzer: DasAnalyzerClient) { }
constructor(private readonly analyzer: DasAnalyzerClient, private readonly fileTracker: DasFileTracker) { }

public async provideReferences(document: TextDocument, position: Position, context: ReferenceContext, token: CancellationToken): Promise<Location[] | undefined> {
// If we want to include the decleration, kick off a request for that.
Expand Down Expand Up @@ -33,11 +34,16 @@ export class DartReferenceProvider implements ReferenceProvider, DefinitionProvi
}

public async provideDefinition(document: TextDocument, position: Position, token: CancellationToken): Promise<DefinitionLink[] | undefined> {
const resp = await this.analyzer.analysisGetNavigation({
file: fsPath(document.uri),
length: 0,
offset: document.offsetAt(position),
});
let resp1 = this.fileTracker.getNavigationTargets(fsPath(document.uri), document.offsetAt(position));
DanTup marked this conversation as resolved.
Show resolved Hide resolved
if (!resp1) {
resp1 = await this.analyzer.analysisGetNavigation({
file: fsPath(document.uri),
length: 0,
offset: document.offsetAt(position),
});
}
if (!resp1) return undefined;
DanTup marked this conversation as resolved.
Show resolved Hide resolved
const resp = resp1;

if (token && token.isCancellationRequested)
return;
Expand Down