Skip to content

Commit 8ac1cbb

Browse files
committed
Add format hotkey setting
1 parent d742c7f commit 8ac1cbb

File tree

5 files changed

+106
-25
lines changed

5 files changed

+106
-25
lines changed

Pasfmt.Main.pas

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ TPlugin = class(TObject)
3939
procedure OnSettingsActionExecute(Sender: TObject);
4040
procedure Format;
4141

42+
procedure RegisterKeyboardBinding;
43+
procedure UnregisterKeyboardBinding;
4244
procedure ConfigureFormatter(var Formatter: TEditBufferFormatter);
4345
public
4446
constructor Create;
@@ -115,8 +117,15 @@ constructor TPlugin.Create;
115117
FPasfmtMenu.Add(MenuItem);
116118

117119
(BorlandIDEServices as INTAServices).AddActionMenu('CustomToolsItem', nil, FPasfmtMenu);
118-
FKeyboardBindingIndex :=
119-
(BorlandIDEServices as IOTAKeyboardServices).AddKeyboardBinding(TPasfmtKeyboardBinding.Create);
120+
121+
RegisterKeyboardBinding;
122+
PasfmtSettings.OnFormatHotkeyChanged :=
123+
procedure
124+
begin
125+
UnregisterKeyboardBinding;
126+
RegisterKeyboardBinding;
127+
end;
128+
120129
FAddInOptions := TPasfmtAddInOptions.Create;
121130
(BorlandIDEServices as INTAEnvironmentOptionsServices).RegisterAddInOptions(FAddInOptions);
122131

@@ -141,7 +150,7 @@ destructor TPlugin.Destroy;
141150
(BorlandIDEServices as IOTAEditorServices).RemoveNotifier(FEditorIndex);
142151
(BorlandIDEServices as IOTAAboutBoxServices).RemovePluginInfo(FInfoIndex);
143152
(BorlandIDEServices as INTAEnvironmentOptionsServices).UnregisterAddInOptions(FAddInOptions);
144-
(BorlandIDEServices as IOTAKeyboardServices).RemoveKeyboardBinding(FKeyboardBindingIndex);
153+
UnregisterKeyboardBinding;
145154
FreeAndNil(FPasfmtMenu);
146155
FreeAndNil(FBitmaps);
147156
inherited;
@@ -192,6 +201,17 @@ procedure TPlugin.OnSettingsActionExecute(Sender: TObject);
192201
(BorlandIDEServices as IOTAServices).GetEnvironmentOptions.EditOptions('', 'Pasfmt');
193202
end;
194203

