-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathPluginSettings.pas
More file actions
118 lines (102 loc) · 3.11 KB
/
PluginSettings.pas
File metadata and controls
118 lines (102 loc) · 3.11 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
unit PluginSettings;
interface
uses
System.SysUtils,
System.Classes,
System.JSON,
System.IOUtils,
Vcl.Menus;
const
DEFAULT_VSCODE_COMMAND = 'code';
DEFAULT_SHORTCUT_TEXT = 'Ctrl+\';
type
TShortcutChangedProc = reference to procedure(AShortcut: TShortCut);
/// <summary>
/// Persists the plugin's user-configurable settings:
/// the VS Code executable and the keyboard shortcut.
/// Settings are stored as JSON in %APPDATA%\EditInVSCode\settings.json.
/// </summary>
TPluginSettings = class
strict private
class var FVSCodeCommand: string;
class var FShortcut: TShortCut;
class var FOnShortcutChanged: TShortcutChangedProc;
class function SettingsFilePath: string; static;
class constructor Create;
public
class property VSCodeCommand: string read FVSCodeCommand write FVSCodeCommand;
class property Shortcut: TShortCut read FShortcut write FShortcut;
/// <summary>
/// Called by the IDE Options frame after saving, so the menu action
/// can update its shortcut without a circular unit dependency.
/// </summary>
class property OnShortcutChanged: TShortcutChangedProc
read FOnShortcutChanged write FOnShortcutChanged;
/// <summary>Loads settings from disk. Missing or unreadable file leaves defaults intact.</summary>
class procedure Load;
/// <summary>Persists current settings to disk, creating the directory if needed.</summary>
class procedure Save;
/// <summary>Fires OnShortcutChanged if assigned.</summary>
class procedure NotifyShortcutChanged;
end;
implementation
{ TPluginSettings }
class constructor TPluginSettings.Create;
begin
FVSCodeCommand := DEFAULT_VSCODE_COMMAND;
FShortcut := TextToShortCut(DEFAULT_SHORTCUT_TEXT);
end;
class function TPluginSettings.SettingsFilePath: string;
begin
Result := TPath.Combine(
TPath.Combine(GetEnvironmentVariable('APPDATA'), 'EditInVSCode'),
'settings.json');
end;
class procedure TPluginSettings.Load;
var
Root: TJSONObject;
Val: TJSONValue;
sc: TShortCut;
begin
if not TFile.Exists(SettingsFilePath) then
Exit;
Root := TJSONObject.ParseJSONValue(TFile.ReadAllText(SettingsFilePath)) as TJSONObject;
if Root = nil then
Exit;
try
Val := Root.GetValue('vsCodeCommand');
if (Val <> nil) and (Val.Value <> '') then
FVSCodeCommand := Val.Value;
Val := Root.GetValue('shortcut');
if Val <> nil then begin
sc := TextToShortCut(Val.Value);
if sc <> 0 then
FShortcut := sc;
end;
finally
Root.Free;
end;
end;
class procedure TPluginSettings.Save;
var
DirPath: string;
Root: TJSONObject;
begin
DirPath := ExtractFilePath(SettingsFilePath);
if not TDirectory.Exists(DirPath) then
TDirectory.CreateDirectory(DirPath);
Root := TJSONObject.Create;
try
Root.AddPair('vsCodeCommand', FVSCodeCommand);
Root.AddPair('shortcut', ShortCutToText(FShortcut));
TFile.WriteAllText(SettingsFilePath, Root.Format, TEncoding.UTF8);
finally
Root.Free;
end;
end;
class procedure TPluginSettings.NotifyShortcutChanged;
begin
if Assigned(FOnShortcutChanged) then
FOnShortcutChanged(FShortcut);
end;
end.