From f1f2ad6b7e17ed34cb3d634d73ea3bd803b21edc Mon Sep 17 00:00:00 2001 From: Basile Burg Date: Sun, 29 May 2016 20:22:06 +0200 Subject: [PATCH] add editor command to comment the current identifier --- src/ce_editor.lfm | 6 +++++- src/ce_editor.pas | 10 +++++++++ src/ce_synmemo.pas | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/ce_editor.lfm b/src/ce_editor.lfm index 15f7397b..740e25a7 100644 --- a/src/ce_editor.lfm +++ b/src/ce_editor.lfm @@ -98,9 +98,13 @@ inherited CEEditorWidget: TCEEditorWidget Caption = '-' end object mnuedComm: TMenuItem - Caption = 'Comment' + Caption = 'Comment selected lines' OnClick = mnuedCommClick end + object MenuItem6: TMenuItem + Caption = 'Comment identifier' + OnClick = MenuItem6Click + end object mnuedInvAllNone: TMenuItem Caption = 'Invert version all none' OnClick = mnuedInvAllNoneClick diff --git a/src/ce_editor.pas b/src/ce_editor.pas index e4502240..65541e41 100644 --- a/src/ce_editor.pas +++ b/src/ce_editor.pas @@ -35,11 +35,14 @@ type destructor Destroy; override; end; + { TCEEditorWidget } + TCEEditorWidget = class(TCEWidget, ICEMultiDocObserver, ICEMultiDocHandler, ICEProjectObserver) MenuItem1: TMenuItem; MenuItem2: TMenuItem; MenuItem3: TMenuItem; MenuItem5: TMenuItem; + MenuItem6: TMenuItem; mnuedRename: TMenuItem; mnuedInvAllNone: TMenuItem; mnuedComm: TMenuItem; @@ -59,6 +62,7 @@ type editorStatus: TStatusBar; mnuEditor: TPopupMenu; procedure MenuItem5Click(Sender: TObject); + procedure MenuItem6Click(Sender: TObject); procedure mnuedRenameClick(Sender: TObject); procedure mnuedInvAllNoneClick(Sender: TObject); procedure mnuedCommClick(Sender: TObject); @@ -722,6 +726,12 @@ begin end; end; +procedure TCEEditorWidget.MenuItem6Click(Sender: TObject); +begin + if fDoc.isNotNil then + fDoc.CommandProcessor(ecCommentIdentifier, '', nil); +end; + procedure TCEEditorWidget.mnuedRenameClick(Sender: TObject); begin if fDoc.isNotNil then diff --git a/src/ce_synmemo.pas b/src/ce_synmemo.pas index f13b6028..d289ab5e 100644 --- a/src/ce_synmemo.pas +++ b/src/ce_synmemo.pas @@ -247,6 +247,7 @@ const ecCommentSelection = ecUserFirst + 10; ecSwapVersionAllNone = ecUserFirst + 11; ecRenameIdentifier = ecUserFirst + 12; + ecCommentIdentifier = ecUserFirst + 13; var D2Syn: TSynD2Syn; // used as model to set the options when no editor exists. @@ -735,6 +736,7 @@ begin AddKey(ecCommentSelection, ord('/'), [ssCtrl], 0, []); AddKey(ecSwapVersionAllNone, 0, [], 0, []); AddKey(ecRenameIdentifier, VK_F2, [ssCtrl], 0, []); + AddKey(ecCommentIdentifier, 0, [], 0, []); end; end; @@ -753,6 +755,7 @@ begin 'ecCommentSelection': begin Int := ecCommentSelection; exit(true); end; 'ecSwapVersionAllNone': begin Int := ecSwapVersionAllNone; exit(true); end; 'ecRenameIdentifier': begin Int := ecRenameIdentifier; exit(true); end; + 'ecCommentIdentifier': begin Int := ecCommentIdentifier; exit(true); end; else exit(false); end; end; @@ -772,6 +775,7 @@ begin ecCommentSelection: begin Ident := 'ecCommentSelection'; exit(true); end; ecSwapVersionAllNone: begin Ident := 'ecSwapVersionAllNone'; exit(true); end; ecRenameIdentifier: begin Ident := 'ecRenameIdentifier'; exit(true); end; + ecCommentIdentifier: begin Ident := 'ecCommentIdentifier'; exit(true); end; else exit(false); end; end; @@ -908,6 +912,54 @@ begin end; end; +procedure commentIdentifier(editor: TSynEdit); +var + str: string; + comment: boolean = true; + tkType, st: Integer; + attrib: TSynHighlighterAttributes; +begin + if not editor.GetHighlighterAttriAtRowColEx(editor.CaretXY, str, tkType, st, attrib) then + exit; + if str.isEmpty then + exit; + + if (str.length > 1) and ((str[1..2] = '/*') or + (str[str.length-1..str.length] = '*/')) then + comment := false; + + if comment then + begin + editor.BeginUndoBlock; + editor.ExecuteCommand(ecWordLeft, '', nil); + editor.ExecuteCommand(ecChar, '/', nil); + editor.ExecuteCommand(ecChar, '*', nil); + editor.ExecuteCommand(ecWordEndRight, '', nil); + editor.ExecuteCommand(ecChar, '*', nil); + editor.ExecuteCommand(ecChar, '/', nil); + editor.EndUndoBlock; + end else + //TODO-ceditor: handle spaces between ident and comment beg end. + begin + editor.BeginUndoBlock; + if str[1..2] = '/*' then + begin + editor.ExecuteCommand(ecWordLeft, '', nil); + editor.ExecuteCommand(ecLeft, '', nil); + editor.ExecuteCommand(ecLeft, '', nil); + editor.ExecuteCommand(ecDeleteChar, '', nil); + editor.ExecuteCommand(ecDeleteChar, '', nil); + end; + if str[str.length-1..str.length] = '*/' then + begin + editor.ExecuteCommand(ecWordEndRight, '', nil); + editor.ExecuteCommand(ecDeleteChar, '', nil); + editor.ExecuteCommand(ecDeleteChar, '', nil); + end; + editor.EndUndoBlock; + end; +end; + procedure TCESynMemo.DoOnProcessCommand(var Command: TSynEditorCommand; var AChar: TUTF8Char; Data: pointer); begin @@ -943,6 +995,8 @@ begin invertVersionAllNone; ecRenameIdentifier: renameIdentifier; + ecCommentIdentifier: + commentIdentifier(self); end; if fOverrideColMode and not SelAvail then begin