提交 6e30aa52 编写于 作者: B Benjamin Pasero

debt - introduce and use in-memory storage for standalone editor

上级 c06861f4
......@@ -14,7 +14,7 @@ import { TextModel } from 'vs/editor/common/model/textModel';
import * as modes from 'vs/editor/common/modes';
import { createTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { IStorageService, NullStorageService } from 'vs/platform/storage/common/storage';
import { IStorageService, InMemoryStorageService } from 'vs/platform/storage/common/storage';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils';
import { ParameterHintsModel } from '../parameterHintsWidget';
......@@ -44,7 +44,7 @@ suite('ParameterHintsModel', () => {
model: textModel,
serviceCollection: new ServiceCollection(
[ITelemetryService, NullTelemetryService],
[IStorageService, NullStorageService]
[IStorageService, InMemoryStorageService]
)
});
disposables.push(textModel);
......
......@@ -437,9 +437,6 @@ export class SuggestWidget implements IContentWidget, IListVirtualDelegate<IComp
private detailsFocusBorderColor: string;
private detailsBorderColor: string;
private storageServiceAvailable: boolean = true;
private expandSuggestionDocs: boolean = false;
private firstFocusInCurrentList: boolean = false;
constructor(
......@@ -461,14 +458,6 @@ export class SuggestWidget implements IContentWidget, IListVirtualDelegate<IComp
this.focusedItem = null;
this.storageService = storageService;
// :facepalm: No other smart way to determine if this is monaco or vscode.
// The former doesnt have a storage service
this.storageService.store('___suggest___', true, StorageScope.GLOBAL);
if (!this.storageService.get('___suggest___', StorageScope.GLOBAL)) {
this.storageServiceAvailable = false;
}
this.storageService.remove('___suggest___', StorageScope.GLOBAL);
this.element = $('.editor-widget.suggest-widget');
if (!this.editor.getConfiguration().contribInfo.iconsInSuggestions) {
addClass(this.element, 'no-icons');
......@@ -1100,19 +1089,11 @@ export class SuggestWidget implements IContentWidget, IListVirtualDelegate<IComp
}
private expandDocsSettingFromStorage(): boolean {
if (this.storageServiceAvailable) {
return this.storageService.getBoolean('expandSuggestionDocs', StorageScope.GLOBAL, expandSuggestionDocsByDefault);
} else {
return this.expandSuggestionDocs;
}
return this.storageService.getBoolean('expandSuggestionDocs', StorageScope.GLOBAL, expandSuggestionDocsByDefault);
}
private updateExpandDocsSetting(value: boolean) {
if (this.storageServiceAvailable) {
this.storageService.store('expandSuggestionDocs', value, StorageScope.GLOBAL);
} else {
this.expandSuggestionDocs = value;
}
this.storageService.store('expandSuggestionDocs', value, StorageScope.GLOBAL);
}
dispose(): void {
......
......@@ -23,7 +23,7 @@ import { ISelectedSuggestion } from 'vs/editor/contrib/suggest/suggestWidget';
import { TestCodeEditor, createTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor';
import { MockMode } from 'vs/editor/test/common/mocks/mockMode';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { IStorageService, NullStorageService } from 'vs/platform/storage/common/storage';
import { IStorageService, InMemoryStorageService } from 'vs/platform/storage/common/storage';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { NullTelemetryService } from 'vs/platform/telemetry/common/telemetryUtils';
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorkerService';
......@@ -42,7 +42,7 @@ function createMockEditor(model: TextModel): TestCodeEditor {
model: model,
serviceCollection: new ServiceCollection(
[ITelemetryService, NullTelemetryService],
[IStorageService, NullStorageService]
[IStorageService, InMemoryStorageService]
),
});
editor.registerAndInstantiateContribution(SnippetController2);
......
......@@ -37,7 +37,7 @@ import { MarkerService } from 'vs/platform/markers/common/markerService';
import { IMarkerService } from 'vs/platform/markers/common/markers';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { IProgressService } from 'vs/platform/progress/common/progress';
import { IStorageService, NullStorageService } from 'vs/platform/storage/common/storage';
import { IStorageService, InMemoryStorageService } from 'vs/platform/storage/common/storage';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IThemeService } from 'vs/platform/theme/common/themeService';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
......@@ -143,7 +143,7 @@ export module StaticServices {
export const progressService = define(IProgressService, () => new SimpleProgressService());
export const storageService = define(IStorageService, () => NullStorageService);
export const storageService = define(IStorageService, () => InMemoryStorageService);
export const logService = define(ILogService, () => new NullLogService());
......
......@@ -4,7 +4,9 @@
*--------------------------------------------------------------------------------------------*/
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { Event } from 'vs/base/common/event';
import { Event, Emitter } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import { isUndefinedOrNull } from 'vs/base/common/types';
export const IStorageService = createDecorator<IStorageService>('storageService');
......@@ -91,32 +93,88 @@ export interface IWorkspaceStorageChangeEvent {
scope: StorageScope;
}
export const NullStorageService: IStorageService = new class implements IStorageService {
export const InMemoryStorageService: IStorageService = new class extends Disposable implements IStorageService {
_serviceBrand = undefined;
onDidChangeStorage = Event.None;
onWillSaveState = Event.None;
private _onDidChangeStorage: Emitter<IWorkspaceStorageChangeEvent> = this._register(new Emitter<IWorkspaceStorageChangeEvent>());
get onDidChangeStorage(): Event<IWorkspaceStorageChangeEvent> { return this._onDidChangeStorage.event; }
readonly onWillSaveState = Event.None;
private globalCache: Map<string, string> = new Map<string, string>();
private workspaceCache: Map<string, string> = new Map<string, string>();
private getCache(scope: StorageScope): Map<string, string> {
return scope === StorageScope.GLOBAL ? this.globalCache : this.workspaceCache;
}
get(key: string, scope: StorageScope, fallbackValue: string): string;
get(key: string, scope: StorageScope, fallbackValue?: string): string | undefined {
return fallbackValue;
const value = this.getCache(scope).get(key);
if (isUndefinedOrNull(value)) {
return fallbackValue;
}
return value;
}
getBoolean(key: string, scope: StorageScope, fallbackValue: boolean): boolean;
getBoolean(key: string, scope: StorageScope, fallbackValue?: boolean): boolean | undefined {
return fallbackValue;
const value = this.getCache(scope).get(key);
if (isUndefinedOrNull(value)) {
return fallbackValue;
}
return value === 'true';
}
getInteger(key: string, scope: StorageScope, fallbackValue: number): number;
getInteger(key: string, scope: StorageScope, fallbackValue?: number): number | undefined {
return fallbackValue;
const value = this.getCache(scope).get(key);
if (isUndefinedOrNull(value)) {
return fallbackValue;
}
return parseInt(value, 10);
}
store(key: string, value: any, scope: StorageScope): Promise<void> {
// We remove the key for undefined/null values
if (isUndefinedOrNull(value)) {
return this.remove(key, scope);
}
// Otherwise, convert to String and store
const valueStr = String(value);
// Return early if value already set
const currentValue = this.getCache(scope).get(key);
if (currentValue === valueStr) {
return Promise.resolve();
}
// Update in cache
this.getCache(scope).set(key, valueStr);
// Events
this._onDidChangeStorage.fire({ scope, key });
return Promise.resolve();
}
remove(key: string, scope: StorageScope): Promise<void> {
const wasDeleted = this.getCache(scope).delete(key);
if (!wasDeleted) {
return Promise.resolve(); // Return early if value already deleted
}
// Events
this._onDidChangeStorage.fire({ scope, key });
return Promise.resolve();
}
};
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册