我正在为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))
实际上,当您知道单击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
,供您选择要使用的内容。复制。您必须代替Jsonplaceholder,向Java服务器发送请求以获取字符串列表并相应地修改示例。
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 !')
这里是插件的演示(这里我已将命令绑定到按键绑定):-
希望这符合您的要求。