Skip to content

Commit

Permalink
Add basic tooltip support
Browse files Browse the repository at this point in the history
  • Loading branch information
iRyusa authored and groteworld committed Apr 11, 2017
1 parent 657c425 commit c095ff0
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
"caption": "SublimeLinter: Choose Gutter Theme",
"command": "sublimelinter_choose_gutter_theme"
},
{
"caption": "SublimeLinter: Choose Tooltip Theme",
"command": "sublimelinter_choose_tooltip_theme"
},
{
"caption": "SublimeLinter: Lint This View",
"command": "sublimelinter_lint"
Expand Down
3 changes: 3 additions & 0 deletions SublimeLinter.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"javascript (babel)": "javascript",
"php": "html"
},
"tooltip_fontsize": 16,
"tooltip_theme": "Packages/SublimeLinter/tooltip-themes/Default/Default.tooltip-theme",
"tooltip_theme_excludes": [],
"warning_color": "DDB700",
"wrap_find": true
}
Expand Down
98 changes: 98 additions & 0 deletions commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,104 @@ def transform_setting(self, setting, matching=False):
else:
return setting

@choose_setting_command('tooltip_theme', preview=True)
class SublimelinterChooseTooltipThemeCommand(ChooseSettingCommand):
"""A command that selects a tooltip theme from a list."""

def get_settings(self):
"""
Return a list of all available Tooltip themes, with 'None' at the end.
Whether the theme is colorized and is a SublimeLinter or user theme
is indicated below the theme name.
"""

settings = self.find_tooltip_themes()
settings.append(['None', 'Do not display tooltip'])
self.themes.append('none')

return settings

def find_tooltip_themes(self):
"""
Find all SublimeLinter.tooltip-theme resources.
For each found resource, if it doesn't match one of the patterns
from the "tooltip_theme_excludes" setting, return the base name
of resource and info on whether the theme is a standard theme
or a user theme, as well as whether it is colorized.
The list of paths to the resources is appended to self.themes.
"""

self.themes = []
settings = []
tooltip_themes = sublime.find_resources('*.tooltip-theme')
excludes = persist.settings.get('tooltip_theme_excludes', [])

for theme in tooltip_themes:
exclude = False
parent = os.path.dirname(theme)
htmls = sublime.find_resources('*.html')

if '{}/tooltip.html'.format(parent) not in htmls:
continue

# Now see if the theme name is in tooltip_theme_excludes
name = os.path.splitext(os.path.basename(theme))[0]

for pattern in excludes:
if fnmatch(name, pattern):
exclude = True
break

if exclude:
continue

self.themes.append(theme)

std_theme = theme.startswith('Packages/SublimeLinter/tooltip-themes/')

settings.append([
name,
'SublimeLinter theme' if std_theme else 'User theme'
])

# Sort self.themes and settings in parallel using the zip trick
settings, self.themes = zip(*sorted(zip(settings, self.themes)))

# zip returns tuples, convert back to lists
settings = list(settings)
self.themes = list(self.themes)

return settings

def selected_setting(self, index):
"""Return the theme name with the given index."""
return self.themes[index]

def transform_setting(self, setting, matching=False):
"""
Return a transformed version of setting.
For Tooltip themes, setting is a Packages-relative path
to a .tooltip-theme file.
If matching == False, return the original setting text,
tooltip theme settings are not lowercased.
If matching == True, return the base name of the filename
without the .tooltip-theme extension.
"""

if matching:
return os.path.splitext(os.path.basename(setting))[0]
else:
return setting


class SublimelinterToggleLinterCommand(sublime_plugin.WindowCommand):
"""A command that toggles, enables, or disables linter plugins."""
Expand Down
35 changes: 35 additions & 0 deletions sublimelinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .lint.highlight import HighlightSet
from .lint.queue import queue
from .lint import persist, util
from string import Template


def plugin_loaded():
Expand Down Expand Up @@ -398,13 +399,47 @@ def on_selection_modified_async(self, view):
status = 'Error: '

status += '; '.join(line_errors)
self.open_tooltip(lineno, line_errors)
else:
status = '%i error%s' % (count, plural)
self.close_tooltip()

view.set_status('sublimelinter', status)
else:
view.erase_status('sublimelinter')

def get_active_view(self):
return sublime.active_window().active_view()

def get_template(self):
tooltip_theme = persist.settings.get('tooltip_theme')

if tooltip_theme == 'none':
return False

theme_path = os.path.dirname(tooltip_theme)
template_path = os.path.join(theme_path, 'tooltip.html')
tooltip_text = sublime.load_resource(template_path)

return Template(tooltip_text)

def open_tooltip(self, line, errors):
template = self.get_template()

if not template:
return

active_view = self.get_active_view()
tooltip_content = template.substitute(line=line, message='<br />'.join(errors), font_size=persist.settings.get('tooltip_fontsize'))
active_view.show_popup(tooltip_content,
flags=sublime.HIDE_ON_MOUSE_MOVE_AWAY,
location=-1,
max_width=600)

def close_tooltip(self):
active_view = self.get_active_view()
active_view.hide_popup()

def on_pre_save(self, view):
"""
Called before view is saved.
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions tooltip-themes/Default/tooltip.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<style>
body {
font-size: ${font_size}px;
}
div {
margin: 5px;
word-wrap:break-word;
}
</style>
<div><b>Line ${line}</b></div>
<div style="margin-left: 15px">${message}</div>
Empty file.
10 changes: 10 additions & 0 deletions tooltip-themes/Inline/tooltip.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<style>
body {
font-size: ${font_size}px;
}
div {
margin: 5px;
word-wrap:break-word;
}
</style>
<div><b>Line ${line} : ${message}</b></div>

0 comments on commit c095ff0

Please sign in to comment.