@@ -20,16 +20,31 @@ TDTSQueryForm = class(TForm)
20
20
cbPatternIdx: TComboBox;
21
21
sgPredicateSteps: TStringGrid;
22
22
Splitter2: TSplitter;
23
+ pnlMatches: TPanel;
24
+ pnlMatchesTop: TPanel;
25
+ sgMatchCaptures: TStringGrid;
26
+ btnMatchStart: TButton;
27
+ btnMatchNext: TButton;
28
+ lblMatch: TLabel;
23
29
procedure btnExecuteClick (Sender: TObject);
24
30
procedure FormDestroy (Sender: TObject);
25
31
procedure FormClose (Sender: TObject; var Action: TCloseAction);
26
32
procedure cbPatternIdxClick (Sender: TObject);
33
+ procedure btnMatchStartClick (Sender: TObject);
34
+ procedure btnMatchNextClick (Sender: TObject);
35
+ procedure sgMatchCapturesSelectCell (Sender: TObject; ACol, ARow: Integer;
36
+ var CanSelect: Boolean);
27
37
private
28
38
FTree: TTSTree;
29
39
FQuery: TTSQuery;
30
40
FQueryCursor: TTSQueryCursor;
41
+ FCurrentMatch: TTSQueryMatch;
42
+ procedure ClearQuery ;
43
+ procedure ClearMatches ;
44
+ procedure ClearPredicates ;
31
45
public
32
- { Public declarations }
46
+ procedure TreeDeleted ;
47
+ procedure NewTreeGenerated (ATree: TTSTree);
33
48
end ;
34
49
35
50
var
@@ -39,6 +54,9 @@ procedure ShowQueryForm(ATree: TTSTree);
39
54
40
55
implementation
41
56
57
+ uses
58
+ Math, frmDTSMain;
59
+
42
60
{ $R *.dfm}
43
61
44
62
procedure ShowQueryForm (ATree: TTSTree);
@@ -47,7 +65,7 @@ procedure ShowQueryForm(ATree: TTSTree);
47
65
begin
48
66
Application.Createform(TDTSQueryForm, DTSQueryForm);
49
67
end ;
50
- DTSQueryForm.FTree:= ATree.Clone ;
68
+ DTSQueryForm.FTree:= ATree;
51
69
DTSQueryForm.cbPatternIdxClick(nil );
52
70
DTSQueryForm.Show;
53
71
DTSQueryForm.BringToFront;
@@ -64,8 +82,8 @@ procedure TDTSQueryForm.btnExecuteClick(Sender: TObject);
64
82
errorType: TTSQueryError;
65
83
i: Integer;
66
84
begin
67
- cbPatternIdx.Items.Clear ;
68
- FreeAndNil(FQuery);
85
+ ClearQuery ;
86
+
69
87
FQuery:= TTSQuery.Create(FTree.Language, memQuery.Lines.Text, errorOffset, errorType);
70
88
if errorType <> TTSQueryError.TSQueryErrorNone then
71
89
begin
@@ -80,12 +98,50 @@ procedure TDTSQueryForm.btnExecuteClick(Sender: TObject);
80
98
[FQuery.PatternCount, FQuery.CaptureCount, FQuery.StringCount]);
81
99
for i:= 0 to FQuery.PatternCount - 1 do
82
100
cbPatternIdx.Items.Add(IntToStr(i));
101
+ btnMatchStart.Enabled:= True;
83
102
end ;
84
103
if cbPatternIdx.Items.Count > 0 then
85
104
cbPatternIdx.ItemIndex:= 0 ;
86
105
cbPatternIdxClick(nil );
87
106
end ;
88
107
108
+ procedure TDTSQueryForm.btnMatchNextClick (Sender: TObject);
109
+ var
110
+ i: Integer;
111
+ captures: TTSQueryCaptureArray;
112
+ begin
113
+ if not FQueryCursor.NextMatch(FCurrentMatch) then
114
+ begin
115
+ ClearMatches;
116
+ lblMatch.Caption:= ' No more matches' ;
117
+ Exit;
118
+ end ;
119
+ lblMatch.Caption:= Format(' Match id = %d, pattern idx = %d' , [FCurrentMatch.id, FCurrentMatch.pattern_index]);
120
+
121
+ captures:= FCurrentMatch.CapturesArray;
122
+ sgMatchCaptures.RowCount:= Length(captures) + 1 ;
123
+ sgMatchCaptures.FixedRows:= 1 ;
124
+ for i:= 0 to FCurrentMatch.capture_count - 1 do
125
+ begin
126
+ sgMatchCaptures.Cells[0 , i + 1 ]:= IntToStr(captures[i].index);
127
+ sgMatchCaptures.Cells[1 , i + 1 ]:= captures[i].node.NodeType;
128
+ end ;
129
+ if InRange(sgMatchCaptures.Selection.Top, 1 , Length(captures)) then
130
+ DTSMainForm.SelectedTSNode:= captures[sgMatchCaptures.Selection.Top - 1 ].node;
131
+ end ;
132
+
133
+ procedure TDTSQueryForm.btnMatchStartClick (Sender: TObject);
134
+ begin
135
+ if FQueryCursor = nil then
136
+ FQueryCursor:= TTSQueryCursor.Create;
137
+ FQueryCursor.Execute(FQuery, FTree.RootNode);
138
+ ClearMatches;
139
+ sgMatchCaptures.Cells[0 , 0 ]:= ' Capture index' ;
140
+ sgMatchCaptures.Cells[1 , 0 ]:= ' Node' ;
141
+ btnMatchNext.Enabled:= True;
142
+ btnMatchNextClick(nil );
143
+ end ;
144
+
89
145
procedure TDTSQueryForm.cbPatternIdxClick (Sender: TObject);
90
146
const
91
147
stepTypeStrings: array [TTSQueryPredicateStepType] of string = (
@@ -131,6 +187,28 @@ procedure TDTSQueryForm.cbPatternIdxClick(Sender: TObject);
131
187
end ;
132
188
end ;
133
189
190
+ procedure TDTSQueryForm.ClearMatches ;
191
+ begin
192
+ sgMatchCaptures.RowCount:= 1 ;
193
+ lblMatch.Caption:= ' ' ;
194
+ btnMatchNext.Enabled:= False;
195
+ end ;
196
+
197
+ procedure TDTSQueryForm.ClearPredicates ;
198
+ begin
199
+ cbPatternIdx.Items.Clear;
200
+ sgPredicateSteps.RowCount:= 1 ;
201
+ end ;
202
+
203
+ procedure TDTSQueryForm.ClearQuery ;
204
+ begin
205
+ FreeAndNil(FQuery);
206
+ btnMatchStart.Enabled:= False;
207
+ lblQueryState.Caption:= ' ' ;
208
+ ClearPredicates;
209
+ ClearMatches;
210
+ end ;
211
+
134
212
procedure TDTSQueryForm.FormClose (Sender: TObject; var Action: TCloseAction);
135
213
begin
136
214
Action:= caFree;
@@ -140,9 +218,32 @@ procedure TDTSQueryForm.FormDestroy(Sender: TObject);
140
218
begin
141
219
FreeAndNil(FQueryCursor);
142
220
FreeAndNil(FQuery);
143
- FreeAndNil(FTree);
221
+ // FTree is no longer a clone/copy but identical to main form, otherwise
222
+ // finding the node in the main forms tree would not work
223
+ // (nodes belowing to different trees are not considered equal)
224
+ FTree:= nil ;
144
225
if Self = DTSQueryForm then
145
226
DTSQueryForm:= nil ;
146
227
end ;
147
228
229
+ procedure TDTSQueryForm.NewTreeGenerated (ATree: TTSTree);
230
+ begin
231
+ ClearQuery;
232
+ FTree:= ATree;
233
+ end ;
234
+
235
+ procedure TDTSQueryForm.sgMatchCapturesSelectCell (Sender: TObject; ACol,
236
+ ARow: Integer; var CanSelect: Boolean);
237
+ begin
238
+ if not InRange(ARow, 1 , FCurrentMatch.capture_count) then
239
+ Exit;
240
+
241
+ DTSMainForm.SelectedTSNode:= FCurrentMatch.captures[ARow - 1 ].node;
242
+ end ;
243
+
244
+ procedure TDTSQueryForm.TreeDeleted ;
245
+ begin
246
+ ClearQuery;
247
+ end ;
248
+
148
249
end .
0 commit comments