Skip to content

Commit 6e30aa5

Browse files
author
Benjamin Pasero
committed
debt - introduce and use in-memory storage for standalone editor
1 parent c06861f commit 6e30aa5

5 files changed

Lines changed: 73 additions & 34 deletions

File tree

src/vs/editor/contrib/parameterHints/test/parameterHintsModel.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { TextModel } from 'vs/editor/common/model/textModel';
1414
import * as modes from 'vs/editor/common/modes';
1515
import { createTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
1616
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
17-
import { IStorageService, NullStorageService } from 'vs/platform/storage/common/storage';
17+
import { IStorageService, InMemoryStorageService } from 'vs/platform/storage/common/storage';
1818
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
1919
import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils';
2020
import { ParameterHintsModel } from '../parameterHintsWidget';
@@ -44,7 +44,7 @@ suite('ParameterHintsModel', () => {
4444
model: textModel,
4545
serviceCollection: new ServiceCollection(
4646
[ITelemetryService, NullTelemetryService],
47-
[IStorageService, NullStorageService]
47+
[IStorageService, InMemoryStorageService]
4848
)
4949
});
5050
disposables.push(textModel);

src/vs/editor/contrib/suggest/suggestWidget.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,6 @@ export class SuggestWidget implements IContentWidget, IListVirtualDelegate<IComp
437437
private detailsFocusBorderColor: string;
438438
private detailsBorderColor: string;
439439

440-
private storageServiceAvailable: boolean = true;
441-
private expandSuggestionDocs: boolean = false;
442-
443440
private firstFocusInCurrentList: boolean = false;
444441

445442
constructor(
@@ -461,14 +458,6 @@ export class SuggestWidget implements IContentWidget, IListVirtualDelegate<IComp
461458
this.focusedItem = null;
462459
this.storageService = storageService;
463460

464-
// :facepalm: No other smart way to determine if this is monaco or vscode.
465-
// The former doesnt have a storage service
466-
this.storageService.store('___suggest___', true, StorageScope.GLOBAL);
467-
if (!this.storageService.get('___suggest___', StorageScope.GLOBAL)) {
468-
this.storageServiceAvailable = false;
469-
}
470-
this.storageService.remove('___suggest___', StorageScope.GLOBAL);
471-
472461
this.element = $('.editor-widget.suggest-widget');
473462
if (!this.editor.getConfiguration().contribInfo.iconsInSuggestions) {
474463
addClass(this.element, 'no-icons');
@@ -1100,19 +1089,11 @@ export class SuggestWidget implements IContentWidget, IListVirtualDelegate<IComp
11001089
}
11011090

11021091
private expandDocsSettingFromStorage(): boolean {
1103-
if (this.storageServiceAvailable) {
1104-
return this.storageService.getBoolean('expandSuggestionDocs', StorageScope.GLOBAL, expandSuggestionDocsByDefault);
1105-
} else {
1106-
return this.expandSuggestionDocs;
1107-
}
1092+
return this.storageService.getBoolean('expandSuggestionDocs', StorageScope.GLOBAL, expandSuggestionDocsByDefault);
11081093
}
11091094

11101095
private updateExpandDocsSetting(value: boolean) {
1111-
if (this.storageServiceAvailable) {
1112-
this.storageService.store('expandSuggestionDocs', value, StorageScope.GLOBAL);
1113-
} else {
1114-
this.expandSuggestionDocs = value;
1115-
}
1096+
this.storageService.store('expandSuggestionDocs', value, StorageScope.GLOBAL);
11161097
}
11171098

11181099
dispose(): void {

src/vs/editor/contrib/suggest/test/suggestModel.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { ISelectedSuggestion } from 'vs/editor/contrib/suggest/suggestWidget';
2323
import { TestCodeEditor, createTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
2424
import { MockMode } from 'vs/editor/test/common/mocks/mockMode';
2525
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
26-
import { IStorageService, NullStorageService } from 'vs/platform/storage/common/storage';
26+
import { IStorageService, InMemoryStorageService } from 'vs/platform/storage/common/storage';
2727
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
2828
import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils';
2929
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService';
@@ -42,7 +42,7 @@ function createMockEditor(model: TextModel): TestCodeEditor {
4242
model: model,
4343
serviceCollection: new ServiceCollection(
4444
[ITelemetryService, NullTelemetryService],
45-
[IStorageService, NullStorageService]
45+
[IStorageService, InMemoryStorageService]
4646
),
4747
});
4848
editor.registerAndInstantiateContribution(SnippetController2);

src/vs/editor/standalone/browser/standaloneServices.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { MarkerService } from 'vs/platform/markers/common/markerService';
3737
import { IMarkerService } from 'vs/platform/markers/common/markers';
3838
import { INotificationService } from 'vs/platform/notification/common/notification';
3939
import { IProgressService } from 'vs/platform/progress/common/progress';
40-
import { IStorageService, NullStorageService } from 'vs/platform/storage/common/storage';
40+
import { IStorageService, InMemoryStorageService } from 'vs/platform/storage/common/storage';
4141
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
4242
import { IThemeService } from 'vs/platform/theme/common/themeService';
4343
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
@@ -143,7 +143,7 @@ export module StaticServices {
143143

144144
export const progressService = define(IProgressService, () => new SimpleProgressService());
145145

146-
export const storageService = define(IStorageService, () => NullStorageService);
146+
export const storageService = define(IStorageService, () => InMemoryStorageService);
147147

148148
export const logService = define(ILogService, () => new NullLogService());
149149

src/vs/platform/storage/common/storage.ts

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
7-
import { Event } from 'vs/base/common/event';
7+
import { Event, Emitter } from 'vs/base/common/event';
8+
import { Disposable } from 'vs/base/common/lifecycle';
9+
import { isUndefinedOrNull } from 'vs/base/common/types';
810

911
export const IStorageService = createDecorator<IStorageService>('storageService');
1012

@@ -91,32 +93,88 @@ export interface IWorkspaceStorageChangeEvent {
9193
scope: StorageScope;
9294
}
9395

94-
export const NullStorageService: IStorageService = new class implements IStorageService {
96+
export const InMemoryStorageService: IStorageService = new class extends Disposable implements IStorageService {
9597
_serviceBrand = undefined;
9698

97-
onDidChangeStorage = Event.None;
98-
onWillSaveState = Event.None;
99+
private _onDidChangeStorage: Emitter<IWorkspaceStorageChangeEvent> = this._register(new Emitter<IWorkspaceStorageChangeEvent>());
100+
get onDidChangeStorage(): Event<IWorkspaceStorageChangeEvent> { return this._onDidChangeStorage.event; }
101+
102+
readonly onWillSaveState = Event.None;
103+
104+
private globalCache: Map<string, string> = new Map<string, string>();
105+
private workspaceCache: Map<string, string> = new Map<string, string>();
106+
107+
private getCache(scope: StorageScope): Map<string, string> {
108+
return scope === StorageScope.GLOBAL ? this.globalCache : this.workspaceCache;
109+
}
99110

100111
get(key: string, scope: StorageScope, fallbackValue: string): string;
101112
get(key: string, scope: StorageScope, fallbackValue?: string): string | undefined {
102-
return fallbackValue;
113+
const value = this.getCache(scope).get(key);
114+
115+
if (isUndefinedOrNull(value)) {
116+
return fallbackValue;
117+
}
118+
119+
return value;
103120
}
104121

105122
getBoolean(key: string, scope: StorageScope, fallbackValue: boolean): boolean;
106123
getBoolean(key: string, scope: StorageScope, fallbackValue?: boolean): boolean | undefined {
107-
return fallbackValue;
124+
const value = this.getCache(scope).get(key);
125+
126+
if (isUndefinedOrNull(value)) {
127+
return fallbackValue;
128+
}
129+
130+
return value === 'true';
108131
}
109132

110133
getInteger(key: string, scope: StorageScope, fallbackValue: number): number;
111134
getInteger(key: string, scope: StorageScope, fallbackValue?: number): number | undefined {
112-
return fallbackValue;
135+
const value = this.getCache(scope).get(key);
136+
137+
if (isUndefinedOrNull(value)) {
138+
return fallbackValue;
139+
}
140+
141+
return parseInt(value, 10);
113142
}
114143

115144
store(key: string, value: any, scope: StorageScope): Promise<void> {
145+
146+
// We remove the key for undefined/null values
147+
if (isUndefinedOrNull(value)) {
148+
return this.remove(key, scope);
149+
}
150+
151+
// Otherwise, convert to String and store
152+
const valueStr = String(value);
153+
154+
// Return early if value already set
155+
const currentValue = this.getCache(scope).get(key);
156+
if (currentValue === valueStr) {
157+
return Promise.resolve();
158+
}
159+
160+
// Update in cache
161+
this.getCache(scope).set(key, valueStr);
162+
163+
// Events
164+
this._onDidChangeStorage.fire({ scope, key });
165+
116166
return Promise.resolve();
117167
}
118168

119169
remove(key: string, scope: StorageScope): Promise<void> {
170+
const wasDeleted = this.getCache(scope).delete(key);
171+
if (!wasDeleted) {
172+
return Promise.resolve(); // Return early if value already deleted
173+
}
174+
175+
// Events
176+
this._onDidChangeStorage.fire({ scope, key });
177+
120178
return Promise.resolve();
121179
}
122180
};

0 commit comments

Comments
 (0)