我的屏幕分辨率宽度是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);
|