* Colorize, Dialog to set/save/load options and add copyright notice

git-svn-id: trunk@59146 -
This commit is contained in:
michael 2018-09-23 11:03:26 +00:00
parent fc0237b5ab
commit 3a2e657bca
6 changed files with 302 additions and 60 deletions

View File

@ -11,4 +11,10 @@ If you type multiple words, all words must match.
The key combination can be configured in the key options, search for The key combination can be configured in the key options, search for
spotter. spotter.
You can set
* the color of the matches,
* color of shortcut key (and whether it should be shown at all)
in the IDE options.
Todo: Search recent files/packages

View File

@ -15,7 +15,7 @@ object SpotterForm: TSpotterForm
OnShow = FormShow OnShow = FormShow
Position = poDefaultSizeOnly Position = poDefaultSizeOnly
ShowInTaskBar = stNever ShowInTaskBar = stNever
LCLVersion = '1.9.0.0' LCLVersion = '2.1.0.0'
Visible = True Visible = True
object ECommand: TEdit object ECommand: TEdit
Left = 0 Left = 0
@ -37,12 +37,13 @@ object SpotterForm: TSpotterForm
Align = alClient Align = alClient
ClickOnSelChange = False ClickOnSelChange = False
ItemHeight = 0 ItemHeight = 0
OnClick = LBMatchesClickC OnClick = LBMatchesClick
OnEnter = LBMatchesEnter OnDrawItem = LBMatchesDrawItem
OnKeyUp = LBMatchesKeyUp OnKeyUp = LBMatchesKeyUp
ParentShowHint = False ParentShowHint = False
ScrollWidth = 495 ScrollWidth = 495
ShowHint = True ShowHint = True
Style = lbOwnerDrawFixed
TabOrder = 1 TabOrder = 1
TabStop = False TabStop = False
TopIndex = -1 TopIndex = -1

View File

