gpt4 book ai didi

delphi - 在 Delphi 中调用实时模板时如何创建 GUID?

转载 作者:行者123 更新时间:2023-12-03 14:35:13 28 4
gpt4 key购买 nike

我在 Delphi 中经常使用实时模板,但尝试想出一个将 GUIDS 添加到模板的解决方案。有谁知道该怎么做吗?

在我现在拥有的模板下方,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>

最佳答案

您可以通过编写自定义脚本引擎来扩展 IDE 来执行此操作。 ( Here 是 Nick Hodges 撰写的一篇文章,其中包含插入当前日期的类似示例。)

我假设示例模板中的两个不同接口(interface)需要两个不同的 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>

NewGuidScriptEngine.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.

将上述单元放入仅设计时包中,在其 requires 子句中添加对 designide.dcp 的引用,然后在 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常量,并在接口(interface)声明中重用它们。在这种情况下,插入的 GUID 值不应括在方括号中。您有多种选择来调整脚本引擎来执行此操作:

  1. 修改代码以不使用方括号
  2. 引入一个单独的新函数NewGuidNoBrackets并在模板中使用它
  3. 引入一些简单的语法,例如 NewGuid(false),您的引擎可以解析这些语法并使用参数值来确定是否应使用方括号。

关于delphi - 在 Delphi 中调用实时模板时如何创建 GUID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39248003/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com