-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsupport.py
More file actions
59 lines (45 loc) · 1.75 KB
/
support.py
File metadata and controls
59 lines (45 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sublime
import sublime_plugin
from os.path import expandvars
def get_file_location():
settings = sublime.load_settings('Hosts.sublime-settings')
user_setting = settings.get('file_location')
if user_setting:
return expandvars(user_setting)
return expandvars(settings.get(
'default_file_locations')[sublime.platform()])
class OpenHostsFileCommand(sublime_plugin.TextCommand):
def run(self, edit):
window = self.view.window()
if not window:
print('Missing window for view')
return
view = window.open_file(get_file_location())
class HostsFileViewListener(sublime_plugin.ViewEventListener):
SYNTAX = 'hosts.sublime-syntax'
@classmethod
def is_applicable(cls, settings):
try:
return (settings and
settings.get('syntax', '').lower().endswith(cls.SYNTAX))
except Exception as e:
return False
def on_hover(self, point, hover_zone):
if ((hover_zone != sublime.HOVER_TEXT or not
self.view.match_selector(point, 'meta.hostname meta.punycode'))):
return
expression_region = next(
r for r in self.view.find_by_selector('meta.hostname')
if r.contains(point))
hostname = self.view.substr(expression_region)
try:
hover_text = str.encode(hostname).decode('idna')
except Exception as e:
hover_text = 'Could not parse Punycode expression'
html = '''
<body id="render-punycode">
<div>{}</div>
</body>
'''.format(hover_text)
self.view.show_popup(html, sublime.HIDE_ON_MOUSE_MOVE_AWAY,
location=point, max_width=512, max_height=60)