CnPack Forum


 
Subject: 代码的行后注释,是自动格式化的那种紧随模式好,还是统一对齐到Right Margin更好?
xychen
普通灌水员
Rank: 2



UID 28175
Digest Posts 0
Credits 56
Posts 16
点点分 56
Reading Access 10
Registered 2007-10-5
Status Offline
Post at 2015-3-6 10:10  Profile | Blog | P.M. 
代码的行后注释,是自动格式化的那种紧随模式好,还是统一对齐到Right Margin更好?

两种不同风格的注释,大家更喜欢哪一种?


Image Attachment: 1.png (2015-3-6 10:10, 22.85 K)



Image Attachment: 2.png (2015-3-6 10:10, 21.44 K)

Top
xychen
普通灌水员
Rank: 2



UID 28175
Digest Posts 0
Credits 56
Posts 16
点点分 56
Reading Access 10
Registered 2007-10-5
Status Offline
Post at 2015-3-6 10:20  Profile | Blog | P.M. 
我的屏幕分辨率宽度是1920,编辑区比较大,我喜欢把Right Margin设置为i120,然后把注释对齐Right Margin,让代码和注释相对分离,觉得这样更清爽一些,不知道有没有朋友和我有一样的感觉?
因为在Delphi自带的格式化功能和CnWizards中没有这个的实现,所以我自己在阅读了CnWizards的代码后加上了这个功能,代码如下:

CnWizConsts.pas添加菜单信息:

  // CnEditorCodeCommentAlign
  SCnEditorCodeCommentAlignMenuCaption: string = 'Comment Ali&gn';
  SCnEditorCodeCommentAlignMenuHint: string = 'Comment Align Colum 120';
  SCnEditorCodeCommentAlignName: string = 'Comment Align Tool';


CnEditorCodeComment.pas添加注释对齐类:

// ==============================================================================
// 注释对齐工具类
// ==============================================================================

  { TCnEditorCodeCommentAlign }
  TCnEditorCodeCommentAlign = class(TCnEditorCodeTool)
  protected
    function ProcessText(const Str: string): string; override;
    function GetStyle: TCnCodeToolStyle; override;
  public
    function GetCaption: string; override;
    function GetHint: string; override;
    procedure GetEditorInfo(var Name, Author, Email: string); override;
  end;



{ TCnEditorCodeCommentAlign }

function TCnEditorCodeCommentAlign.ProcessText(const Str: string): string;
var
  i, j: Integer;
  Line, LineCode: string;
  Code: TStringList;
begin
  Code := TStringList.Create;
  Code.Text := StringReplace(Str, #9, '  ', [rfReplaceAll]);                                                            // 替换TAB为两个空格
  for i := 0 to Code.Count - 1 do
  begin
    Line := Code.Strings;
    j := Pos('//', Trim(Line));                                                                                         // 查找注释的位置
    if j > 1 then                                                                                                       // 如果注释在代码行后且没有对齐到折返线,则进行后注释的对齐操作
    begin
      j := Pos('//', Line);                                                                                             // 定位注释
      if j<121 then
      begin
        LineCode := Copy(Line, 1, j - 1);
        Delete(Line, 1, j - 1);
        Code.Strings := LineCode + DupeString(' ', 121 - j) + Line;                                                  // 在注释前插入空格确保注释对齐
      end;
    end;
  end;
  Result := Code.Text;
  Code.Free;
end;

function TCnEditorCodeCommentAlign.GetStyle: TCnCodeToolStyle;
begin
  Result := csAllText;
end;

function TCnEditorCodeCommentAlign.GetCaption: string;
begin
  Result := SCnEditorCodeCommentAlignMenuCaption;
end;

function TCnEditorCodeCommentAlign.GetHint: string;
begin
  Result := SCnEditorCodeCommentAlignMenuHint;
end;

procedure TCnEditorCodeCommentAlign.GetEditorInfo(var Name, Author, Email: string);
begin
  Name := SCnEditorCodeCommentAlignName;
  Author := SCnPack_LiuXiao;
  Email := SCnPack_LiuXiaoEmail;
end;

初始化节添加菜单注册:
RegisterCnEditor(TCnEditorCodeCommentAlign);
Top
xychen
普通灌水员
Rank: 2



UID 28175
Digest Posts 0
Credits 56
Posts 16
点点分 56
Reading Access 10
Registered 2007-10-5
Status Offline
Post at 2015-3-6 10:27  Profile | Blog | P.M. 
我把这组代码添加后重新编译,用生成的dll替换掉原来的dll,经过测试发现对齐行后注释的功能有的时候会异常,主要表现在有时会莫名其妙的在某行的代码中插入一回车,代码行数较多的时候,最后几十行的代码中的注释还可能没有对齐,就像是csAllText取得的并非是全部编辑区中代码一样,恳请各位大大帮助分析一下这段代码实现中的错误原因。谢谢!
如果开发组能在下一版中加这个一个功能那可就太好了。。。
Top
 




All times are GMT++8, the time now is 2024-4-24 02:22

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.008044 second(s), 8 queries , Gzip enabled

Clear Cookies - Contact Us - CnPack Website - Archiver - WAP