diff --git a/packages/markers/src/browser/marker-manager.spec.ts b/packages/markers/src/browser/marker-manager.spec.ts deleted file mode 100644 index 81046bb85e30e..0000000000000 --- a/packages/markers/src/browser/marker-manager.spec.ts +++ /dev/null @@ -1,72 +0,0 @@ -/******************************************************************************** - * Copyright (C) 2018 Ericsson and others. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v. 2.0 which is available at - * http://www.eclipse.org/legal/epl-2.0. - * - * This Source Code may also be made available under the following Secondary - * Licenses when the conditions for such availability set forth in the Eclipse - * Public License v. 2.0 are satisfied: GNU General Public License, version 2 - * with the GNU Classpath Exception which is available at - * https://www.gnu.org/software/classpath/license.html. - * - * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 - ********************************************************************************/ - -import { ContainerModule, Container, injectable } from "inversify"; -import { MarkerManager, Uri2MarkerEntry } from "./marker-manager"; -import { StorageService } from "@theia/core/lib/browser/storage-service"; -import { MockStorageService } from "@theia/core/lib/browser/test/mock-storage-service"; -import { FileSystemWatcher } from "@theia/filesystem/lib/browser/filesystem-watcher"; -import URI from "@theia/core/lib/common/uri"; -import { Deferred } from "@theia/core/lib/common/promise-util"; -import { expect } from "chai"; - -let container: Container; - -@injectable() -class TestMarkerManager extends MarkerManager { - public getKind(): string { - return "test"; - } -} - -beforeEach(function () { - const m = new ContainerModule(bind => { - bind(MarkerManager).to(TestMarkerManager).inSingletonScope(); - bind(MockStorageService).toSelf().inSingletonScope(); - bind(StorageService).toDynamicValue(ctx => ctx.container.get(MockStorageService)); - bind(FileSystemWatcher).toConstantValue(undefined); - }); - - container = new Container(); - container.load(m); -}); - -describe("marker-manager", function () { - it("should not save URIs without markers to local storage", async function () { - // We want to be able to wait until the marker manager has set the data in the local storage. - let setDataPromise = new Deferred(); - const storageService = container.get(MockStorageService); - storageService.onSetData(() => setDataPromise.resolve()); - - // Provide one marker - const mm = container.get(MarkerManager); - const uri = new URI('/path/to/file1.ts'); - const owner = 'test-owner'; - mm.setMarkers(uri, 'test-owner', ['allo']); - await setDataPromise.promise; - - let entries = await storageService.getData('marker-test'); - expect(entries).to.have.lengthOf(1); - - // Provide no marker. The entry for the uri should not be saved in the local storage. - setDataPromise = new Deferred(); - mm.setMarkers(uri, owner, []); - await setDataPromise.promise; - - entries = await storageService.getData('marker-test'); - expect(entries).to.have.lengthOf(0); - }); -}); diff --git a/packages/markers/src/browser/marker-manager.ts b/packages/markers/src/browser/marker-manager.ts index fb4c0100babaa..0145be179ac15 100644 --- a/packages/markers/src/browser/marker-manager.ts +++ b/packages/markers/src/browser/marker-manager.ts @@ -14,15 +14,12 @@ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ -import { injectable, inject } from 'inversify'; +import { injectable, inject, postConstruct } from 'inversify'; import { Event, Emitter } from "@theia/core/lib/common"; import URI from "@theia/core/lib/common/uri"; -import { StorageService } from '@theia/core/lib/browser/storage-service'; import { FileSystemWatcher, FileChangeType } from '@theia/filesystem/lib/browser/filesystem-watcher'; import { Marker } from '../common/marker'; -const debounce = require("lodash.debounce"); - /* * argument to the `findMarkers` method. */ @@ -113,66 +110,23 @@ export abstract class MarkerManager { protected readonly uri2MarkerCollection = new Map>(); protected readonly onDidChangeMarkersEmitter = new Emitter(); - readonly initialized: Promise; - constructor( - @inject(StorageService) protected storageService: StorageService, - @inject(FileSystemWatcher) protected fileWatcher?: FileSystemWatcher) { - this.initialized = this.loadMarkersFromStorage(); - if (fileWatcher) { - fileWatcher.onFilesChanged(changes => { - for (const change of changes) { - if (change.type === FileChangeType.DELETED) { - const uriString = change.uri.toString(); - const collection = this.uri2MarkerCollection.get(uriString); - if (collection !== undefined) { - this.uri2MarkerCollection.delete(uriString); - this.fireOnDidChangeMarkers(change.uri); - } + @inject(FileSystemWatcher) protected fileWatcher: FileSystemWatcher; + + @postConstruct() + protected init(): void { + this.fileWatcher.onFilesChanged(changes => { + for (const change of changes) { + if (change.type === FileChangeType.DELETED) { + const uriString = change.uri.toString(); + const collection = this.uri2MarkerCollection.get(uriString); + if (collection !== undefined) { + this.uri2MarkerCollection.delete(uriString); + this.fireOnDidChangeMarkers(change.uri); } } - }); - } - } - - protected getStorageKey(): string | undefined { - return 'marker-' + this.getKind(); - } - - protected async loadMarkersFromStorage(): Promise { - const key = this.getStorageKey(); - if (key) { - const entries = await this.storageService.getData(key, []); - for (const entry of entries) { - for (const ownerEntry of entry.markers) { - this.internalSetMarkers(new URI(entry.uri), ownerEntry.owner, ownerEntry.markerData as D[]); - } } - this.onDidChangeMarkers(() => this.saveMarkersToStorage()); - } - } - - protected readonly saveMarkersToStorage = debounce(() => this.doSaveMarkersToStorage(), 500); - protected doSaveMarkersToStorage(): void { - const key = this.getStorageKey(); - if (key) { - const result: Uri2MarkerEntry[] = []; - for (const [uri, collection] of this.uri2MarkerCollection.entries()) { - const ownerEntries: Owner2MarkerEntry[] = []; - for (const owner of collection.getOwners()) { - const markers = collection.getMarkers(owner); - ownerEntries.push({ - owner, - markerData: Array.from(markers.map(m => m.data)) - }); - } - result.push({ - uri, - markers: ownerEntries - }); - } - this.storageService.setData(key, result); - } + }); } get onDidChangeMarkers(): Event { @@ -186,14 +140,9 @@ export abstract class MarkerManager { /* * replaces the current markers for the given uri and owner with the given data. */ - async setMarkers(uri: URI, owner: string, data: D[]): Promise[]> { - await this.initialized; - return this.internalSetMarkers(uri, owner, data); - } - - protected internalSetMarkers(uri: URI, owner: string, data: D[]): Marker[] { + setMarkers(uri: URI, owner: string, data: D[]): Marker[] { const uriString = uri.toString(); - const collection = this.uri2MarkerCollection.get(uriString) || new MarkerCollection(uri, this.getKind()); + const collection = this.uri2MarkerCollection.get(uriString) ||  new MarkerCollection(uri, this.getKind()); const oldMarkers = collection.setMarkers(owner, data); if (data.length > 0) { this.uri2MarkerCollection.set(uriString, collection); diff --git a/packages/markers/src/browser/problem/problem-manager.spec.ts b/packages/markers/src/browser/problem/problem-manager.spec.ts index be7c465d691b1..b1710a6426f54 100644 --- a/packages/markers/src/browser/problem/problem-manager.spec.ts +++ b/packages/markers/src/browser/problem/problem-manager.spec.ts @@ -19,6 +19,7 @@ import * as chai from 'chai'; import { ProblemManager } from './problem-manager'; import URI from "@theia/core/lib/common/uri"; import { LocalStorageService, StorageService } from '@theia/core/lib/browser/storage-service'; +import { Event } from '@theia/core/lib/common/event'; import { ILogger } from '@theia/core/lib/common/logger'; import { MockLogger } from '@theia/core/lib/common/test/mock-logger'; import { FileSystemWatcher } from '@theia/filesystem/lib/browser/filesystem-watcher'; @@ -27,17 +28,18 @@ const expect = chai.expect; let manager: ProblemManager; let testContainer: Container; -before(async () => { +before(() => { testContainer = new Container(); testContainer.bind(ILogger).to(MockLogger); testContainer.bind(StorageService).to(LocalStorageService).inSingletonScope(); testContainer.bind(LocalStorageService).toSelf().inSingletonScope(); // tslint:disable-next-line:no-any - testContainer.bind(FileSystemWatcher).toConstantValue(undefined); + testContainer.bind(FileSystemWatcher).toConstantValue({ + onFilesChanged: Event.None + } as FileSystemWatcher); testContainer.bind(ProblemManager).toSelf(); manager = testContainer.get(ProblemManager); - await manager.initialized; manager.setMarkers(new URI('file:/foo/bar.txt'), 'me', [ { range: { @@ -98,13 +100,13 @@ before(async () => { }); describe('problem-manager', () => { - it('replaces markers', async () => { + it('replaces markers', () => { let events = 0; manager.onDidChangeMarkers(() => { events++; }); expect(events).equal(0); - const previous = await manager.setMarkers(new URI('file:/foo/bar.txt'), 'me', [ + const previous = manager.setMarkers(new URI('file:/foo/bar.txt'), 'me', [ { range: { start: { @@ -155,10 +157,4 @@ describe('problem-manager', () => { dataFilter: data => data.range.end.character > 1 }).length).equal(1); }); - - it('should persist markers', async () => { - const newManager = testContainer.get(ProblemManager); - await newManager.initialized; - expect(newManager.findMarkers().length).eq(4); - }); }); diff --git a/packages/markers/src/browser/problem/problem-manager.ts b/packages/markers/src/browser/problem/problem-manager.ts index 0881c10a0c87e..9f289c93c5f20 100644 --- a/packages/markers/src/browser/problem/problem-manager.ts +++ b/packages/markers/src/browser/problem/problem-manager.ts @@ -14,12 +14,10 @@ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ -import { injectable, inject } from 'inversify'; +import { injectable } from 'inversify'; import { MarkerManager } from '../marker-manager'; import { PROBLEM_KIND } from '../../common/problem-marker'; import { Marker } from '../../common/marker'; -import { StorageService } from '@theia/core/lib/browser/storage-service'; -import { FileSystemWatcher } from '@theia/filesystem/lib/browser/filesystem-watcher'; import URI from '@theia/core/lib/common/uri'; import { Diagnostic } from "vscode-languageserver-types"; @@ -35,12 +33,6 @@ export class ProblemManager extends MarkerManager { return PROBLEM_KIND; } - constructor( - @inject(StorageService) storageService: StorageService, - @inject(FileSystemWatcher) protected fileWatcher?: FileSystemWatcher) { - super(storageService, fileWatcher); - } - getProblemStat(): ProblemStat { const allMarkers: Marker[] = []; for (const uri of this.getUris()) {