-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPasfmt.FormatEditor.pas
More file actions
201 lines (171 loc) · 5.76 KB
/
Pasfmt.FormatEditor.pas
File metadata and controls
201 lines (171 loc) · 5.76 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
unit Pasfmt.FormatEditor;
interface
uses
ToolsAPI,
Pasfmt.FormatCore,
Pasfmt.Cursors;
type
TEditBufferFormatter = record
Core: TFormatter;
MaxFileKiBWithUndoHistory: Integer;
procedure Format(Buffer: IOTAEditBuffer);
private
procedure FormatWithCursors(Buffer: IOTAEditBuffer; SourceEditor: IOTAEditorContent; Cursors: TCursors);
end;
implementation
uses
System.SysUtils,
System.Classes,
Winapi.ActiveX,
Vcl.AxCtrls,
Pasfmt.Log,
System.StrUtils,
Vcl.Dialogs,
System.UITypes,
System.IOUtils;
//______________________________________________________________________________________________________________________
function StreamToUTF8(Stream: IStream): UTF8String;
var
OleStream: TOleStream;
begin
OleStream := TOleStream.Create(Stream);
try
SetLength(Result, OleStream.Size);
OleStream.Seek(0, soBeginning);
if OleStream.Read(Result[1], Length(Result)) <> OleStream.Size then
raise Exception.Create('The stream could not be read');
finally
FreeAndNil(OleStream);
end;
end;
//______________________________________________________________________________________________________________________
procedure TrimTrailingNewlines(Data: PAnsiChar; Length: Integer);
var
StrEnd: PAnsiChar;
begin
if Data = nil then begin
Exit;
end;
StrEnd := Data + Length;
while (StrEnd > Data) and ((StrEnd - 1)^ in [#$0A, #$0D]) do
Dec(StrEnd);
StrEnd^ := #0;
end;
//______________________________________________________________________________________________________________________
procedure SetBufferViewMessages(Buffer: IOTAEditBuffer; Msg: string);
var
I: Integer;
begin
for I := 0 to Buffer.EditViewCount - 1 do begin
Buffer.EditViews[I].SetTempMsg(Msg);
end;
end;
//______________________________________________________________________________________________________________________
function ConfirmFormatWhileDebugging: Boolean;
begin
Result :=
MessageDlg('Formatting will disrupt debugging in this file. Continue?', mtWarning, [mbOK, mbCancel], 0, mbCancel)
= mrOK;
end;
//______________________________________________________________________________________________________________________
procedure TEditBufferFormatter.Format(Buffer: IOTAEditBuffer);
var
SourceEditor: IOTAEditorContent;
DebuggerServices: IOTADebuggerServices;
Cursors: TCursors;
Extension: string;
begin
Extension := TPath.GetExtension(Buffer.FileName);
if not MatchText(Extension, ['.pas', '.dpr', '.dpk', '.inc']) then begin
Log.Debug('Format request ignored: unsupported file extension "%s"', [Extension]);
Exit;
end
else if not Supports(Buffer, IOTAEditorContent, SourceEditor) then begin
Log.Debug('Format request ignored: the editor is not formattable', [Buffer.FileName]);
Exit;
end
else if Buffer.IsReadOnly then begin
Log.Debug('Format request ignored: "%s" is read-only', [Buffer.FileName]);
Exit;
end
else if Supports(BorlandIDEServices, IOTADebuggerServices, DebuggerServices)
and Assigned(DebuggerServices.CurrentProcess)
and not ConfirmFormatWhileDebugging then
begin
Log.Debug('Format request ignored: debugger is running and user has cancelled');
Exit;
end;
SetBufferViewMessages(Buffer, 'Formatting...');
Cursors := TCursors.Create(Buffer);
try
FormatWithCursors(Buffer, SourceEditor, Cursors);
finally
FreeAndNil(Cursors);
end;
end;
procedure TEditBufferFormatter.FormatWithCursors(
Buffer: IOTAEditBuffer;
SourceEditor: IOTAEditorContent;
Cursors: TCursors
);
const
CSuccessMsg = 'Formatted ✓';
CErrMsg = 'Format error';
var
EditorContent: UTF8String;
FormatResult: TFormatResult;
Writer: IOTAEditWriter;
FileSizeKiB: Integer;
begin
EditorContent := StreamToUTF8(SourceEditor.Content);
try
FormatResult := Core.Format(EditorContent, Cursors.Serialize, TPath.GetDirectoryName(Buffer.FileName));
except
on E: Exception do begin
Log.Error('Format invocation failed: %s', [E.Message]);
SetBufferViewMessages(Buffer, CErrMsg);
Exit;
end;
end;
if FormatResult.ErrorInfo <> '' then begin
if ContainsText(FormatResult.ErrorInfo, 'error') then begin
Log.Error(FormatResult.ErrorInfo);
end
else begin
Log.Warn(FormatResult.ErrorInfo);
end;
end;
if FormatResult.ExitCode <> 0 then begin
Log.Error('Format of "%s" failed', [Buffer.FileName]);
SetBufferViewMessages(Buffer, CErrMsg);
Exit;
end;
if FormatResult.Output = EditorContent then begin
SetBufferViewMessages(Buffer, CSuccessMsg);
Log.Debug('"%s" is already formatted, skipping buffer update', [Buffer.FileName]);
Exit;
end;
SetBufferViewMessages(Buffer, 'Rendering...');
// the IDE always inserts a line ending after whatever we insert, so we have to trim the trailing line ending
// that pasfmt creates.
TrimTrailingNewlines(PAnsiChar(FormatResult.Output), Length(FormatResult.Output));
FileSizeKiB := Length(EditorContent) div 1024;
if FileSizeKiB > MaxFileKiBWithUndoHistory then begin
Log.Warn(
'Losing undo history for "%s" (file size %d KiB is above threshold %d KiB)',
[Buffer.FileName, FileSizeKiB, MaxFileKiBWithUndoHistory]
);
SourceEditor.Content := TStreamAdapter.Create(TStringStream.Create(FormatResult.Output), soOwned) as IStream;
end
else begin
Writer := Buffer.CreateUndoableWriter;
Writer.DeleteTo(MaxInt);
Writer.Insert(PAnsiChar(FormatResult.Output));
end;
Cursors.Deserialize(FormatResult.Cursors);
Cursors.UpdateBuffer(Buffer);
Log.Debug('Formatted "%s", %d cursors updated', [Buffer.FileName, Length(FormatResult.Cursors)]);
SetBufferViewMessages(Buffer, CSuccessMsg);
end;
//______________________________________________________________________________________________________________________
end.