【问题标题】:How to make multi-line pop-up in Sublime Text 3 plugin如何在 Sublime Text 3 插件中制作多行弹出窗口
【发布时间】:2020-04-16 17:35:59
【问题描述】:

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

  • TEXT1 - 复制

  • TEXT2 - 复制

  • TEXT3 - 复制

  • ...

以下是在弹出窗口中显示范围名称的插件代码:

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))

【问题讨论】:

    标签: python sublimetext3 sublime-text-plugin


    【解决方案1】:

    当您知道点击Copy 链接时所复制的内容后,这实际上非常简单。 根据官方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),将其显示为一个列表,每个都有自己的 Copy 供您选择要做什么复制。您必须向 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 !')
    

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

    希望这能满足您的要求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-24
      • 2016-09-27
      • 2019-03-05
      • 2015-06-15
      • 2017-01-26
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      相关资源
      最近更新 更多