forked from microsoft/vscode-cpptools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpersistentState.ts
More file actions
78 lines (67 loc) · 2.7 KB
/
Copy pathpersistentState.ts
File metadata and controls
78 lines (67 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* See 'LICENSE' in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
import * as util from '../common';
import * as vscode from 'vscode';
import * as path from 'path';
class PersistentStateBase<T> {
private key: string;
private defaultvalue: T;
private state?: vscode.Memento;
private curvalue: T;
constructor(key: string, defaultValue: T, state?: vscode.Memento) {
this.key = key;
this.defaultvalue = defaultValue;
this.state = state;
this.curvalue = defaultValue;
}
public get Value(): T {
return this.state ? this.state.get<T>(this.key, this.defaultvalue) : this.curvalue;
}
public set Value(newValue: T) {
if (this.state) {
this.state.update(this.key, newValue);
}
this.curvalue = newValue;
}
public get DefaultValue(): T {
return this.defaultvalue;
}
public setDefault(): void {
if (this.state) {
this.state.update(this.key, this.defaultvalue);
}
this.curvalue = this.defaultvalue;
}
}
// Abstraction for global state that persists across activations but is not present in a settings file
export class PersistentState<T> extends PersistentStateBase<T> {
constructor(key: string, defaultValue: T) {
super(key, defaultValue, util.extensionContext ? util.extensionContext.globalState : undefined);
}
}
export class PersistentWorkspaceState<T> extends PersistentStateBase<T> {
constructor(key: string, defaultValue: T) {
super(key, defaultValue, util.extensionContext ? util.extensionContext.workspaceState : undefined);
}
}
export class PersistentFolderState<T> extends PersistentWorkspaceState<T> {
constructor(key: string, defaultValue: T, folder: vscode.WorkspaceFolder) {
// Check for the old key. If found, remove it and update the new key with the old value.
const old_key: string = key + (folder ? `-${path.basename(folder.uri.fsPath)}` : "-untitled");
let old_val: T | undefined;
if (util.extensionContext) {
old_val = util.extensionContext.workspaceState.get(old_key);
if (old_val !== undefined) {
util.extensionContext.workspaceState.update(old_key, undefined);
}
}
const newKey: string = key + (folder ? `-${folder.uri.fsPath}` : "-untitled");
super(newKey, defaultValue);
if (old_val !== undefined) {
this.Value = old_val;
}
}
}