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

Don't show a source if diagnostic doesn't have a source #2119

Merged
merged 16 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 22 additions & 24 deletions plugin/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,10 +837,6 @@ def diagnostic_severity(diagnostic: Diagnostic) -> DiagnosticSeverity:
return diagnostic.get("severity", DiagnosticSeverity.Error)


def diagnostic_source(diagnostic: Diagnostic) -> str:
return diagnostic.get("source", "unknown-source")


def format_diagnostic_for_panel(diagnostic: Diagnostic) -> Tuple[str, Optional[int], Optional[str], Optional[str]]:
"""
Turn an LSP diagnostic into a string suitable for an output panel.
Expand All @@ -853,14 +849,15 @@ def format_diagnostic_for_panel(diagnostic: Diagnostic) -> Tuple[str, Optional[i
"""
formatted, code, href = diagnostic_source_and_code(diagnostic)
lines = diagnostic["message"].splitlines() or [""]
# \u200B is the zero-width space
result = " {:>4}:{:<4}{:<8}{} \u200B{}".format(
result = " {:>4}:{:<4}{:<8}{}".format(
diagnostic["range"]["start"]["line"] + 1,
diagnostic["range"]["start"]["character"] + 1,
format_severity(diagnostic_severity(diagnostic)),
lines[0],
formatted
lines[0]
)
if formatted != "" or code is not None:
# \u200B is the zero-width space
result += " \u200B{}".format(formatted)
offset = len(result) if href else None
for line in itertools.islice(lines, 1, None):
result += "\n" + 18 * " " + line
Expand All @@ -871,22 +868,21 @@ def format_diagnostic_source_and_code(diagnostic: Diagnostic) -> str:
formatted, code, href = diagnostic_source_and_code(diagnostic)
if href is None or code is None:
return formatted
return formatted + code
return formatted + "({})".format(code)


def diagnostic_source_and_code(diagnostic: Diagnostic) -> Tuple[str, Optional[str], Optional[str]]:
formatted = [diagnostic_source(diagnostic)]
formatted = diagnostic.get("source", "")
href = None
code = diagnostic.get("code")
if code is not None:
code = str(code)
formatted.append(":")
code_description = diagnostic.get("codeDescription")
if code_description:
href = code_description["href"]
else:
formatted.append(code)
return "".join(formatted), code, href
formatted += "({})".format(code)
return formatted, code, href


def location_to_human_readable(
Expand Down Expand Up @@ -956,10 +952,6 @@ def _with_color(text: Any, hexcolor: str) -> str:
return '<span style="color: {};">{}</span>'.format(hexcolor, text)


def _with_scope_color(view: sublime.View, text: Any, scope: str) -> str:
return _with_color(text, view.style_for_scope(scope)["foreground"])


def format_diagnostic_for_html(
view: sublime.View,
config: ClientConfig,
Expand All @@ -973,16 +965,22 @@ def format_diagnostic_for_html(
text2html(diagnostic["message"])
]
code_description = diagnostic.get("codeDescription")
if code_description:
code = make_link(code_description["href"], diagnostic.get("code")) # type: Optional[str]
elif "code" in diagnostic:
code = _with_color(diagnostic["code"], "color(var(--foreground) alpha(0.6))")
if "code" in diagnostic:
code = [_with_color("(", "color(var(--foreground) alpha(0.6))")]
if code_description:
code.append(make_link(code_description["href"], diagnostic.get("code")))
else:
code.append(_with_color(diagnostic["code"], "color(var(--foreground) alpha(0.6))"))
code.append(_with_color(")", "color(var(--foreground) alpha(0.6))"))
else:
code = None
source = diagnostic_source(diagnostic)
formatted.extend((" ", _with_color(source, "color(var(--foreground) alpha(0.6))")))
source = diagnostic.get("source")
if source or code:
formatted.append(" ")
if source:
formatted.append(_with_color(source, "color(var(--foreground) alpha(0.6))"))
if code:
formatted.extend((_with_scope_color(view, ":", "punctuation.separator.lsp"), code))
Sainan marked this conversation as resolved.
Show resolved Hide resolved
formatted.extend(code)
related_infos = diagnostic.get("relatedInformation")
if related_infos:
formatted.append('<pre class="related_info">')
Expand Down
2 changes: 1 addition & 1 deletion plugin/core/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ def _update_panel_main_thread(self, characters: str, prephantoms: List[Tuple[int
for row, col, code, href in prephantoms:
point = panel.text_point(row, col)
region = sublime.Region(point, point)
phantoms.append(sublime.Phantom(region, make_link(href, code), sublime.LAYOUT_INLINE))
phantoms.append(sublime.Phantom(region, "({})".format(make_link(href, code)), sublime.LAYOUT_INLINE))
self._panel_code_phantoms.update(phantoms)


Expand Down