集成开发环境代码片段扩展

Gambas IDE 提供了一组可修改的编码快捷方式(Code Snippets),大大简化了在IDE中的输入工作。 在行首输入代码快捷方式并按下该TAB键会自动将快捷方式扩展为定义的代码段。 IDE 提供的标准快捷方式可以在“首选项”表单的“代码格式”选项卡上看到。该选项卡的底部是一个可滚动的触发字符串列表和相关的扩展宏。

使用 IDE 代码自动扩展

激活/停用

为了在编码时使用 IDE Snippets功能,必须在首选项中选中激活代码片段复选框。如果没有激活该选项,则在输入触发字符串后 按TAB键将简单地在当前代码行上插入一个制表符。 IDE Snippets 扩展并包含 Gambas 的内置代码完成功能。

一些常用的例子

pr TAB 将插入一个属性声明。光标将定位在可以插入属性名称的位置。可以简单地重复键入所选文本。再次按TAB会将光标定位在属性数据类型的输入点。最后按下Enter将提交代码片段扩展并将 IDE 编辑器的使用返回到其“正常”状态。

f TAB 将插入一个函数声明块。光标将再次定位用于输入函数名称。但在这种情况下,按TAB多次将移动到函数名称、签名、返回类型的入口点,最后到达函数第一行代码的入口点。请注意,在这种情况下,IDE 编辑器似乎返回到“正常操作”。事实上,片段解析器仍在运行,直到您将光标移到功能块之外为止。因此,例如,如果你忽略了输入返回类型,则将光标放回声明行上的任意位置并按下Tab将突出显示被忽略的扩展宏标记。

你可能想要了解的其他一些触发因素是:

  • main TAB : 生成一个 Main 子程序.

  • fe TAB : 生成一个For Each ... Next 代码块

  • d TAB and di tab 和另外一些变形 : 产生 Dim 定义行

  • (我最喜欢的!) gpl Tab : 生成关于 GPL 的简短声明

最后,如果初始扩展出来的根本不是你想要的,只需按Ctrl+z即可恢复你的原始输入。

扩展宏

你可以定义自己的 IDE 片段!

同样,在**首选项** 表单, 代码格式 选项卡是用于创建、更改或删除自定义代码片段的按钮。(它还可以让你以只读模式查看标准代码片段 - 这很方便如果你想复制现有的宏)。要创建新片段,请单击New...按钮。你将看到一个包含两个控件的表单,一个用于输入触发器字符串的文本框和一个用于输入扩展宏的编辑器。 如果您尝试使用已定义的触发器字符串,IDE 将弹出一个气球“此触发器字符串已在使用中”,当您尝试保存片段时。

Syntax

The syntax for IDE Snippets is very simple but can take some getting used to. There are two parts, as indicated above, the trigger string and the expansion macro.

Trigger Strings
The trigger string can be any character string without inner spaces. However, it is probably safer to use lower case alpha or alpha+numeric strings. Not too difficult eh!

Expansion Macros
Expansion macros have three aspects.

Literal code
which is any sequence of characters and escaped characters not included below. So text like

If IsInteger(sText) Then
Return False
will be inserted as is including the leading spaces, newlines and tabs (that you will see as escape sequences in the Preferences list). In fact, inserting escape sequences in the macro should be avoided unless they are part of a literal string, as they will be double escaped in the internal macro code.

(Yes, even the typo will be entered verbatim.)

Insertion points
indicate where the cursor will be positioned after expansion. They are indicated by a sub-string ${n:lit_token} in the code where n is the ordinal cursor position of the token and lit_token is the text that will be displayed and highlighted for that token. So

? ${1:Variable}

will result in

? Variable

(which will automatically expand to "Print ..." when you leave that line.)

Project Metadata
The macro expansion also allows you to insert a few bits of project metadata in your macro. The metadata currently available is:
  • The project title. (use a ${n:$TITLE} token)

  • The project description. (use a ${n:$DESCRIPTION) token), and

  • The project author. (use a ${n:$AUTHOR} token)