Custom syntax highlighting in Delphi 7

'

How to use IOTAHighlighter interface

Syntax highlighting changes the attributes and colors of your text in the Code editor, making it easier to quickly identify parts of your code.  Delphi Open Tools API (OTA) provides a way to create own syntax highlighters for Delphi IDE code editor.

Using OTA you can create highlighters for new source file types.  To create custom syntax highlighter you have to implement IOTAHighlighter interface and its inherited interfaces. The main problem that Borland does not provide any description of IOTAHighlighter interface.

The main methods of IOTAHighlighter are Tokenize and TokenizeLineClass.

Parameters of Tokenize procedure:

Parameters of TokenizeLineClass function:

This function returns the new start class for current tokenizing buffer. It can be useful to highlight source code if multi-line comments can be used, for example.

unit HighWizard;

interface

uses
  Windows, SysUtils, Classes, ToolsAPI, Dialogs;


type

  TSimpleHighlight = class(TNotifierObject, IUnknown, IOTANotifier, 
                        IOTAHighlighter, IOTAWizard)
  public
    function GetIDString: string;
    function GetName: string;
    procedure Tokenize(StartClass: TOTALineClass; LineBuf: POTAEdChar;
      LineBufLen: TOTALineSize; HighlightCodes: POTASyntaxCode);
    function TokenizeLineClass(StartClass: TOTALineClass;
      LineBuf: POTAEdChar; LineBufLen: TOTALineSize): TOTALineClass;
    function GetState: TWizardState;
    procedure Execute;
    constructor Create;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterPackageWizard(TSimpleHighlight.Create);
end;

{ TSimpleHighlight }


constructor TSimpleHighlight.Create;
begin
  inherited;
  (BorlandIDEServices as IOTAHighlightServices).AddHighlighter(Self);
end;

procedure TSimpleHighlight.Execute;
begin
end;

function TSimpleHighlight.GetIDString: string;
begin
  Result := 'psv.SimpleHighlight';
end;

function TSimpleHighlight.GetName: string;
begin
  Result := 'Simple';
end;


function TSimpleHighlight.GetState: TWizardState;
begin
  Result := [wsEnabled];
end;

procedure TSimpleHighlight.Tokenize(StartClass: TOTALineClass;
  LineBuf: POTAEdChar; LineBufLen: TOTALineSize;
  HighlightCodes: POTASyntaxCode);
var
 Codes : PChar;
 i : integer;
begin
  Codes := PChar(HighlightCodes);
  FillChar(HighlightCodes^, LineBufLen, $E);
  for i := 0 to LineBufLen -1 do
   begin
     if StartClass = 1 then
       begin
         Codes[i] := Char(atComment);
         if LineBuf[i] = '}' then
          StartClass := 0;
       end
        else
          begin
             if ( LineBuf[i] in ['0'..'9'] ) then
               Codes[i] := Char(atNumber);
             if LineBuf[i] = '{' then
              begin
                Codes[i] := Char(atComment);
                StartClass := 1;
              end;
          end;
   end;
end;

function TSimpleHighlight.TokenizeLineClass(StartClass: TOTALineClass;
  LineBuf: POTAEdChar; LineBufLen: TOTALineSize): TOTALineClass;
var
 i : integer;
begin
  Result := StartClass;
  for i := 0 to LineBufLen - 1 do
   begin
     case LineBuf[i] of
      '{' :  Result := 1;
       '}':  Result := 0;
     end;
   end;
end;

end.

This tutorial was kindly provided by serge perevoznyk


Google
Web www.Delphi-Central.com
Delphi Central - Delphi Programming Tutorials, Hints and Tips