Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve whitespace in diagnostics popup #168

Closed
wants to merge 13 commits into from
65 changes: 42 additions & 23 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@

window_client_configs = dict() # type: Dict[int, List[ClientConfig]]

popup_css = '''
.lsp_popup {
margin: 0.5rem 0.5rem 0 0.5rem;
}
.lsp_popup .highlight {
border-width: 0;
border-radius: 0;
}
.lsp_popup p {
margin-bottom: 0.5rem;
padding: 0 0.5rem;
font-family: system;
}
'''


class DiagnosticSeverity(object):
Error = 1
Expand Down Expand Up @@ -1050,9 +1065,6 @@ def run(self):
Visit [langserver.org](https://langserver.org) to find out if a language server exists for this language."""


setup_css = ".mdpopups .lsp_documentation { margin: 20px; font-family: sans-serif; font-size: 1.2rem; line-height: 2}"


class LspSetupLanguageServerCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.active_view()
Expand All @@ -1071,7 +1083,16 @@ def run(self):
mdpopups.show_popup(
view,
"\n".join([title, content]),
css=setup_css,
css='''
.lsp_documentation {
margin: 1rem 1rem 0.5rem 1rem;
font-family: system;
}
.lsp_documentation h1,
.lsp_documentation p {
margin: 0 0 0.5rem 0;
}
''',
md=True,
wrapper_class="lsp_documentation",
max_width=800,
Expand Down Expand Up @@ -2025,11 +2046,11 @@ def show_diagnostics_hover(self, point, diagnostics):
mdpopups.show_popup(
self.view,
"\n".join(formatted),
css=".mdpopups .lsp_hover { margin: 4px; }",
css=popup_css,
md=True,
flags=sublime.HIDE_ON_MOUSE_MOVE_AWAY,
location=point,
wrapper_class="lsp_hover",
wrapper_class="lsp_popup",
max_width=800,
on_navigate=lambda href: self.on_diagnostics_navigate(href, point, diagnostics))

Expand All @@ -2055,28 +2076,29 @@ def show_hover(self, point, contents):
else:
value = item.get("value")
language = item.get("language")

if language:
formatted.append("```{}\n{}\n```".format(language, value))
formatted.append("```{}\n{}\n```\n".format(language, value))
else:
formatted.append(value)
formatted.append(preserve_whitespace(value))

mdpopups.show_popup(
self.view,
preserve_whitespace("\n".join(formatted)),
css=".mdpopups .lsp_hover { margin: 4px; } .mdpopups p { margin: 0.1rem; }",
"\n".join(formatted),
css=popup_css,
md=True,
flags=sublime.HIDE_ON_MOUSE_MOVE_AWAY,
location=point,
wrapper_class="lsp_hover",
wrapper_class="lsp_popup",
max_width=800)


def preserve_whitespace(contents: str) -> str:
"""Preserve empty lines and whitespace for markdown conversion."""
contents = contents.strip(' \t\r\n')
contents = contents.replace('\t', ' ' * 4)
contents = contents.replace(' ', ' ' * 2)
contents = contents.replace('\n\n', '\n \n')
contents = contents.replace('\r\n', '\n')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which language-server returns \r\n?

Copy link
Member

@rwols rwols Oct 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be all of them, according to protocol?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsts in my case at least

contents = contents.replace('\t', '\u00A0' * 4)
contents = contents.replace(' ', '\u00A0' * 2)
return contents


Expand Down Expand Up @@ -2298,9 +2320,6 @@ def run_auto_complete(self):

class SignatureHelpListener(sublime_plugin.ViewEventListener):

css = ".mdpopups .lsp_signature { margin: 4px; } .mdpopups p { margin: 0.1rem; }"
wrapper_class = "lsp_signature"

def __init__(self, view):
self.view = view
self._initialized = False
Expand Down Expand Up @@ -2370,11 +2389,11 @@ def handle_response(self, response, point):
if len(self._signatures) > 0:
mdpopups.show_popup(self.view,
self._build_popup_content(),
css=self.__class__.css,
css=popup_css,
md=True,
flags=sublime.HIDE_ON_MOUSE_MOVE_AWAY,
location=point,
wrapper_class=self.__class__.wrapper_class,
wrapper_class="lsp_popup",
max_width=800,
on_hide=self._on_hide)
self._visible = True
Expand All @@ -2398,9 +2417,9 @@ def on_query_context(self, key, _, operand, __):
self._active_signature = new_index
mdpopups.update_popup(self.view,
self._build_popup_content(),
css=self.__class__.css,
css=popup_css,
md=True,
wrapper_class=self.__class__.wrapper_class)
wrapper_class="lsp_popup")

return True # We handled this keybinding.

Expand All @@ -2412,11 +2431,11 @@ def _build_popup_content(self) -> str:
formatted = []

if len(self._signatures) > 1:
signature_navigation = "**{}** of **{}** overloads (use the arrow keys to navigate):\n".format(
signature_navigation = "**{}** of **{}** overloads (use the ↑ ↓ keys to navigate):\n".format(
str(self._active_signature + 1), str(len(self._signatures)))
formatted.append(signature_navigation)

label = "```{}\n{}\n```".format(self._language_id, signature.get('label'))
label = "```{}\n{}\n```\n".format(self._language_id, signature.get('label'))
formatted.append(label)

params = signature.get('parameters')
Expand Down