Board logo

Subject: Insert code at beginning of every procedure [Print This Page]

Author: PierreYager    Time: 2016-11-29 01:13     Subject: 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 :

[Copy to clipboard]
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.
Author: Passion    Time: 2016-12-1 09:43

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.
Author: Melissa    Time: 2017-6-2 12:40

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 ]
Author: Brookesparks    Time: 2017-11-29 17:52

Most people find it helpful to indent their code.

[ Last edited by  Brookesparks at 2017-11-29 17:55 ]




Welcome to CnPack Forum (http://bbs.cnpack.org/) Powered by Discuz! 5.0.0