Skip to content

Commit

Permalink
Merge branch 'main' into fix/sync-on-open
Browse files Browse the repository at this point in the history
* main:
  add NO_UNDO flags to all regions (#2370)
  Fix some mypy type issues in stubs (#2374)
  • Loading branch information
rchl committed Dec 13, 2023
2 parents c359c67 + 97b5558 commit 22deb1e
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 66 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ jobs:
python-version: '3.8'
- run: sudo apt update
- run: sudo apt install --no-install-recommends -y x11-xserver-utils
- run: pip3 install mypy==0.971 flake8==5.0.4 pyright==1.1.335 yapf==0.31.0 --user
- run: pip3 install mypy==1.7.1 flake8==5.0.4 pyright==1.1.339 --user
- run: echo "$HOME/.local/bin" >> $GITHUB_PATH
# - run: mypy -p plugin
- run: mypy stubs
- run: flake8 plugin tests
- run: pyright plugin
3 changes: 2 additions & 1 deletion plugin/code_lens.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,11 @@ def render(self, mode: str) -> None:
self._phantom.update(phantoms)
else: # 'annotation'
self._clear_annotations()
flags = sublime.NO_UNDO
accent = self.view.style_for_scope("region.greenish markup.accent.codelens.lsp")["foreground"]
for index, lens in enumerate(self._flat_iteration()):
self.view.add_regions(
self._region_key(lens.session_name, index), [lens.region], "", "", 0, [lens.small_html], accent)
self._region_key(lens.session_name, index), [lens.region], "", "", flags, [lens.small_html], accent)

def get_resolved_code_lenses_for_region(self, region: sublime.Region) -> Generator[CodeLensExtended, None, None]:
region = self.view.line(region)
Expand Down
5 changes: 5 additions & 0 deletions plugin/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
HOVER_PROVIDER_COUNT_KEY = 'lsp_hover_provider_count'
SHOW_DEFINITIONS_KEY = 'show_definitions'

# Region flags
DOCUMENT_LINK_FLAGS = sublime.HIDE_ON_MINIMAP | sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_SOLID_UNDERLINE | sublime.NO_UNDO # noqa: E501
REGIONS_INITIALIZE_FLAGS = sublime.HIDDEN | sublime.NO_UNDO
SEMANTIC_TOKEN_FLAGS = sublime.DRAW_NO_OUTLINE | sublime.NO_UNDO

# sublime.Kind tuples for sublime.CompletionItem, sublime.QuickPanelItem, sublime.ListInputItem
# https://www.sublimetext.com/docs/api_reference.html#sublime.Kind
KIND_ARRAY = (sublime.KIND_ID_TYPE, "a", "Array")
Expand Down
40 changes: 21 additions & 19 deletions plugin/core/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,31 +320,33 @@ def r(name: str, default: Union[bool, int, str, list, dict]) -> None:
set_debug_logging(self.log_debug)

def highlight_style_region_flags(self, style_str: str) -> Tuple[int, int]:
default = sublime.NO_UNDO
if style_str in ("background", "fill"): # Backwards-compatible with "fill"
return sublime.DRAW_NO_OUTLINE, sublime.DRAW_NO_OUTLINE
elif style_str == "outline":
return sublime.DRAW_NO_FILL, sublime.DRAW_NO_FILL
elif style_str == "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 # noqa: E501
style = default | sublime.DRAW_NO_OUTLINE
return style, style
if style_str == "outline":
style = default | sublime.DRAW_NO_FILL
return style, style
if style_str == "stippled":
return default | sublime.DRAW_NO_FILL, default | sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_STIPPLED_UNDERLINE # noqa: E501
return default | sublime.DRAW_NO_FILL, default | 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]:
default = sublime.DRAW_EMPTY_AS_OVERWRITE | sublime.DRAW_NO_FILL | sublime.NO_UNDO
# This method could be a dict or lru_cache
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
return default | sublime.DRAW_NO_OUTLINE
if style_str == "box":
return default
if style_str == "underline":
return default | sublime.DRAW_NO_OUTLINE | sublime.DRAW_SOLID_UNDERLINE
if style_str == "stippled":
return default | sublime.DRAW_NO_OUTLINE | sublime.DRAW_STIPPLED_UNDERLINE
if style_str == "squiggly":
return default | sublime.DRAW_NO_OUTLINE | sublime.DRAW_SQUIGGLY_UNDERLINE
# default style (includes NO_UNDO)
return None

