From 9a8ecaceff6f29950732b8f1792eb429d3569c0f Mon Sep 17 00:00:00 2001 From: mattias Date: Mon, 16 Feb 2015 18:54:21 +0000 Subject: [PATCH] codetools: fixed ChangeLineEndings if #10 at end git-svn-id: trunk@47831 - --- components/codetools/sourcelog.pas | 36 +++++++++++++-------- components/codetools/stdcodetools.pas | 46 +++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 13 deletions(-) diff --git a/components/codetools/sourcelog.pas b/components/codetools/sourcelog.pas index b580d8d95c..ff3ee9f815 100644 --- a/components/codetools/sourcelog.pas +++ b/components/codetools/sourcelog.pas @@ -226,11 +226,9 @@ end; function ChangeLineEndings(const s, NewLineEnding: string): string; var NewLength: Integer; - p, StartPos: Integer; - Src: PChar; - Dest: PChar; + p: Integer; + Src, Dest, StartPos, EndPos: PChar; EndLen: Integer; - EndPos: PChar; begin if s='' then begin Result:=s; @@ -238,16 +236,28 @@ begin end; EndLen:=length(NewLineEnding); NewLength:=length(s); - p:=1; - while pSrc[1]) then begin + inc(Src,2); + inc(NewLength,EndLen-2); + end else begin + inc(Src); + inc(NewLength,EndLen-1); + end; + end; + else inc(p); - if (s[p] in [#10,#13]) and (s[p]<>s[p-1]) then inc(p); - inc(NewLength,EndLen-(p-StartPos)); - end else - inc(p); - end; + end; + until false; SetLength(Result,NewLength); Src:=PChar(s); Dest:=PChar(Result); diff --git a/components/codetools/stdcodetools.pas b/components/codetools/stdcodetools.pas index f66dc8691d..ac3a2eeccb 100644 --- a/components/codetools/stdcodetools.pas +++ b/components/codetools/stdcodetools.pas @@ -222,6 +222,13 @@ type function RemoveIdentifierDefinition(const CursorPos: TCodeXYPosition; SourceChangeCache: TSourceChangeCache): boolean; + function InsertStatements(const CursorPos: TCodeXYPosition; + Statements: string; FrontGap, AfterGap: TGapTyp; + SourceChangeCache: TSourceChangeCache): boolean; + function InsertStatements(CleanPos: integer; + Statements: string; FrontGap, AfterGap: TGapTyp; + SourceChangeCache: TSourceChangeCache): boolean; + // blocks (e.g. begin..end) function FindBlockCounterPart(const CursorPos: TCodeXYPosition; out NewPos: TCodeXYPosition; out NewTopLine: integer): boolean; @@ -4959,6 +4966,45 @@ begin end; end; +function TStandardCodeTool.InsertStatements(const CursorPos: TCodeXYPosition; + Statements: string; FrontGap, AfterGap: TGapTyp; + SourceChangeCache: TSourceChangeCache): boolean; +var + CleanCursorPos: integer; +begin + BeginParsingAndGetCleanPos(lsrEnd,CursorPos,CleanCursorPos); + Result:=InsertStatements(CleanCursorPos,Statements,FrontGap,AfterGap, + SourceChangeCache); + Result:=SourceChangeCache.Apply; +end; + +function TStandardCodeTool.InsertStatements(CleanPos: integer; + Statements: string; FrontGap, AfterGap: TGapTyp; + SourceChangeCache: TSourceChangeCache): boolean; +{ + ToDo: check for "uses" in Statements and extend uses section + e.g. "uses unit1, unit2 in 'filename'; statements + ToDo: check for single statement (e.g. for .. do | dosome;) and add begin/end + + } +var + Node: TCodeTreeNode; +begin + Node:=FindDeepestNodeAtPos(CleanPos,true); + if not (Node.Desc in AllPascalStatements) then begin + MoveCursorToCleanPos(CleanPos); + RaiseException('invalid position for insertion'); + end; + if Node.Desc=ctnBeginBlock then + Node:=BuildSubTreeAndFindDeepestNodeAtPos(Node,CleanPos,true); + + // ToDo: check for CleanPos + + + SourceChangeCache.MainScanner:=Scanner; + Result:=SourceChangeCache.Replace(FrontGap,AfterGap,CleanPos,CleanPos,Statements); +end; + function TStandardCodeTool.FindBlockCounterPart( const CursorPos: TCodeXYPosition; out NewPos: TCodeXYPosition; out NewTopLine: integer): boolean;