IDE: Format a comment shown in code tooltip better. Issue #28430, patch from Yann Mérignac.

git-svn-id: trunk@49583 -
This commit is contained in:
juha 2015-08-01 08:48:43 +00:00
parent 411ecaa94b
commit 22ea26d2cc
3 changed files with 84 additions and 9 deletions

View File

@ -83,7 +83,8 @@ span.keyword {
/* comments in source fragments */
span.comment {
color: darkcyan;
font-style: italic
font-style: italic;
font-family: monospace;
}
/* directives in source fragments */

View File

@ -2693,6 +2693,43 @@ var
NestedComments: Boolean;
CommentStr, LastComment: String;
function ShiftLeft(const Comment: String) : String;
var
Lines : TStringList;
S : String;
I, J, LeftMost : Integer;
begin
try
Lines := nil;
Lines := TStringList.Create;
Lines.Text := Comment;
LeftMost := Length(Comment);
for I := 0 to Lines.Count - 1 do
begin
if LeftMost <= 1 then
Break;
S := Lines[I];
J := 1;
while (J <= Length(S)) and (J < LeftMost) and (S[J] = ' ') do
Inc(J);
if J < LeftMost then
LeftMost := J;
end;
if LeftMost > 1 then
for I := 0 to Lines.Count - 1 do
Lines[I] := Copy(Lines[I], LeftMost, Length(Lines[I]) - LeftMost + 1);
Result := Lines.Text;
finally
FreeAndNil(Lines);
end;
end;
procedure AddComment;
begin
if (CodeXYPos=nil) or (LastCodeXYPos=nil)
@ -2700,7 +2737,7 @@ var
or (CodeXYPos^.Y-LastCodeXYPos^.Y>10) then begin
// the last comment is at a different position => add a source link
if LastComment<>'' then
Result:=Result+'<span class="comment">'+TextToHTML(LastComment)
Result:=Result+'<span class="comment">'+TextToHTML(ShiftLeft(LastComment))
+' ('+SourcePosToFPDocHint(LastCodeXYPos^,'Source')+')'
+'</span><br>'+LineEnding;
LastComment:=CommentStr;
@ -2713,6 +2750,40 @@ var
LastCodeXYPos:=CodeXYPos;
end;
function ExtractComment(const Source: String;
CommentStart: Integer) : String;
var
CommentEnd, XPos: Integer;
begin
XPos := CodeXYPos^.X;
CommentEnd := FindCommentEnd(Source, CommentStart, NestedComments);
case Source[CommentStart] of
'/':
begin
CommentStart := CommentStart + 2;
XPos := 0;
end;
'(':
begin
CommentStart := CommentStart + 2;
CommentEnd := CommentEnd - 2;
XPos := XPos + 1;
end;
'{':
begin
CommentStart := CommentStart + 1;
CommentEnd := CommentEnd - 1;
end;
end;
Result:=Copy(Source, CommentStart, CommentEnd - CommentStart);
Result := TrimRight(Result);
if XPos > 0 then
Result := StringOfChar(' ', XPos) + Result;
end;
begin
Result:='';
if (Tool=nil) or (Node=nil) then exit;
@ -2731,14 +2802,12 @@ begin
if (CommentStart<1) or (CommentStart>CommentCode.SourceLength)
then
continue;
CommentStr:=ExtractCommentContent(CommentCode.Source,CommentStart,
NestedComments,true,true,true);
CommentStr := ExtractComment(CommentCode.Source, CommentStart);
AddComment;
end;
CommentStr:='';
CodeXYPos:=nil;
AddComment;
finally
FreeListOfPCodeXYPosition(ListOfPCodeXYPosition);
end;
@ -2828,6 +2897,7 @@ begin
while p>0 do
begin
case Result[p] of
' ': Result:=copy(Result,1,p-1)+'&nbsp;'+copy(Result,p+1,length(Result));
'<': Result:=copy(Result,1,p-1)+'&lt;'+copy(Result,p+1,length(Result));
'>': Result:=copy(Result,1,p-1)+'&gt;'+copy(Result,p+1,length(Result));
'&': Result:=copy(Result,1,p-1)+'&amp;'+copy(Result,p+1,length(Result));

View File

@ -464,12 +464,12 @@ begin
Result:=copy(Result,1,p-1)+NewTag+copy(Result,EndPos,length(Result));
inc(p,length(NewTag));
end;
end else if Result[p] in [' ',#9,#10,#13] then begin
end else if Result[p] in [#9,#10,#13] then begin
// replace spaces and newline characters with a single space
EndPos:=p+1;
while (EndPos<=length(Result)) and (Result[EndPos] in [' ',#9,#10,#13]) do
while (EndPos<=length(Result)) and (Result[EndPos] in [#9,#10,#13]) do
inc(EndPos);
if (p > 1) and not (Result[p-1] in [' ',#9,#10,#13]) then
if (p > 1) and not (Result[p-1] in [#9,#10,#13]) then
begin
Result:=copy(Result,1,p-1)+' '+copy(Result,EndPos,length(Result));
inc(p);
@ -477,7 +477,7 @@ begin
else
Result:=copy(Result,1,p-1)+copy(Result,EndPos,length(Result));
end else if Result[p]='&' then begin
// special chars: &lt; &gt; &amp;
// special chars: &lt; &gt; &amp; &nbsp;
if (p+2<Length(Result)) and (Result[p+1]='l') and (Result[p+2]='t') and (Result[p+3]=';') then begin
EndPos:=p+4;
Result:=copy(Result,1,p-1)+'<'+copy(Result,EndPos,length(Result));
@ -486,6 +486,10 @@ begin
EndPos:=p+4;
Result:=copy(Result,1,p-1)+'>'+copy(Result,EndPos,length(Result));
end else
if (p+4<Length(Result)) and (Result[p+1]='n') and (Result[p+2]='b') and (Result[p+3]='s') and (Result[p+4]='p') and (Result[p+5]=';') then begin
EndPos:=p+6;
Result:=copy(Result,1,p-1)+' '+copy(Result,EndPos,length(Result));
end else
if (p+3<Length(Result)) and (Result[p+1]='a') and (Result[p+2]='m') and (Result[p+3]='p') and (Result[p+4]=';') then begin
EndPos:=p+5;
Result:=copy(Result,1,p-1)+'&'+copy(Result,EndPos,length(Result));