Skip to content

Commit bbe16e4

Browse files
committed
Cache commonly called ALS requests in the extension state
1 parent 185c7ac commit bbe16e4

File tree

2 files changed

+96
-32
lines changed

2 files changed

+96
-32
lines changed

integration/vscode/ada/src/ExtensionState.ts

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as vscode from 'vscode';
2-
import { Disposable, LanguageClient } from 'vscode-languageclient/node';
2+
import { Disposable, ExecuteCommandRequest, LanguageClient } from 'vscode-languageclient/node';
33
import { AdaCodeLensProvider } from './AdaCodeLensProvider';
44
import { createClient } from './clients';
55
import { AdaInitialDebugConfigProvider, initializeDebugging } from './debugConfigProvider';
66
import { GnatTaskProvider } from './gnatTaskProvider';
7+
import { initializeTesting } from './gnattest';
78
import { GprTaskProvider } from './gprTaskProvider';
89
import { TERMINAL_ENV_SETTING_NAME } from './helpers';
910
import { registerTaskProviders } from './taskProviders';
@@ -32,6 +33,21 @@ export class ExtensionState {
3233

3334
public readonly codelensProvider = new AdaCodeLensProvider();
3435

36+
/**
37+
* The following fields are caches for ALS requests
38+
*/
39+
cachedProjectFile: string | undefined;
40+
cachedObjectDir: string | undefined;
41+
cachedMains: string[] | undefined;
42+
cachedExecutables: string[] | undefined;
43+
44+
public clearALSCache() {
45+
this.cachedProjectFile = undefined;
46+
this.cachedObjectDir = undefined;
47+
this.cachedMains = undefined;
48+
this.cachedExecutables = undefined;
49+
}
50+
3551
constructor(context: vscode.ExtensionContext) {
3652
this.context = context;
3753
this.gprClient = createClient(
@@ -110,6 +126,7 @@ export class ExtensionState {
110126
e.affectsConfiguration('ada.scenarioVariables') ||
111127
e.affectsConfiguration('ada.projectFile')
112128
) {
129+
this.clearALSCache();
113130
this.unregisterTaskProviders();
114131
this.registerTaskProviders();
115132
}
@@ -121,4 +138,60 @@ export class ExtensionState {
121138
void this.showReloadWindowPopup();
122139
}
123140
};
141+
142+
/**
143+
* @returns the full path of the main project file from the ALS
144+
*/
145+
public async getProjectFile(): Promise<string> {
146+
if (!this.cachedProjectFile) {
147+
this.cachedProjectFile = (await this.adaClient.sendRequest(ExecuteCommandRequest.type, {
148+
command: 'als-project-file',
149+
})) as string;
150+
}
151+
152+
return this.cachedProjectFile;
153+
}
154+
155+
/**
156+
*
157+
* @returns the full path of the project object directory obtained from the ALS
158+
*/
159+
public async getObjectDir(): Promise<string> {
160+
if (!this.cachedObjectDir) {
161+
this.cachedObjectDir = (await this.adaClient.sendRequest(ExecuteCommandRequest.type, {
162+
command: 'als-object-dir',
163+
})) as string;
164+
}
165+
166+
return this.cachedObjectDir;
167+
}
168+
169+
/**
170+
*
171+
* @returns the list of full paths of main sources defined in the project from the ALS
172+
*/
173+
public async getMains(): Promise<string[]> {
174+
if (!this.cachedMains) {
175+
this.cachedMains = (await this.adaClient.sendRequest(ExecuteCommandRequest.type, {
176+
command: 'als-mains',
177+
})) as string[];
178+
}
179+
180+
return this.cachedMains;
181+
}
182+
183+
/**
184+
*
185+
* @returns the list of full paths of executables corresponding to main
186+
* sources defined in the project from the ALS
187+
*/
188+
public async getExecutables(): Promise<string[]> {
189+
if (!this.cachedExecutables) {
190+
this.cachedExecutables = (await this.adaClient.sendRequest(ExecuteCommandRequest.type, {
191+
command: 'als-executables',
192+
})) as string[];
193+
}
194+
195+
return this.cachedExecutables;
196+
}
124197
}

integration/vscode/ada/src/helpers.ts

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ import assert from 'assert';
1818
import { platform } from 'os';
1919
import * as path from 'path';
2020
import * as vscode from 'vscode';
21-
import { ExecuteCommandRequest, LanguageClient } from 'vscode-languageclient/node';
21+
import { CancellationError, CancellationToken, DocumentSymbol, SymbolKind } from 'vscode';
22+
import { LanguageClient } from 'vscode-languageclient/node';
2223
import winston from 'winston';
24+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
25+
import { ExtensionState } from './ExtensionState';
2326
import { adaExtState, logger } from './extension';
24-
import { DocumentSymbol, SymbolKind, CancellationToken, CancellationError } from 'vscode';
2527

2628
/**
2729
* Substitue any variable reference present in the given string. VS Code
@@ -244,21 +246,16 @@ export function logErrorAndThrow(msg: string, logger: winston.Logger) {
244246
*/
245247

246248
/**
247-
* Get the project file from the workspace configuration if available, or from
248-
* the ALS if not.
249+
* Get the project file from the ALS.
249250
*
250-
* @param client - the client to send the request to. If not provided, the main
251+
* @param _client - the client to send the request to. If not provided, the main
251252
* Ada client of the extension is used.
252253
* @returns the full path of the currently loaded project file
254+
* @deprecated in favor of {@link ExtensionState.getProjectFile}
253255
*/
254-
export async function getProjectFile(client?: LanguageClient): Promise<string> {
255-
if (!client) {
256-
client = adaExtState.adaClient;
257-
}
258-
const result: string = (await client.sendRequest(ExecuteCommandRequest.type, {
259-
command: 'als-project-file',
260-
})) as string;
261-
return result;
256+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
257+
export async function getProjectFile(_client?: LanguageClient): Promise<string> {
258+
return adaExtState.getProjectFile();
262259
}
263260

264261
/**
@@ -273,39 +270,33 @@ export async function getProjectFileRelPath(): Promise<string> {
273270
* Get the Object Directory path
274271
* @param client - the client to send the request to
275272
* @returns a string path
273+
* @deprecated in favor of {@link ExtensionState.getObjectDir}
276274
*/
277-
export async function getObjectDir(client: LanguageClient): Promise<string> {
278-
const result: string = (await client.sendRequest(ExecuteCommandRequest.type, {
279-
command: 'als-object-dir',
280-
})) as string;
281-
return result;
275+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
276+
export async function getObjectDir(_client?: LanguageClient): Promise<string> {
277+
return adaExtState.getObjectDir();
282278
}
283279

284280
/**
285281
* Get the mains in the project
286282
* @param client - the client to send the request to
287283
* @returns an array of full paths to the main sources
284+
* @deprecated in favor of {@link ExtensionState.getMains}
288285
*/
289-
export async function getMains(client?: LanguageClient): Promise<string[]> {
290-
if (!client) {
291-
client = adaExtState.adaClient;
292-
}
293-
const result: string[] = (await client.sendRequest(ExecuteCommandRequest.type, {
294-
command: 'als-mains',
295-
})) as string[];
296-
return result;
286+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
287+
export async function getMains(_client?: LanguageClient): Promise<string[]> {
288+
return adaExtState.getMains();
297289
}
298290

299291
/**
300292
* Get the executables in the project
301293
* @param client - the client to send the request to
302294
* @returns a vector of string paths
295+
* @deprecated in favor of {@link ExtensionState.getExecutables}
303296
*/
304-
export async function getExecutables(client: LanguageClient): Promise<string[]> {
305-
const result: string[] = (await client.sendRequest(ExecuteCommandRequest.type, {
306-
command: 'als-executables',
307-
})) as string[];
308-
return result;
297+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
298+
export async function getExecutables(_client: LanguageClient): Promise<string[]> {
299+
return adaExtState.getExecutables();
309300
}
310301

311302
/**

0 commit comments

Comments
 (0)