-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathULLMSuggestForm.pas
More file actions
235 lines (210 loc) · 6.29 KB
/
ULLMSuggestForm.pas
File metadata and controls
235 lines (210 loc) · 6.29 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
unit ULLMSuggestForm;
interface
uses
Winapi.Messages,
System.Classes,
System.ImageList,
Vcl.Controls,
Vcl.Forms,
Vcl.ImgList,
Vcl.VirtualImageList,
Vcl.BaseImageCollection,
TB2Item,
TB2Dock,
TB2Toolbar,
SpTBXItem,
SynEdit,
SVGIconImageCollection;
type
TSuggestWindow = class(TForm)
SpTBXDock: TSpTBXDock;
SpTBXToolbar: TSpTBXToolbar;
vilImages: TVirtualImageList;
spiAccept: TSpTBXItem;
spiCancel: TSpTBXItem;
SpTBXRightAlignSpacerItem1: TSpTBXRightAlignSpacerItem;
SpTBXSeparatorItem1: TSpTBXSeparatorItem;
spiAcceptLine: TSpTBXItem;
spiAcceptWord: TSpTBXItem;
icImages: TSVGIconImageCollection;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure spiAcceptClick(Sender: TObject);
procedure spiAcceptLineClick(Sender: TObject);
procedure spiAcceptWordClick(Sender: TObject);
procedure spiCancelClick(Sender: TObject);
procedure WMNCHitTest(var Message: TWMNCHitTest); message WM_NCHITTEST;
protected
FSeSuggest: TSynEdit;
FEditor: TCustomSynEdit;
procedure CreateParams(var Params: TCreateParams); override;
end;
procedure ShowSuggestion(const Suggestion: string; Editor: TCustomSynEdit);
var
SuggestWindow: TSuggestWindow;
implementation
{$R *.dfm}
uses
Winapi.Windows,
System.Types,
Vcl.Graphics,
System.Math,
SynEditTypes,
SynEditKeyCmds,
UConfiguration;
procedure TSuggestWindow.FormCreate(Sender: TObject);
begin
FSeSuggest:= TSynEdit.Create(Self);
with FSeSuggest do begin
Left:= 0;
Top:= 0;
Align:= alClient;
Font.Charset:= ANSI_CHARSET;
Font.Color:= clWindowText;
Font.Height:= -13;
Font.Name:= 'Consolas';
Font.Style:= [];
Font.Quality:= fqClearTypeNatural;
TabOrder:= 0;
UseCodeFolding:= False;
Gutter.Font.Charset:= DEFAULT_CHARSET;
Gutter.Font.Color:= clWindowText;
Gutter.Font.Height:= -11;
Gutter.Font.Name:= 'Consolas';
Gutter.Font.Style:= [];
Gutter.Font.Quality:= fqClearTypeNatural;
Gutter.Visible:= False;
RightEdge:= 0;
Highlighter:= FConfiguration.JavaHighlighter;
end;
end;
procedure TSuggestWindow.WMNCHitTest(var Message: TWMNCHitTest);
// Makes the form resizable
var
Size: Integer;
Posi: TPoint;
begin
Size := GetSystemMetrics(SM_CXSIZEFRAME);
Posi := ScreenToClient(Message.Pos);
if Posi.Y < Size then
begin
if Posi.X < Size then
Message.Result := HTTOPLEFT
else if Posi.X > ClientWidth - Size then
Message.Result := HTTOPRIGHT
else
Message.Result := HTTOP;
end
else if Posi.Y > ClientHeight - Size then
begin
if Posi.X < Size then
Message.Result := HTBOTTOMLEFT
else if Posi.X > ClientWidth - Size then
Message.Result := HTBOTTOMRIGHT
else
Message.Result := HTBOTTOM;
end
else
begin
if Posi.X < Size then
Message.Result := HTLEFT
else if Posi.X > ClientWidth - Size then
Message.Result := HTRIGHT;
end;
if Message.Result = 0 then
inherited;
end;
{ TSuggestWindow }
procedure TSuggestWindow.CreateParams(var Params: TCreateParams);
begin
inherited;
with Params do
begin
{
WS_THICKFRAME causes Windows 10 to display a 6 pixel title bar
Also with VCL Styles and WS_BORDER the window is not resizable
So we use WS_BORDER and make the window sizeable by handling WM_NCHITTEST
}
Style := WS_POPUP or WS_BORDER or WS_CLIPCHILDREN;
ExStyle := WS_EX_TOOLWINDOW;
// Only affects the first time you create the handle
// https://stackoverflow.com/questions/44521877/window-class-style-cs-noclose-does-not-work-after-calling-to-recreatewnd
WindowClass.style := WindowClass.style or CS_DROPSHADOW;
end;
end;
procedure TSuggestWindow.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
SuggestWindow := nil;
end;
procedure TSuggestWindow.spiAcceptClick(Sender: TObject);
begin
FEditor.SelText := FSeSuggest.Text;
Close;
end;
procedure TSuggestWindow.spiAcceptLineClick(Sender: TObject);
begin
var Line := FSeSuggest.Lines[0];
if FSeSuggest.Lines.Count > 1 then
Line := Line + sLineBreak;
FSeSuggest.Lines.Delete(0);
FEditor.SelText := Line;
if FSeSuggest.Lines.Count = 0 then
Close;
end;
procedure TSuggestWindow.spiAcceptWordClick(Sender: TObject);
begin
FSeSuggest.BlockBegin := BufferCoord(0, 0);
FSeSuggest.ExecuteCommand(ecSelWordRight, ' ', nil);
var FirstWord := FSeSuggest.SelText;
FSeSuggest.SelText := '';
var TrimTrailingSpaces := eoTrimTrailingSpaces in FEditor.Options;
FEditor.Options := FEditor.Options - [eoTrimTrailingSpaces];
FEditor.SelText := FirstWord;
if TrimTrailingSpaces then
FEditor.Options := FEditor.Options + [eoTrimTrailingSpaces];
if FSeSuggest.Text = '' then
Close;
end;
procedure TSuggestWindow.spiCancelClick(Sender: TObject);
begin
Close;
end;
procedure ShowSuggestion(const Suggestion: string; Editor: TCustomSynEdit);
const
MaxLines = 10;
BordersSize = 8;
begin
if not Assigned(SuggestWindow) then begin
SuggestWindow := TSuggestWindow.Create(Application.MainForm);
SuggestWindow.FSeSuggest.Parent:= SuggestWindow;
end;
SuggestWindow.FEditor := Editor;
if Editor.SelAvail then
begin
SuggestWindow.spiAcceptWord.Visible := False;
SuggestWindow.spiAcceptWord.Enabled := False;
SuggestWindow.spiAcceptLine.Visible := False;
SuggestWindow.spiAcceptLine.Enabled := False;
end;
var Monitor := Screen.MonitorFromWindow(Editor.Handle);
var BlockBegin := Editor.BlockBegin;
var DisplayBegin := Editor.BufferToDisplayPos(BlockBegin);
var Point := Editor.RowColumnToPixels(DisplayBegin);
var ScreenPoint := Editor.ClientToScreen(Point);
SuggestWindow.FSeSuggest.Text := Suggestion;
var LineCount := Min(MaxLines, SuggestWindow.FSeSuggest.Lines.Count);
SuggestWindow.ScaleForPPI(Editor.CurrentPPI);
// Window size
var Height := LineCount * SuggestWindow.FSeSuggest.LineHeight +
SuggestWindow.SpTBXToolbar.Height + BordersSize;
var Width := SuggestWindow.FSeSuggest.CharWidth * 80 + BordersSize;
// Window position
var Left := Min(ScreenPoint.X, Monitor.Left + Monitor.Width - Width);
var Top := ScreenPoint.Y - Height;
if Top < Monitor.Top then
Top := ScreenPoint.Y + Editor.LineHeight;
SuggestWindow.SetBounds(Left, Top, Width, Height);
SuggestWindow.ShowModal;
end;
end.