CnPack Forum » CnPack IDE 专家包(CnWizards) » 代码的行后注释,是自动格式化的那种紧随模式好,还是统一对齐到Right Margin更好?


2015-3-6 10:10 xychen
代码的行后注释,是自动格式化的那种紧随模式好,还是统一对齐到Right Margin更好?

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

2015-3-6 10:20 xychen
我的屏幕分辨率宽度是1920,编辑区比较大,我喜欢把Right Margin设置为i120,然后把注释对齐Right Margin,让代码和注释相对分离,觉得这样更清爽一些,不知道有没有朋友和我有一样的感觉?
因为在Delphi自带的格式化功能和CnWizards中没有这个的实现,所以我自己在阅读了CnWizards的代码后加上了这个功能,代码如下:
[font=仿宋_GB2312]
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[i];
    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[i] := 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);[/font]

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

页: [1]


Powered by Discuz! Archiver 5.0.0  © 2001-2006 Comsenz Inc.