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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
unit RegTestInsight;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,
LazLoggerBase, Forms, Controls,
IDECommands, MenuIntf, IDEWindowIntf, LazIDEIntf, IDEMsgIntf,
IDEOptEditorIntf, IDEOptionsIntf, IDEExternToolIntf,
Projectintf,
CodeTree, CodeCache, CodeToolManager, FindDeclarationTool,
frmTestInsight, TestInsightServer, StrTestCaseOpts,
TestInsightController, fraTestInsightOpts;
Type
{ TLazTestInsightForm }
TLazTestInsightForm = class(TTestInsightForm)
private
Public
procedure ShowMessage(aType: TInsightMessageType; Const Msg : String); override;
Function GetTestProject : String; override;
procedure RunTestProject(aExecutable : string; SendNamesOnly : Boolean); override;
procedure NavigateTo(const aClass, aMethod, aUnit, aLocationFile: String; aLocationLine: Integer); override;
procedure ConfigureServer(aServer: TTestInsightServer); override;
Function AutoFetchTests: Boolean; override;
function ShowRefreshTestproject: Boolean; override;
end;
procedure register;
implementation
const
TestInsightFormName = 'TestInsightForm';
var
TestInsightOptionsFrameID: integer = 1001;
Type
{ TFormFreeNotifier }
TFormFreeNotifier = Class(TComponent)
Public
Type
TFreeNotificationProc = Procedure(aForm : TCustomForm);
Private
FOnNotify : TFreeNotificationProc;
FForm : TCustomForm;
private
procedure SetForm(AValue: TCustomForm);
Protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
function DoProjectChanged(Sender: TObject; {%H-}AProject: TLazProject): TModalResult;
Public
Constructor CustomCreate(aOwner : TComponent; aOnNotify : TFreeNotificationProc);
Property Form : TCustomForm Read FForm Write SetForm;
end;
var
FreeNotifier : TFormFreeNotifier;
TestInsightForm: TTestInsightForm;
Function ShowFileAt(aLocationFile : String; aLocationLine : integer) : Boolean;
var
F : TLazProjectFile;
begin
F:=LazarusIDE.ActiveProject.FindFile(aLocationFile,[]);
Result:=Assigned(F);
if Result then
Result:=(mrOK=LazarusIDE.DoOpenFileAndJumpToPos(F.GetFullFilename,Point(aLocationLine-1,0),0,0,0,0,0,[ofOnlyIfExists]));
end;
Function ShowMethod(aClass,aMethod, aUnit : String) : Boolean;
var
F : TLazProjectFile;
Tool : TCodeTool;
ClassTool : TFindDeclarationTool;
C : TCodeBuffer;
ClassNode,NewNode,Node : TCodeTreeNode;
NewTopLine : Integer;
Params: TFindDeclarationParams;
LProj : TLazProject;
Ctx : TFindContext;
caret : TCodeXYPosition;
begin
Result:=False;
LProj:=LazarusIDE.ActiveProject;
if (aUnit<>'') then
begin
F:=LProj.FindFile(aunit+'.pp',[pfsfOnlyProjectFiles]);
if Not Assigned(F) then
F:=LProj.FindFile(aunit+'.pas',[pfsfOnlyProjectFiles]);
if Not Assigned(F) then
F:=LProj.FindFile(aunit+'.p',[pfsfOnlyProjectFiles]);
// Writeln('Found unit "',aunit,'" : ',Assigned(F));
end;
// Search in unit, if available
Tool:=Nil;
Node:=Nil;
if Assigned(F) then
begin
C:=CodeToolBoss.LoadFile(F.GetFullFilename,False,false);
if CodetoolBoss.Explore(C,Tool,False,false) then
Node:=Tool.FindImplementationNode;
end;
// If not found
if not Assigned(Node) then
begin
C:=CodeToolBoss.LoadFile(LProj.MainFile.GetFullFilename,False,false);
if CodetoolBoss.Explore(C,Tool,False,false) then
Node:=Tool.FindMainBeginEndNode;
end;
// Writeln('Node found for unit "',aunit,'" : ',Assigned(Node));
if Node=nil then
exit;
Params:=TFindDeclarationParams.Create;
Params.ContextNode:=Node;
Params.Flags:=[fdfSearchInParentNodes];
Params.SetIdentifier(Tool,PChar(aClass),nil);
if not Tool.FindIdentifierInContext(Params) then
begin
// Writeln('Class not found for unit "',aunit,'", identifier ',aClass);
exit;
end;
NewNode:=Params.NewNode;
ClassTool:=Params.NewCodeTool;
ClassNode:=NewNode.FirstChild;
if (ClassNode=nil)
or (NewNode.Desc<>ctnTypeDefinition)
or (ClassNode.Desc<>ctnClass) then
begin
// Writeln('Class identifier in unit "',aunit,'", is not a class identifier ',aClass);
Exit;
end;
Ctx:=ClassTool.FindClassMember(ClassNode,aMethod,true);
if not Assigned(Ctx.Node) then
begin
// Writeln('Method ',aMethod,' for class ',aClass,' not found in unit "',aunit);
exit;
end;
Ctx.Tool.CleanPosToCaretAndTopLine(Ctx.Node.StartPos,Caret,NewTopLine);
Result:=(mrOK=LazarusIDE.DoOpenFileAndJumpToPos(Caret.Code.Filename,Point(Caret.X,Caret.Y),NewTopLine,-1,-1,[ofRegularFile]));
end;
Var
TestInsightWindowCreator: TIDEWindowCreator;
ViewTestInsightWindowCommand: TIDECommand;
Controller : TTestInsightController;
procedure ShowTestInsightWindow(Sender: TObject);
var
F : TCustomForm;
begin
if TestInsightForm=nil then
begin
F:=IDEWindowCreators.ShowForm(TestInsightWindowCreator.FormName,true);
FreeNotifier.Form:=F;
end
else
IDEWindowCreators.ShowForm(TestInsightForm,true);
end;
procedure CreateTestInsightWindow(Sender: TObject; aFormName: string;
var AForm: TCustomForm; DoDisableAutoSizing: boolean);
begin
if not SameText(aFormName,TestInsightFormName) then
begin
debugln(['ERROR: TestInsightFormName : there is already a form with this name']);
exit;
end;
IDEWindowCreators.CreateForm(AForm,TLazTestInsightForm,DoDisableAutoSizing,
LazarusIDE.OwningComponent);
AForm.Name:=aFormName;
TestInsightForm:=TTestInsightForm(AForm);
end;
procedure ClearTestInsightForm(aForm: TCustomForm);
begin
if aForm=TestInsightForm then
TestInsightForm:=nil;
end;
procedure register;
var
CmdCatView: TIDECommandCategory;
begin
Controller:=TTestInsightController.Create;
Controller.Options.LoadFromFile(TestInsightConfig);
FreeNotifier:=TFormFreeNotifier.CustomCreate(Nil,@ClearTestInsightForm);
CmdCatView:=IDECommandList.FindCategoryByName(CommandCategoryViewName);
if CmdCatView=nil then
raise Exception.Create('simplewebsrv: command category '+CommandCategoryViewName+' not found');
// Windows - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TestInsightWindowCreator:=IDEWindowCreators.Add(TestInsightFormName,
@CreateTestInsightWindow,nil,'20%','20%','+50%','+20%');
// Windows - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ViewTestInsightWindowCommand:=RegisterIDECommand(CmdCatView, 'ViewTestInsightWindow',
rsTestInsightTitle, CleanIDEShortCut, CleanIDEShortCut, nil, @ ShowTestInsightWindow);
RegisterIDEMenuCommand(itmViewMainWindows, 'ViewTestInsightWindow',
rsTestInsightTitle, nil, nil, ViewTestInsightWindowCommand);
// Options Frame - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
TestInsightOptionsFrameID:=RegisterIDEOptionsEditor(GroupEnvironment,TTestInsightOptionsFrame,TestInsightOptionsFrameID)^.Index;
LazarusIDE.AddHandlerOnProjectOpened(@FreeNotifier.DoProjectChanged);
end;
{ TFormFreeNotifier }
function TFormFreeNotifier.DoProjectChanged(Sender: TObject; AProject: TLazProject): TModalResult;
begin
Result:=mrOK;
if Assigned(FForm) and (FForm is TLazTestInsightForm) then
TLazTestInsightForm(FForm).MaybeStartTestProject;
end;
procedure TFormFreeNotifier.SetForm(AValue: TCustomForm);
begin
if FForm=AValue then Exit;
if Assigned(FForm) then FForm.RemoveFreeNotification(Self);
FForm:=AValue;
if Assigned(FForm) then FForm.FreeNotification(Self);
end;
procedure TFormFreeNotifier.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation=opRemove) and (aComponent=FForm) then
begin
FOnNotify(FForm);
FForm:=Nil;
end;
end;
constructor TFormFreeNotifier.CustomCreate(aOwner: TComponent; aOnNotify: TFreeNotificationProc);
begin
Inherited Create(aOwner);
FForm:=Nil;
FOnNotify:=aOnNotify;
end;
{ TLazTestInsightForm }
procedure TLazTestInsightForm.ShowMessage(aType: TInsightMessageType; const Msg: String);
Const
MLU : Array[TInsightMessageType] of TMessageLineUrgency = (TMessageLineUrgency.mluImportant,TMessageLineUrgency.mluError);
begin
if Assigned(IDEMessagesWindow) then
IDEMessagesWindow.AddCustomMessage(MLU[aType],Msg,'',0,0,rsTestInsightTitle)
end;
function TLazTestInsightForm.GetTestProject: String;
begin
// When the IDE restores windows, the ActiveProject is not yet there, so we need to check.
if Assigned(LazarusIDE)
and Assigned(LazarusIDE.ActiveProject)
and assigned(LazarusIDE.ActiveProject.LazCompilerOptions) then
Result:=LazarusIDE.ActiveProject.LazCompilerOptions.CreateTargetFilename;
end;
procedure TLazTestInsightForm.RunTestProject(aExecutable : string; SendNamesOnly: Boolean);
begin
// Probably the controller should do this.
inherited RunTestProject(aExecutable,SendNamesOnly);
end;
procedure TLazTestInsightForm.ConfigureServer(aServer: TTestInsightServer);
begin
aServer.Port:=Controller.Options.Port;
aServer.BasePath:=Controller.Options.BasePath;
end;
function TLazTestInsightForm.AutoFetchTests: Boolean;
begin
Result:=Controller.Options.AutoFetchTests
end;
procedure TLazTestInsightForm.NavigateTo(const aClass,aMethod, aUnit, aLocationFile: String; aLocationLine: Integer);
var
NavOK : Boolean;
begin
NavOK:=False;
if (aLocationFile<>'') then
NavOK:=ShowFileAt(aLocationFile,aLocationLine);
if not NavOK then
NavOK:=ShowMethod(aClass,aMethod,aUnit);
if not NavOK then
ShowMessage(imtError,Format('Failed to navigate to test %s.%s in unit %s',[aClass,aMethod,aUnit]));
end;
function TLazTestInsightForm.ShowRefreshTestproject: Boolean;
begin
Result:=True;
end;
finalization
FreeAndNil(Controller);
FreeAndNil(FreeNotifier);
end.
|