CnPack Forum » CnWizards IDE Wizards » Insert code at beginning of every procedure


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

[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.[/code]

Regard,

--
Pierre Y.

2016-12-1 09:43 Passion
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.

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

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

2017-11-29 17:52 Brookesparks
Most people find it helpful to indent their code.

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

页: [1]


Powered by Discuz! Archiver 5.0.0  © 2001-2006 Comsenz Inc.