def diagnostics_highlight_style_flags(self) -> List[Optional[int]]:
"""Returns flags for highlighting diagnostics on single lines per severity"""
Expand Down
13 changes: 6 additions & 7 deletions plugin/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@

MarkdownLangMap = Dict[str, Tuple[Tuple[str, ...], Tuple[str, ...]]]

DOCUMENT_LINK_FLAGS = sublime.HIDE_ON_MINIMAP | sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_SOLID_UNDERLINE # noqa: E501

_baseflags = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_EMPTY_AS_OVERWRITE
_baseflags = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.DRAW_EMPTY_AS_OVERWRITE | sublime.NO_UNDO
_multilineflags = sublime.DRAW_NO_FILL | sublime.NO_UNDO

DIAGNOSTIC_SEVERITY = [
# Kind CSS class Scope for color Icon resource add_regions flags for single-line diagnostic multi-line diagnostic # noqa: E501
("error", "errors", "region.redish markup.error.lsp", "Packages/LSP/icons/error.png", _baseflags | sublime.DRAW_SQUIGGLY_UNDERLINE, sublime.DRAW_NO_FILL), # noqa: E501
("warning", "warnings", "region.yellowish markup.warning.lsp", "Packages/LSP/icons/warning.png", _baseflags | sublime.DRAW_SQUIGGLY_UNDERLINE, sublime.DRAW_NO_FILL), # noqa: E501
("info", "info", "region.bluish markup.info.lsp", "Packages/LSP/icons/info.png", _baseflags | sublime.DRAW_STIPPLED_UNDERLINE, sublime.DRAW_NO_FILL), # noqa: E501
("hint", "hints", "region.bluish markup.info.hint.lsp", "", _baseflags | sublime.DRAW_STIPPLED_UNDERLINE, sublime.DRAW_NO_FILL), # noqa: E501
("error", "errors", "region.redish markup.error.lsp", "Packages/LSP/icons/error.png", _baseflags | sublime.DRAW_SQUIGGLY_UNDERLINE, _multilineflags), # noqa: E501
("warning", "warnings", "region.yellowish markup.warning.lsp", "Packages/LSP/icons/warning.png", _baseflags | sublime.DRAW_SQUIGGLY_UNDERLINE, _multilineflags), # noqa: E501
("info", "info", "region.bluish markup.info.lsp", "Packages/LSP/icons/info.png", _baseflags | sublime.DRAW_STIPPLED_UNDERLINE, _multilineflags), # noqa: E501
("hint", "hints", "region.bluish markup.info.hint.lsp", "", _baseflags | sublime.DRAW_STIPPLED_UNDERLINE, _multilineflags), # noqa: E501
] # type: List[Tuple[str, str, str, str, int, int]]


Expand Down
5 changes: 3 additions & 2 deletions plugin/diagnostics.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .core.constants import DIAGNOSTIC_KINDS
from .core.constants import REGIONS_INITIALIZE_FLAGS
from .core.protocol import Diagnostic
from .core.protocol import DiagnosticSeverity
from .core.settings import userprefs
Expand All @@ -17,13 +18,13 @@ def __init__(self, view: sublime.View, config_name: str) -> None:
def initialize_region_keys(self) -> None:
r = [sublime.Region(0, 0)]
for severity in DIAGNOSTIC_KINDS.keys():
self._view.add_regions(self._annotation_region_key(severity), r)
self._view.add_regions(self._annotation_region_key(severity), r, flags=REGIONS_INITIALIZE_FLAGS)

def _annotation_region_key(self, severity: DiagnosticSeverity) -> str:
return 'lsp_da-{}-{}'.format(severity, self._config_name)

