-
Notifications
You must be signed in to change notification settings - Fork 1
/
SimpleRefactor.pas
115 lines (104 loc) · 2.7 KB
/
SimpleRefactor.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
unit SimpleRefactor;
interface
uses
Classes, Types, SysUtils, SynEdit;
type
TSimpleRefactor = class
private
FEdit: TSynEdit;
FLines: TStrings;
public
constructor Create(AEdit: TSynEdit); overload;
constructor Create(ALines: TStrings); overload;
procedure RenameHeader(AName: string);
procedure CompleteBlocks();
end;
implementation
uses
StrUtils, Lexer, Token;
{ TSimpleRefactor }
procedure TSimpleRefactor.CompleteBlocks;
var
LLexer: TLexer;
LToken, LOpenToken: TToken;
LOpen, LClose, LIdent: Integer;
LText: string;
begin
LOpen := 0;
LClose := 0;
LOpenToken := nil;
LLexer := TLexer.Create();
try
LLexer.LoadFromString(FEdit.Text);
while not LLexer.EOF do
begin
LToken := LLexer.GetToken();
if LToken.IsType(ttReserved) then
begin
if AnsiIndexText(LToken.Content, ['unit', 'asm', 'begin']) > -1 then
begin
Inc(LOpen);
if LToken.FoundInLine < FEdit.CaretY then
begin
LOpenToken := LToken;
end;
end
else
begin
if SameText(LToken.Content, 'end') then
begin
Inc(LClose);
end;
end;
end;
end;
if (LOpen > LClose) and (FEdit.CaretY > 0) and Assigned(LOpenToken) then
begin
FEdit.LInes.Strings[FEdit.CaretY-1] := FEdit.Lines.Strings[FEdit.CaretY-1] + #9; //jump into nextline with increased ident
FEdit.CaretX := FEdit.CaretX + 1;
LText := FEdit.Lines.Strings[LOpenToken.FoundInLine];
LIdent := (Length(LText) - Length(TrimLeft(LText))) - 1;
FEdit.Lines.Insert(FEdit.CaretY, StringOfChar(' ', LIdent) + 'end;');
end;
finally
LLexer.Free;
end;
end;
constructor TSimpleRefactor.Create(AEdit: TSynEdit);
begin
FEdit := AEdit;
FLines := FEdit.Lines;
end;
constructor TSimpleRefactor.Create(ALines: TStrings);
begin
FLines := ALines;
end;
procedure TSimpleRefactor.RenameHeader(AName: string);
var
i, LPos: Integer;
LEnd: string;
begin
for i := 0 to FLines.Count - 1 do
begin
if StartsText('unit ', Trim(FLines.Strings[i]))
or StartsText('program ', Trim(FLines.Strings[i]))
then
begin
LPos := Pos(';', FLines.Strings[i]);
if LPos > 0 then
begin
LEnd := copy(FLines.Strings[i], LPos + 1, Length(FLines.Strings[i]));
end;
if StartsText('unit ', Trim(FLines.Strings[i])) then
begin
FLines.Strings[i] := 'unit ' + AName + ';' + LEnd;
end
else
begin
FLines.Strings[i] := 'program ' + AName + ';' + LEnd;
end;
Break;
end;
end;
end;
end.