mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-28 21:00:43 +02:00
implemented enclose selection
git-svn-id: trunk@4494 -
This commit is contained in:
parent
2e3f11e1ec
commit
a27b3206fe
@ -1366,7 +1366,7 @@ begin
|
|||||||
inc(i);
|
inc(i);
|
||||||
if (i<=length(Txt)) and (Txt[i] in [#10,#13]) and (Txt[i-1]<>Txt[i]) then
|
if (i<=length(Txt)) and (Txt[i] in [#10,#13]) and (Txt[i-1]<>Txt[i]) then
|
||||||
inc(i);
|
inc(i);
|
||||||
LastLineEndPos:=i;
|
LastLineEndPos:=i-1;
|
||||||
end else
|
end else
|
||||||
inc(i);
|
inc(i);
|
||||||
end;
|
end;
|
||||||
|
@ -177,7 +177,7 @@ begin
|
|||||||
inc(i);
|
inc(i);
|
||||||
if (i<=TxtLen) and (Txt[i] in [#10,#13]) and (Txt[i-1]<>Txt[i]) then
|
if (i<=TxtLen) and (Txt[i] in [#10,#13]) and (Txt[i-1]<>Txt[i]) then
|
||||||
inc(i);
|
inc(i);
|
||||||
LastLineEndPos:=i;
|
LastLineEndPos:=i-1;
|
||||||
end else
|
end else
|
||||||
inc(i);
|
inc(i);
|
||||||
end;
|
end;
|
||||||
|
@ -113,39 +113,39 @@ begin
|
|||||||
+' <selection>'+LineBreak
|
+' <selection>'+LineBreak
|
||||||
+'finally'+LineBreak
|
+'finally'+LineBreak
|
||||||
+' |'+LineBreak
|
+' |'+LineBreak
|
||||||
+'end;';
|
+'end;'+LineBreak;
|
||||||
|
|
||||||
estTryExcept:
|
estTryExcept:
|
||||||
Template:='try'+LineBreak
|
Template:='try'+LineBreak
|
||||||
+' <selection>'+LineBreak
|
+' <selection>'+LineBreak
|
||||||
+'except'+LineBreak
|
+'except'+LineBreak
|
||||||
+' |'+LineBreak
|
+' |'+LineBreak
|
||||||
+'end;';
|
+'end;'+LineBreak;
|
||||||
|
|
||||||
estBeginEnd:
|
estBeginEnd:
|
||||||
Template:='begin'+LineBreak
|
Template:='begin'+LineBreak
|
||||||
+' |<selection>'+LineBreak
|
+' |<selection>'+LineBreak
|
||||||
+'end;';
|
+'end;'+LineBreak;
|
||||||
|
|
||||||
estForBeginEnd:
|
estForBeginEnd:
|
||||||
Template:='for | do begin'+LineBreak
|
Template:='for | do begin'+LineBreak
|
||||||
+' <selection>'+LineBreak
|
+' <selection>'+LineBreak
|
||||||
+'end;';
|
+'end;'+LineBreak;
|
||||||
|
|
||||||
estWhileDoBeginEnd:
|
estWhileDoBeginEnd:
|
||||||
Template:='while | do begin'+LineBreak
|
Template:='while | do begin'+LineBreak
|
||||||
+' <selection>'+LineBreak
|
+' <selection>'+LineBreak
|
||||||
+'end;';
|
+'end;'+LineBreak;
|
||||||
|
|
||||||
estRepeatUntil:
|
estRepeatUntil:
|
||||||
Template:='repeat'+LineBreak
|
Template:='repeat'+LineBreak
|
||||||
+' <selection>'+LineBreak
|
+' <selection>'+LineBreak
|
||||||
+'until |;';
|
+'until |;'+LineBreak;
|
||||||
|
|
||||||
estPascalComment:
|
estPascalComment:
|
||||||
Template:='{'+LineBreak
|
Template:='{'+LineBreak
|
||||||
+' |<selection>'+LineBreak
|
+' |<selection>'+LineBreak
|
||||||
+'}';
|
+'}'+LineBreak;
|
||||||
|
|
||||||
else
|
else
|
||||||
RaiseException('GetEnclosedSelectionParams');
|
RaiseException('GetEnclosedSelectionParams');
|
||||||
@ -157,7 +157,7 @@ procedure EncloseTextSelection(const Template: string; Source: TStrings;
|
|||||||
Indent: integer;
|
Indent: integer;
|
||||||
var NewSelection: string; var NewCursor: TPoint);
|
var NewSelection: string; var NewCursor: TPoint);
|
||||||
const
|
const
|
||||||
TemplateIndent = 2;
|
TemplateBaseIndent = 2;
|
||||||
var
|
var
|
||||||
TemplateLen: Integer;
|
TemplateLen: Integer;
|
||||||
TemplatePos: Integer;
|
TemplatePos: Integer;
|
||||||
@ -165,29 +165,54 @@ var
|
|||||||
NewSelect: TMemoryStream;
|
NewSelect: TMemoryStream;
|
||||||
Y: Integer;
|
Y: Integer;
|
||||||
X: Integer;
|
X: Integer;
|
||||||
ExtraIndent: Integer;
|
OldSelectionIndent: Integer;
|
||||||
|
TemplateIndent: Integer;
|
||||||
|
CutLastLineBreak: Boolean;
|
||||||
|
CutPos: Integer;
|
||||||
|
|
||||||
procedure AddBeautified(const s: string);
|
procedure AddBeautified(const s: string);
|
||||||
var
|
var
|
||||||
NewStr: String;
|
NewStr: String;
|
||||||
LengthOfLastLine: integer;
|
LengthOfLastLine: integer;
|
||||||
LineEndCnt: Integer;
|
LineEndCnt: Integer;
|
||||||
|
CurIndent: Integer;
|
||||||
|
FirstLineIndent: Integer;
|
||||||
|
EndPos: Integer;
|
||||||
begin
|
begin
|
||||||
if s='' then exit;
|
if s='' then exit;
|
||||||
NewStr:=s;
|
NewStr:=s;
|
||||||
writeln('AddBeautified A X=',X,' Y=',Y,' ExtraIndent=',ExtraIndent,' NewSTr="',NewSTr,'"');
|
CurIndent:=OldSelectionIndent;
|
||||||
|
if NewSelect.Position=0 then begin
|
||||||
|
FirstLineIndent:=OldSelectionIndent-SelectionStart.X+1;
|
||||||
|
if FirstLineIndent<0 then FirstLineIndent:=0;
|
||||||
|
NewStr:=GetIndentStr(FirstLineIndent)+NewStr;
|
||||||
|
dec(CurIndent,FirstLineIndent);
|
||||||
|
if CurIndent<0 then CurIndent:=0;
|
||||||
|
end;
|
||||||
|
//writeln('AddBeautified A X=',X,' Y=',Y,' CurIndent=',CurIndent,' NewStr="',NewStr,'"');
|
||||||
|
dec(CurIndent,GetLineIndent(NewStr,1));
|
||||||
|
if CurIndent<0 then CurIndent:=0;
|
||||||
NewStr:=CodeToolBoss.SourceChangeCache.BeautifyCodeOptions.BeautifyStatement(
|
NewStr:=CodeToolBoss.SourceChangeCache.BeautifyCodeOptions.BeautifyStatement(
|
||||||
NewStr,ExtraIndent,
|
NewStr,CurIndent,
|
||||||
[bcfIndentExistingLineBreaks,bcfDoNotIndentFirstLine]);
|
[bcfIndentExistingLineBreaks,bcfDoNotIndentFirstLine]);
|
||||||
LineEndCnt:=LineEndCount(NewStr,LengthOfLastLine);
|
LineEndCnt:=LineEndCount(NewStr,LengthOfLastLine);
|
||||||
|
if (TemplatePos>TemplateLen) then begin
|
||||||
|
// cut indent at end of template
|
||||||
|
if LineEndCnt>0 then begin
|
||||||
|
EndPos:=length(NewStr);
|
||||||
|
while (EndPos>=1) and (NewStr[EndPos]=' ') do dec(EndPos);
|
||||||
|
NewStr:=copy(NewStr,1,length(NewStr)-CurIndent);
|
||||||
|
LineEndCnt:=LineEndCount(NewStr,LengthOfLastLine);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
inc(Y,LineEndCnt);
|
inc(Y,LineEndCnt);
|
||||||
if LineEndCnt=0 then
|
if LineEndCnt=0 then
|
||||||
inc(X,LengthOfLastLine)
|
inc(X,LengthOfLastLine)
|
||||||
else begin
|
else
|
||||||
X:=LengthOfLastLine+1;
|
X:=LengthOfLastLine+1;
|
||||||
ExtraIndent:=GetLineIndent(NewStr,length(NewStr)+1);
|
if (LineEndCnt>0) or (NewSelect.Position=0) then
|
||||||
end;
|
TemplateIndent:=GetLineIndent(NewStr,length(NewStr)+1);
|
||||||
writeln('AddBeautified B X=',X,' Y=',Y,' ExtraIndent=',ExtraIndent,' NewSTr="',NewSTr,'"');
|
//writeln('AddBeautified B X=',X,' Y=',Y,' TemplateIndent=',TemplateIndent,' LengthOfLastLine=',LengthOfLastLine,' NewStr="',NewSTr,'"');
|
||||||
NewSelect.Write(NewStr[1],length(NewStr));
|
NewSelect.Write(NewStr[1],length(NewStr));
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -217,24 +242,38 @@ var
|
|||||||
MinX: Integer;
|
MinX: Integer;
|
||||||
MaxX: Integer;
|
MaxX: Integer;
|
||||||
begin
|
begin
|
||||||
IndentStr:=GetIndentStr(ExtraIndent);
|
IndentStr:=GetIndentStr(TemplateIndent-OldSelectionIndent);
|
||||||
for CurY:=SelectionStart.Y to SelectionEnd.Y do begin
|
for CurY:=SelectionStart.Y to SelectionEnd.Y do begin
|
||||||
CurLine:=Source[CurY-1];
|
CurLine:=Source[CurY-1];
|
||||||
MinX:=1;
|
MinX:=1;
|
||||||
MaxX:=length(CurLine);
|
MaxX:=length(CurLine);
|
||||||
if (CurY=SelectionStart.Y) then begin
|
if (CurY=SelectionStart.Y) then begin
|
||||||
MinX:=SelectionStart.X;
|
MinX:=SelectionStart.X;
|
||||||
|
if MinX<=OldSelectionIndent then
|
||||||
|
MinX:=OldSelectionIndent+1;
|
||||||
if MinX>MaxX then
|
if MinX>MaxX then
|
||||||
MinX:=MaxX;
|
MinX:=MaxX;
|
||||||
end;
|
end;
|
||||||
if (CurY=SelectionEnd.Y) and (MaxX>SelectionEnd.X) then
|
if (CurY=SelectionEnd.Y) and (MaxX>SelectionEnd.X) then
|
||||||
MaxX:=SelectionEnd.X;
|
MaxX:=SelectionEnd.X;
|
||||||
|
//writeln('InsertSelection CurY=',CurY,' Range=',MinX,'-',MaxX,' Indent=',length(IndentStr),' "',copy(CurLine,MinX,MaxX-MinX+1),'"');
|
||||||
|
X:=1;
|
||||||
// write indent
|
// write indent
|
||||||
if (IndentStr<>'') and (CurY<>SelectionStart.Y) then
|
if (IndentStr<>'') and (CurY<>SelectionStart.Y) then begin
|
||||||
NewSelect.Write(IndentStr[1],length(IndentStr));
|
NewSelect.Write(IndentStr[1],length(IndentStr));
|
||||||
|
inc(X,length(IndentStr));
|
||||||
|
end;
|
||||||
// write line
|
// write line
|
||||||
if MaxX>MinX then
|
if MaxX>MinX then begin
|
||||||
NewSelect.Write(CurLine[MinX],MaxX-MinX+1);
|
NewSelect.Write(CurLine[MinX],MaxX-MinX+1);
|
||||||
|
inc(X,MaxX-MinX+1);
|
||||||
|
end;
|
||||||
|
// write line break and adjust cursor
|
||||||
|
if CurY<SelectionEnd.Y then begin
|
||||||
|
NewSelect.Write(EndOfLine[1],length(EndOfLine));
|
||||||
|
inc(Y);
|
||||||
|
X:=1;
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
@ -266,17 +305,40 @@ var
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure GetOldSelectionIndent;
|
||||||
|
var
|
||||||
|
CurY: Integer;
|
||||||
|
CurLine: string;
|
||||||
|
CurIndent: Integer;
|
||||||
|
begin
|
||||||
|
OldSelectionIndent:=0;
|
||||||
|
CurY:=SelectionStart.Y;
|
||||||
|
while CurY<Source.Count do begin
|
||||||
|
CurLine:=Source[CurY-1];
|
||||||
|
CurIndent:=GetLineIndent(CurLine,1);
|
||||||
|
if CurIndent<length(CurLine) then begin
|
||||||
|
OldSelectionIndent:=CurIndent;
|
||||||
|
break;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
begin
|
begin
|
||||||
writeln('EncloseTextSelection A ',SelectionStart.X,',',SelectionStart.Y,'-',SelectionEnd.X,',',SelectionEnd.Y,
|
//writeln('EncloseTextSelection A ',SelectionStart.X,',',SelectionStart.Y,'-',SelectionEnd.X,',',SelectionEnd.Y,
|
||||||
' indent=',Indent,' Template="',Template,'"');
|
// ' indent=',Indent,' Template="',Template,'"');
|
||||||
|
CutLastLineBreak:=true;
|
||||||
|
if (SelectionEnd.X=1) and (SelectionEnd.Y>SelectionStart.Y) then begin
|
||||||
|
CutLastLineBreak:=false;
|
||||||
|
dec(SelectionEnd.Y);
|
||||||
|
if SelectionEnd.Y<Source.Count then
|
||||||
|
SelectionEnd.X:=length(Source[SelectionEnd.Y-1])+1;
|
||||||
|
end;
|
||||||
NewSelect:=TMemoryStream.Create;
|
NewSelect:=TMemoryStream.Create;
|
||||||
NewCursor:=SelectionStart;
|
NewCursor:=SelectionStart;
|
||||||
X:=NewCursor.X;
|
X:=NewCursor.X;
|
||||||
Y:=NewCursor.Y;
|
Y:=NewCursor.Y;
|
||||||
ExtraIndent:=0;
|
GetOldSelectionIndent;
|
||||||
if Y<Source.Count then
|
TemplateIndent:=OldSelectionIndent;
|
||||||
ExtraIndent:=GetLineIndent(Source[Y-1],X);
|
|
||||||
writeln('AAA1 ',X,',',Y,' ',ExtraIndent,' "',Source[Y-1],'"');
|
|
||||||
try
|
try
|
||||||
TemplateLen:=length(Template);
|
TemplateLen:=length(Template);
|
||||||
TemplatePos:=1;
|
TemplatePos:=1;
|
||||||
@ -311,6 +373,17 @@ begin
|
|||||||
if NewSelection<>'' then begin
|
if NewSelection<>'' then begin
|
||||||
NewSelect.Position:=0;
|
NewSelect.Position:=0;
|
||||||
NewSelect.Read(NewSelection[1],length(NewSelection));
|
NewSelect.Read(NewSelection[1],length(NewSelection));
|
||||||
|
if CutLastLineBreak then begin
|
||||||
|
CutPos:=length(NewSelection);
|
||||||
|
if NewSelection[CutPos] in [#10,#13] then begin
|
||||||
|
dec(CutPos);
|
||||||
|
if (CutPos>=1) and (NewSelection[CutPos] in [#10,#13])
|
||||||
|
and (NewSelection[CutPos]<>NewSelection[CutPos+1]) then begin
|
||||||
|
dec(CutPos);
|
||||||
|
end;
|
||||||
|
NewSelection:=copy(NewSelection,1,CutPos);
|
||||||
|
end;
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
NewSelect.Free;
|
NewSelect.Free;
|
||||||
end;
|
end;
|
||||||
|
@ -1757,8 +1757,8 @@ begin
|
|||||||
FEditor.BlockBegin,FEditor.BlockEnd,
|
FEditor.BlockBegin,FEditor.BlockEnd,
|
||||||
FEditor.BlockIndent,
|
FEditor.BlockIndent,
|
||||||
NewSelection,NewCaretXY);
|
NewSelection,NewCaretXY);
|
||||||
writeln('TSourceEditor.EncloseSelection A NewCaretXY=',NewCaretXY.X,',',NewCaretXY.Y,
|
//writeln('TSourceEditor.EncloseSelection A NewCaretXY=',NewCaretXY.X,',',NewCaretXY.Y,
|
||||||
' "',NewSelection,'"');
|
// ' "',NewSelection,'"');
|
||||||
FEditor.SelText:=NewSelection;
|
FEditor.SelText:=NewSelection;
|
||||||
FEditor.CaretXY:=NewCaretXY;
|
FEditor.CaretXY:=NewCaretXY;
|
||||||
end;
|
end;
|
||||||
|
Loading…
Reference in New Issue
Block a user