Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions Pasfmt.Main.pas
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ TPlugin = class(TObject)
procedure OnSettingsActionExecute(Sender: TObject);
procedure Format;

procedure RegisterKeyboardBinding;
procedure UnregisterKeyboardBinding;
procedure ConfigureFormatter(var Formatter: TEditBufferFormatter);
public
constructor Create;
Expand Down Expand Up @@ -115,8 +117,15 @@ constructor TPlugin.Create;
FPasfmtMenu.Add(MenuItem);

(BorlandIDEServices as INTAServices).AddActionMenu('CustomToolsItem', nil, FPasfmtMenu);
FKeyboardBindingIndex :=
(BorlandIDEServices as IOTAKeyboardServices).AddKeyboardBinding(TPasfmtKeyboardBinding.Create);

RegisterKeyboardBinding;
PasfmtSettings.OnFormatHotkeyChanged :=
procedure
begin
UnregisterKeyboardBinding;
RegisterKeyboardBinding;
end;

FAddInOptions := TPasfmtAddInOptions.Create;
(BorlandIDEServices as INTAEnvironmentOptionsServices).RegisterAddInOptions(FAddInOptions);

Expand All @@ -141,7 +150,7 @@ destructor TPlugin.Destroy;
(BorlandIDEServices as IOTAEditorServices).RemoveNotifier(FEditorIndex);
(BorlandIDEServices as IOTAAboutBoxServices).RemovePluginInfo(FInfoIndex);
(BorlandIDEServices as INTAEnvironmentOptionsServices).UnregisterAddInOptions(FAddInOptions);
(BorlandIDEServices as IOTAKeyboardServices).RemoveKeyboardBinding(FKeyboardBindingIndex);
UnregisterKeyboardBinding;
FreeAndNil(FPasfmtMenu);
FreeAndNil(FBitmaps);
inherited;
Expand Down Expand Up @@ -192,6 +201,17 @@ procedure TPlugin.OnSettingsActionExecute(Sender: TObject);
(BorlandIDEServices as IOTAServices).GetEnvironmentOptions.EditOptions('', 'Pasfmt');
end;

procedure TPlugin.RegisterKeyboardBinding;
begin
FKeyboardBindingIndex :=
(BorlandIDEServices as IOTAKeyboardServices).AddKeyboardBinding(TPasfmtKeyboardBinding.Create);
end;

procedure TPlugin.UnregisterKeyboardBinding;
begin
(BorlandIDEServices as IOTAKeyboardServices).RemoveKeyboardBinding(FKeyboardBindingIndex);
end;

