This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
/
fpdochints.pas
230 lines (208 loc) · 7.67 KB
/
fpdochints.pas
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
{
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code 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 *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
A hint using the fpdoc data.
}
unit FPDocHints;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LCLProc, Forms, Controls, Graphics, StdCtrls,
CodeToolManager, CodeCache, BasicCodeTools, IdentCompletionTool, CodeTree,
CodeAtom,
IDEHelpIntf, SrcEditorIntf, SrcEditHintFrm, CodeHelp;
type
{ TFPDocHintProvider }
TFPDocHintProvider = class(TCodeHintProvider)
private
FHintValid: boolean;
FWaitingForIdle: boolean;
FBaseURL: string;
FHTMLHint: string;
FHTMLControl: TControl;
FHTMLProvider: TAbstractIDEHTMLProvider;
FTextControl: TLabel;
procedure SetHintValid(const AValue: boolean);
procedure SetWaitingForIdle(const AValue: boolean);
procedure ApplicationIdle(Sender: TObject; var Done: Boolean);
procedure DoUpdateHint;
procedure UpdateHintControl;
public
destructor Destroy; override;
procedure UpdateHint; override;
property WaitingForIdle: boolean read FWaitingForIdle write SetWaitingForIdle;
property HintValid: boolean read FHintValid write SetHintValid;
end;
implementation
{ TFPDocHintProvider }
procedure TFPDocHintProvider.SetWaitingForIdle(const AValue: boolean);
begin
if FWaitingForIdle=AValue then exit;
FWaitingForIdle:=AValue;
if Application<>nil then begin
if FWaitingForIdle then
Application.AddOnIdleHandler(@ApplicationIdle)
else
Application.RemoveOnIdleHandler(@ApplicationIdle);
end;
end;
procedure TFPDocHintProvider.SetHintValid(const AValue: boolean);
begin
if FHintValid=AValue then exit;
FHintValid:=AValue;
end;
procedure TFPDocHintProvider.ApplicationIdle(Sender: TObject; var Done: Boolean);
begin
WaitingForIdle:=false;
DoUpdateHint;
end;
procedure TFPDocHintProvider.DoUpdateHint;
var
Position: LongInt;
Item: TIdentifierListItem;
CacheWasUsed: boolean;
Node: TCodeTreeNode;
HelpResult: TCodeHelpParseResult;
Caret: TCodeXYPosition;
CleanPos: LongInt;
BaseDir: String;
begin
FBaseURL:='';
FHTMLHint:='';
if (Control=nil) or (not Control.IsVisible) then exit;
//debugln(['TFPDocHintProvider.DoUpdateHint ',DbgSName(Control)]);
// find current completion item
if (SourceEditorManagerIntf=nil) or (CodeToolBoss=nil)
or (CodeToolBoss.IdentifierList=nil) then
exit;
Position:=SourceEditorManagerIntf.CompletionBoxPosition;
if (Position<0) or (Position>=CodeToolBoss.IdentifierList.GetFilteredCount) then
exit;
Item:=CodeToolBoss.IdentifierList.FilteredItems[Position];
DebugLn(['TFPDocHintProvider.DoUpdateHint Identifier=',Item.Identifier]);
try
FBaseURL:='';
FHTMLHint:='<HTML><BODY>No help available.</BODY></HTML>';
// find current codetool node
Node:=Item.Node;
if (Node=nil) then begin
if (Item.DefaultDesc=ctnUnit)
and (CodeToolBoss.IdentifierList.StartContextPos.Code<>nil) then begin
BaseDir:=CodeToolBoss.IdentifierList.StartContextPos.Code.Filename;
HelpResult:=CodeHelpBoss.GetHTMLHintForUnit(Item.Identifier,'',BaseDir,
[chhoDeclarationHeader],
FBaseURL,FHTMLHint,CacheWasUsed);
if HelpResult<>chprSuccess then begin
DebugLn(['TFPDocHintProvider.DoUpdateHint FAILED Unit=',Item.Identifier]);
end;
exit;
end;
DebugLn(['TFPDocHintProvider.DoUpdateHint FAILED no node ',NodeDescriptionAsString(Item.DefaultDesc),' Identifier=',Item.Identifier]);
exit;
end;
if (Item.Tool.Scanner=nil) then exit;
//DebugLn(['TFPDocHintProvider.DoUpdateHint Src=',copy(Item.Tool.Src,Node.StartPos,30),' ',Node.DescAsString]);
// search the position of the identifier, not the keyword
CleanPos:=Node.StartPos;
case Node.Desc of
ctnProcedure:
begin
Item.Tool.MoveCursorToProcName(Node,true);
CleanPos:=Item.Tool.CurPos.StartPos;
end;
ctnProperty:
begin
if Item.Tool.MoveCursorToPropName(Node) then
CleanPos:=Item.Tool.CurPos.StartPos;
end;
end;
// get help text
if (not Item.Tool.CleanPosToCaret(CleanPos,Caret)) then begin
DebugLn(['TFPDocHintProvider.DoUpdateHint FAILED CleanPosToCaret Tool=',Item.Tool.MainFilename,' CleanPos=',CleanPos,' SrcLen=',Item.Tool.SrcLen]);
exit;
end;
//DebugLn(['TFPDocHintProvider.DoUpdateHint ',Item.Identifier,' ',Item.Tool.MainFilename,' ',Caret.Code.Filename,' ',Caret.X,',',Caret.Y]);
HelpResult:=CodeHelpBoss.GetHTMLHint(Caret.Code,Caret.X,Caret.Y,
[chhoDeclarationHeader],
FBaseURL,FHTMLHint,CacheWasUsed);
if HelpResult<>chprSuccess then begin
DebugLn(['TFPDocHintProvider.DoUpdateHint FAILED Identifier=',Item.Identifier]);
exit;
end;
finally
UpdateHintControl;
end;
end;
procedure TFPDocHintProvider.UpdateHintControl;
var
IsHTML: Boolean;
ms: TMemoryStream;
begin
IsHTML:=SysUtils.CompareText(copy(FHTMLHint,1,6),'<HTML>')=0;
if IsHTML then begin
if (FHTMLControl=nil) then begin
FHTMLProvider:=nil;
FHTMLControl:=CreateIDEHTMLControl(nil,FHTMLProvider);
FHTMLControl.Parent:=Control;
FHTMLControl.Align:=alClient;
end;
if FTextControl<>nil then
FTextControl.Visible:=false;
FHTMLControl.Visible:=true;
FHTMLProvider.BaseURL:=FBaseURL;
//debugln(['TFPDocHintProvider.UpdateHintControl FHTMLControl=',DbgSName(FHTMLControl),' FHTMLProvider=',DbgSName(FHTMLProvider)]);
ms:=TMemoryStream.Create;
try
if FHTMLHint<>'' then
ms.Write(FHTMLHint[1],length(FHTMLHint));
ms.Position:=0;
FHTMLProvider.ControlIntf.SetHTMLContent(ms,'');
finally
ms.Free;
end;
end else begin
if (FTextControl=nil) then begin
FTextControl:=TLabel.Create(nil);
FTextControl.Parent:=Control;
FTextControl.Align:=alClient;
FTextControl.WordWrap:=true;
end;
if FHTMLControl<>nil then
FHTMLControl.Visible:=false;
FTextControl.Visible:=true;
FTextControl.Caption:=FHTMLHint;
end;
end;
destructor TFPDocHintProvider.Destroy;
begin
// important: free provider before control
FreeAndNil(FHTMLProvider);
FreeAndNil(FHTMLControl);
FreeAndNil(FTextControl);
WaitingForIdle:=false;
inherited Destroy;
end;
procedure TFPDocHintProvider.UpdateHint;
begin
WaitingForIdle:=true;
inherited UpdateHint;
end;
end.