Skip to content

Commit

Permalink
only one error msg should shown if rg path not found, onUpdate events…
Browse files Browse the repository at this point in the history
… are eliminated
  • Loading branch information
mattia72 committed Feb 9, 2025
1 parent c1fbbe1 commit c974524
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 68 deletions.
9 changes: 4 additions & 5 deletions UnitTest/RipGrepper.Settings.RipGrepSettingsTest.pas
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ implementation
DEFPAS_DEFDFM = '*.defpas;*.defdfm';
C_PATH_TO_DIR = 'c:\path\to\dir';
PAS_DFM = '*.pas;*.dfm';
RG_EXE_PATH = 'rg.exe';
EGUIOPTION_MATCHCASE = '[MatchCase]';

constructor TRipGrepSettingsTest.Create;
Expand Down Expand Up @@ -152,7 +151,7 @@ procedure TRipGrepSettingsTest.SetDefaultsAndCurrentValues;
// Assert.AreEqual(0, FSettings.SettingsDict.Keys.Count, 'SettingsDict should be empty');
FSettings.StoreAsDefaultsToDict;

FSettings.RipGrepPath := RG_EXE_PATH;
FSettings.RipGrepPath := RG_EXE;
FSettings.FileMasks := PAS_DFM;
FSettings.SearchPath := C_PATH_TO_DIR;
FSettings.GuiSearchTextParams.SetSearchOptions(
Expand Down Expand Up @@ -190,7 +189,7 @@ procedure TRipGrepSettingsTest.LoadDefaultsTest;
begin
SetDefaultsAndCurrentValues;
FSettings.LoadDefaultsFromDict;
Assert.IsTrue(FSettings.RipGrepPath.Contains(RG_EXE_PATH), 'RipGrepPath should be set');
Assert.IsTrue(FSettings.RipGrepPath.Contains(RG_EXE), 'RipGrepPath should be set');

Assert.AreEqual(DEFPAS_DEFDFM, FSettings.FileMasks, 'FileMasks should be set');
Assert.AreEqual(C_DEF_PATH_TO_DIR, FSettings.SearchPath, 'SearchPath should be set');
Expand All @@ -209,7 +208,7 @@ procedure TRipGrepSettingsTest.LoadActualTest;
end;

FSettings.LoadFromDict;
Assert.IsTrue(FSettings.RipGrepPath.Contains(RG_EXE_PATH), 'RipGrepPath should be set');
Assert.IsTrue(FSettings.RipGrepPath.Contains(RG_EXE), 'RipGrepPath should be set');
Assert.AreEqual(PAS_DFM, FSettings.FileMasks, '2. FileMasks shouldn''t be the default');
Assert.AreEqual(C_PATH_TO_DIR, FSettings.SearchPath, 'SearchPath should be set');
Assert.AreEqual(MATCHWORD_USEREGEX, FSettings.GuiSearchTextParams.GetAsString(true), 'GuiSearchTextParams should be set');
Expand Down Expand Up @@ -268,7 +267,7 @@ procedure TRipGrepSettingsTest.UpdateIniReloadTest;
FSettings.SearchPath := '';
FSettings.ReLoad; // fills only settings dict
FSettings.LoadFromDict; // why loads defaults ?????
Assert.IsTrue(FSettings.RipGrepPath.Contains(RG_EXE_PATH), 'RipGrepPath should be set');
Assert.IsTrue(FSettings.RipGrepPath.Contains(RG_EXE), 'RipGrepPath should be set');
Assert.AreEqual(PAS_DFM, FSettings.FileMasks, 'FileMasks should be set');
Assert.AreEqual(C_PATH_TO_DIR, FSettings.SearchPath, 'SearchPath should be set');
Assert.AreEqual(MATCHWORD_USEREGEX,
Expand Down
5 changes: 3 additions & 2 deletions src/Common/RipGrepper.Common.Constants.pas
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface
{ } {$ELSE}
APP_PLATFORM = 'x86';
{ } {$ENDIF}
RG_EXE = 'rg.exe';
APPNAME = 'DRipGrepper';
EXTENSION_NAME = 'DRipExtensions';
EXTENSION_NAME_DLL = EXTENSION_NAME + '.dll';
Expand Down Expand Up @@ -242,8 +243,8 @@ interface

SHOW_CMD_IN_ONE_LINE = 'Show command in one line';
SHOW_CMD_IN_SEPARATE_LINES = 'Show command in lines';
FORMAT_RIPGREP_EXE_NOT_FOUND = 'RipGrep executable (rg.exe) not found.' + CRLF +
{ } 'Check your PATH or in settings file: ' + CRLF +
FORMAT_RIPGREP_EXE_NOT_FOUND = 'RipGrep executable (' + RG_EXE + ') not found.' + CRLF +
{ } 'Please provide a valid path in the settings, or in' + CRLF +
{ } '%s';

MAX_COMMAND_LINE_LENGTH = 32767;
Expand Down
67 changes: 45 additions & 22 deletions src/Settings/RipGrepper.Settings.RipGrepParameterSettings.pas
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ interface
RipGrepper.Common.SimpleTypes;

type
ERipGrepPathInitResult = (rgpiNotSet, rgpiFound, rgpiNotFound);

TRipGrepParameterSettings = class(TPersistableSettings)
const
INI_SECTION = 'RipGrepSettings';

private
FbRgPathInitOk : Boolean;
FRipGrepArguments : TRipGrepArguments;
FRgExeOptions : TOptionStrings;
FSearchPath : string;
FSearchText : string;
FReplaceText : string;
FFileMasks : string;
FGuiSearchTextParams : TGuiSearchTextParams;
FRipGrepPathInitResult : ERipGrepPathInitResult;
FRipGrepPath : string;
function GetIsRgPathInitOk() : Boolean;
function GetRipGrepPath : string;
procedure SetFileMasks(const Value : string);
procedure SetGuiSearchTextParams(const Value : TGuiSearchTextParams);
Expand All @@ -38,6 +40,7 @@ TRipGrepParameterSettings = class(TPersistableSettings)
procedure SetReplaceText(const Value : string);
procedure SetRipGrepPath(const Value : string);
procedure SetSearchText(const Value : string);
function TryGetRipGrepPath(out _rgPath : string) : ERipGrepPathInitResult;

protected
procedure Init; override;
Expand All @@ -56,12 +59,14 @@ TRipGrepParameterSettings = class(TPersistableSettings)
procedure StoreAsDefaultsToDict; override;
property FileMasks : string read FFileMasks write SetFileMasks;
property GuiSearchTextParams : TGuiSearchTextParams read FGuiSearchTextParams write SetGuiSearchTextParams;
property IsRgPathInitOk : Boolean read GetIsRgPathInitOk;
property RgExeOptions : TOptionStrings read FRgExeOptions write SetRgExeOptions;
property SearchPath : string read FSearchPath write SetSearchPath;
property SearchText : string read FSearchText write SetSearchText;
property RipGrepArguments : TRipGrepArguments read FRipGrepArguments write FRipGrepArguments;
property RipGrepPath : string read GetRipGrepPath write SetRipGrepPath;
property ReplaceText : string read FReplaceText write SetReplaceText;
property RipGrepPathInitResult : ERipGrepPathInitResult read FRipGrepPathInitResult write FRipGrepPathInitResult;
end;

implementation
Expand All @@ -87,7 +92,7 @@ constructor TRipGrepParameterSettings.Create(const _Owner : TPersistableSettings
inherited Create(_Owner);
FGuiSearchTextParams := TGuiSearchTextParams.Create(_Owner, INI_SECTION);
AddChildSettings(FGuiSearchTextParams);
FbRgPathInitOk := False;
FRipGrepPathInitResult := rgpiNotSet;
RipGrepPath := '';
FRipGrepArguments := TStringList.Create;
end;
Expand Down Expand Up @@ -130,19 +135,18 @@ function TRipGrepParameterSettings.GetCommandLine : string;
end;
end;

function TRipGrepParameterSettings.GetIsRgPathInitOk() : Boolean;
begin
if rgpiFound <> FRipGrepPathInitResult then begin
FRipGrepPathInitResult := TryGetRipGrepPath(FRipGrepPath);
end;
Result := FRipGrepPathInitResult = rgpiFound;
end;

function TRipGrepParameterSettings.GetRipGrepPath : string;
begin
if (not FbRgPathInitOk) or (not FileExists(FRipGrepPath)) then begin
FbRgPathInitOk := False;
var
iniVal := IniFile.ReadString(IniSectionName, RG_INI_KEY_RGPATH, '');
if iniVal.IsEmpty or not FileExists(iniVal) then begin
FRipGrepPath := TryFindRipGrepExePath();
TDebugUtils.DebugMessage('TRipGrepParameterSettings.GetRipGrepPath - Found:' + FRipGrepPath);
end else begin
FRipGrepPath := iniVal;
end;
FbRgPathInitOk := FileExists(FRipGrepPath);
if (rgpiNotSet = FRipGrepPathInitResult) then begin
FRipGrepPathInitResult := TryGetRipGrepPath(FRipGrepPath);
end;
Result := FRipGrepPath;
end;
Expand Down Expand Up @@ -179,28 +183,27 @@ function TRipGrepParameterSettings.TryFindRipGrepExePath : string;
dbgMsg := TDebugMsgBeginEnd.New('TRipGrepParameterSettings.TryFindRipGrepExePath');
rgPath := FRipGrepPath;
if rgPath.IsEmpty or (not FileExists(rgPath)) then begin
rgExists := TFileUtils.FindExecutable('rg.exe', rgPath);
rgExists := TFileUtils.FindExecutable(RG_EXE, rgPath);
dbgMsg.MsgFmt('rgExists=%s, rgPath=%s', [BoolToStr(rgExists), rgPath]);
if not rgExists then begin
scoopRgPath := TPath.Combine(GetEnvironmentVariable('SCOOP'), 'apps\ripgrep\current\rg.exe');
scoopRgPath := TPath.Combine(GetEnvironmentVariable('SCOOP'), 'apps\ripgrep\current\' + RG_EXE);
if FileExists(scoopRgPath) then begin
rgPath := scoopRgPath;
dbgMsg.MsgFmt('rg.exe found in scoopRgPath=%s', [scoopRgPath]);
dbgMsg.MsgFmt('%s found in scoopRgPath=%s', [RG_EXE, scoopRgPath]);
end else begin
var
sVsDir : string := TFileUtils.GetVsCodeDir;
if not sVsDir.IsEmpty then begin
sVsDir := TFileUtils.ShortToLongPath(sVsDir.Remove(sVsDir.Length - '\bin'.Length));
vscodeRgPath := TFileUtils.FindFileInSubDirs(TPath.Combine(sVsDir, VSCODE_RG_EXE_FIND_PATH), 'rg.exe');
vscodeRgPath := TFileUtils.FindFileInSubDirs(TPath.Combine(sVsDir, VSCODE_RG_EXE_FIND_PATH), RG_EXE);
if not vscodeRgPath.IsEmpty then begin
rgPath := vscodeRgPath;
dbgMsg.MsgFmt('rg.exe found in vscodeRgPath=%s', [vscodeRgPath]);
dbgMsg.MsgFmt('%s found in vscodeRgPath=%s', [RG_EXE, vscodeRgPath]);
end;
end;
end;
if not FileExists(rgPath) then begin
dbgMsg.MsgFmt('rg.exe not found in rgPath=%s', [rgPath]);
TAsyncMsgBox.ShowError(Format(FORMAT_RIPGREP_EXE_NOT_FOUND, [IniFile.FileName]));
dbgMsg.MsgFmt('%s not found in rgPath=%s', [RG_EXE, rgPath]);
rgPath := '';
end;
// raise Exception.Create('RipGrep(rg.exe) not found');
Expand All @@ -211,7 +214,7 @@ function TRipGrepParameterSettings.TryFindRipGrepExePath : string;

procedure TRipGrepParameterSettings.LoadFromDict();
begin
FRipGrepPath := SettingsDict.GetSetting(RG_INI_KEY_RGPATH);
RipGrepPath := SettingsDict.GetSetting(RG_INI_KEY_RGPATH);
FSearchPath := SettingsDict.GetSetting('SearchPath');
FFileMasks := SettingsDict.GetSetting('FileMasks');
FGuiSearchTextParams.LoadFromDict();
Expand Down Expand Up @@ -257,13 +260,15 @@ procedure TRipGrepParameterSettings.SetReplaceText(const Value : string);
if FReplaceText <> Value then begin
TDebugUtils.Msg('SetReplaceText=' + Value);
FReplaceText := Value;
// FIsModified := True;
end;
end;

procedure TRipGrepParameterSettings.SetRipGrepPath(const Value : string);
begin
FRipGrepPath := Value;
if FileExists(FRipGrepPath) then begin
FRipGrepPathInitResult := rgpiFound;
end;
end;

procedure TRipGrepParameterSettings.SetSearchText(const Value : string);
Expand Down Expand Up @@ -291,4 +296,22 @@ procedure TRipGrepParameterSettings.StoreAsDefaultsToDict;
inherited StoreAsDefaultsToDict;
end;

function TRipGrepParameterSettings.TryGetRipGrepPath(out _rgPath : string) : ERipGrepPathInitResult;
begin
var
dbgMsg := TDebugMsgBeginEnd.New('TRipGrepParameterSettings.TryGetRipGrepPath');
Result := rgpiNotFound;
var
iniVal := IniFile.ReadString(IniSectionName, RG_INI_KEY_RGPATH, '');
if iniVal.IsEmpty or not FileExists(iniVal) then begin
_rgPath := TryFindRipGrepExePath();
dbgMsg.Msg('Found:' + FRipGrepPath);
end else begin
_rgPath := iniVal;
end;
if FileExists(_rgPath) then begin
Result := rgpiFound;
end;
end;

end.
13 changes: 9 additions & 4 deletions src/UI/RipGrepper.UI.BottomFrame.pas
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ TRipGrepperBottomFrame = class(TFrame, IFrameEvents)
FStatusBarMessage : string;
FStatusBarStatistic : string;
FStatusBarStatus : string;

public
constructor Create(AOwner : TComponent); override;
procedure AfterHistObjChange;
procedure Init;
procedure AfterSearch;
procedure BeforeSearch;
procedure BeforeSearch(var _bAbort : Boolean);
procedure SetRunningStatus;
procedure SetReadyStatus;
procedure SetStatusBarMessage;
Expand Down Expand Up @@ -82,12 +83,16 @@ procedure TRipGrepperBottomFrame.AfterHistObjChange;

procedure TRipGrepperBottomFrame.AfterSearch;
begin
//SetStatusBarMessage();
// SetStatusBarMessage();
end;

procedure TRipGrepperBottomFrame.BeforeSearch;
procedure TRipGrepperBottomFrame.BeforeSearch(var _bAbort : Boolean);
begin
StatusBarStatistic := 'Searching...';
if _bAbort then begin
// StatusBarStatistic := 'ERROR';
end else begin;
StatusBarStatistic := 'Searching...';
end;
end;

procedure TRipGrepperBottomFrame.FrameResize(Sender : TObject);
Expand Down
2 changes: 1 addition & 1 deletion src/UI/RipGrepper.UI.IFrameEvents.pas
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface
IFrameEvents = interface
['{4E501DFF-A887-4EF3-A376-9A177EE499FE}']
procedure Init;
procedure BeforeSearch;
procedure BeforeSearch(var _bAbort : Boolean);
procedure AfterSearch;
procedure AfterHistObjChange;
procedure UpdateUIStyle(_sNewStyle : string = '');
Expand Down
21 changes: 14 additions & 7 deletions src/UI/RipGrepper.UI.MiddleFrame.pas
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ TRipGrepperMiddleFrame = class(TFrame, IFrameEvents, INewLineEventHandler, ITer
procedure AfterHistObjChange;
procedure AfterSearch;
procedure AlignToolBars;
procedure BeforeSearch;
procedure BeforeSearch(var _bAbort : Boolean);
procedure ClearFilter;
procedure CopyToClipboardFileOfSelected;
procedure CopyToClipboardPathOfSelected;
Expand Down Expand Up @@ -480,8 +480,12 @@ procedure TRipGrepperMiddleFrame.AlignToolBars;
end;
end;

procedure TRipGrepperMiddleFrame.BeforeSearch;
procedure TRipGrepperMiddleFrame.BeforeSearch(var _bAbort : Boolean);
begin
if _bAbort then begin
Exit;
end;

InitSearch();
FAbortSearch := False;
UpdateArgumentsAndSettings;
Expand Down Expand Up @@ -524,8 +528,11 @@ function TRipGrepperMiddleFrame.CreateNewHistObject : IHistoryItemObject;

procedure TRipGrepperMiddleFrame.DoSearch;
begin
ParentFrame.BeforeSearch();
RunRipGrep();
var bAbort := not Settings.RipGrepParameters.IsRgPathInitOk;
ParentFrame.BeforeSearch(bAbort);
if not bAbort then begin
RunRipGrep();
end;
end;

procedure TRipGrepperMiddleFrame.EnableActionIfResultSelected(_act : TAction);
Expand Down Expand Up @@ -911,7 +918,7 @@ procedure TRipGrepperMiddleFrame.RunRipGrep;
workDir : string;
args : TStrings;
argsArrs : TStringsArrayEx;
rgPath: string;
rgPath : string;
begin
rgPath := Settings.RipGrepParameters.RipGrepPath;
if not FileExists(rgPath) then begin
Expand All @@ -929,11 +936,11 @@ procedure TRipGrepperMiddleFrame.RunRipGrep;
args := TStringList.Create;
try
argsArrs := SliceArgs(Settings.RipGrepParameters);
for var i := 0 to argsArrs.MaxIndex do begin
for var i := 0 to argsArrs.MaxIndex do begin
args.Clear;
args.AddStrings(argsArrs[i]);
if i < argsArrs.MaxIndex then begin
// if cmd line is too long, we slice it and run in separate processes...
// if cmd line is too long, we slice it and run in separate processes...
FHistItemObj.RipGrepResult := TProcessUtils.RunProcess(
{ } rgPath,
{ } args,
Expand Down
2 changes: 1 addition & 1 deletion src/UI/RipGrepper.UI.MiddleLeftFrame.pas
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ procedure TMiddleLeftFrame.VstHistoryGetHint(Sender : TBaseVirtualTree; Node : P
LineBreakStyle := TVTTooltipLineBreakStyle.hlbForceMultiLine;
var
lineBreakTab := CRLF + ' ';
HintText := 'rg.exe' + lineBreakTab + string.Join(lineBreakTab, ho.RipGrepArguments.GetValues);
HintText := RG_EXE + lineBreakTab + string.Join(lineBreakTab, ho.RipGrepArguments.GetValues);
end;

procedure TMiddleLeftFrame.VstHistoryGetHintKind(Sender : TBaseVirtualTree; Node : PVirtualNode; Column : TColumnIndex;
Expand Down
15 changes: 9 additions & 6 deletions src/UI/RipGrepper.UI.ParentFrame.pas
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TParentFrame = class(TFrame, IFrameEvents)
destructor Destroy; override;
procedure AfterHistObjChange;
procedure AfterSearch;
procedure BeforeSearch;
procedure BeforeSearch(var _bAbort : Boolean);
procedure Init;
procedure OnClose(Sender : TObject; var Action : TCloseAction);
procedure FrameOnShow(Sender : TObject);
Expand Down Expand Up @@ -82,18 +82,21 @@ procedure TParentFrame.AfterHistObjChange;

procedure TParentFrame.AfterSearch;
begin
var
var
dbgMsg := TDebugMsgBeginEnd.New('TParentFrame.AfterSearch');

TopFrame.AfterSearch();
BottomFrame.AfterSearch();
end;

procedure TParentFrame.BeforeSearch;
procedure TParentFrame.BeforeSearch(var _bAbort : Boolean);
begin
TopFrame.BeforeSearch();
MainFrame.BeforeSearch();
BottomFrame.BeforeSearch();
if _bAbort then begin
Exit;
end;
TopFrame.BeforeSearch(_bAbort);
MainFrame.BeforeSearch(_bAbort);
BottomFrame.BeforeSearch(_bAbort);
end;

procedure TParentFrame.OnClose(Sender : TObject; var Action : TCloseAction);
Expand Down
Loading

0 comments on commit c974524

Please sign in to comment.