CnPack Forum


 
Subject: Insert code at beginning of every procedure
PierreYager
新警察
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

Hello,

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;

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

var
  EditView: IOTAEditView;
  Proc, O, M: string;
  DotPos: Integer;
begin
  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
  begin
    O := Copy(Proc, 1, DotPos - 1);
    M := Copy(Proc, DotPos + 1, MaxInt);
  end;

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

Regard,

--
Pierre Y.
Top
Passion (LiuXiao)
管理员
Rank: 9Rank: 9Rank: 9


UID 359
Digest Posts 19
Credits 6373
Posts 3364
点点分 6373
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.
Top
Melissa
新警察
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.
Top
 




All times are GMT++8, the time now is 2017-6-26 06:25

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

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