Permalink
Browse files

add experiment support for a statically positioned HUD as an alternat…

…ive to tooltips
  • Loading branch information...
DisposaBoy committed Nov 20, 2018
1 parent 8c18501 commit 79ef1d7b0ceb70754a6bdd95a8a46fcd0a10f011
@@ -135,7 +135,15 @@
},
{
"keys": ["ctrl+9"],
"command": "gs9o_open"
"command": "gs9o_win_open"
},
{
"keys": ["ctrl+.","ctrl+9"],
"command": "gs9o_win_open"
},
{
"keys": ["ctrl+.","ctrl+0"],
"command": "margo_show_hud"
},
{
"keys": ["ctrl+space"],
@@ -129,7 +129,15 @@
},
{
"keys": ["super+9"],
"command": "gs9o_open"
"command": "gs9o_win_open"
},
{
"keys": ["super+.","super+9"],
"command": "gs9o_win_open"
},
{
"keys": ["super+.","super+0"],
"command": "margo_show_hud"
},
{
"keys": ["shift+space"],
@@ -135,7 +135,15 @@
},
{
"keys": ["ctrl+9"],
"command": "gs9o_open"
"command": "gs9o_win_open"
},
{
"keys": ["ctrl+.","ctrl+9"],
"command": "gs9o_win_open"
},
{
"keys": ["ctrl+.","ctrl+0"],
"command": "margo_show_hud"
},
{
"keys": ["ctrl+space"],
@@ -755,6 +755,7 @@ def tm_path(name):
'doc': 'syntax/GoSublime-GsDoc.sublime-syntax',
'go': 'syntax/GoSublime-Go.sublime-syntax',
'gohtml': 'syntax/GoSublime-HTML.sublime-syntax',
'hud': 'syntax/GoSublime-HUD.sublime-syntax',
}

return 'Packages/GoSublime/%s' % d[name]
@@ -2,7 +2,7 @@
from . import gs, gsq, sh
from .margo_agent import MargoAgent
from .margo_common import OutputLogger, TokenCounter
from .margo_render import render, render_src
from .margo_render import render
from .margo_state import State, actions, client_actions, Config, _view_scope_lang, view_is_9o, MgView
from collections import namedtuple
import glob
@@ -30,7 +30,9 @@ def __init__(self):
client_actions.CmdOutput: self._handle_act_output,
}
self.file_ids = []

self._hud_cache = {}
self.hud_name = 'GoSublime/HUD'
self.hud_id = self.hud_name.replace('/','-').lower()
self._views = {}
self._view_lock = threading.Lock()
self._gopath = ''
@@ -64,7 +66,12 @@ def render(self, rs=None):
gs._mg_override_settings = cfg.override_settings

def _render():
render(view=gs.active_view(), state=self.state, status=self.status)
render(
mg=mg,
view=gs.active_view(),
state=self.state,
status=self.status,
)

if rs:
self._handle_client_actions(rs)
@@ -162,6 +169,52 @@ def _gs_init(self):
mg._ready = True
mg.start()

def _hud_create_panel(self, win):
view = win.create_output_panel(self.hud_name)
win.focus_view(win.active_view())
syntax = gs.tm_path('hud')
settings = view.settings()
if settings.get('syntax') == syntax:
return view

view.set_syntax_file(syntax)
view.set_read_only(True)
opts = {
'line_numbers': False,
'gutter': False,
'margin': 0,
'highlight_line': False,
'rulers': [],
'fold_buttons': False,
'scroll_past_end': False,
}
settings.erase('color_scheme')
for k, v in opts.items():
settings.set(k, v)

return view

def hud_panel(self, win):
wid = win.id()
view, phantoms = self._hud_cache.get(wid) or (None, None)
m = self._hud_cache

if view is None:
view = self._hud_create_panel(win)
m[wid] = (view, phantoms)

if phantoms is None:
phantoms = sublime.PhantomSet(view, self.hud_name)
m[wid] = (view, phantoms)

if len(m) > 1:
wids = [w.id() for w in sublime.windows()]
for id in m.keys():
if id not in wids:
del m[k]

return (view, phantoms)

