在 RAD Studio 中突出显示 delphi 代码

Highlighting delphi code in RAD Studio

我正在尝试使用 Delphi 插件在 RAD Studio 中突出显示 Delphi 代码。 我使用 OpentoolsAPI 从编辑器中获取代码。

EditorServices := BorlandIDEServices as IOTAEditorServices;

Buffer := EditorServices.TopBuffer;
Editblock := EditorServices.TopView.GetBlock;
Buffer.EditPosition.Move(1,1);
Editblock.BeginBlock;
Editblock.Extend(10,5);

之后,打开工具FAQ告诉我使用自定义荧光笔。 我从这里复制了一个自定义荧光笔:http://www.delphi-central.com/syntax_highlighting.aspx

但是,文档仍然非常有限,我想不出使用这个自定义荧光笔的方法。 我目前正在尝试的是:

HighlightServices := BorlandIDEServices as IOTAHighlightServices;
SimpleHighLight := TSimpleHighlight.Create;
HighlightServices.AddHighlighter(SimpleHighLight);

Text := Editblock.Text; //string
StartClass := 1; //integer
SyntaxByte := SyntaxOff; //byte
SyntaxCode := @SyntaxByte; //POTASyntaxCode

SimpleHighLight.Tokenize(StartClass,Addr(Text),Text.Length, SyntaxCode);

但这会导致演示代码的这一行发生访问冲突错误:

FillChar(HighlightCodes^, LineBufLen, $E);

有人可以给我一个正确实施的例子吗?或者帮我解决我做错了什么?

我不再寻找使用荧光笔的方法。我无法让它工作。

幸运的是,对于任何感兴趣的人,我找到了解决我问题的其他方法:)

OpenToolsAPI 使添加通知程序成为可能。可以在此处找到示例通知程序:http://www.gexperts.org/examples/IdeNotifier.pas.

我不得不将示例中的通知程序修改为我自己的通知程序。这是结果:

unit ViewPaintNotifier;

interface

uses
  ToolsAPI, System.Types, Vcl.Graphics;

type
  TViewPaintNotifier = class(TInterfacedObject, IOTANotifier, INTAEditViewNotifier)
  private
  public
    constructor Create;
    destructor Destroy; override;
  public
    // INTAEditViewNotifier
    procedure BeginPaint(const View: IOTAEditView; var FullRepaint: Boolean);
    procedure EditorIdle(const View: IOTAEditView);
    procedure EndPaint(const View: IOTAEditView);
    procedure PaintLine(const View: IOTAEditView; LineNumber: Integer;
      const LineText: PAnsiChar; const TextWidth: Word;
      const LineAttributes: TOTAAttributeArray; const Canvas: TCanvas;
      const TextRect: TRect; const LineRect: TRect; const CellSize: TSize);
    // IOTANotifier
    procedure AfterSave;
    procedure BeforeSave;
    procedure Destroyed;
    procedure Modified;
  end;

  procedure register(View: IOTAEditView);
  procedure RemoveNotifier(View: IOTAEditView);

implementation

uses
  System.SysUtils, Vcl.Dialogs, System.Generics.Collections;

var
  NotifierIndexDictionary : TDictionary<string,Integer>;

procedure Register(View: IOTAEditView);
var
  Services: IOTAEditorServices;
  NotifierIndexPair : Tpair<string,Integer>;
begin
  if not Assigned(NotifierIndexDictionary) then
     NotifierIndexDictionary := TDictionary<string,Integer>.create;
  NotifierIndexPair := NotifierIndexDictionary.ExtractPair(View.Buffer.FileName);

  if (NotifierIndexDictionary.ExtractPair(View.Buffer.FileName).Value = 0) then
  begin
    NotifierIndexDictionary.Add(View.Buffer.FileName,View.addNotifier(TViewPaintNotifier.Create));
  end;
end;

procedure RemoveNotifier(View: IOTAEditView);
var
  Services: IOTAEditorServices;
begin
  if (NotifierIndexDictionary.ExtractPair(View.Buffer.FileName).Value > 0) then
  begin
    View.RemoveNotifier(NotifierIndexDictionary.ExtractPair(View.Buffer.FileName).Value);
    NotifierIndexDictionary.Add(View.Buffer.FileName,0);
  end;
end;


{ TViewPaintNotifier }

constructor TViewPaintNotifier.Create;
begin

end;

destructor TViewPaintNotifier.Destroy;
begin

  inherited;
end;

procedure TViewPaintNotifier.EditorIdle(const View: IOTAEditView);
begin

end;

procedure TViewPaintNotifier.BeginPaint(const View: IOTAEditView;
  var FullRepaint: Boolean);
begin

end;

procedure TViewPaintNotifier.EndPaint(const View: IOTAEditView);
begin

end;

procedure TViewPaintNotifier.PaintLine(const View: IOTAEditView;
  LineNumber: Integer; const LineText: PAnsiChar; const TextWidth: Word;
  const LineAttributes: TOTAAttributeArray; const Canvas: TCanvas;
  const TextRect, LineRect: TRect; const CellSize: TSize);
begin
  //use the canvas to draw something
  //LineRect is the position of the line on the canvas, draw it here for every line
end;

procedure TViewPaintNotifier.AfterSave;
begin

end;

procedure TViewPaintNotifier.BeforeSave;
begin

end;

procedure TViewPaintNotifier.Destroyed;
begin

end;

procedure TViewPaintNotifier.Modified;
begin

end;

有了这个通知程序,我可以用 canvas 画一些东西来引起用户的注意。