forked from integrated-application-development/pasfmt-rad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasfmt.FormatEditor.pas
210 lines (174 loc) · 5.92 KB
/
Pasfmt.FormatEditor.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
unit Pasfmt.FormatEditor;
interface
uses
ToolsAPI,
Pasfmt.FormatCore;
type
TEditBufferFormatter = record
Core: TFormatter;
MaxFileKiBWithUndoHistory: Integer;
procedure Format(Buffer: IOTAEditBuffer);
end;
implementation
uses
System.SysUtils,
System.Classes,
Winapi.ActiveX,
Vcl.AxCtrls,
Pasfmt.Log,
System.StrUtils;
//______________________________________________________________________________________________________________________
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;
//______________________________________________________________________________________________________________________
type
TCursors = record
Offsets: TArray<Integer>;
RowsFromTop: TArray<Integer>;
end;
function GetBufferViewCursors(Buffer: IOTAEditBuffer): TCursors;
var
I: Integer;
EditView: IOTAEditView;
EditPos: TOTAEditPos;
CharPos: TOTACharPos;
begin
SetLength(Result.Offsets, Buffer.EditViewCount);
SetLength(Result.RowsFromTop, Buffer.EditViewCount);
for I := 0 to Buffer.EditViewCount - 1 do begin
EditView := Buffer.EditViews[I];
EditPos := EditView.CursorPos;
EditView.ConvertPos(True, EditPos, CharPos);
Result.Offsets[I] := EditView.CharPosToPos(CharPos);
Result.RowsFromTop[I] := EditView.Position.Row - EditView.TopRow;
end;
end;
//______________________________________________________________________________________________________________________
procedure SetBufferViewCursors(Buffer: IOTAEditBuffer; Cursors: TCursors);
var
I: Integer;
EditView: IOTAEditView;
EditPos: TOTAEditPos;
CharPos: TOTACharPos;
begin
for I := 0 to Buffer.EditViewCount - 1 do begin
if (I >= Length(Cursors.Offsets)) or (Cursors.Offsets[I] < 0) then begin
Continue;
end;
EditView := Buffer.EditViews[I];
CharPos := EditView.PosToCharPos(Cursors.Offsets[I]);
EditView.ConvertPos(False, EditPos, CharPos);
EditView.CursorPos := EditPos;
EditView.Scroll((EditView.Position.Row - EditView.TopRow) - Cursors.RowsFromTop[I], 0);
EditView.Paint;
end;
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;
//______________________________________________________________________________________________________________________
procedure TEditBufferFormatter.Format(Buffer: IOTAEditBuffer);
const
CSuccessMsg = 'Formatted ✓';
CErrMsg = 'Format error';
var
SourceEditor: IOTAEditorContent;
EditorContent: UTF8String;
FormatResult: TFormatResult;
Writer: IOTAEditWriter;
Cursors: TCursors;
FileSizeKiB: Integer;
begin
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;
SetBufferViewMessages(Buffer, 'Formatting...');
EditorContent := StreamToUTF8(SourceEditor.Content);
Cursors := GetBufferViewCursors(Buffer);
try
FormatResult := Core.Format(EditorContent, Cursors.Offsets);
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.Offsets := FormatResult.Cursors;
SetBufferViewCursors(Buffer, Cursors);
Log.Debug('Formatted "%s", %d cursors updated', [Buffer.FileName, Length(FormatResult.Cursors)]);
SetBufferViewMessages(Buffer, CSuccessMsg);
end;
//______________________________________________________________________________________________________________________
end.