def view(self, id, view=None):
with self._view_lock:
mgv = self._views.get(id)
@@ -1,19 +1,23 @@
from . import about
from . import _dbg
from . import gs
from . import gspatch
from .margo_state import ViewPathName
import sublime
import webbrowser


STATUS_KEY = '#mg.Status'
STATUS_PFX = ' '
STATUS_SFX = ' '
STATUS_SEP = ' '

def render(view, state, status=[]):
def render(*, mg, view, state, status):
def cb():
_render_tooltips(view, state.tooltips)
_render_status(view, status + state.status)
_render_issues(view, state.issues)
_render_hud(mg, state)

sublime.set_timeout(cb)

@@ -150,3 +154,94 @@ def on_hide():
on_navigate=on_navigate,
on_hide=on_hide
)

def _hud_navigate(*, mg, href):
if href.startswith('https://') or href.startswith('http://'):
webbrowser.open_new_tab(href)

def _render_hud(mg, st):
html = '''
<body id="%s">
<style>
body {
padding: 0.25rem;
}
ul, ol {
padding: 0 0 0 1.1rem;
}
ul, ol, li {
margin: 0;
}
.header{
font-size: 0.6rem;
}
.header, .header a {
color: color(var(--foreground) alpha(0.50))
}
.footer {}
.articles {}
.article, .header {
margin-bottom: 0.5rem;
}
.article-title {
font-size: 1rem;
font-weight: bold;
color: color(var(--foreground) alpha(0.60))
}
.article-content {
font-size: 0.8rem;
}
.spacer {
padding: 0 0.5rem;
}
.highlight {
background-color: color(var(--foreground) alpha(0.10));
}
</style>
<div class="header">
GoSublime/HUD
<span class="spacer">·</span>
<a href="https://margo.sh/cl/%s?_r=gs-hud">margo.sh/cl/%s</a>
</div>
<div class="articles">
%s
</div>
</body>
''' % (
mg.hud_id,
about.VERSION,
about.VERSION,
_render_hud_articles(st.hud.articles),
)

def ren(win):
view, phantoms = mg.hud_panel(win)
phantom = sublime.Phantom(
sublime.Region(view.size()),
html,
sublime.LAYOUT_INLINE,
lambda href: _hud_navigate(mg=mg, href=href)
)
vp = view.viewport_position()
phantoms.update([phantom])
view.set_viewport_position(vp)

for w in sublime.windows():
ren(w)

def _render_hud_article(article):
content = ''.join('''
<li><div class="article-content">%s</div></li>
''' % s for s in article.content)
return '''
<div class="article">
<div class="article-title">%s</div>
<ul>%s</ul>
</div>
''' % (article.title, content)

def _render_hud_articles(articles):
return ''.join((_render_hud_article(a) for a in articles))
@@ -89,6 +89,7 @@ def __init__(self, v={}):
self.tooltips = [Tooltip(t) for t in (v.get('Tooltips') or [])]
self.issues = [Issue(l) for l in (v.get('Issues') or [])]
self.user_cmds = [UserCmd(c) for c in (v.get('UserCmds') or [])]
self.hud = HUD(v=v.get('HUD') or {})

self.client_actions = []
for ca in (v.get('ClientActions') or []):
@@ -231,6 +232,15 @@ def __init__(self, v={}):
self.args = v.get('Args') or []
self.prompts = v.get('Prompts') or []

class HUDArticle(object):
def __init__(self, v={}):
self.title = v.get('Title') or ''
self.content = v.get('Content') or []

class HUD(object):
def __init__(self, v={}):
self.articles = [HUDArticle(v=a) for a in v.get('Articles') or []]

# in testing, we should be able to push 50MiB+ files constantly without noticing a performance problem
# but keep this number low (realistic source files sizes) at least until we optimize things
MAX_VIEW_SIZE = 512 << 10
@@ -200,3 +200,8 @@ def run(self):
if fn:
gs.focus(fn, row=-1, focus_pat='')

class margo_show_hud(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command('show_panel', {'panel': 'output.%s' % mg.hud_name})
self.window.focus_view(self.window.active_view())

@@ -0,0 +1,8 @@
%YAML 1.2
---
name: 'GoSublime: HUD'
hidden: true
scope: text.gosublime-hud
contexts:
main: []

0 comments on commit 79ef1d7

Please sign in to comment.