]> Git Repo - pico-vscode.git/blob - src/settings.mts
Add global env vars and compile button
[pico-vscode.git] / src / settings.mts
1 import type { Memento, WorkspaceConfiguration } from "vscode";
2 import { workspace } from "vscode";
3
4 export enum SettingsKey {
5   picoSDK = "sdk",
6   envSuffix = "envSuffix",
7   picoSDKPath = "sdkPath",
8   toolchainPath = "toolchainPath",
9   cmakePath = "cmakePath",
10   python3Path = "python3Path",
11   ninjaPath = "ninjaPath",
12   cmakeAutoConfigure = "cmakeAutoConfigure",
13 }
14
15 export type Setting = string | boolean | string[] | null | undefined;
16
17 export interface PackageJSON {
18   name: string;
19   publisher: string;
20 }
21
22 export default class Settings {
23   private config: WorkspaceConfiguration;
24   public context: Memento;
25   private pkg: PackageJSON;
26
27   constructor(context: Memento, packageJSON: PackageJSON) {
28     this.context = context;
29     this.pkg = packageJSON;
30
31     this.config = workspace.getConfiguration(packageJSON.name);
32   }
33
34   public get(key: SettingsKey): Setting {
35     return this.config.get(key);
36   }
37
38   public getIt<T>(key: SettingsKey): T | undefined {
39     const value = this.config.get(key);
40     // TODO: typeof value !== T does currently not work in TypeScript
41     // but if it could be a good backend for getString, getBoolean and so on
42     if (value === undefined) {
43       return undefined;
44     }
45
46     return value as T;
47   }
48
49   public getString(key: SettingsKey): string | undefined {
50     const value = this.get(key);
51
52     return typeof value === "string" ? value : undefined;
53   }
54
55   public getBoolean(key: SettingsKey): boolean | undefined {
56     const value = this.get(key);
57
58     return typeof value === "boolean" ? value : undefined;
59   }
60
61   public getArray(key: SettingsKey): string[] | undefined {
62     const value = this.get(key);
63
64     return Array.isArray(value) ? value : undefined;
65   }
66
67   public update<T>(key: SettingsKey, value: T): Thenable<void> {
68     return this.config.update(key, value, false);
69   }
70
71   public updateGlobal<T>(key: SettingsKey, value: T): Thenable<void> {
72     return this.config.update(key, value, true);
73   }
74
75   // helpers
76   public getExtensionName(): string {
77     return this.pkg.name;
78   }
79
80   public getExtensionId(): string {
81     return [this.pkg.publisher, this.pkg.name].join(".");
82   }
83 }
This page took 0.037296 seconds and 4 git commands to generate.