procedure TPlugin.OnFormatKeyPress(
const Context: IOTAKeyContext;
KeyCode: TShortCut;
Expand Down Expand Up @@ -229,7 +249,7 @@ function TPlugin.GetPluginVersion: string;
procedure TPasfmtKeyboardBinding.BindKeyboard(const BindingServices: IOTAKeyBindingServices);
begin
BindingServices
.AddKeyBinding([ShortCut(Ord('F'), [ssCtrl, ssAlt])], GPlugin.OnFormatKeyPress, nil, 0, '', 'PasfmtFormatItem');
.AddKeyBinding([PasfmtSettings.FormatHotkey], GPlugin.OnFormatKeyPress, nil, 0, '', 'PasfmtFormatItem');
Comment thread
TimoHocker marked this conversation as resolved.
end;

function TPasfmtKeyboardBinding.GetBindingType: TBindingType;
Expand Down
41 changes: 38 additions & 3 deletions Pasfmt.Settings.pas
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ interface

uses
Pasfmt.Log,
System.Win.Registry;
System.Win.Registry,
System.Classes,
System.SysUtils;

type
TPasfmtSettings = class(TObject)
Expand All @@ -15,6 +17,7 @@ TPasfmtSettings = class(TObject)
CFormatOnSaveName = 'Format On Save';
CFormatTimeoutName = 'Format Timeout';
CMaxFileKiBWithUndoHistory = 'Max File Size With Undo History';
CFormatHotkey = 'Format Hotkey';
private
FRegistry: TRegistry;
FBaseKey: string;
Expand All @@ -24,12 +27,16 @@ TPasfmtSettings = class(TObject)
FFormatOnSave: Boolean;
FFormatTimeout: Integer;
FMaxFileKiBWithUndoHistory: Integer;
FFormatHotkey: TShortCut;

FFormatHotkeyChanged: TProc;

procedure SetLogLevel(Value: TLogLevel);
procedure SetExecutablePath(Value: string);
procedure SetFormatOnSave(Value: Boolean);
procedure SetFormatTimeout(Value: Integer);
procedure SetMaxFileKiBWithUndoHistory(Value: Integer);
procedure SetFormatHotkey(Value: TShortCut);
public
constructor Create;
destructor Destroy; override;
Expand All @@ -41,6 +48,9 @@ TPasfmtSettings = class(TObject)
property FormatOnSave: Boolean read FFormatOnSave write SetFormatOnSave;
property FormatTimeout: Integer read FFormatTimeout write SetFormatTimeout;
property MaxFileKiBWithUndoHistory: Integer read FMaxFileKiBWithUndoHistory write SetMaxFileKiBWithUndoHistory;
property FormatHotkey: TShortCut read FFormatHotkey write SetFormatHotkey;

property OnFormatHotkeyChanged: TProc write FFormatHotkeyChanged;
end;

function PasfmtSettings: TPasfmtSettings;
Expand All @@ -49,8 +59,8 @@ implementation

uses
ToolsAPI,
System.SysUtils,
Winapi.Windows;
Winapi.Windows,
Vcl.Menus;

var
GSettings: TPasfmtSettings;
Expand All @@ -72,6 +82,7 @@ constructor TPasfmtSettings.Create;
FBaseKey := (BorlandIDEServices as IOTAServices).GetBaseRegistryKey + '\Pasfmt';
FRegistry := TRegistry.Create(KEY_ALL_ACCESS);
FRegistry.RootKey := HKEY_CURRENT_USER;
FFormatHotkeyChanged := nil;
end;

//______________________________________________________________________________________________________________________
Expand All @@ -96,6 +107,24 @@ procedure TPasfmtSettings.SetExecutablePath(Value: string);
end;
end;

procedure TPasfmtSettings.SetFormatHotkey(Value: TShortCut);
begin
if FFormatHotkey = Value then
exit;

FFormatHotkey := Value;

FRegistry.OpenKey(FBaseKey, True);
try
FRegistry.WriteString(CFormatHotkey, ShortCutToText(Value));
finally
FRegistry.CloseKey;
end;

if Assigned(FFormatHotkeyChanged) then
FFormatHotkeyChanged;
end;

//______________________________________________________________________________________________________________________

procedure TPasfmtSettings.SetFormatOnSave(Value: Boolean);
Expand Down Expand Up @@ -163,6 +192,7 @@ procedure TPasfmtSettings.Load;
FFormatOnSave := False;
FFormatTimeout := 500;
FMaxFileKiBWithUndoHistory := 1024;
FFormatHotkey := ShortCut(Ord('F'), [ssCtrl, ssAlt]);

if FRegistry.ValueExists(CLogLevelName) then
FLogLevel := TLogLevel(FRegistry.ReadInteger(CLogLevelName))
Expand All @@ -188,6 +218,11 @@ procedure TPasfmtSettings.Load;
FMaxFileKiBWithUndoHistory := FRegistry.ReadInteger(CMaxFileKiBWithUndoHistory)
else
FRegistry.WriteInteger(CMaxFileKiBWithUndoHistory, FMaxFileKiBWithUndoHistory);

if FRegistry.ValueExists(CFormatHotkey) then
FFormatHotkey := TextToShortCut(FRegistry.ReadString(CFormatHotkey))
else
FRegistry.WriteString(CFormatHotkey, ShortCutToText(FFormatHotkey));
finally
FRegistry.CloseKey;
end;
Expand Down
52 changes: 35 additions & 17 deletions Pasfmt.SettingsFrame.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
Left = 0
Top = 0
Width = 392
Height = 376
Height = 395
Constraints.MinHeight = 180
Constraints.MinWidth = 390
TabOrder = 0
object LogLevelLabel: TLabel
Left = 3
Top = 48
Top = 105
Width = 116
Height = 15
Caption = 'Minimum log severity'
end
object ExePathLabel: TLabel
Left = 3
Top = 205
Width = 103
Top = 262
Width = 102
Height = 15
Caption = 'Executable location'
end
Expand All @@ -29,26 +29,35 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
end
object TimeoutLabel: TLabel
Left = 3
Top = 104
Top = 161
Width = 110
Height = 15
Caption = 'Format timeout (ms)'
end
object FastModeThresholdLabel: TLabel
Left = 3
Top = 155
Width = 220
Top = 212
Width = 219
Height = 15
Cursor = crHelp
Hint =
'To avoid performance issues with updating large files, a faster method ' +
'is used when the file size exceeds this configured threshold.'#13#10'This ' +
'faster method unfortunately clears the undo history of the edit buffer.'
'To avoid performance issues with updating large files, a faster ' +
'method is used when the file size exceeds this configured thresh' +
'old.'#13#10'This faster method unfortunately clears the undo history o' +
'f the edit buffer.'
Caption = 'Maximum file size with undo history (KiB)'
end
object FormatHotkeyLabel: TLabel
Left = 3
Top = 49
Width = 75
Height = 15
Cursor = crHelp
Caption = 'Fomat Hotkey'
end
object LogLevelCombo: TComboBox
Left = 11
Top = 66
Top = 123
Width = 129
Height = 23
Style = csDropDownList
Expand All @@ -62,7 +71,7 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
end
object ExePathBrowseButton: TButton
Left = 309
Top = 304
Top = 361
Width = 71
Height = 23
Caption = 'Browse...'
Expand All @@ -71,7 +80,7 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
end
object ExePathRadioGroup: TRadioGroup
Left = 3
Top = 210
Top = 267
Width = 185
Height = 65
ItemIndex = 0
Expand All @@ -84,7 +93,7 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
end
object ExePathEdit: TEdit
Left = 29
Top = 275
Top = 332
Width = 351
Height = 23
TabOrder = 1
Expand All @@ -99,7 +108,7 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
end
object TimeoutEdit: TEdit
Left = 11
Top = 122
Top = 179
Width = 129
Height = 23
NumbersOnly = True
Expand All @@ -110,7 +119,7 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
end
object FastModeThresholdEdit: TEdit
Left = 11
Top = 173
Top = 230
Width = 129
Height = 23
NumbersOnly = True
Expand All @@ -119,10 +128,19 @@ object PasfmtSettingsFrame: TPasfmtSettingsFrame
TabOrder = 6
Text = '1024'
end
object FormatHotkeyEdit: THotKey
Left = 11
Top = 67
Width = 121
Height = 23
HotKey = 0
Modifiers = []
TabOrder = 7
end
object ExeChooseDialog: TOpenDialog
Filter = 'Executable files (*.exe)|*.exe'
Options = [ofHideReadOnly, ofPathMustExist, ofFileMustExist, ofEnableSizing]
Left = 291
Top = 52
Top = 109
end
end
8 changes: 7 additions & 1 deletion Pasfmt.SettingsFrame.pas
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ interface
Vcl.Dialogs,
ToolsAPI,
Vcl.StdCtrls,
Vcl.ExtCtrls;
Vcl.ExtCtrls,
Vcl.ComCtrls;

type
TPasfmtSettingsFrame = class(TFrame)
Expand All @@ -26,6 +27,8 @@ TPasfmtSettingsFrame = class(TFrame)
TimeoutEdit: TEdit;
FastModeThresholdEdit: TEdit;
FastModeThresholdLabel: TLabel;
FormatHotkeyEdit: THotKey;
FormatHotkeyLabel: TLabel;
procedure ExePathBrowseButtonClick(Sender: TObject);
procedure ExePathRadioGroupClick(Sender: TObject);
public
Expand Down Expand Up @@ -71,6 +74,7 @@ procedure TPasfmtAddInOptions.FrameCreated(AFrame: TCustomFrame);
FFrame.OnSaveCheckBox.Checked := PasfmtSettings.FormatOnSave;
FFrame.TimeoutEdit.Text := IntToStr(PasfmtSettings.FormatTimeout);
FFrame.FastModeThresholdEdit.Text := IntToStr(PasfmtSettings.MaxFileKiBWithUndoHistory);
FFrame.FormatHotkeyEdit.HotKey := PasfmtSettings.FormatHotkey;
end;

//______________________________________________________________________________________________________________________
Expand Down Expand Up @@ -104,6 +108,8 @@ procedure TPasfmtAddInOptions.DialogClosed(Accepted: Boolean);
if TryStrToInt(FFrame.FastModeThresholdEdit.Text, NewThreshold) then begin
PasfmtSettings.MaxFileKiBWithUndoHistory := NewThreshold;
end;

PasfmtSettings.FormatHotkey := FFrame.FormatHotkeyEdit.HotKey;
end;

FFrame := nil;
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ and opinionated formatter for Delphi code, into the IDE.

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

The hotkey for formatting can be changed in the settings menu (`Tools > Pasfmt > Settings...`)

The formatter can optionally be triggered on save; this can be enabled in `Tools > Pasfmt > Settings...` by toggling
`Format on save`.

Expand Down