首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Delphi中调用Live模板时如何创建GUID?

在Delphi中调用Live模板时如何创建GUID?
EN

Stack Overflow用户
提问于 2016-08-31 19:09:48
回答 1查看 726关注 0票数 19

我在Delphi中经常使用Live模板,但我试图想出一个解决方案来将GUIDS添加到模板中。有人知道怎么做吗?

下面的模板,我现在有一个GUID作为我需要手动替换的单词。

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-01 00:08:21

您可以通过编写自定义脚本引擎来扩展IDE。(Here是尼克·霍奇斯写的一篇文章,上面有一个类似的插入当前日期的例子。)

我假设示例模板中的两个不同接口需要两个不同的IID,所以我编写了脚本引擎来从“脚本”(它只是一个name=value对的列表,其中名称是点名,值必须为NewGuid,否则它将被忽略)中加载点名称,以便您可以创建具有多个点的模板,每个点接收一个单独的新IID。

示例模板intf.xml:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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.

另一个有用的类似模板可能如下所示:

代码语言:javascript
复制
<?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>

这将声明string和TGUID常量,并在接口声明中重用它们。在这种情况下,插入的GUID值不应包括在方括号中。您有几个选项可以调整脚本引擎来执行此操作:

将代码修改为不使用方括号,而是使用一个单独的新函数brackets

  • introduce并在template

  • introduce中使用它一些简单的语法,如NewGuid(

  • ),您的引擎可以解析这些语法并使用参数值来确定是否应该使用方括号。
票数 23
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39248003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档