Skip to content

Commit 4d0c41d

Browse files
feat: clear completions cache on Refresh action on the sidebar VSCODE-408 (#517)
* feat: clear completions cache on Refresh action on the sidebar VSCODE-408 * refactor: use async await * refactor: reuse ClearCompletionsCache type
1 parent 7b10092 commit 4d0c41d

File tree

8 files changed

+186
-63
lines changed

8 files changed

+186
-63
lines changed

src/language/languageServerController.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
import * as vscode from 'vscode';
22
import * as path from 'path';
33
import type { MongoClientOptions } from 'mongodb';
4-
import {
5-
LanguageClient,
4+
import type {
65
LanguageClientOptions,
76
ServerOptions,
7+
} from 'vscode-languageclient/node';
8+
import {
9+
LanguageClient,
810
TransportKind,
911
CancellationTokenSource,
1012
} from 'vscode-languageclient/node';
11-
import { workspace, ExtensionContext } from 'vscode';
13+
import type { ExtensionContext } from 'vscode';
14+
import { workspace } from 'vscode';
1215

1316
import { createLogger } from '../logging';
14-
import {
17+
import type {
1518
PlaygroundEvaluateParams,
1619
ShellEvaluateResult,
1720
ExportToLanguageMode,
1821
ExportToLanguageNamespace,
1922
PlaygroundTextAndSelection,
2023
} from '../types/playgroundType';
24+
import type { ClearCompletionsCache } from '../types/completionsCache';
2125
import { ServerCommands } from './serverCommands';
2226

2327
const log = createLogger('language server controller');
@@ -185,6 +189,13 @@ export default class LanguageServerController {
185189
);
186190
}
187191

192+
async resetCache(clear: ClearCompletionsCache): Promise<void> {
193+
await this._client.sendRequest(
194+
ServerCommands.CLEAR_CACHED_COMPLETIONS,
195+
clear
196+
);
197+
}
198+
188199
cancelAll(): void {
189200
// Send a request for cancellation. As a result
190201
// the associated CancellationToken will be notified of the cancellation,

src/language/mongoDBService.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import type {
3333
PlaygroundTextAndSelection,
3434
MongoClientOptions,
3535
} from '../types/playgroundType';
36+
import type { ClearCompletionsCache } from '../types/completionsCache';
3637
import { Visitor } from './visitor';
3738
import type { CompletionState } from './visitor';
3839

@@ -134,10 +135,11 @@ export default class MongoDBService {
134135
* Disconnect from CliServiceProvider.
135136
*/
136137
async disconnectFromServiceProvider(): Promise<void> {
137-
this._clearCachedDatabases();
138-
this._clearCachedCollections();
139-
this._clearCachedFields();
140-
138+
this.clearCachedCompletions({
139+
databases: true,
140+
collections: true,
141+
fields: true,
142+
});
141143
await this._clearCurrentConnection();
142144
}
143145

@@ -148,7 +150,7 @@ export default class MongoDBService {
148150
params: PlaygroundEvaluateParams,
149151
token: CancellationToken
150152
): Promise<ShellEvaluateResult | undefined> {
151-
this._clearCachedFields();
153+
this.clearCachedFields();
152154

153155
return new Promise((resolve) => {
154156
if (this._connectionId !== params.connectionId) {
@@ -511,7 +513,7 @@ export default class MongoDBService {
511513
);
512514

513515
// Create and cache field completion items.
514-
this._cacheFields(namespace, schemaFields);
516+
this.cacheFields(namespace, schemaFields);
515517
}
516518
}
517519
}
@@ -1021,7 +1023,7 @@ export default class MongoDBService {
10211023
/**
10221024
* Convert schema field names to Completion Items and cache them.
10231025
*/
1024-
_cacheFields(namespace: string, fields: string[]): void {
1026+
cacheFields(namespace: string, fields: string[]): void {
10251027
if (namespace) {
10261028
this._fields[namespace] = fields ? fields : [];
10271029
}
@@ -1045,15 +1047,15 @@ export default class MongoDBService {
10451047
this._collections[database] = collections.map((item) => item.name);
10461048
}
10471049

1048-
_clearCachedFields(): void {
1050+
clearCachedFields(): void {
10491051
this._fields = {};
10501052
}
10511053

1052-
_clearCachedDatabases(): void {
1054+
clearCachedDatabases(): void {
10531055
this._databaseCompletionItems = [];
10541056
}
10551057

1056-
_clearCachedCollections(): void {
1058+
clearCachedCollections(): void {
10571059
this._collections = {};
10581060
}
10591061

@@ -1068,4 +1070,16 @@ export default class MongoDBService {
10681070
await serviceProvider.close(true);
10691071
}
10701072
}
1073+
1074+
clearCachedCompletions(clear: ClearCompletionsCache): void {
1075+
if (clear.fields) {
1076+
this._fields = {};
1077+
}
1078+
if (clear.databases) {
1079+
this._databaseCompletionItems = [];
1080+
}
1081+
if (clear.collections) {
1082+
this._collections = {};
1083+
}
1084+
}
10711085
}

src/language/server.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1+
import type {
2+
InitializeParams,
3+
CompletionItem,
4+
TextDocumentPositionParams,
5+
Connection,
6+
} from 'vscode-languageserver/node';
17
import {
28
createConnection,
39
TextDocuments,
410
ProposedFeatures,
5-
InitializeParams,
611
DidChangeConfigurationNotification,
7-
CompletionItem,
8-
TextDocumentPositionParams,
912
RequestType,
1013
TextDocumentSyncKind,
11-
Connection,
1214
} from 'vscode-languageserver/node';
1315
import { TextDocument } from 'vscode-languageserver-textdocument';
1416

1517
import MongoDBService from './mongoDBService';
16-
1718
import { ServerCommands } from './serverCommands';
18-
import {
19+
import type {
1920
PlaygroundEvaluateParams,
2021
PlaygroundTextAndSelection,
2122
} from '../types/playgroundType';
23+
import type { ClearCompletionsCache } from '../types/completionsCache';
2224

2325
// Create a connection for the server. The connection uses Node's IPC as a transport.
2426
// Also include all preview / proposed LSP features.
@@ -180,7 +182,7 @@ connection.onRequest(ServerCommands.DISCONNECT_TO_SERVICE_PROVIDER, () => {
180182
connection.onRequest(
181183
ServerCommands.UPDATE_CURRENT_SESSION_FIELDS,
182184
({ namespace, schemaFields }) => {
183-
return mongoDBService._cacheFields(namespace, schemaFields);
185+
return mongoDBService.cacheFields(namespace, schemaFields);
184186
}
185187
);
186188

@@ -200,6 +202,14 @@ connection.onRequest(
200202
}
201203
);
202204

205+
// Clear cached completions by provided cache names.
206+
connection.onRequest(
207+
ServerCommands.CLEAR_CACHED_COMPLETIONS,
208+
(clear: ClearCompletionsCache) => {
209+
return mongoDBService.clearCachedCompletions(clear);
210+
}
211+
);
212+
203213
// Provide MongoDB completion items.
204214
connection.onCompletion((params: TextDocumentPositionParams) => {
205215
const document = documents.get(params.textDocument.uri);

src/language/serverCommands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export enum ServerCommands {
99
GET_NAMESPACE_FOR_SELECTION = 'GET_NAMESPACE_FOR_SELECTION',
1010
GET_EXPORT_TO_LANGUAGE_MODE = 'GET_EXPORT_TO_LANGUAGE_MODE',
1111
UPDATE_CURRENT_SESSION_FIELDS = 'UPDATE_CURRENT_SESSION_FIELDS',
12+
CLEAR_CACHED_COMPLETIONS = 'CLEAR_CACHED_COMPLETIONS',
1213
}
1314

1415
export type PlaygroundRunParameters = {

src/mdbExtensionController.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,16 @@ export default class MDBExtensionController implements vscode.Disposable {
306306
);
307307
this.registerCommand(
308308
EXTENSION_COMMANDS.MDB_REFRESH_CONNECTION,
309-
(connectionTreeItem: ConnectionTreeItem): Promise<boolean> => {
309+
async (connectionTreeItem: ConnectionTreeItem): Promise<boolean> => {
310310
connectionTreeItem.resetCache();
311311
this._explorerController.refresh();
312+
await this._languageServerController.resetCache({
313+
databases: true,
314+
collections: true,
315+
fields: true,
316+
});
312317

313-
return Promise.resolve(true);
318+
return true;
314319
}
315320
);
316321
this.registerCommand(
@@ -410,11 +415,15 @@ export default class MDBExtensionController implements vscode.Disposable {
410415
);
411416
this.registerCommand(
412417
EXTENSION_COMMANDS.MDB_REFRESH_DATABASE,
413-
(databaseTreeItem: DatabaseTreeItem): Promise<boolean> => {
418+
async (databaseTreeItem: DatabaseTreeItem): Promise<boolean> => {
414419
databaseTreeItem.resetCache();
415420
this._explorerController.refresh();
421+
await this._languageServerController.resetCache({
422+
collections: true,
423+
fields: true,
424+
});
416425

417-
return Promise.resolve(true);
426+
return true;
418427
}
419428
);
420429
this.registerCommand(
@@ -473,11 +482,12 @@ export default class MDBExtensionController implements vscode.Disposable {
473482
);
474483
this.registerCommand(
475484
EXTENSION_COMMANDS.MDB_REFRESH_COLLECTION,
476-
(collectionTreeItem: CollectionTreeItem): Promise<boolean> => {
485+
async (collectionTreeItem: CollectionTreeItem): Promise<boolean> => {
477486
collectionTreeItem.resetCache();
478487
this._explorerController.refresh();
488+
await this._languageServerController.resetCache({ fields: true });
479489

480-
return Promise.resolve(true);
490+
return true;
481491
}
482492
);
483493
this.registerCommand(
@@ -505,8 +515,9 @@ export default class MDBExtensionController implements vscode.Disposable {
505515
async (documentsListTreeItem: DocumentListTreeItem): Promise<boolean> => {
506516
await documentsListTreeItem.resetCache();
507517
this._explorerController.refresh();
518+
await this._languageServerController.resetCache({ fields: true });
508519

509-
return Promise.resolve(true);
520+
return true;
510521
}
511522
);
512523
this.registerCommand(
@@ -522,11 +533,12 @@ export default class MDBExtensionController implements vscode.Disposable {
522533
);
523534
this.registerCommand(
524535
EXTENSION_COMMANDS.MDB_REFRESH_SCHEMA,
525-
(schemaTreeItem: SchemaTreeItem): Promise<boolean> => {
536+
async (schemaTreeItem: SchemaTreeItem): Promise<boolean> => {
526537
schemaTreeItem.resetCache();
527538
this._explorerController.refresh();
539+
await this._languageServerController.resetCache({ fields: true });
528540

529-
return Promise.resolve(true);
541+
return true;
530542
}
531543
);
532544
this.registerCommand(

0 commit comments

Comments
 (0)