From a27b3206fe9f948be8368a2901c794452cb1ba8f Mon Sep 17 00:00:00 2001 From: mattias Date: Mon, 18 Aug 2003 11:52:51 +0000 Subject: [PATCH] implemented enclose selection git-svn-id: trunk@4494 - --- components/codetools/basiccodetools.pas | 2 +- components/codetools/sourcelog.pas | 2 +- ide/encloseselectiondlg.pas | 121 +++++++++++++++++++----- ide/uniteditor.pp | 4 +- 4 files changed, 101 insertions(+), 28 deletions(-) diff --git a/components/codetools/basiccodetools.pas b/components/codetools/basiccodetools.pas index 2a0ed64872..5a776389f9 100644 --- a/components/codetools/basiccodetools.pas +++ b/components/codetools/basiccodetools.pas @@ -1366,7 +1366,7 @@ begin inc(i); if (i<=length(Txt)) and (Txt[i] in [#10,#13]) and (Txt[i-1]<>Txt[i]) then inc(i); - LastLineEndPos:=i; + LastLineEndPos:=i-1; end else inc(i); end; diff --git a/components/codetools/sourcelog.pas b/components/codetools/sourcelog.pas index 1a72de1a35..e735282296 100644 --- a/components/codetools/sourcelog.pas +++ b/components/codetools/sourcelog.pas @@ -177,7 +177,7 @@ begin inc(i); if (i<=TxtLen) and (Txt[i] in [#10,#13]) and (Txt[i-1]<>Txt[i]) then inc(i); - LastLineEndPos:=i; + LastLineEndPos:=i-1; end else inc(i); end; diff --git a/ide/encloseselectiondlg.pas b/ide/encloseselectiondlg.pas index 6656ac4141..f91e788264 100644 --- a/ide/encloseselectiondlg.pas +++ b/ide/encloseselectiondlg.pas @@ -113,39 +113,39 @@ begin +' '+LineBreak +'finally'+LineBreak +' |'+LineBreak - +'end;'; + +'end;'+LineBreak; estTryExcept: Template:='try'+LineBreak +' '+LineBreak +'except'+LineBreak +' |'+LineBreak - +'end;'; + +'end;'+LineBreak; estBeginEnd: Template:='begin'+LineBreak +' |'+LineBreak - +'end;'; + +'end;'+LineBreak; estForBeginEnd: Template:='for | do begin'+LineBreak +' '+LineBreak - +'end;'; + +'end;'+LineBreak; estWhileDoBeginEnd: Template:='while | do begin'+LineBreak +' '+LineBreak - +'end;'; + +'end;'+LineBreak; estRepeatUntil: Template:='repeat'+LineBreak +' '+LineBreak - +'until |;'; + +'until |;'+LineBreak; estPascalComment: Template:='{'+LineBreak +' |'+LineBreak - +'}'; + +'}'+LineBreak; else RaiseException('GetEnclosedSelectionParams'); @@ -157,7 +157,7 @@ procedure EncloseTextSelection(const Template: string; Source: TStrings; Indent: integer; var NewSelection: string; var NewCursor: TPoint); const - TemplateIndent = 2; + TemplateBaseIndent = 2; var TemplateLen: Integer; TemplatePos: Integer; @@ -165,29 +165,54 @@ var NewSelect: TMemoryStream; Y: Integer; X: Integer; - ExtraIndent: Integer; + OldSelectionIndent: Integer; + TemplateIndent: Integer; + CutLastLineBreak: Boolean; + CutPos: Integer; procedure AddBeautified(const s: string); var NewStr: String; LengthOfLastLine: integer; LineEndCnt: Integer; + CurIndent: Integer; + FirstLineIndent: Integer; + EndPos: Integer; begin if s='' then exit; 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,ExtraIndent, + NewStr,CurIndent, [bcfIndentExistingLineBreaks,bcfDoNotIndentFirstLine]); 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); if LineEndCnt=0 then inc(X,LengthOfLastLine) - else begin + else X:=LengthOfLastLine+1; - ExtraIndent:=GetLineIndent(NewStr,length(NewStr)+1); - end; - writeln('AddBeautified B X=',X,' Y=',Y,' ExtraIndent=',ExtraIndent,' NewSTr="',NewSTr,'"'); + if (LineEndCnt>0) or (NewSelect.Position=0) then + TemplateIndent:=GetLineIndent(NewStr,length(NewStr)+1); + //writeln('AddBeautified B X=',X,' Y=',Y,' TemplateIndent=',TemplateIndent,' LengthOfLastLine=',LengthOfLastLine,' NewStr="',NewSTr,'"'); NewSelect.Write(NewStr[1],length(NewStr)); end; @@ -217,24 +242,38 @@ var MinX: Integer; MaxX: Integer; begin - IndentStr:=GetIndentStr(ExtraIndent); + IndentStr:=GetIndentStr(TemplateIndent-OldSelectionIndent); for CurY:=SelectionStart.Y to SelectionEnd.Y do begin CurLine:=Source[CurY-1]; MinX:=1; MaxX:=length(CurLine); if (CurY=SelectionStart.Y) then begin MinX:=SelectionStart.X; + if MinX<=OldSelectionIndent then + MinX:=OldSelectionIndent+1; if MinX>MaxX then MinX:=MaxX; end; if (CurY=SelectionEnd.Y) and (MaxX>SelectionEnd.X) then MaxX:=SelectionEnd.X; + //writeln('InsertSelection CurY=',CurY,' Range=',MinX,'-',MaxX,' Indent=',length(IndentStr),' "',copy(CurLine,MinX,MaxX-MinX+1),'"'); + X:=1; // write indent - if (IndentStr<>'') and (CurY<>SelectionStart.Y) then + if (IndentStr<>'') and (CurY<>SelectionStart.Y) then begin NewSelect.Write(IndentStr[1],length(IndentStr)); + inc(X,length(IndentStr)); + end; // write line - if MaxX>MinX then + if MaxX>MinX then begin NewSelect.Write(CurLine[MinX],MaxX-MinX+1); + inc(X,MaxX-MinX+1); + end; + // write line break and adjust cursor + if CurYSelectionStart.Y) then begin + CutLastLineBreak:=false; + dec(SelectionEnd.Y); + if SelectionEnd.Y'' then begin NewSelect.Position:=0; 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; NewSelect.Free; end; diff --git a/ide/uniteditor.pp b/ide/uniteditor.pp index 18611b677f..7a9e61b481 100644 --- a/ide/uniteditor.pp +++ b/ide/uniteditor.pp @@ -1757,8 +1757,8 @@ begin FEditor.BlockBegin,FEditor.BlockEnd, FEditor.BlockIndent, NewSelection,NewCaretXY); - writeln('TSourceEditor.EncloseSelection A NewCaretXY=',NewCaretXY.X,',',NewCaretXY.Y, - ' "',NewSelection,'"'); + //writeln('TSourceEditor.EncloseSelection A NewCaretXY=',NewCaretXY.X,',',NewCaretXY.Y, + // ' "',NewSelection,'"'); FEditor.SelText:=NewSelection; FEditor.CaretXY:=NewCaretXY; end;