【问题标题】:GNAT GPS: Add a custom commandGNAT GPS:添加自定义命令
【发布时间】:2019-12-15 16:26:16
【问题描述】:

我想知道是否可以在 GNAT Programming Studio (GPS) 中添加自定义命令?

如果调用自定义命令(通过菜单栏中的按钮或键盘快捷键),则应使用在编辑器中打开和选择的文件的完整/绝对路径调用外部 Python 脚本。

【问题讨论】:

  • 建议您运行 GPS 并转到菜单帮助 > GPS > Python 扩展。

标签: ada gnat gnat-gps


【解决方案1】:

这是一个简单粗暴的脚本,可能会提供一些指导。我在 Linux 上对其进行了测试,但它也应该可以在 Windows 上运行。更改接近尾声的操作以调用您喜欢的脚本。要实际使用它,您必须将它放在(隐藏的).gps/plug-ins 目录中,该目录可以在您的主目录中找到。可以从源代码窗口中的上下文菜单调用实际操作。

run_my_script.py

"""Run Python Script

This plug-in executes a python script.
"""

###########################################################################
# No user customization below this line
###########################################################################

import os, sys
import GPS
from gps_utils import interactive

def __contextualMenuFilter(context):

    # Check if the context is generated by the source editor
    if not (context.module_name == "Source_Editor"):
        return False

    # If all OK, show the menu item in the context menu.
    return True

def __contextualMenuLabel(context):

    # Get current buffer
    name = context.file().name()
    basename = os.path.basename(name)

    # Name of the menu item.
    return "Run Python script for <b>{}</b>".format(basename)

@interactive(
    name       ="Run Python script",
    contextual = __contextualMenuLabel,
    filter     = __contextualMenuFilter)
def on_activate():

    # If modified, then save before proceeding.
    eb = GPS.EditorBuffer.get()
    if eb.is_modified:
        eb.save()

    # Run the action (defined below).
    GPS.execute_action("my_script")

GPS.parse_xml ("""
   <action name="my_script">
     <external output="Output of my_script">python3 /home/deedee/my_script.py %F</external>
   </action>""")

my_script.py(一些测试脚本)

import sys

print ("Running script {0} for {1}".format(sys.argv[0], sys.argv[1]));

输出(显示在 GPS 中名为“输出 my_script”的新选项卡上)

python3 /home/deedee/my_script.py /home/deedee/example/src/main.adb
Running script /home/deedee/my_script.py for /home/deedee/example/src/main.adb

GNAT Studio(以前称为 GPS)文档中的一些相关信息:

【讨论】:

  • 感谢您提供的漂亮示例脚本。如果我的 Python 脚本修改了磁盘上的引用文件,GPS 会询问我是否有兴趣重新加载该文件。 Python插件脚本可以抑制这种行为吗?我不想全局禁用此重新加载问题。
  • 您可能想尝试将回调添加到适当的挂钩(例如 file_changed_detected,如 GNAT Studio documentation 所述)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-09
相关资源
最近更新 更多