私は Delphi でライブ テンプレートを頻繁に使用していますが、テンプレートに GUID を追加するソリューションを考え出そうとしています。これを行う方法を誰か知っていますか?
現在使用しているテンプレートの下にある GUID という単語は、手動で置き換える必要があります。
<?xml version="1.0" encoding="utf-8" ?>
<codetemplate xmlns="http://schemas.borland.com/Delphi/2005/codetemplates"
version="1.0.0">
<template name="iacc" surround="false" invoke="manual">
<point name="name">
<text>
IntfAccessors
</text>
<hint>
Accessors name
</hint>
</point>
<description>
accessor declaration
</description>
<author>
PMH
</author>
<code language="Delphi" context="methoddecl" delimiter="|"> <![CDATA[I|name|Accessors = interface(IInterface)
GUID <-- here I want a GUID
end;
I|name| = interface(I|name|Accessors)
GUID <-- here I want a GUID
end;
]]>
</code>
</template>
</codetemplate>
ベストアンサー1
これを実行するには、カスタム スクリプト エンジンを作成して IDE を拡張できます。(ここNick Hodges が書いた記事には、現在の日付を挿入する同様の例が記載されています。
サンプル テンプレートの 2 つの異なるインターフェイスには 2 つの異なる IID が必要であると想定しているため、スクリプト エンジンを作成して「スクリプト」からポイント名をロードしました (これは単なる名前 = 値のペアのリストで、名前はポイント名で、値は NewGuid である必要があります。それ以外の場合は無視されます)。これにより、複数のポイントを持つテンプレートを作成し、それぞれに個別の新しい IID を割り当てることができます。
テンプレート intf.xml の例:
<?xml version="1.0" encoding="utf-8" ?>
<codetemplate xmlns="http://schemas.borland.com/Delphi/2005/codetemplates" version="1.0.0">
<template name="iacc" surround="false" invoke="manual">
<point name="name">
<text>
Accessor
</text>
<hint>
Accessors name
</hint>
</point>
<point name="guid1"/>
<point name="guid2"/>
<description>
accessor declaration
</description>
<author>
PMH
</author>
<script language="NewGuidScript" onvalidate="true">
guid1=NewGuid
guid2=NewGuid
</script>
<code language="Delphi" context="any" delimiter="|"> <![CDATA[I|name|Accessors = interface(IInterface)
|*||guid1|
end;
I|name| = interface(I|name|Accessors)
|*||guid2|
end;]]>
</code>
</template>
</codetemplate>
新しいGuidScriptEngine.pas:
unit NewGuidScriptEngine;
interface
uses
Classes, SysUtils,
ToolsApi, CodeTemplateApi, DesignEditors;
type
TNewGuidScriptEngine = class(TNotifierObject, IOTACodeTemplateScriptEngine)
public
procedure Execute(const ATemplate: IOTACodeTemplate; const APoint: IOTACodeTemplatePoint; const ASyncPoints: IOTASyncEditPoints; const AScript: IOTACodeTemplateScript; var Cancel: Boolean);
function GetIDString: WideString;
function GetLanguage: WideString;
end;
procedure Register;
implementation
uses
ActiveX,
ComObj;
procedure Register;
begin
(BorlandIDEServices as IOTACodeTemplateServices).RegisterScriptEngine(TNewGuidScriptEngine.Create);
end;
procedure TNewGuidScriptEngine.Execute(const ATemplate: IOTACodeTemplate; const APoint: IOTACodeTemplatePoint;
const ASyncPoints: IOTASyncEditPoints; const AScript: IOTACodeTemplateScript; var Cancel: Boolean);
var
I: Integer;
Guid: TGUID;
P: IOTACodeTemplatePoint;
Points: TStringList;
begin
Cancel := False;
if not Assigned(ATemplate) then
Exit;
Points := TStringList.Create;
try
Points.Text := AScript.Script;
for I := 0 to Points.Count - 1 do
Points.Strings[I] := Trim(Points[I]);
for I := 0 to Points.Count - 1 do
if Points.ValueFromIndex[I] = 'NewGuid' then
begin
P := ATemplate.FindPoint(Points.Names[I]);
if Assigned(P) then
begin
OleCheck(CoCreateGuid(Guid));
P.Editable := False;
P.Value := '[''' + GUIDToString(Guid) + ''']';
end;
end;
finally
Points.Free;
end;
end;
function TNewGuidScriptEngine.GetIDString: WideString;
begin
Result := 'OndrejKelle.NewGuidScriptEngine';
end;
function TNewGuidScriptEngine.GetLanguage: WideString;
begin
Result := 'NewGuidScript';
end;
end.
上記のユニットをデザインタイム専用パッケージに配置し、その句に designide.dcp への参照を追加してrequires
、IDE にパッケージをインストールします。
もう一つの便利な類似のテンプレートは次のようになります。
<?xml version="1.0" encoding="utf-8" ?>
<codetemplate xmlns="http://schemas.borland.com/Delphi/2005/codetemplates" version="1.0.0">
<template name="iacc" surround="false" invoke="manual">
<point name="name">
<text>
</text>
<hint>
Accessors name
</hint>
</point>
<point name="guid1"/>
<point name="guid2"/>
<description>
accessor declaration
</description>
<author>
PMH
</author>
<script language="NewGuidScript" onvalidate="true">
guid1=NewGuid
guid2=NewGuid
</script>
<code language="Delphi" context="any" delimiter="|"> <![CDATA[const
SIID_I|name|Accessors = |guid1|;
IID_I|name|Accessors: TGUID = SIID_I|name|Accessors;
SIID_I|name| = |guid2|;
IID_I|name|: TGUID = SIID_I|name|;
type
I|name|Accessors = interface
[SIID_I|name|Accessors]
end;
I|name| = interface(I|name|Accessors)
[SIID_I|name|]
end;]]>
</code>
</template>
</codetemplate>
これにより、文字列とTGUID
定数が宣言され、インターフェイス宣言でも再利用されます。この場合、挿入された GUID 値は角括弧で囲まないでください。これを行うには、スクリプト エンジンを調整するいくつかのオプションがあります。
- コードを修正して角括弧を使わないようにします
- 別途新しい関数 NewGuidNoBrackets を導入し、テンプレートで使用します。
- エンジンが解析できる NewGuid(false) のような簡単な構文を導入し、パラメータ値を使用して角括弧を使用するかどうかを判断します。