def draw(self, diagnostics: List[Tuple[Diagnostic, sublime.Region]]) -> None:
flags = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE
flags = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.NO_UNDO
max_severity_level = userprefs().show_diagnostics_annotations_severity_level
# To achieve the correct order of annotations (most severe having priority) we have to add regions from the
# most to the least severe.
Expand Down
2 changes: 1 addition & 1 deletion plugin/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ def _on_code_actions(self, responses: List[CodeActionsByConfigName]) -> None:
regions = [sublime.Region(region.b, region.a)]
scope = ""
icon = ""
flags = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE
flags = sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.NO_UNDO
annotations = []
annotation_color = ""
if userprefs().show_code_actions == 'bulb':
Expand Down
2 changes: 1 addition & 1 deletion plugin/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def _show_references_in_output_panel(self, word: str, session: Session, location
})
# highlight all word occurrences
regions = panel.find_all(r"\b{}\b".format(word))
panel.add_regions('ReferenceHighlight', regions, 'comment', flags=sublime.DRAW_NO_FILL)
panel.add_regions('ReferenceHighlight', regions, 'comment', flags=sublime.DRAW_NO_FILL | sublime.NO_UNDO)


def _get_relative_path(base_dir: Optional[str], file_path: str) -> str:
Expand Down
5 changes: 3 additions & 2 deletions plugin/session_buffer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .core.constants import DOCUMENT_LINK_FLAGS
from .core.constants import SEMANTIC_TOKEN_FLAGS
from .core.protocol import ColorInformation
from .core.protocol import Diagnostic
from .core.protocol import DocumentDiagnosticParams
Expand Down Expand Up @@ -34,7 +36,6 @@
from .core.views import did_open
from .core.views import did_save
from .core.views import document_color_params
from .core.views import DOCUMENT_LINK_FLAGS
from .core.views import entire_content_range
from .core.views import lsp_color_to_phantom
from .core.views import MissingUriError
Expand Down Expand Up @@ -673,7 +674,7 @@ def _draw_semantic_tokens_async(self) -> None:
if region_key not in self.semantic_tokens.active_region_keys:
self.semantic_tokens.active_region_keys.add(region_key)
for sv in self.session_views:
sv.view.add_regions("lsp_semantic_{}".format(region_key), regions, scope, flags=sublime.DRAW_NO_OUTLINE)
sv.view.add_regions("lsp_semantic_{}".format(region_key), regions, scope, flags=SEMANTIC_TOKEN_FLAGS)

