| 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:
- StartClass - The value which is used for this type in the
Highlighter interfaces is user definable (it is used to gain context
for lines).
- LineBuf - The buffer to be tokenized
- LineBufLen - Length of line buffer to be tokenized
- HighlightCodes - The pointer to memory buffer. The size of the
buffer is equal LineBufLen. Tokenize should fill this buffer with
syntax highlight codes. The possible values must be between
atWhiteSpace and SyntaxOff.
Parameters of TokenizeLineClass function:
- StartClass - The value which is used for this type in the
Highlighter interfaces is user definable (it is used to gain context
for lines).
- LineBuf - The buffer to be tokenized
- LineBufLen - Length of line buffer to be tokenized
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.
|