Skip to content

Commit c79e0c9

Browse files
author
Orta
authored
Merge pull request #724 from kylebebak/master
Info and error popup content copyable to clipboard
2 parents 1d5160e + ccc4cb9 commit c79e0c9

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

typescript/commands/quick_info.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sublime
2+
13
from ..libs.view_helpers import *
24
from ..libs.text_helpers import escape_html
35
from .base_command import TypeScriptBaseTextCommand
@@ -40,10 +42,11 @@ class TypescriptQuickInfoDoc(TypeScriptBaseTextCommand):
4042
def handle_quick_info(self, quick_info_resp_dict, display_point):
4143
info_str = ""
4244
doc_str = ""
45+
info_text = ""
4346

4447
if quick_info_resp_dict["success"]:
4548
info_str = self.format_display_parts_html(quick_info_resp_dict["body"]["displayParts"])
46-
status_info_str = self.format_display_parts_plain(quick_info_resp_dict["body"]["displayParts"])
49+
info_text = self.format_display_parts_plain(quick_info_resp_dict["body"]["displayParts"])
4750

4851
if "documentation" in quick_info_resp_dict["body"]:
4952
doc_str = self.format_display_parts_html(quick_info_resp_dict["body"]["documentation"])
@@ -58,26 +61,27 @@ def handle_quick_info(self, quick_info_resp_dict, display_point):
5861
)
5962
doc_panel.settings().set('color_scheme', "Packages/Color Scheme - Default/Blackboard.tmTheme")
6063
sublime.active_window().run_command('show_panel', {'panel': 'output.doc'})
61-
status_info_str = info_str + " (^T^Q for more)"
62-
self.view.set_status("typescript_info", status_info_str)
64+
info_text = info_str + " (^T^Q for more)"
65+
self.view.set_status("typescript_info", info_text)
6366

6467
else:
6568
self.view.erase_status("typescript_info")
6669

6770
# Fetch any errors and show tooltips if available
71+
errors = self.get_errors(sublime.Region(display_point, display_point))
6872
error_html = self.get_error_text_html(sublime.Region(display_point, display_point))
6973
if info_str != "" or doc_str != "" or error_html != "":
70-
self.show_tooltip_popup(display_point, error_html, info_str, doc_str)
74+
self.show_tooltip_popup(display_point, errors, error_html, info_text, info_str, doc_str)
7175

72-
def show_tooltip_popup(self, display_point, error, info, doc):
76+
def show_tooltip_popup(self, display_point, errors, error_html: str, info_text: str, info_html: str, doc):
7377
if not TOOLTIP_SUPPORT:
7478
return
7579

7680
theme_styles = get_theme_styles(self.view)
7781

7882
parameters = {
79-
"error": error or '',
80-
"info_str": info or '',
83+
"error": error_html or '',
84+
"info_str": info_html or '',
8185
"doc_str": doc or '',
8286
"typeStyles": theme_styles["type"],
8387
"keywordStyles": theme_styles["keyword"],
@@ -97,25 +101,39 @@ def show_tooltip_popup(self, display_point, error, info, doc):
97101
self.template = Template(load_quickinfo_and_error_popup_template())
98102
html = self.template.substitute(parameters)
99103

104+
def on_navigate(href: str) -> None:
105+
if href == "copy":
106+
copy_text = "\n\n".join(s for s in errors if s)
107+
if info_text:
108+
copy_text = copy_text + "\n\n" + info_text
109+
sublime.set_clipboard(copy_text)
110+
sublime.active_window().status_message("TypeScript: info copied to clipboard")
111+
self.view.hide_popup()
112+
100113
settings = sublime.load_settings("TypeScript.sublime-settings")
101114
self.view.show_popup(
102-
html,
115+
html + "<a href=\"copy\">Copy</a>",
103116
flags=sublime.HIDE_ON_MOUSE_MOVE_AWAY,
104117
location=display_point,
105118
max_height=300,
106-
max_width=settings.get("quick_info_popup_max_width") or self.view.viewport_extent()[0]
119+
max_width=settings.get("quick_info_popup_max_width") or self.view.viewport_extent()[0],
120+
on_navigate=on_navigate,
107121
)
108122

109-
def get_error_text_html(self, span):
123+
def get_errors(self, span):
110124
client_info = cli.get_or_add_file(self.view.file_name())
111125
all_errors = client_info.errors['syntacticDiag'] + client_info.errors['semanticDiag']
112126

113127
errors = []
114128
for (region, text) in all_errors:
115129
if region.intersects(span):
116-
errors.append(escape_html(text))
130+
errors.append(text)
117131

118-
return '<br/>'.join(errors)
132+
return errors
133+
134+
def get_error_text_html(self, span):
135+
errors = self.get_errors(span)
136+
return '<br/>'.join(escape_html(error) for error in errors)
119137

120138
def run(self, text, hover_point=None, hover_zone=None):
121139
check_update_view(self.view)

typescript/libs/popup_manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def get_signature_data(on_done):
6565
if last_command == "insert":
6666
if len(args['characters']) > 1 and '\n' in args['characters']:
6767
reload_buffer(view)
68-
68+
6969
# Send a signagure_help request to server
7070
self.proxy.async_signature_help(filename, point, '', on_done)
7171

@@ -240,7 +240,7 @@ def concat_display_parts(parts, underline_name=False):
240240
def get_current_signature_parts(self):
241241
def encode(str, kind):
242242
return '<br />' if kind == "lineBreak" else str
243-
243+
244244
if self.signature_index == -1:
245245
return ""
246246
if self.signature_index >= len(self.signature_help["items"]):
@@ -259,7 +259,7 @@ def encode(str, kind):
259259
param = item["parameters"][self.current_parameter]
260260
activeParam = '<span class="param">{0}:</span> <i>{1}</i>'.format(
261261
param["name"],
262-
''.join([encode(doc["text"], doc["kind"]) for doc in param["documentation"]])
262+
''.join([encode(doc["text"], doc["kind"]) for doc in param["documentation"]])
263263
if param["documentation"] else "")
264264
else:
265265
activeParam = ''

0 commit comments

Comments
 (0)