首页 > 解决方案 > GNAT GPS:添加自定义命令

问题描述

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

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

标签: adagnatgnat-gps

解决方案


这是一个快速而肮脏的脚本,可能会给出一些方向。我在 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]));

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

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)文档中的一些相关信息:


推荐阅读