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

Allow customizing diagnostics styles #1856

Merged
merged 8 commits into from Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions LSP.sublime-settings
Expand Up @@ -85,6 +85,22 @@
// When set to the empty string (""), no document highlighting is requested.
"document_highlight_style": "underline",

// Highlight style of code diagnostics.
// Can be a string, or a mapping of string->string for severity-based styling.
// Valid string values are "box", "underline", "stippled", "squiggly" or "".
// When set to a valid string value, all severities will use that style.
// When set to the empty string (""), no diagnostics are drawn.
// When disabled, gutter markers are still drawn, unless "diagnostics_gutter_marker"
// is set to "".
// Diagnostics which span multiple lines are always drawn with the "box" style.
// See also: "show_multiline_diagnostics_highlights".
"diagnostics_highlight_style": {
"error": "squiggly",
"warning": "squiggly",
"info": "stippled",
"hint": "stippled"
},

// Gutter marker for code diagnostics.
// Valid values are "dot", "circle", "bookmark", "sign" or ""
"diagnostics_gutter_marker": "dot",
Expand All @@ -105,10 +121,9 @@
// Show code actions in hover popup if available
"show_code_actions_in_hover": true,

// Show diagnostics (error/warning squigglies) in the view. When disabled,
// gutter markers are still drawn, unless "diagnostics_gutter_marker" is
// set to "".
"show_diagnostics_highlights": true,
// Show diagnostics spanning multiple lines in the view (as outlines).
// See also: "diagnostics_highlight_style".
"show_multiline_diagnostics_highlights": true,

// Show symbol action links in hover popup if available
"show_symbol_action_links": true,
Expand Down
53 changes: 45 additions & 8 deletions plugin/core/types.py
Expand Up @@ -187,6 +187,7 @@ class Settings:
diagnostics_additional_delay_auto_complete_ms = None # type: int
diagnostics_delay_ms = None # type: int
diagnostics_gutter_marker = None # type: str
diagnostics_highlight_style = None # type: Union[str, Dict[str, str]]
iamkroot marked this conversation as resolved.
Show resolved Hide resolved
diagnostics_panel_include_severity_level = None # type: int
disabled_capabilities = None # type: List[str]
document_highlight_style = None # type: str
Expand All @@ -205,7 +206,7 @@ class Settings:
show_code_lens = None # type: str
show_code_actions_in_hover = None # type: bool
show_diagnostics_count_in_view_status = None # type: bool
show_diagnostics_highlights = None # type: bool
show_multiline_diagnostics_highlights = None # type: bool
show_diagnostics_in_view_status = None # type: bool
show_diagnostics_panel_on_save = None # type: int
show_diagnostics_severity_level = None # type: int
Expand Down Expand Up @@ -241,7 +242,7 @@ def r(name: str, default: Union[bool, int, str, list, dict]) -> None:
r("show_code_actions_in_hover", True)
r("show_diagnostics_count_in_view_status", False)
r("show_diagnostics_in_view_status", True)
r("show_diagnostics_highlights", True)
r("show_multiline_diagnostics_highlights", True)
r("show_diagnostics_panel_on_save", 2)
r("show_diagnostics_severity_level", 2)
r("show_references_in_quick_panel", False)
Expand Down Expand Up @@ -275,11 +276,12 @@ def r(name: str, default: Union[bool, int, str, list, dict]) -> None:
r("inhibit_snippet_completions", False)
r("inhibit_word_completions", True)

# Backwards-compatible with "diagnostics_highlight_style"
diagnostics_highlight_style = s.get("diagnostics_highlight_style")
if isinstance(diagnostics_highlight_style, str):
if not diagnostics_highlight_style:
self.show_diagnostics_highlights = False
# correctness checking will happen inside diagnostics_highlight_style_flags method
self.diagnostics_highlight_style = s.get("diagnostics_highlight_style") # type: ignore

# Backwards-compatible with "show_diagnostics_highlights"
if s.get("show_diagnostics_highlights") is False:
self.diagnostics_highlight_style = ""

# Backwards-compatible with "code_action_on_save_timeout_ms"
code_action_on_save_timeout_ms = s.get("code_action_on_save_timeout_ms")
Expand All @@ -294,7 +296,42 @@ def document_highlight_style_region_flags(self) -> Tuple[int, int]:
elif self.document_highlight_style == "stippled":
return sublime.DRAW_NO_FILL, sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_STIPPLED_UNDERLINE # noqa: E501
else:
return sublime.DRAW_NO_FILL, sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_SOLID_UNDERLINE
return sublime.DRAW_NO_FILL, sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_SOLID_UNDERLINE # noqa: E501

@staticmethod
def _style_str_to_flag(style_str: str) -> Optional[int]:
# This method could be a dict or lru_cache
iamkroot marked this conversation as resolved.
Show resolved Hide resolved
if style_str == "":
return sublime.DRAW_EMPTY_AS_OVERWRITE | sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE
elif style_str == "box":
return sublime.DRAW_EMPTY_AS_OVERWRITE | sublime.DRAW_NO_FILL
elif style_str == "underline":
return sublime.DRAW_EMPTY_AS_OVERWRITE | sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_SOLID_UNDERLINE # noqa: E501
elif style_str == "stippled":
return sublime.DRAW_EMPTY_AS_OVERWRITE | sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_STIPPLED_UNDERLINE # noqa: E501
elif style_str == "squiggly":
return sublime.DRAW_EMPTY_AS_OVERWRITE | sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_SQUIGGLY_UNDERLINE # noqa: E501
else:
# default style
return None