204+
procedure TPlugin.RegisterKeyboardBinding;
205+
begin
206+
FKeyboardBindingIndex :=
207+
(BorlandIDEServices as IOTAKeyboardServices).AddKeyboardBinding(TPasfmtKeyboardBinding.Create);
208+
end;
209+
210+
procedure TPlugin.UnregisterKeyboardBinding;
211+
begin
212+
(BorlandIDEServices as IOTAKeyboardServices).RemoveKeyboardBinding(FKeyboardBindingIndex);
213+
end;
214+
195215
procedure TPlugin.OnFormatKeyPress(
196216
const Context: IOTAKeyContext;
197217
KeyCode: TShortCut;
@@ -229,7 +249,7 @@ function TPlugin.GetPluginVersion: string;
229249
procedure TPasfmtKeyboardBinding.BindKeyboard(const BindingServices: IOTAKeyBindingServices);
230250
begin
231251
BindingServices
232-
.AddKeyBinding([ShortCut(Ord('F'), [ssCtrl, ssAlt])], GPlugin.OnFormatKeyPress, nil, 0, '', 'PasfmtFormatItem');
252+
.AddKeyBinding([PasfmtSettings.FormatHotkey], GPlugin.OnFormatKeyPress, nil, 0, '', 'PasfmtFormatItem');
233253
end;
234254

235255
function TPasfmtKeyboardBinding.GetBindingType: TBindingType;

Pasfmt.Settings.pas

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ interface
44

55
uses
66
Pasfmt.Log,
7-
System.Win.Registry;
7+
System.Win.Registry,
8+
System.Classes,
9+
System.SysUtils;
810

911
type
1012
TPasfmtSettings = class(TObject)
@@ -15,6 +17,7 @@ TPasfmtSettings = class(TObject)
1517
CFormatOnSaveName = 'Format On Save';
1618
CFormatTimeoutName = 'Format Timeout';
1719
CMaxFileKiBWithUndoHistory = 'Max File Size With Undo History';
20+
CFormatHotkey = 'Format Hotkey';
1821
private
1922
FRegistry: TRegistry;
2023
FBaseKey: string;
@@ -24,12 +27,16 @@ TPasfmtSettings = class(TObject)
2427
FFormatOnSave: Boolean;
2528
FFormatTimeout: Integer;
2629
FMaxFileKiBWithUndoHistory: Integer;
30+
FFormatHotkey: TShortCut;
31+
32+
FFormatHotkeyChanged: TProc;
2733

2834
procedure SetLogLevel(Value: TLogLevel);
2935
procedure SetExecutablePath(Value: string);
3036
procedure SetFormatOnSave(Value: Boolean);
3137
procedure SetFormatTimeout(Value: Integer);
3238
procedure SetMaxFileKiBWithUndoHistory(Value: Integer);
39+
procedure SetFormatHotkey(Value: TShortCut);
3340
public
3441
constructor Create;
3542
destructor Destroy; override;
@@ -41,6 +48,9 @@ TPasfmtSettings = class(TObject)
4148
property FormatOnSave: Boolean read FFormatOnSave write SetFormatOnSave;
4249
property FormatTimeout: Integer read FFormatTimeout write SetFormatTimeout;
4350
property MaxFileKiBWithUndoHistory: Integer read FMaxFileKiBWithUndoHistory write SetMaxFileKiBWithUndoHistory;
51+
property FormatHotkey: TShortCut read FFormatHotkey write SetFormatHotkey;
52+
53+
property OnFormatHotkeyChanged: TProc write FFormatHotkeyChanged;
4454
end;
4555

4656
function PasfmtSettings: TPasfmtSettings;
@@ -49,8 +59,8 @@ implementation
4959

5060
uses
5161
ToolsAPI,
52-
System.SysUtils,
53-
Winapi.Windows;
62+
Winapi.Windows,
63+
Vcl.Menus;
5464

5565
var
5666
GSettings: TPasfmtSettings;
@@ -72,6 +82,7 @@ constructor TPasfmtSettings.Create;
7282
FBaseKey := (BorlandIDEServices as IOTAServices).GetBaseRegistryKey + '\Pasfmt';
7383
FRegistry := TRegistry.Create(KEY_ALL_ACCESS);
7484
FRegistry.RootKey := HKEY_CURRENT_USER;
85+
FFormatHotkeyChanged := nil;
7586
end;
7687

7788
//______________________________________________________________________________________________________________________
@@ -96,6 +107,24 @@ procedure TPasfmtSettings.SetExecutablePath(Value: string);
96107
end;
97108
end;
98109

110+
procedure TPasfmtSettings.SetFormatHotkey(Value: TShortCut);
111+
begin
112+
if FFormatHotkey = Value then
113+
exit;
114+
115+
FFormatHotkey := Value;
116+
117+
FRegistry.OpenKey(FBaseKey, True);
118+
try
119+
FRegistry.WriteString(CFormatHotkey, ShortCutToText(Value));
120+
finally
121+
FRegistry.CloseKey;
122+
end;
123+
124+
if Assigned(FFormatHotkeyChanged) then
125+
FFormatHotkeyChanged;
126+
end;
127+
99128
//______________________________________________________________________________________________________________________
100129

101130
procedure TPasfmtSettings.SetFormatOnSave(Value: Boolean);
@@ -163,6 +192,7 @@ procedure TPasfmtSettings.Load;
163192
FFormatOnSave := False;
164193
FFormatTimeout := 500;
165194
FMaxFileKiBWithUndoHistory := 1024;
195+
FFormatHotkey := ShortCut(Ord('F'), [ssCtrl, ssAlt]);
166196

167197
if FRegistry.ValueExists(CLogLevelName) then
168198
FLogLevel := TLogLevel(FRegistry.ReadInteger(CLogLevelName))
@@ -188,6 +218,11 @@ procedure TPasfmtSettings.Load;
188218
FMaxFileKiBWithUndoHistory := FRegistry.ReadInteger(CMaxFileKiBWithUndoHistory)
189219
else
190220
FRegistry.WriteInteger(CMaxFileKiBWithUndoHistory, FMaxFileKiBWithUndoHistory);
221+
222+
if FRegistry.ValueExists(CFormatHotkey) then
223+
FFormatHotkey := TextToShortCut(FRegistry.ReadString(CFormatHotkey))
224+
else
225+
FRegistry.WriteString(CFormatHotkey, ShortCutToText(FFormatHotkey));
191226
finally
192227
FRegistry.CloseKey;
193228
end;

Pasfmt.SettingsFrame.dfm

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
22
Left = 0
33
Top = 0
44
Width = 392
5-
Height = 376
5+
Height = 395
66
Constraints.MinHeight = 180
77
Constraints.MinWidth = 390
88
TabOrder = 0
99
object LogLevelLabel: TLabel
1010
Left = 3
11-
Top = 48
11+
Top = 105
1212
Width = 116
1313
Height = 15
1414
Caption = 'Minimum log severity'
1515
end
1616
object ExePathLabel: TLabel
1717
Left = 3
18-
Top = 205
19-
Width = 103
18+
Top = 262
19+
Width = 102
2020
Height = 15
2121
Caption = 'Executable location'
2222
end
@@ -29,26 +29,35 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
2929
end
3030
object TimeoutLabel: TLabel
3131
Left = 3
32-
Top = 104
32+
Top = 161
3333
Width = 110
3434
Height = 15
3535
Caption = 'Format timeout (ms)'
3636
end
3737
object FastModeThresholdLabel: TLabel
3838
Left = 3
39-
Top = 155
40-
Width = 220
39+
Top = 212
40+
Width = 219
4141
Height = 15
4242
Cursor = crHelp
4343
Hint =
44-
'To avoid performance issues with updating large files, a faster method ' +
45-
'is used when the file size exceeds this configured threshold.'#13#10'This ' +
46-
'faster method unfortunately clears the undo history of the edit buffer.'
44+
'To avoid performance issues with updating large files, a faster ' +
45+
'method is used when the file size exceeds this configured thresh' +
46+
'old.'#13#10'This faster method unfortunately clears the undo history o' +
47+
'f the edit buffer.'
4748
Caption = 'Maximum file size with undo history (KiB)'
4849
end
50+
object FormatHotkeyLabel: TLabel
51+
Left = 3
52+
Top = 49
53+
Width = 75
54+
Height = 15
55+
Cursor = crHelp
56+
Caption = 'Fomat Hotkey'
57+
end
4958
object LogLevelCombo: TComboBox
5059
Left = 11
51-
Top = 66
60+
Top = 123
5261
Width = 129
5362
Height = 23
5463
Style = csDropDownList
@@ -62,7 +71,7 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
6271
end
6372
object ExePathBrowseButton: TButton
6473
Left = 309
65-
Top = 304
74+
Top = 361
6675
Width = 71
6776
Height = 23
6877
Caption = 'Browse...'
@@ -71,7 +80,7 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
7180
end
7281
object ExePathRadioGroup: TRadioGroup
7382
Left = 3
74-
Top = 210
83+
Top = 267
7584
Width = 185
7685
Height = 65
7786
ItemIndex = 0
@@ -84,7 +93,7 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
8493
end
8594
object ExePathEdit: TEdit
8695
Left = 29
87-
Top = 275
96+
Top = 332
8897
Width = 351
8998
Height = 23
9099
TabOrder = 1
@@ -99,7 +108,7 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
99108
end
100109
object TimeoutEdit: TEdit
101110
Left = 11
102-
Top = 122
111+
Top = 179
103112
Width = 129
104113
Height = 23
105114
NumbersOnly = True
@@ -110,7 +119,7 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
110119
end
111120
object FastModeThresholdEdit: TEdit
112121
Left = 11
113-
Top = 173
122+
Top = 230
114123
Width = 129
115124
Height = 23
116125
NumbersOnly = True
@@ -119,10 +128,19 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
119128
TabOrder = 6
120129
Text = '1024'
121130
end
131+
object FormatHotkeyEdit: THotKey
132+
Left = 11
133+
Top = 67
134+
Width = 121
135+
Height = 23
136+
HotKey = 0
137+
Modifiers = []
138+
TabOrder = 7
139+
end
122140
object ExeChooseDialog: TOpenDialog
123141
Filter = 'Executable files (*.exe)|*.exe'
124142
Options = [ofHideReadOnly, ofPathMustExist, ofFileMustExist, ofEnableSizing]
125143
Left = 291
126-
Top = 52
144+
Top = 109
127145
end
128146
end

Pasfmt.SettingsFrame.pas

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ interface
99
Vcl.Dialogs,
1010
ToolsAPI,
1111
Vcl.StdCtrls,
12-
Vcl.ExtCtrls;
12+
Vcl.ExtCtrls,
13+
Vcl.ComCtrls;
1314

1415
type
1516
TPasfmtSettingsFrame = class(TFrame)
@@ -26,6 +27,8 @@ TPasfmtSettingsFrame = class(TFrame)
2627
TimeoutEdit: TEdit;
2728
FastModeThresholdEdit: TEdit;
2829
FastModeThresholdLabel: TLabel;
30+
FormatHotkeyEdit: THotKey;
31+
FormatHotkeyLabel: TLabel;
2932
procedure ExePathBrowseButtonClick(Sender: TObject);
3033
procedure ExePathRadioGroupClick(Sender: TObject);
3134
public
@@ -71,6 +74,7 @@ procedure TPasfmtAddInOptions.FrameCreated(AFrame: TCustomFrame);
7174
FFrame.OnSaveCheckBox.Checked := PasfmtSettings.FormatOnSave;
7275
FFrame.TimeoutEdit.Text := IntToStr(PasfmtSettings.FormatTimeout);
7376
FFrame.FastModeThresholdEdit.Text := IntToStr(PasfmtSettings.MaxFileKiBWithUndoHistory);
77+
FFrame.FormatHotkeyEdit.HotKey := PasfmtSettings.FormatHotkey;
7478
end;
7579

7680
//______________________________________________________________________________________________________________________
@@ -104,6 +108,8 @@ procedure TPasfmtAddInOptions.DialogClosed(Accepted: Boolean);
104108
if TryStrToInt(FFrame.FastModeThresholdEdit.Text, NewThreshold) then begin
105109
PasfmtSettings.MaxFileKiBWithUndoHistory := NewThreshold;
106110
end;
111+
112+
PasfmtSettings.FormatHotkey := FFrame.FormatHotkeyEdit.HotKey;
107113
end;
108114

109115
FFrame := nil;

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ and opinionated formatter for Delphi code, into the IDE.
2222

2323
With a file open in the editor, a format can be triggered with `Ctrl+Alt+F` (`Tools > Pasfmt > Format`).
2424

25+
The hotkey for formatting can be changed in the settings menu (`Tools > Pasfmt > Settings...`)
26+
2527
The formatter can optionally be triggered on save; this can be enabled in `Tools > Pasfmt > Settings...` by toggling
2628
`Format on save`.
2729

0 commit comments

Comments
 (0)