Skip to content

Commit

Permalink
[markers] don't store by default
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <[email protected]>
  • Loading branch information
akosyakov committed Jul 11, 2018
1 parent 4b631e9 commit a5f7852
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 179 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/browser/tree/tree-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export class TreeWidget extends ReactWidget implements StatefulWidget {
}

protected rows = new Map<string, TreeWidget.NodeRow>();
protected updateRows(): void {
protected updateRows = debounce(() => this.doUpdateRows(), 10);
protected doUpdateRows(): void {
const root = this.model.root;
if (root) {
const depths = new Map<CompositeTreeNode | undefined, number>();
Expand Down
72 changes: 0 additions & 72 deletions packages/markers/src/browser/marker-manager.spec.ts

This file was deleted.

83 changes: 16 additions & 67 deletions packages/markers/src/browser/marker-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -113,66 +110,23 @@ export abstract class MarkerManager<D extends object> {

protected readonly uri2MarkerCollection = new Map<string, MarkerCollection<D>>();
protected readonly onDidChangeMarkersEmitter = new Emitter<URI>();
readonly initialized: Promise<void>;

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<void> {
const key = this.getStorageKey();
if (key) {
const entries = await this.storageService.getData<Uri2MarkerEntry[]>(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<Uri2MarkerEntry[]>(key, result);
}
});
}

get onDidChangeMarkers(): Event<URI> {
Expand All @@ -186,14 +140,9 @@ export abstract class MarkerManager<D extends object> {
/*
* replaces the current markers for the given uri and owner with the given data.
*/
async setMarkers(uri: URI, owner: string, data: D[]): Promise<Marker<D>[]> {
await this.initialized;
return this.internalSetMarkers(uri, owner, data);
}

protected internalSetMarkers(uri: URI, owner: string, data: D[]): Marker<D>[] {
setMarkers(uri: URI, owner: string, data: D[]): Marker<D>[] {
const uriString = uri.toString();
const collection = this.uri2MarkerCollection.get(uriString) || new MarkerCollection<D>(uri, this.getKind());
const collection = this.uri2MarkerCollection.get(uriString) ||  new MarkerCollection<D>(uri, this.getKind());
const oldMarkers = collection.setMarkers(owner, data);
if (data.length > 0) {
this.uri2MarkerCollection.set(uriString, collection);
Expand Down
18 changes: 7 additions & 11 deletions packages/markers/src/browser/problem/problem-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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(<any>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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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);
});
});
10 changes: 1 addition & 9 deletions packages/markers/src/browser/problem/problem-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -35,12 +33,6 @@ export class ProblemManager extends MarkerManager<Diagnostic> {
return PROBLEM_KIND;
}

constructor(
@inject(StorageService) storageService: StorageService,
@inject(FileSystemWatcher) protected fileWatcher?: FileSystemWatcher) {
super(storageService, fileWatcher);
}

getProblemStat(): ProblemStat {
const allMarkers: Marker<Diagnostic>[] = [];
for (const uri of this.getUris()) {
Expand Down
35 changes: 16 additions & 19 deletions packages/markers/src/browser/problem/problem-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import { injectable, inject } from 'inversify';
import { ProblemManager } from './problem-manager';
import { ProblemMarker } from '../../common/problem-marker';
import { ProblemTreeModel } from './problem-tree-model';
import { MarkerInfoNode, MarkerNode } from '../marker-tree';
import { MarkerInfoNode, MarkerNode, MarkerRootNode } from '../marker-tree';
import { TreeWidget, TreeProps, ContextMenuRenderer, TreeNode, NodeProps, TreeModel } from "@theia/core/lib/browser";
import { DiagnosticSeverity } from 'vscode-languageserver-types';
import URI from '@theia/core/lib/common/uri';
import { UriSelection } from '@theia/core/lib/common/selection';
import * as React from "react";

@injectable()
Expand All @@ -45,22 +43,18 @@ export class ProblemWidget extends TreeWidget {
this.addClipboardListener(this.node, 'copy', e => this.handleCopy(e));
}

protected deflateForStorage(node: TreeNode): object {
const result = super.deflateForStorage(node) as any;
if (UriSelection.is(node) && node.uri) {
result.uri = node.uri.toString();
}
return result;
storeState(): object {
// no-op
return {};
}

protected inflateFromStorage(node: any, parent?: TreeNode): TreeNode {
if (node.uri) {
node.uri = new URI(node.uri);
}
if (node.selected) {
node.selected = false;
}
return super.inflateFromStorage(node);
protected superStoreState(): object {
return super.storeState();
}
restoreState(state: object): void {
// no-op
}
protected superRestoreState(state: object): void {
return super.restoreState(state);
}

protected handleCopy(event: ClipboardEvent) {
Expand All @@ -72,7 +66,10 @@ export class ProblemWidget extends TreeWidget {
}

protected renderTree(model: TreeModel): React.ReactNode {
return super.renderTree(model) || <div className='noMarkers'>No problems have been detected in the workspace so far.</div>;
if (MarkerRootNode.is(model.root) && model.root.children.length > 0) {
return super.renderTree(model);
}
return <div className='noMarkers'>No problems have been detected in the workspace so far.</div>;
}

protected renderCaption(node: TreeNode, props: NodeProps): React.ReactNode {
Expand Down

0 comments on commit a5f7852

Please sign in to comment.