@ -35,10 +35,32 @@ interface
uses uses
Classes, SysUtils, FileUtil, LazUTF8, Forms, Controls, Graphics, Dialogs, Classes, SysUtils, FileUtil, LazUTF8, Forms, Controls, Graphics, Dialogs,
StdCtrls, IDECommands, LazIDEIntf; StdCtrls, IDECommands, LazIDEIntf, Types;
type type
{ TSearchItem }
TMatchPos = Array of Integer;
TSearchItem = Class(TObject)
private
FKeyStroke: String;
FMatchLen: TMatchPos;
FMatchPos: TMatchPos;
FOwnsSource: Boolean;
FPrefix: String;
FSource: TObject;
procedure SetSource(AValue: TObject);
Public
Constructor Create(aSource : TObject;AOwnsSource : Boolean = False);
Destructor Destroy; override;
published
Property OwnsSource: Boolean read FOwnsSource Write FOwnsSource;
Property Source : TObject read FSource write FSource;
Property KeyStroke : String Read FKeyStroke Write FKeyStroke;
Property Prefix : String Read FPrefix Write FPrefix;
Property MatchPos : TMatchPos Read FMatchPos Write FMatchPos;
Property MatchLen : TMatchPos Read FMatchLen Write FMatchLen;
end;
{ TSpotterForm } { TSpotterForm }
TSpotterForm = class(TForm) TSpotterForm = class(TForm)
@ -50,36 +72,141 @@ type
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject); procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject); procedure FormShow(Sender: TObject);
procedure LBMatchesClickC(Sender: TObject); procedure LBMatchesClick(Sender: TObject);
procedure LBMatchesEnter(Sender: TObject); procedure LBMatchesDrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
procedure LBMatchesKeyUp(Sender: TObject; var Key: Word; {%H-}Shift: TShiftState procedure LBMatchesKeyUp(Sender: TObject; var Key: Word; {%H-}Shift: TShiftState
); );
private private
FKeyStrokeColor: TColor;
FMatchColor: TColor;
FShowCategory: Boolean; FShowCategory: Boolean;
FCommands: TStringList; FSearchItems: TStringList;
FOrgCaption : String; FOrgCaption : String;
FShowShortCutKey: Boolean;
Procedure Initialize;
procedure ExecuteSelected; procedure ExecuteSelected;
Procedure FillCommands; Procedure FillCommands;
procedure FilterCommandList(aCmd: String); procedure FilterList(aSearchTerm: String);
function GetCommandCategoryString(Cmd: TIDECommand): String; function GetCommandCategoryString(Cmd: TIDECommand): String;
procedure RefreshCaption(aCount: Integer);
public public
Property ShowCategory : Boolean Read FShowCategory Write FShowCategory; Property ShowCategory : Boolean Read FShowCategory Write FShowCategory;
Property ShowShortCutKey : Boolean Read FShowShortCutKey Write FShowShortCutKey;
property KeyStrokeColor : TColor Read FKeyStrokeColor Write FKeyStrokeColor;
property MatchColor : TColor Read FMatchColor Write FMatchColor;
end; end;
Var
ShowCmdCategory : Boolean = True;
ShowShortCutKey : Boolean = True;
MatchColor : TColor = clRed;
KeyStrokeColor : TColor = clGreen;
Procedure ShowSpotterForm;
Procedure ApplySpotterOptions;
Procedure SaveSpotterOptions;
procedure LoadSpotterOptions;
implementation
Uses BaseIDEIntf, LazConfigStorage, StrUtils, LCLIntf, LCLType, LCLProc, srceditorintf;
{$R *.lfm}
var var
SpotterForm: TSpotterForm; SpotterForm: TSpotterForm;
implementation
Uses StrUtils,LCLType, LCLProc; Const
IDESpotterOptsFile = 'idespotter.xml';
{$R *.lfm} KeyShowCategory = 'showcategory/value';
KeyShowShortCut = 'showShortCut/value';
KeyShortCutColor = 'ShortCutColor/value';
KeyMatchColor = 'MatchColor/value';
procedure LoadSpotterOptions;
var
Cfg: TConfigStorage;
begin
Cfg:=GetIDEConfigStorage(IDESpotterOptsFile,true);
try
ShowCmdCategory:=Cfg.GetValue(KeyShowCategory,ShowCmdCategory);
ShowShortCutKey:=Cfg.GetValue(KeyShowShortCut,ShowShortCutKey);
KeyStrokeColor:=TColor(Cfg.GetValue(KeyShortCutColor,Ord(KeyStrokeColor)));
MatchColor:=TColor(Cfg.GetValue(KeyMatchColor,Ord(MatchColor)));
ApplySpotterOptions;
finally
Cfg.Free;
end;
end;
procedure SaveSpotterOptions;
var
Cfg: TConfigStorage;
begin
Cfg:=GetIDEConfigStorage(IDESpotterOptsFile,false);
try
Cfg.SetValue(KeyShowCategory,ShowCmdCategory);
Cfg.SetValue(KeyShowShortCut,ShowShortCutKey);
Cfg.SetValue(KeyShortCutColor,Ord(KeyStrokeColor));
Cfg.SetValue(KeyMatchColor,Ord(MatchColor));
finally
Cfg.Free;
end;
end;
Procedure ApplySpotterOptions;
begin
if Assigned(SpotterForm) then
begin
SpotterForm.ShowCategory:=ShowCmdCategory;
SpotterForm.MatchColor:=MatchColor;
SpotterForm.KeyStrokeColor:=KeyStrokeColor;
SpotterForm.ShowShortCutKey:=ShowShortCutKey;
SpotterForm.Initialize;
end;
end;
Procedure ShowSpotterForm;
begin
if SpotterForm=Nil then
begin
SpotterForm:=TSpotterForm.Create(Application);
ApplySpotterOptions;
end;
SpotterForm.Show;
end;
{ TSearchItem }
procedure TSearchItem.SetSource(AValue: TObject);
begin
if FSource=AValue then Exit;
FSource:=AValue;
end;
constructor TSearchItem.Create(aSource: TObject; AOwnsSource: Boolean);
begin
FSource:=aSource;
FOwnsSource:=AOwnsSource;
end;
destructor TSearchItem.Destroy;
begin
if OwnsSource then
FreeAndNil(FSource);
Inherited;
end;
{ TSpotterForm } { TSpotterForm }
procedure TSpotterForm.ECommandChange(Sender: TObject); procedure TSpotterForm.ECommandChange(Sender: TObject);
begin begin
FilterCommandList(ECommand.Text); FilterList(ECommand.Text);
end; end;
procedure TSpotterForm.ECommandKeyUp(Sender: TObject; var Key: Word; procedure TSpotterForm.ECommandKeyUp(Sender: TObject; var Key: Word;
@ -118,14 +245,23 @@ begin
CloseAction:=caHide; CloseAction:=caHide;
end; end;
procedure TSpotterForm.FilterCommandList(aCmd: String); procedure TSpotterForm.RefreshCaption(aCount : Integer);
begin
if ACount=-1 then
Caption:=FOrgCaption+Format(' (%d)',[FSearchItems.Count])
else
Caption:=FOrgCaption+Format(' (%d/%d)',[aCount,FSearchItems.Count]);
end;
procedure TSpotterForm.FilterList(aSearchTerm: String);
Var Var
i : Integer; i : Integer;
Cmd : TIDECommand; Itm : TSearchItem;
pref,ks : string;
Words : Array of string; Words : Array of string;
MatchPos : Array of Integer; MatchPos : Array of Integer;
MatchLen : Array of Integer;
Function Match(S : String) : Boolean; Function Match(S : String) : Boolean;
@ -144,30 +280,27 @@ Var
end; end;
begin begin
aCmd:=LowerCase(aCmd); aSearchTerm:=LowerCase(aSearchTerm);
Setlength(Words,WordCount(aCmd,[' '])); Setlength(Words,WordCount(aSearchTerm,[' ']));
Setlength(MatchPos,Length(Words)); Setlength(MatchPos,Length(Words));
Setlength(MatchLen,Length(Words));
For I:=1 to Length(Words) do For I:=1 to Length(Words) do
Words[I-1]:=ExtractWord(I,aCmd,[' ']); begin
Words[I-1]:=ExtractWord(I,aSearchTerm,[' ']);
MatchLen[I-1]:=Length(Words[I-1]);
end;
LBMatches.Items.BeginUpdate; LBMatches.Items.BeginUpdate;
try try
LBMatches.Items.Clear; LBMatches.Items.Clear;
For I:=0 to FCommands.Count-1 do For I:=0 to FSearchItems.Count-1 do
if Match(FCommands[I]) then if Match(FSearchItems[I]) then
begin begin
Cmd:=TIDECommand(FCommands.Objects[i]); Itm:=FSearchItems.Objects[I] as TSearchItem;
Pref:=GetCommandCategoryString(Cmd); Itm.MatchPos:=Copy(MatchPos,0,Length(MatchPos));
ks:=''; Itm.MatchLen:=Copy(MatchLen,0,Length(MatchLen));
if Cmd.ShortcutA.Key1<>VK_UNKNOWN then LBMatches.Items.AddObject(FSearchItems[I],Itm);
begin
ks:=' ('+KeyAndShiftStateToKeyString(Cmd.ShortcutA.Key1,Cmd.ShortcutA.Shift1);
if Cmd.ShortcutA.Key2<>VK_UNKNOWN then
ks:=Ks+', '+KeyAndShiftStateToKeyString(Cmd.ShortcutA.Key1,Cmd.ShortcutA.Shift1);
ks:=ks+')';
end; end;
LBMatches.Items.AddObject(Pref+Cmd.LocalizedName+ks,Cmd); RefreshCaption(LBMatches.Items.Count);
end;
Caption:=FOrgCaption+Format(' (%d/%d)',[LBMatches.Items.Count,FCommands.Count]);
finally finally
LBMatches.Items.EndUpdate; LBMatches.Items.EndUpdate;
LBMatches.Visible:=LBMatches.Items.Count>0; LBMatches.Visible:=LBMatches.Items.Count>0;
@ -178,9 +311,9 @@ end;
procedure TSpotterForm.FormCreate(Sender: TObject); procedure TSpotterForm.FormCreate(Sender: TObject);
begin begin
FCommands:=TStringList.Create; FSearchItems:=TStringList.Create;
FSearchItems.OwnsObjects:=True;
FOrgCaption:=Caption; FOrgCaption:=Caption;
FillCommands;
end; end;
procedure TSpotterForm.FormShow(Sender: TObject); procedure TSpotterForm.FormShow(Sender: TObject);
@ -196,31 +329,88 @@ begin
end; end;
ECommand.Clear; ECommand.Clear;
LBMatches.Clear; LBMatches.Clear;
RefreshCaption(-1);
end; end;
procedure TSpotterForm.LBMatchesClickC(Sender: TObject); procedure TSpotterForm.LBMatchesClick(Sender: TObject);
begin begin
ExecuteSelected; ExecuteSelected;
end; end;
procedure TSpotterForm.LBMatchesDrawItem(Control: TWinControl; Index: Integer;
ARect: TRect; State: TOwnerDrawState);
Var
LB : TListbox;
DS,S : String;
Itm : TSearchItem;
R : TRect;
P,I,SP,W,SW : Integer;
FC : TColor;
begin
SW := GetSystemMetrics(SM_CXVSCROLL);
LB:=Control as TListBox;
LB.Canvas.FillRect(ARect);
FC:=LB.Canvas.Font.Color;
Itm:=LB.Items.Objects[Index] as TSearchItem;
S:=LB.Items[Index];
R:=ARect;
if ShowShortCutKey and (Itm.KeyStroke<>'') then
begin
W:=LB.Canvas.TextWidth(Itm.KeyStroke);
R.Right:=R.Right-W-SW;
end;
Inc(R.Left,2);
SP:=1;
For I:=0 to Length(Itm.MatchPos)-1 do
begin
P:=Itm.MatchPos[i];
if (P-SP>0) then
begin
DS:=Copy(S,SP,P-SP);
LB.Canvas.TextRect(R,R.Left,R.Top,DS);
Inc(R.Left,LB.Canvas.TextWidth(DS));
end;
DS:=Copy(S,P,Itm.MatchLen[i]);
LB.Canvas.Font.Color:=MatchColor;
LB.Canvas.TextRect(R,R.Left,R.Top,DS);
LB.Canvas.Font.Color:=FC;
Inc(R.Left,LB.Canvas.TextWidth(DS));
SP:=P+Itm.MatchLen[i];
end;
if SP<=Length(S) then
begin
DS:=Copy(S,SP,Length(S)-SP+1);
LB.Canvas.TextRect(R,R.Left,R.Top,DS);
end;
if Itm.KeyStroke<>'' then
begin
R.Left:=R.Right+1;
R.Right:=aRect.Right;
LB.Canvas.Font.Color:=KeyStrokeColor;
LB.Canvas.TextRect(R,R.Left,R.Top,Itm.KeyStroke);
end;
end;
procedure TSpotterForm.ExecuteSelected; procedure TSpotterForm.ExecuteSelected;
Var Var
idx: Integer; idx: Integer;
itm : TSearchItem;
begin begin
Idx:=LBMatches.ItemIndex; Idx:=LBMatches.ItemIndex;
if (Idx>=0) then if (Idx>=0) then
begin begin
Hide; Hide;
TIDECommand(LBMatches.Items.Objects[Idx]).Execute(Application); Itm:=LBMatches.Items.Objects[Idx] as TSearchItem;
if Itm.Source is TIDECommand then
TIDECommand(Itm.Source).Execute(SourceEditorManagerIntf.ActiveEditor);
end; end;
end; end;
procedure TSpotterForm.LBMatchesEnter(Sender: TObject);
begin
end;
procedure TSpotterForm.LBMatchesKeyUp(Sender: TObject; var Key: Word; procedure TSpotterForm.LBMatchesKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState); Shift: TShiftState);
@ -229,6 +419,11 @@ begin
Hide; Hide;
end; end;
procedure TSpotterForm.Initialize;
begin
FillCommands;
end;
function TSpotterForm.GetCommandCategoryString(Cmd: TIDECommand): String; function TSpotterForm.GetCommandCategoryString(Cmd: TIDECommand): String;
Const Const
@ -259,8 +454,9 @@ end;
procedure TSpotterForm.FillCommands; procedure TSpotterForm.FillCommands;
var var
I, J: Integer; I, J: Integer;
Itm : TSearchItem;
Cmd : TIDECommand; Cmd : TIDECommand;
Pref : String; Ks,Pref : String;
begin begin
For I:=0 to IDECommandList.CategoryCount-1 do For I:=0 to IDECommandList.CategoryCount-1 do
@ -270,11 +466,22 @@ begin
begin begin
Cmd:=TIDECommand(IDECommandList.Categories[I].Items[J]); Cmd:=TIDECommand(IDECommandList.Categories[I].Items[J]);
Pref:=GetCommandCategoryString(Cmd); Pref:=GetCommandCategoryString(Cmd);
FCommands.AddObject(UTF8LowerCase(Pref+Cmd.LocalizedName),Cmd); ks:='';
if Cmd.ShortcutA.Key1<>VK_UNKNOWN then
begin
ks:=' ('+KeyAndShiftStateToKeyString(Cmd.ShortcutA.Key1,Cmd.ShortcutA.Shift1);
if Cmd.ShortcutA.Key2<>VK_UNKNOWN then
ks:=Ks+', '+KeyAndShiftStateToKeyString(Cmd.ShortcutA.Key1,Cmd.ShortcutA.Shift1);
ks:=ks+')';
end;
Itm:=TSearchItem.Create(Cmd);
Itm.Prefix:=Pref;
Itm.KeyStroke:=Ks;
FSearchItems.AddObject(UTF8LowerCase(Pref+Cmd.LocalizedName),Itm);
end; end;
end; end;
FCommands.Sort; FSearchItems.Sort;
Caption:=FOrgCaption+Format(' (%d)',[FCommands.Count]); RefreshCaption(-1);
end; end;
end. end.

View File

@ -13,7 +13,7 @@
<Description Value="IDE addon showing the &quot;Sleuth - a quick search window for IDE commands"/> <Description Value="IDE addon showing the &quot;Sleuth - a quick search window for IDE commands"/>
<License Value="Modified LGPL-2"/> <License Value="Modified LGPL-2"/>
<Version Release="1"/> <Version Release="1"/>
<Files Count="3"> <Files Count="4">
<Item1> <Item1>
<Filename Value="regidespotter.pas"/> <Filename Value="regidespotter.pas"/>
<HasRegisterProc Value="True"/> <HasRegisterProc Value="True"/>
@ -27,6 +27,10 @@
<Filename Value="ReadMe.txt"/> <Filename Value="ReadMe.txt"/>
<Type Value="Binary"/> <Type Value="Binary"/>
</Item3> </Item3>
<Item4>
<Filename Value="idespotteroptions.pas"/>
<UnitName Value="IDESPotterOptions"/>
</Item4>
</Files> </Files>
<i18n> <i18n>
<EnableI18N Value="True"/> <EnableI18N Value="True"/>

View File

@ -8,13 +8,13 @@ unit idespotter;
interface interface
uses uses
regidespotter, frmspotter, LazarusPackageIntf; RegIDESpotter, frmspotter, IDESPotterOptions, LazarusPackageIntf;
implementation implementation
procedure Register; procedure Register;
begin begin
RegisterUnit('regidespotter', @regidespotter.Register); RegisterUnit('RegIDESpotter', @RegIDESpotter.Register);
end; end;
initialization initialization

View File

@ -1,3 +1,32 @@
{ Register IDE Spotter unit
Copyright (C) 2018 Michael van Canneyt michael@freepascal.org
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}
unit RegIDESpotter; unit RegIDESpotter;
{$mode objfpc}{$H+} {$mode objfpc}{$H+}
@ -5,27 +34,19 @@ unit RegIDESpotter;
interface interface
uses uses
Classes, SysUtils; // IdeIntf
IDEOptionsIntf, IDEOptEditorIntf, IDEUtils, Classes, SysUtils;
Procedure Register; Procedure Register;
implementation implementation
uses forms,lcltype,idecommands,toolbarintf,menuintf, frmspotter; uses IDESpotterOptions,forms, graphics,lcltype,idecommands,toolbarintf,menuintf, frmspotter;
Var
SpotterFrm : TSpotterForm;
ShowCmdCategory : Boolean = True;
Procedure IdeMenuClicked(Sender : TObject); Procedure IdeMenuClicked(Sender : TObject);
begin begin
if SpotterFrm=Nil then ShowSpotterForm;
begin
SpotterFrm:=TSpotterForm.Create(Application);
SpotterFrm.ShowCategory:=ShowCmdCategory;
end;
SpotterFrm.Show;
end; end;
Procedure Register; Procedure Register;
@ -42,6 +63,8 @@ var
IDEShortCutX: TIDEShortCut; IDEShortCutX: TIDEShortCut;
IDECommandCategory: TIDECommandCategory; IDECommandCategory: TIDECommandCategory;
IDECommand: TIDECommand; IDECommand: TIDECommand;
IDESpotteroptionsFrameID: Integer = 2000;
begin begin
IDEShortCutX := IDEShortCut(VK_P, ShiftKeys, VK_UNKNOWN, []); IDEShortCutX := IDEShortCut(VK_P, ShiftKeys, VK_UNKNOWN, []);
IDECommandCategory := IDECommandList.FindCategoryByName(CommandCategoryViewName); IDECommandCategory := IDECommandList.FindCategoryByName(CommandCategoryViewName);
@ -52,7 +75,8 @@ begin
if IDECommand <> nil then if IDECommand <> nil then
RegisterIDEButtonCommand(IDECommand); RegisterIDEButtonCommand(IDECommand);
end; end;
IDESpotteroptionsFrameID:=RegisterIDEOptionsEditor(GroupEnvironment,TIDESpotterOptionsFrame,
IDESpotteroptionsFrameID)^.Index;
RegisterIDEMenuCommand(itmViewIDEInternalsWindows, 'Spotter', 'Spotter', nil, RegisterIDEMenuCommand(itmViewIDEInternalsWindows, 'Spotter', 'Spotter', nil,
@IDEMenuClicked,IDECommand); @IDEMenuClicked,IDECommand);
end; end;