def diagnostics_highlight_style_flags(self) -> List[Optional[int]]:
"""Returns flags for highlighting diagnostics on single lines per severity"""
if isinstance(self.diagnostics_highlight_style, str):
# same style for all severity levels
return [self._style_str_to_flag(self.diagnostics_highlight_style)] * 4
elif isinstance(self.diagnostics_highlight_style, dict):
flags = [] # type: List[Optional[int]]
for sev in ("error", "warning", "info", "hint"):
user_style = self.diagnostics_highlight_style.get(sev)
if user_style is None: # user did not provide a style
flags.append(None) # default styling, see comment below
else:
flags.append(self._style_str_to_flag(user_style))
return flags
else:
# Defaults are defined in DIAGNOSTIC_SEVERITY in plugin/core/views.py
return [None] * 4 # default styling


class ClientStates:
Expand Down
7 changes: 4 additions & 3 deletions plugin/session_view.py
Expand Up @@ -202,11 +202,12 @@ def diagnostics_tag_scope(self, tag: int) -> Optional[str]:
return None

def present_diagnostics_async(self) -> None:
flags = 0 if userprefs().show_diagnostics_highlights else sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE
flags = userprefs().diagnostics_highlight_style_flags() # for single lines
multiline_flags = None if userprefs().show_multiline_diagnostics_highlights else sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE # noqa: E501
level = userprefs().show_diagnostics_severity_level
for sev in reversed(range(1, len(DIAGNOSTIC_SEVERITY) + 1)):
self._draw_diagnostics(sev, level, DIAGNOSTIC_SEVERITY[sev - 1][4] if flags == 0 else flags, False)
self._draw_diagnostics(sev, level, DIAGNOSTIC_SEVERITY[sev - 1][5] if flags == 0 else flags, True)
self._draw_diagnostics(sev, level, flags[sev - 1] or DIAGNOSTIC_SEVERITY[sev - 1][4], False)
self._draw_diagnostics(sev, level, multiline_flags or DIAGNOSTIC_SEVERITY[sev - 1][5], True)
listener = self.listener()
if listener:
listener.on_diagnostics_updated_async()
Expand Down
42 changes: 39 additions & 3 deletions sublime-package.json
Expand Up @@ -312,8 +312,40 @@
"markdownDescription": "Add an additional delay when the auto-complete widget is currently visible. Just like `\"diagnostics_delay_ms\"`, the unit is milliseconds. The total amount of delay would be `diagnostics_delay_ms + diagnostics_additional_delay_auto_complete_ms`."
},
"diagnostics_highlight_style": {
"enum": [],
"deprecationMessage": "Use \"show_diagnostics_highlights\" instead",
"oneOf": [
{
"enum": ["box", "underline", "stippled", "squiggly", ""],
},
{
"type": "object",
"properties": {
"error": {
"enum": ["box", "underline", "stippled", "squiggly", ""],
"default": "squiggly"
},
"warning": {
"enum": ["box", "underline", "stippled", "squiggly", ""],
"default": "squiggly"
},
"info": {
"enum": ["box", "underline", "stippled", "squiggly", ""],
"default": "stippled"
},
"hint": {
"enum": ["box", "underline", "stippled", "squiggly", ""],
"default": "stippled"
},
},
"additionalProperties": false
}
],
"default": {
"error": "squiggly",
"warning": "squiggly",
"info": "stippled",
"hint": "stippled"
},
"markdownDescription": "Highlight style of code diagnostics.\nCan be a string, or a mapping of string->string for severity-based styling.\nValid string values are `\"box\"`, `\"underline\"`, `\"stippled\"`, `\"squiggly\"` or the empty string (`\"\"`).\nWhen set to a valid string value, all severities will use that style.\nWhen set to the empty string (`\"\"`), no diagnostics are drawn.\nWhen disabled, gutter markers are still drawn, unless `\"diagnostics_gutter_marker\"` is set to `\"\"`.\nDiagnostics which span multiple lines are always drawn with the `box` style (See `\"show_multiline_diagnostics_highlights\"`)."
},
"document_highlight_style": {
"enum": [
Expand Down Expand Up @@ -368,9 +400,13 @@
"markdownDescription": "Show code actions in hover popup if available."
},
"show_diagnostics_highlights": {
"type": "boolean",
"deprecationMessage": "Use \"diagnostics_highlight_style\" instead",
},
"show_multiline_diagnostics_highlights": {
"type": "boolean",
"default": true,
"markdownDescription": "Show diagnostics (error/warning squigglies) in the view. When disabled, gutter markers are still drawn, unless `\"diagnostics_gutter_marker\"` is set to `\"\"`."
"markdownDescription": "Show diagnostics spanning multiline diagnostics (as outlines) in the view. See also: `\"diagnostics_highlight_style\"`."
},
"show_symbol_action_links": {
"type": "boolean",
Expand Down