CnPack Forum

Subject: Insert code at beginning of every procedure
Rank: 1

UID 364084
Digest Posts 0
Credits 4
Posts 1
点点分 4
Reading Access 10
Registered 2016-11-29
Status Offline
Post at 2016-11-29 01:13  Profile | Blog | P.M. 
Insert code at beginning of every procedure


I would like to instrument my code by adding some lines at beginning of every procedure in current file. I found some examples here on how to find the current procedure name (CnOtaGetCurrentProcedure) and to add text at cursor position (IdeInsertTextIntoEditor) but I can't find how to add text at the very beginning of a procedure nor how to find each procedure in a file.

Do you have some clues on which methods I can use from CnWizards or the OTA API to do that ?

Here is my current code :

program InsertLogger;

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

  EditView: IOTAEditView;
  Proc, O, M: string;
  DotPos: Integer;
  EditView := CnOtaGetTopMostEditView(nil);
  if EditView <> nil then
    EditView.GetPosition.MoveBOL; // Move the cursor to the beginning of the line

  proc := CnOtaGetCurrentProcedure;

  DotPos := Pos('.', proc);
  if DotPos > 0 then //remove the class name if it is present
    O := Copy(Proc, 1, DotPos - 1);
    M := Copy(Proc, DotPos + 1, MaxInt);

    '{$IFDEF CONSOLEAPP}' + #13#10 +
    '  Log(''%s/%s : VERIFY !'');' + #13#10 +
    '{$ENDIF}' + #13#10, [
      O, M


Pierre Y.
Passion (LiuXiao)
Rank: 9Rank: 9Rank: 9

UID 359
Digest Posts 19
Credits 6389
Posts 3372
点点分 6389
Reading Access 102
Registered 2004-3-28
Status Offline
Post at 2016-12-1 09:43  Profile | Blog | P.M. 
Now we don't have any OTA API to move cursor to a specified element according to grammar analysis.

An alternative way is reading whole file and searching "begin" after "procedure" or "function" keyword in current file, but maybe has problems when meeting nested procedure/function.
Rank: 1

UID 406351
Digest Posts 0
Credits 2
Posts 1
点点分 2
Reading Access 10
Registered 2017-6-2
Status Offline
Post at 2017-6-2 12:40  Profile | Blog | P.M. 
I have similar issues I needed to add codes at the beginning but I search it all through the internet but couldn't find any solution.

[ Last edited by  Melissa at 2017-7-15 22:33 ]


All times are GMT++8, the time now is 2017-10-17 15:42

携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.031075 second(s), 7 queries , Gzip enabled

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