def _get_semantic_region_key_for_scope(self, scope: str) -> int:
if scope not in self._semantic_region_keys:
Expand Down
29 changes: 17 additions & 12 deletions plugin/session_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .core.constants import HOVER_ENABLED_KEY
from .core.constants import HOVER_HIGHLIGHT_KEY
from .core.constants import HOVER_PROVIDER_COUNT_KEY
from .core.constants import REGIONS_INITIALIZE_FLAGS
from .core.constants import SHOW_DEFINITIONS_KEY
from .core.promise import Promise
from .core.protocol import CodeLens
Expand Down Expand Up @@ -133,36 +134,39 @@ def _initialize_region_keys(self) -> None:
- gutter icons from region keys which were initialized _first_ are drawn
For more context, see https://github.com/sublimelsp/LSP/issues/1593.
"""
keys = [] # type: List[str]
r = [sublime.Region(0, 0)]
document_highlight_style = userprefs().document_highlight_style
hover_highlight_style = userprefs().hover_highlight_style
line_modes = ["m", "s"]
self.view.add_regions(self.CODE_ACTIONS_KEY, r) # code actions lightbulb icon should always be on top
for key in range(1, 100):
self.view.add_regions("lsp_semantic_{}".format(key), r)
keys.append("lsp_semantic_{}".format(key))
if document_highlight_style in ("background", "fill"):
for kind in DOCUMENT_HIGHLIGHT_KIND_NAMES.values():
for mode in line_modes:
self.view.add_regions("lsp_highlight_{}{}".format(kind, mode), r)
keys.append("lsp_highlight_{}{}".format(kind, mode))
if hover_highlight_style in ("background", "fill"):
self.view.add_regions(HOVER_HIGHLIGHT_KEY, r)
keys.append(HOVER_HIGHLIGHT_KEY)
for severity in range(1, 5):
for mode in line_modes:
for tag in range(1, 3):
self.view.add_regions("lsp{}d{}{}_tags_{}".format(self.session.config.name, mode, severity, tag), r)
self.view.add_regions("lsp_document_link", r)
keys.append("lsp{}d{}{}_tags_{}".format(self.session.config.name, mode, severity, tag))
keys.append("lsp_document_link")
for severity in range(1, 5):
for mode in line_modes:
self.view.add_regions("lsp{}d{}{}_icon".format(self.session.config.name, mode, severity), r)
keys.append("lsp{}d{}{}_icon".format(self.session.config.name, mode, severity))
for severity in range(4, 0, -1):
for mode in line_modes:
self.view.add_regions("lsp{}d{}{}_underline".format(self.session.config.name, mode, severity), r)
keys.append("lsp{}d{}{}_underline".format(self.session.config.name, mode, severity))
if document_highlight_style in ("underline", "stippled"):
for kind in DOCUMENT_HIGHLIGHT_KIND_NAMES.values():
for mode in line_modes:
self.view.add_regions("lsp_highlight_{}{}".format(kind, mode), r)
keys.append("lsp_highlight_{}{}".format(kind, mode))
if hover_highlight_style in ("underline", "stippled"):
self.view.add_regions(HOVER_HIGHLIGHT_KEY, r)
keys.append(HOVER_HIGHLIGHT_KEY)
for key in keys:
self.view.add_regions(key, r, flags=REGIONS_INITIALIZE_FLAGS)
self._diagnostic_annotations.initialize_region_keys()

def _clear_auto_complete_triggers(self, settings: sublime.Settings) -> None:
Expand Down Expand Up @@ -300,7 +304,7 @@ def present_diagnostics_async(
self, is_view_visible: bool, data_per_severity: Dict[Tuple[int, bool], DiagnosticSeverityData]
) -> None:
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
multiline_flags = None if userprefs().show_multiline_diagnostics_highlights else sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.NO_UNDO # noqa: E501
level = userprefs().show_diagnostics_severity_level
for sev in reversed(range(1, len(DIAGNOSTIC_SEVERITY) + 1)):
self._draw_diagnostics(
Expand All @@ -320,7 +324,7 @@ def _draw_diagnostics(
flags: int,
multiline: bool
) -> None:
ICON_FLAGS = sublime.HIDE_ON_MINIMAP | sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE
ICON_FLAGS = sublime.HIDE_ON_MINIMAP | sublime.DRAW_NO_FILL | sublime.DRAW_NO_OUTLINE | sublime.NO_UNDO
key = self.diagnostics_key(severity, multiline)
tags = {tag: TagData('{}_tags_{}'.format(key, tag)) for tag in DIAGNOSTIC_TAG_VALUES}
data = data_per_severity.get((severity, multiline))
Expand All @@ -341,7 +345,8 @@ def _draw_diagnostics(
self.view.erase_regions("{}_underline".format(key))
for data in tags.values():
if data.regions:
self.view.add_regions(data.key, data.regions, data.scope, flags=sublime.DRAW_NO_OUTLINE)
self.view.add_regions(
data.key, data.regions, data.scope, flags=sublime.DRAW_NO_OUTLINE | sublime.NO_UNDO)
else:
self.view.erase_regions(data.key)

Expand Down
1 change: 1 addition & 0 deletions pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"pythonVersion": "3.11",
"reportMissingModuleSource": "none",
"reportIncompatibleMethodOverride": "none",
"stubPath": "./stubs",
}
22 changes: 11 additions & 11 deletions stubs/sublime.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ class Window:
"""
...

