如何在 Sublime Text 3 插件中制作多行弹出窗口

How to make multi-line pop-up in Sublime Text 3 plugin

我正在为 Sublime Text 3 制作一个插件。它在 Java 中联系我的服务器并接收字符串列表形式的响应。当您按下组合键时,我希望弹出 window 出现,您可以在其中查看所有行选项并复制所需的选项。我找到了如何针对一行 (Github) 执行此操作的示例,但我不明白如何针对多行(当然还有几个“复制”按钮)进行修改。它应该是这样的:

以下是在弹出窗口中显示作用域名称的插件代码:

import sublime
import sublime_plugin


def copy(view, text):
    sublime.set_clipboard(text)
    view.hide_popup()
    sublime.status_message('Scope name copied to clipboard')


class ShowScopeNameCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        scope = self.view.scope_name(self.view.sel()[-1].b)

        html = """
            <body id=show-scope>
                <style>
                    p {
                        margin-top: 0;
                    }
                    a {
                        font-family: system;
                        font-size: 1.05rem;
                    }
                </style>
                <p>%s</p>
                <a href="%s">Copy</a>
            </body>
        """ % (scope.replace(' ', '<br>'), scope.rstrip())

        self.view.show_popup(html, max_width=512, on_navigate=lambda x: copy(self.view, x))

一旦您知道当您单击显示 Copy 的 link 时复制的内容,这实际上非常简单。 根据官方 api reference,我们有 :-

on_navigate is a callback that should accept a string contents of the href attribute on the link the user clicked.

因此 href 属性中的任何内容都会被复制到 show_scope_name 命令的剪贴板(或者更正确地说,href 内容作为参数传递给 on_navigate回调)。有了这些信息,这里有一个简单的插件,它从 Jsonplaceholder 中获取一些待办事项(这是一个用于演示目的的假 REST API),将其显示为一个列表,每个列表都有自己的 [=12] =] 给你select 复制什么。您必须向 Java 服务器发送请求以获取字符串列表并相应地修改示例,而不是 Jsonplaceholder。

import json
import sublime
import urllib.parse
import urllib.request
import sublime_plugin


def get_data(num_of_todos):
    """ Fetches some todos from the Jsonplaceholder API (for the purposes of getting fake data).

    Args:
        num_of_todos (int) : The number of todos to be fetched.

    Returns:
        final_data (list) : The number of todos as a list.
    """
    try:
        url = "https://jsonplaceholder.typicode.com/todos"
        req = urllib.request.Request(url)
        req.add_header('User-agent', 'Mozilla/5.0')
        with urllib.request.urlopen(req) as response:
            fake_data = json.loads(response.read().decode("utf-8"))
            final_data = []
            for todo in fake_data:
                final_data.append(todo["title"])
            return final_data[:num_of_todos]
    except urllib.error.HTTPError as error:
        return json.loads(error.read().decode("utf-8"))


class MultilinePopUpCopyCommand(sublime_plugin.TextCommand):
    """ Command for fetching some todos & displaying a Copy link for each one of them,
         which upon being pressed copies the specified todo
    """

    def run(self, edit):
        """ This method is invoked when the command is run.

        Args:
            edit (sublime.Edit) : The edit object necessary for making buffer modifications 
            in the current view.

        Returns:
            None.
        """

        # Construct an li tree to be injected later in the ul tag.
        li_tree = ""
        final_data = get_data(5)
        for i in range(len(final_data)):
            li_tree += "<li>%s <a href='%s'>Copy</a></li>\n" %(final_data[i], final_data[i])

        # The html to be shown.
        html = """
            <body id=copy-multiline>
                <style>
                    ul {
                        margin: 0;
                    }

                    a {
                        font-family: system;
                        font-size: 1.05rem;
                    }
                </style>

                <ul>
                    %s
                </ul>
            </body>
        """ %(li_tree)
        self.view.show_popup(html, max_width=512, on_navigate=lambda todo: self.copy_todo(todo))


    def copy_todo(self, todo):
        """ Copies the todo to the clipboard.

        Args:
            todo (str) : The selected todo.

        Returns:
            None.
        """
        sublime.set_clipboard(todo)
        self.view.hide_popup()
        sublime.status_message('Todo copied to clipboard !')

这是插件的演示(这里我将命令绑定到一个键绑定):-

希望这能满足您的要求。