-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainForm.pas
More file actions
689 lines (614 loc) · 18.9 KB
/
MainForm.pas
File metadata and controls
689 lines (614 loc) · 18.9 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
unit MainForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.ComCtrls,
System.JSON, System.Threading;
type
TLogEvent = procedure(const Text: string) of object;
TMessageEvent = procedure(const Message: string) of object;
TPipeServerThread = class(TThread)
private
FPipeName: string;
FOnMessage: TMessageEvent;
FOnLog: TLogEvent;
FPipeHandle: THandle;
FTerminating: Boolean;
procedure ClosePipe;
protected
procedure Execute; override;
public
constructor Create(const APipeName: string; AOnMessage: TMessageEvent; AOnLog: TLogEvent);
destructor Destroy; override;
procedure TerminateAndWait;
end;
TFormMain = class(TForm)
PageControl1: TPageControl;
TabSheetSend: TTabSheet;
TabSheetReceive: TTabSheet;
TabSheetLog: TTabSheet;
TabSheetMockVSCode: TTabSheet;
// Send tab controls
GroupBox1: TGroupBox;
Label1: TLabel;
EditFilePath: TEdit;
Label2: TLabel;
EditLine: TEdit;
Label3: TLabel;
EditColumn: TEdit;
ButtonSendToVSCode: TButton;
ButtonSendToDelphi: TButton;
// Receive tab controls
GroupBox2: TGroupBox;
ButtonStartServer: TButton;
ButtonStopServer: TButton;
LabelServerStatus: TLabel;
MemoReceived: TMemo;
// Mock VS Code tab controls
GroupBoxMockVSCode: TGroupBox;
ButtonStartMockVSCode: TButton;
ButtonStopMockVSCode: TButton;
LabelMockStatus: TLabel;
MemoMockReceived: TMemo;
ButtonTestDelphiPlugin: TButton;
CheckBoxAutoRespond: TCheckBox;
// Log tab
MemoLog: TMemo;
ButtonClearLog: TButton;
// Status bar
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ButtonSendToVSCodeClick(Sender: TObject);
procedure ButtonSendToDelphiClick(Sender: TObject);
procedure ButtonStartServerClick(Sender: TObject);
procedure ButtonStopServerClick(Sender: TObject);
procedure ButtonStartMockVSCodeClick(Sender: TObject);
procedure ButtonStopMockVSCodeClick(Sender: TObject);
procedure ButtonTestDelphiPluginClick(Sender: TObject);
procedure ButtonClearLogClick(Sender: TObject);
private
FServerThread: TPipeServerThread;
FMockVSCodeThread: TPipeServerThread;
procedure SendMessageToPipe(const PipeName, Message: string);
procedure HandleReceivedMessage(const Message: string);
procedure HandleMockVSCodeMessage(const Message: string);
procedure AddLog(const Text: string);
procedure UpdateServerStatus(Running: Boolean);
procedure UpdateMockVSCodeStatus(Running: Boolean);
procedure SimulateDelphiIDEResponse;
public
end;
var
FormMain: TFormMain;
implementation
{$R *.dfm}
const
PIPE_NAME_TO_VSCODE = '\\.\pipe\IDESwitcher_ToVSCode';
PIPE_NAME_FROM_VSCODE = '\\.\pipe\IDESwitcher_ToDelphi';
{ TFormMain }
procedure TFormMain.FormCreate(Sender: TObject);
begin
PageControl1.ActivePageIndex := 0;
EditFilePath.Text := 'C:\Projects\TestFile.pas';
EditLine.Text := '42';
EditColumn.Text := '15';
UpdateServerStatus(False);
UpdateMockVSCodeStatus(False);
StatusBar1.SimpleText := 'Ready to test IDE Switcher communication';
AddLog('Application started');
AddLog('This tester can simulate both Delphi and VS Code sides');
end;
procedure TFormMain.FormDestroy(Sender: TObject);
begin
if Assigned(FServerThread) then
begin
FServerThread.TerminateAndWait;
FServerThread.Free;
end;
if Assigned(FMockVSCodeThread) then
begin
FMockVSCodeThread.TerminateAndWait;
FMockVSCodeThread.Free;
end;
end;
procedure TFormMain.ButtonSendToVSCodeClick(Sender: TObject);
var
JSONMsg: TJSONObject;
MessageStr: string;
begin
// Create JSON message
JSONMsg := TJSONObject.Create;
try
JSONMsg.AddPair('action', 'switch');
JSONMsg.AddPair('filePath', EditFilePath.Text);
JSONMsg.AddPair('line', TJSONNumber.Create(StrToIntDef(EditLine.Text, 1)));
JSONMsg.AddPair('column', TJSONNumber.Create(StrToIntDef(EditColumn.Text, 1)));
JSONMsg.AddPair('source', 'delphi');
JSONMsg.AddPair('timestamp', DateTimeToStr(Now));
MessageStr := JSONMsg.ToString;
AddLog('Sending to VS Code: ' + MessageStr);
// Send to pipe
try
SendMessageToPipe(PIPE_NAME_TO_VSCODE, MessageStr);
AddLog('Message sent successfully');
StatusBar1.SimpleText := 'Message sent to VS Code pipe';
except
on E: Exception do
begin
AddLog('Error sending: ' + E.Message);
StatusBar1.SimpleText := 'Error: ' + E.Message;
end;
end;
finally
JSONMsg.Free;
end;
end;
procedure TFormMain.ButtonStartServerClick(Sender: TObject);
begin
if not Assigned(FServerThread) then
begin
AddLog('Starting pipe server on ' + PIPE_NAME_FROM_VSCODE);
FServerThread := TPipeServerThread.Create(
PIPE_NAME_FROM_VSCODE,
HandleReceivedMessage,
AddLog
);
UpdateServerStatus(True);
AddLog('Server started');
end;
end;
procedure TFormMain.ButtonStopServerClick(Sender: TObject);
begin
if Assigned(FServerThread) then
begin
AddLog('Stopping pipe server');
FServerThread.TerminateAndWait;
FreeAndNil(FServerThread);
UpdateServerStatus(False);
AddLog('Server stopped');
end;
end;
procedure TFormMain.ButtonClearLogClick(Sender: TObject);
begin
MemoLog.Clear;
AddLog('Log cleared');
end;
procedure TFormMain.ButtonSendToDelphiClick(Sender: TObject);
var
JSONMsg: TJSONObject;
MessageStr: string;
begin
// Create JSON message to send to Delphi IDE plugin
JSONMsg := TJSONObject.Create;
try
JSONMsg.AddPair('action', 'switch');
JSONMsg.AddPair('filePath', EditFilePath.Text);
JSONMsg.AddPair('line', TJSONNumber.Create(StrToIntDef(EditLine.Text, 1)));
JSONMsg.AddPair('column', TJSONNumber.Create(StrToIntDef(EditColumn.Text, 1)));
JSONMsg.AddPair('source', 'vscode');
JSONMsg.AddPair('timestamp', DateTimeToStr(Now));
MessageStr := JSONMsg.ToString;
AddLog('Sending to Delphi IDE: ' + MessageStr);
// Send to Delphi IDE plugin pipe
try
SendMessageToPipe(PIPE_NAME_FROM_VSCODE, MessageStr);
AddLog('Message sent successfully to Delphi IDE plugin');
StatusBar1.SimpleText := 'Message sent to Delphi IDE pipe';
except
on E: Exception do
begin
AddLog('Error sending to Delphi: ' + E.Message);
StatusBar1.SimpleText := 'Error: ' + E.Message;
ShowMessage('Failed to send to Delphi IDE plugin: ' + E.Message + #13#10 +
'Make sure the Delphi IDE is running with the plugin installed.');
end;
end;
finally
JSONMsg.Free;
end;
end;
procedure TFormMain.ButtonStartMockVSCodeClick(Sender: TObject);
begin
if not Assigned(FMockVSCodeThread) then
begin
AddLog('Starting Mock VS Code server on ' + PIPE_NAME_TO_VSCODE);
AddLog('This simulates VS Code extension listening for Delphi messages');
FMockVSCodeThread := TPipeServerThread.Create(
PIPE_NAME_TO_VSCODE,
HandleMockVSCodeMessage,
AddLog
);
UpdateMockVSCodeStatus(True);
AddLog('Mock VS Code server started - Press Ctrl+Shift+D in Delphi IDE to test');
end;
end;
procedure TFormMain.ButtonStopMockVSCodeClick(Sender: TObject);
begin
if Assigned(FMockVSCodeThread) then
begin
AddLog('Stopping Mock VS Code server');
FMockVSCodeThread.TerminateAndWait;
FreeAndNil(FMockVSCodeThread);
UpdateMockVSCodeStatus(False);
AddLog('Mock VS Code server stopped');
end;
end;
procedure TFormMain.ButtonTestDelphiPluginClick(Sender: TObject);
begin
// Test complete round-trip communication
AddLog('--- Starting Delphi Plugin Test ---');
if not Assigned(FMockVSCodeThread) then
begin
ShowMessage('Please start the Mock VS Code server first');
Exit;
end;
AddLog('Test: Waiting for Delphi IDE to send a message...');
AddLog('Action: Press Ctrl+Shift+D in Delphi IDE');
AddLog('Expected: Should receive file info from Delphi plugin');
StatusBar1.SimpleText := 'Waiting for Delphi IDE plugin message...';
end;
procedure TFormMain.SendMessageToPipe(const PipeName, Message: string);
var
PipeHandle: THandle;
BytesWritten: DWORD;
MessageBytes: TBytes;
Retries: Integer;
Success: Boolean;
begin
Success := False;
Retries := 0;
while (not Success) and (Retries < 3) do
begin
PipeHandle := CreateFile(
PChar(PipeName),
GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
0,
0
);
if PipeHandle <> INVALID_HANDLE_VALUE then
begin
try
MessageBytes := TEncoding.UTF8.GetBytes(Message);
if WriteFile(PipeHandle, MessageBytes[0], Length(MessageBytes), BytesWritten, nil) then
begin
FlushFileBuffers(PipeHandle);
Success := True;
end
else
raise Exception.Create('WriteFile failed');
finally
CloseHandle(PipeHandle);
end;
end
else
begin
Inc(Retries);
if Retries < 3 then
begin
AddLog('Pipe not available, retrying...');
Sleep(500);
end
else
raise Exception.Create('Could not connect to pipe (is VS Code extension running?)');
end;
end;
end;
procedure TFormMain.HandleReceivedMessage(const Message: string);
var
JSONValue: TJSONValue;
JSONObject: TJSONObject;
FilePath: string;
Line, Column: Integer;
begin
TThread.Synchronize(nil,
procedure
begin
MemoReceived.Lines.Add('Received at ' + TimeToStr(Now) + ':');
MemoReceived.Lines.Add(Message);
MemoReceived.Lines.Add('---');
// Parse JSON
try
JSONValue := TJSONObject.ParseJSONValue(Message);
if Assigned(JSONValue) and (JSONValue is TJSONObject) then
begin
JSONObject := TJSONObject(JSONValue);
try
if JSONObject.GetValue<string>('action') = 'switch' then
begin
FilePath := JSONObject.GetValue<string>('filePath', '');
Line := JSONObject.GetValue<Integer>('line', 1);
Column := JSONObject.GetValue<Integer>('column', 1);
StatusBar1.SimpleText := Format('Received switch request: %s [%d:%d]',
[ExtractFileName(FilePath), Line, Column]);
AddLog(Format('Switch request from VS Code: %s at %d:%d',
[FilePath, Line, Column]));
end;
finally
JSONObject.Free;
end;
end;
except
on E: Exception do
AddLog('Error parsing message: ' + E.Message);
end;
end);
end;
procedure TFormMain.AddLog(const Text: string);
begin
TThread.Queue(nil,
procedure
begin
MemoLog.Lines.Add('[' + TimeToStr(Now) + '] ' + Text);
// Auto-scroll to bottom
MemoLog.Perform(WM_VSCROLL, SB_BOTTOM, 0);
end);
end;
procedure TFormMain.UpdateServerStatus(Running: Boolean);
begin
if Running then
begin
LabelServerStatus.Caption := 'Server Status: RUNNING';
LabelServerStatus.Font.Color := clGreen;
ButtonStartServer.Enabled := False;
ButtonStopServer.Enabled := True;
end
else
begin
LabelServerStatus.Caption := 'Server Status: STOPPED';
LabelServerStatus.Font.Color := clRed;
ButtonStartServer.Enabled := True;
ButtonStopServer.Enabled := False;
end;
end;
procedure TFormMain.UpdateMockVSCodeStatus(Running: Boolean);
begin
if Running then
begin
LabelMockStatus.Caption := 'Mock VS Code: RUNNING (listening for Delphi)';
LabelMockStatus.Font.Color := clGreen;
ButtonStartMockVSCode.Enabled := False;
ButtonStopMockVSCode.Enabled := True;
ButtonTestDelphiPlugin.Enabled := True;
end
else
begin
LabelMockStatus.Caption := 'Mock VS Code: STOPPED';
LabelMockStatus.Font.Color := clRed;
ButtonStartMockVSCode.Enabled := True;
ButtonStopMockVSCode.Enabled := False;
ButtonTestDelphiPlugin.Enabled := False;
end;
end;
procedure TFormMain.HandleMockVSCodeMessage(const Message: string);
var
JSONValue: TJSONValue;
JSONObject: TJSONObject;
FilePath: string;
Line, Column: Integer;
begin
TThread.Synchronize(nil,
procedure
begin
// Display received message in Mock VS Code tab
MemoMockReceived.Lines.Add('=== Received from Delphi IDE at ' + TimeToStr(Now) + ' ===');
MemoMockReceived.Lines.Add(Message);
MemoMockReceived.Lines.Add('');
// Parse and analyze the message
try
JSONValue := TJSONObject.ParseJSONValue(Message);
if Assigned(JSONValue) and (JSONValue is TJSONObject) then
begin
JSONObject := TJSONObject(JSONValue);
try
if JSONObject.GetValue<string>('action') = 'switch' then
begin
FilePath := JSONObject.GetValue<string>('filePath', '');
Line := JSONObject.GetValue<Integer>('line', 1);
Column := JSONObject.GetValue<Integer>('column', 1);
MemoMockReceived.Lines.Add('Parsed Information:');
MemoMockReceived.Lines.Add(' File: ' + FilePath);
MemoMockReceived.Lines.Add(' Position: Line ' + IntToStr(Line) + ', Column ' + IntToStr(Column));
MemoMockReceived.Lines.Add(' Source: ' + JSONObject.GetValue<string>('source', ''));
MemoMockReceived.Lines.Add('---');
StatusBar1.SimpleText := Format('Received from Delphi: %s [%d:%d]',
[ExtractFileName(FilePath), Line, Column]);
AddLog(Format('Mock VS Code received: %s at %d:%d',
[FilePath, Line, Column]));
// Auto-respond if checkbox is checked
if CheckBoxAutoRespond.Checked then
begin
AddLog('Auto-responding to Delphi IDE...');
SimulateDelphiIDEResponse;
end;
end;
finally
JSONObject.Free;
end;
end;
except
on E: Exception do
begin
MemoMockReceived.Lines.Add('ERROR parsing message: ' + E.Message);
AddLog('Error parsing message from Delphi: ' + E.Message);
end;
end;
end);
end;
procedure TFormMain.SimulateDelphiIDEResponse;
var
JSONMsg: TJSONObject;
MessageStr: string;
begin
// Simulate VS Code sending a response back to Delphi
JSONMsg := TJSONObject.Create;
try
JSONMsg.AddPair('action', 'switch');
JSONMsg.AddPair('filePath', 'C:\MockResponse\TestFile.pas');
JSONMsg.AddPair('line', TJSONNumber.Create(100));
JSONMsg.AddPair('column', TJSONNumber.Create(25));
JSONMsg.AddPair('source', 'vscode');
JSONMsg.AddPair('timestamp', DateTimeToStr(Now));
MessageStr := JSONMsg.ToString;
AddLog('Auto-response: Sending back to Delphi IDE');
try
SendMessageToPipe(PIPE_NAME_FROM_VSCODE, MessageStr);
AddLog('Auto-response sent successfully');
except
on E: Exception do
AddLog('Auto-response failed: ' + E.Message);
end;
finally
JSONMsg.Free;
end;
end;
{ TPipeServerThread }
constructor TPipeServerThread.Create(const APipeName: string;
AOnMessage: TMessageEvent; AOnLog: TLogEvent);
begin
inherited Create(False);
FPipeName := APipeName;
FOnMessage := AOnMessage;
FOnLog := AOnLog;
FPipeHandle := INVALID_HANDLE_VALUE;
FTerminating := False;
FreeOnTerminate := False;
end;
destructor TPipeServerThread.Destroy;
begin
ClosePipe;
inherited;
end;
procedure TPipeServerThread.ClosePipe;
begin
if FPipeHandle <> INVALID_HANDLE_VALUE then
begin
DisconnectNamedPipe(FPipeHandle);
CloseHandle(FPipeHandle);
FPipeHandle := INVALID_HANDLE_VALUE;
end;
end;
procedure TPipeServerThread.TerminateAndWait;
var
DummyHandle: THandle;
BytesWritten: DWORD;
DummyData: AnsiString;
begin
FTerminating := True;
Terminate;
// Connect to our own pipe to unblock ConnectNamedPipe
if FPipeHandle <> INVALID_HANDLE_VALUE then
begin
DummyHandle := CreateFile(
PChar(FPipeName),
GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
0,
0
);
if DummyHandle <> INVALID_HANDLE_VALUE then
begin
DummyData := 'TERMINATE';
WriteFile(DummyHandle, DummyData[1], Length(DummyData), BytesWritten, nil);
CloseHandle(DummyHandle);
end;
end;
// Wait for thread to finish
WaitFor;
end;
procedure TPipeServerThread.Execute;
var
Buffer: array[0..4095] of Byte;
BytesRead: DWORD;
Message: string;
Connected: Boolean;
Overlapped: TOverlapped;
Event: THandle;
WaitResult: DWORD;
begin
while not Terminated and not FTerminating do
begin
FPipeHandle := CreateNamedPipe(
PChar(FPipeName),
PIPE_ACCESS_DUPLEX or FILE_FLAG_OVERLAPPED,
PIPE_TYPE_MESSAGE or PIPE_READMODE_MESSAGE or PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
4096,
4096,
0,
nil
);
if FPipeHandle <> INVALID_HANDLE_VALUE then
begin
try
if Assigned(FOnLog) then
FOnLog('Waiting for connection on ' + FPipeName);
// Create event for overlapped I/O
Event := CreateEvent(nil, True, False, nil);
try
FillChar(Overlapped, SizeOf(Overlapped), 0);
Overlapped.hEvent := Event;
// Start async connection
Connected := ConnectNamedPipe(FPipeHandle, @Overlapped);
if not Connected then
begin
case GetLastError of
ERROR_IO_PENDING:
begin
// Wait for connection with timeout
repeat
WaitResult := WaitForSingleObject(Event, 100);
if Terminated or FTerminating then
Break;
until WaitResult <> WAIT_TIMEOUT;
if WaitResult = WAIT_OBJECT_0 then
Connected := True;
end;
ERROR_PIPE_CONNECTED:
Connected := True;
end;
end;
if Connected and not (Terminated or FTerminating) then
begin
if Assigned(FOnLog) then
FOnLog('Client connected');
if ReadFile(FPipeHandle, Buffer, SizeOf(Buffer) - 1, BytesRead, nil) then
begin
SetString(Message, PAnsiChar(@Buffer[0]), BytesRead);
// Check if this is a termination message
if Message = 'TERMINATE' then
begin
if Assigned(FOnLog) then
FOnLog('Received termination signal');
Break;
end;
if Assigned(FOnLog) then
FOnLog('Received ' + IntToStr(BytesRead) + ' bytes');
if Assigned(FOnMessage) then
FOnMessage(Message);
end;
end;
finally
CloseHandle(Event);
end;
finally
ClosePipe;
end;
end
else
begin
if Assigned(FOnLog) then
FOnLog('Failed to create pipe: ' + SysErrorMessage(GetLastError));
end;
if not (Terminated or FTerminating) then
Sleep(100);
end;
if Assigned(FOnLog) then
FOnLog('Pipe server thread ending');
end;
end.