def hwnd(self):
def hwnd(self) -> int:
"""
A platform specific window handle. Windows only.
"""
Expand Down Expand Up @@ -944,14 +944,14 @@ class Window:
"""
...

def layout(self):
def layout(self) -> Dict[str, Any]:
"""
Get the group layout of the window.
"""
...

@deprecated("Use layout() instead")
def get_layout(self):
def get_layout(self) -> Dict[str, Any]:
...

def set_layout(self, layout: Dict[str, Any]) -> None:
Expand Down Expand Up @@ -1145,7 +1145,7 @@ class Window:
"""
...

def template_settings(self):
def template_settings(self) -> Settings:
"""
Per-window settings that are persisted in the session, and duplicated into new windows.
"""
Expand Down Expand Up @@ -1349,7 +1349,7 @@ class TextChange:
str: str
"""A string of the new contents of the region specified by `a` and `b`."""

def __init__(self, pa: HistoricPosition, pb: HistoricPosition, len_utf16: int, len_utf8: int, s: str) -> None:
def __init__(self, pa: HistoricPosition, pb: HistoricPosition, len_utf16: int, len_utf8: int, s: str) -> None: # type: ignore[valid-type] # noqa
...


Expand Down Expand Up @@ -1388,7 +1388,7 @@ class Selection(Reversible):
"""
...

def __eq__(self, rhs: Selection | None) -> bool:
def __eq__(self, rhs: Selection | None) -> bool: # type: ignore[override]
"""
Whether the selections are identical.
"""
Expand Down Expand Up @@ -1929,7 +1929,7 @@ class View:
"""
...

def style(self):
def style(self) -> Dict[str, str]:
"""
The global style settings for the view. All colors are normalized to the six character hex form with a leading
hash, e.g. `#ff0000`.
Expand Down Expand Up @@ -2462,7 +2462,7 @@ class View:
"""
...

def preserve_auto_complete_on_focus_lost(self):
def preserve_auto_complete_on_focus_lost(self) -> None:
"""
Sets the auto complete popup state to be preserved the next time the `View` loses focus. When the `View` regains
focus, the auto complete window will be re-shown, with the previously selected entry pre-selected.
Expand All @@ -2471,7 +2471,7 @@ class View:

def export_to_html(
self,
regions=None,
regions: Region | List[Region] | None = ...,
minihtml: bool = ...,
enclosing_tags: bool = ...,
font_size: bool = ...,
Expand Down Expand Up @@ -2614,7 +2614,7 @@ class Phantom:
) -> None:
...

def __eq__(self, rhs: 'Phantom') -> bool:
def __eq__(self, rhs: Phantom) -> bool: # type: ignore[override]
...

def to_tuple(self) -> Tuple[Tuple[int, int], str, int, Optional[Callable[[str], None]]]:
Expand Down Expand Up @@ -2726,7 +2726,7 @@ class CompletionItem:
) -> None:
...

def __eq__(self, rhs: 'CompletionItem') -> bool:
def __eq__(self, rhs: CompletionItem) -> bool: # type: ignore[override]
...

@classmethod
Expand Down
6 changes: 3 additions & 3 deletions stubs/sublime_plugin.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CommandInputHandler:
"""
...

def next_input(self, args) -> CommandInputHandler | None:
def next_input(self, args: Any) -> CommandInputHandler | None:
"""
Return the next input after the user has completed this one. May return
:py:`None` to indicate no more input is required, or
Expand Down Expand Up @@ -119,7 +119,7 @@ class ListInputHandler(CommandInputHandler):
"""
...

def description(self, value, text: str) -> str:
def description(self, value: Any, text: str) -> str:
"""
The text to show in the Command Palette when this input handler is not at the top of the input handler stack.
Defaults to the text of the list item the user selected.
Expand Down Expand Up @@ -221,7 +221,7 @@ class TextCommand(Command):
def __init__(self, view: View) -> None:
...

def run(self, edit: Edit, **kwargs: Dict[str, Any]) -> None:
def run(self, edit: Edit, **kwargs: Dict[str, Any]) -> None: # type: ignore[override]
"""
Called when the command is run. Command arguments are passed as keyword arguments.
"""
Expand Down
Loading

0 comments on commit 22deb1e

Please sign in to comment.