【问题标题】:Highlighting delphi code in RAD Studio在 RAD Studio 中突出显示 delphi 代码
【发布时间】:2017-11-09 19:22:31
【问题描述】:

我正在尝试使用 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);

谁能给我一个正确实施的例子?或者帮我解决我做错了什么?

【问题讨论】:

  • 好吧,看看代码,SysntaxByte(通过 SyntaxCode)必须至少和 Text 一样长,但事实并非如此。至于为什么会这样,我不知道。我猜这会将格式代码逐个字符地应用于原始文本。
  • 你使用的是哪个 Delphi 版本?
  • 10.2东京版

标签: delphi syntax-highlighting highlighting


【解决方案1】:

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

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

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;

有了这个通知器,我可以在画布上画一些东西来引起用户的注意。

【讨论】:

    猜你喜欢
    • 2019-08-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    相关资源
    最近更新 更多