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

Various small code style improvements #2537

Merged
merged 8 commits into from
Oct 26, 2024
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
2 changes: 1 addition & 1 deletion docs/src/client_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Below is an example of the `LSP.sublime-settings` file with configurations for t
| settings | per-project settings (equivalent to VS Code's Workspace Settings) |
| initializationOptions | options to send to the server at startup (rarely used) |
| selector | This is _the_ connection between your files and language servers. It's a selector that is matched against the current view's base scope. If the selector matches with the base scope of the the file, the associated language server is started. For more information, see https://www.sublimetext.com/docs/3/selectors.html |
| priority_selector | Used to prioritize a certain language server when choosing which one to query on views with multiple servers active. Certain LSP actions have to pick which server to query and this setting can be used to decide which one to pick based on the current scopes at the cursor location. For example when having both HTML and PHP servers running on a PHP file, this can be used to give priority to the HTML one in HTML blocks and to PHP one otherwise. That would be done by setting "feature_selector" to `text.html` for HTML server and `source.php` to PHP server. Note: when the "feature_selector" is missing, it will be the same as the "document_selector".
| priority_selector | Used to prioritize a certain language server when choosing which one to query on views with multiple servers active. Certain LSP actions have to pick which server to query and this setting can be used to decide which one to pick based on the current scopes at the cursor location. For example when having both HTML and PHP servers running on a PHP file, this can be used to give priority to the HTML one in HTML blocks and to PHP one otherwise. That would be done by setting "priority_selector" to `text.html` for HTML server and `source.php` to PHP server. Note: when the "priority_selector" is missing, it will be the same as the "document_selector".
| diagnostics_mode | Set to `"workspace"` (default is `"open_files"`) to ignore diagnostics for files that are not within the project (window) folders. If project has no folders then this option has no effect and diagnostics are shown for all files. If the server supports _pull diagnostics_ (`diagnosticProvider`), this setting also controls whether diagnostics are requested only for open files (`"open_files"`), or for all files in the project folders (`"workspace"`). |
| tcp_port | see instructions below |
| experimental_capabilities | Turn on experimental capabilities of a language server. This is a dictionary and differs per language server |
Expand Down
1 change: 1 addition & 0 deletions docs/src/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ Furthermore, it is possible to adjust the colors for semantic tokens by applying
| `meta.semantic-token.number` | number |
| `meta.semantic-token.regexp` | regexp |
| `meta.semantic-token.operator` | operator |
| `meta.semantic-token.decorator` | decorator |

By default, LSP will assign scopes based on the [scope naming guideline](https://www.sublimetext.com/docs/scope_naming.html) to each of these token types, but if you define color scheme rules for the scopes specified above, the latter will take precedence.

Expand Down
4 changes: 2 additions & 2 deletions plugin/code_lens.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ def render(self, mode: str) -> None:
phantom_region = self._get_phantom_region(region)
html = '<body id="lsp-code-lens">{}</body>'.format(
'\n<small style="font-family: system">|</small>\n'.join(lens.small_html for lens in group))
phantoms.append(sublime.Phantom(phantom_region, html, sublime.LAYOUT_BELOW))
phantoms.append(sublime.Phantom(phantom_region, html, sublime.PhantomLayout.BELOW))
self._phantom.update(phantoms)
else: # 'annotation'
self._clear_annotations()
flags = sublime.NO_UNDO
flags = sublime.RegionFlags.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(
Expand Down
12 changes: 6 additions & 6 deletions plugin/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def format_completion(
annotation,
# Not using "sublime.format_command" in a hot path to avoid slow json.dumps.
f'lsp_select_completion {{"index":{index},"session_name":"{session_name}"}}',
sublime.COMPLETION_FORMAT_COMMAND,
sublime.CompletionFormat.COMMAND,
kind,
details=" | ".join(details)
)
Expand Down Expand Up @@ -215,9 +215,9 @@ def _resolve_completions_async(self, responses: list[ResolvedCompletions]) -> No
flags = sublime.AutoCompleteFlags.NONE
prefs = userprefs()
if prefs.inhibit_snippet_completions:
flags |= sublime.INHIBIT_EXPLICIT_COMPLETIONS
flags |= sublime.AutoCompleteFlags.INHIBIT_EXPLICIT_COMPLETIONS
if prefs.inhibit_word_completions:
flags |= sublime.INHIBIT_WORD_COMPLETIONS
flags |= sublime.AutoCompleteFlags.INHIBIT_WORD_COMPLETIONS
view_settings = self._view.settings()
include_snippets = view_settings.get("auto_complete_include_snippets") and \
(self._triggered_manually or view_settings.get("auto_complete_include_snippets_when_typing"))
Expand All @@ -233,7 +233,7 @@ def _resolve_completions_async(self, responses: list[ResolvedCompletions]) -> No
response_items = response["items"] or []
item_defaults = response.get('itemDefaults') or {}
if response.get("isIncomplete", False):
flags |= sublime.DYNAMIC_COMPLETIONS
flags |= sublime.AutoCompleteFlags.DYNAMIC_COMPLETIONS
elif isinstance(response, list):
response_items = response
response_items = sorted(response_items, key=lambda item: item.get("sortText") or item["label"])
Expand All @@ -246,7 +246,7 @@ def _resolve_completions_async(self, responses: list[ResolvedCompletions]) -> No
for index, response_item in enumerate(response_items)
if include_snippets or response_item.get("kind") != CompletionItemKind.Snippet)
if items:
flags |= sublime.INHIBIT_REORDER
flags |= sublime.AutoCompleteFlags.INHIBIT_REORDER
if errors:
error_messages = ", ".join(str(error) for error in errors)
sublime.status_message(f'Completion error: {error_messages}')
Expand Down Expand Up @@ -316,7 +316,7 @@ def run_main() -> None:
show_lsp_popup(
self.view,
minihtml_content,
flags=sublime.COOPERATE_WITH_AUTO_COMPLETE,
flags=sublime.PopupFlags.COOPERATE_WITH_AUTO_COMPLETE,
md=False,
on_navigate=self._on_navigate)

Expand Down
92 changes: 46 additions & 46 deletions plugin/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,55 @@
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
DOCUMENT_LINK_FLAGS = sublime.RegionFlags.HIDE_ON_MINIMAP | sublime.RegionFlags.DRAW_NO_FILL | sublime.RegionFlags.DRAW_NO_OUTLINE | sublime.RegionFlags.DRAW_SOLID_UNDERLINE | sublime.RegionFlags.NO_UNDO # noqa: E501
REGIONS_INITIALIZE_FLAGS = sublime.RegionFlags.HIDDEN | sublime.RegionFlags.NO_UNDO
SEMANTIC_TOKEN_FLAGS = sublime.RegionFlags.DRAW_NO_OUTLINE | sublime.RegionFlags.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")
KIND_BOOLEAN = (sublime.KIND_ID_VARIABLE, "b", "Boolean")
KIND_CLASS = (sublime.KIND_ID_TYPE, "c", "Class")
KIND_COLOR = (sublime.KIND_ID_MARKUP, "c", "Color")
KIND_CONSTANT = (sublime.KIND_ID_VARIABLE, "c", "Constant")
KIND_CONSTRUCTOR = (sublime.KIND_ID_FUNCTION, "c", "Constructor")
KIND_ENUM = (sublime.KIND_ID_TYPE, "e", "Enum")
KIND_ENUMMEMBER = (sublime.KIND_ID_VARIABLE, "e", "Enum Member")
KIND_EVENT = (sublime.KIND_ID_FUNCTION, "e", "Event")
KIND_FIELD = (sublime.KIND_ID_VARIABLE, "f", "Field")
KIND_FILE = (sublime.KIND_ID_NAVIGATION, "f", "File")
KIND_FOLDER = (sublime.KIND_ID_NAVIGATION, "f", "Folder")
KIND_FUNCTION = (sublime.KIND_ID_FUNCTION, "f", "Function")
KIND_INTERFACE = (sublime.KIND_ID_TYPE, "i", "Interface")
KIND_KEY = (sublime.KIND_ID_NAVIGATION, "k", "Key")
KIND_KEYWORD = (sublime.KIND_ID_KEYWORD, "k", "Keyword")
KIND_METHOD = (sublime.KIND_ID_FUNCTION, "m", "Method")
KIND_MODULE = (sublime.KIND_ID_NAMESPACE, "m", "Module")
KIND_NAMESPACE = (sublime.KIND_ID_NAMESPACE, "n", "Namespace")
KIND_NULL = (sublime.KIND_ID_VARIABLE, "n", "Null")
KIND_NUMBER = (sublime.KIND_ID_VARIABLE, "n", "Number")
KIND_OBJECT = (sublime.KIND_ID_TYPE, "o", "Object")
KIND_OPERATOR = (sublime.KIND_ID_KEYWORD, "o", "Operator")
KIND_PACKAGE = (sublime.KIND_ID_NAMESPACE, "p", "Package")
KIND_PROPERTY = (sublime.KIND_ID_VARIABLE, "p", "Property")
KIND_REFERENCE = (sublime.KIND_ID_NAVIGATION, "r", "Reference")
KIND_SNIPPET = (sublime.KIND_ID_SNIPPET, "s", "Snippet")
KIND_STRING = (sublime.KIND_ID_VARIABLE, "s", "String")
KIND_STRUCT = (sublime.KIND_ID_TYPE, "s", "Struct")
KIND_TEXT = (sublime.KIND_ID_MARKUP, "t", "Text")
KIND_TYPEPARAMETER = (sublime.KIND_ID_TYPE, "t", "Type Parameter")
KIND_UNIT = (sublime.KIND_ID_VARIABLE, "u", "Unit")
KIND_VALUE = (sublime.KIND_ID_VARIABLE, "v", "Value")
KIND_VARIABLE = (sublime.KIND_ID_VARIABLE, "v", "Variable")

KIND_ERROR = (sublime.KIND_ID_COLOR_REDISH, "e", "Error")
KIND_WARNING = (sublime.KIND_ID_COLOR_YELLOWISH, "w", "Warning")
KIND_INFORMATION = (sublime.KIND_ID_COLOR_BLUISH, "i", "Information")
KIND_HINT = (sublime.KIND_ID_COLOR_BLUISH, "h", "Hint")

KIND_QUICKFIX = (sublime.KIND_ID_COLOR_YELLOWISH, "f", "QuickFix")
KIND_REFACTOR = (sublime.KIND_ID_COLOR_CYANISH, "r", "Refactor")
KIND_SOURCE = (sublime.KIND_ID_COLOR_PURPLISH, "s", "Source")
KIND_ARRAY = (sublime.KindId.TYPE, "a", "Array")
KIND_BOOLEAN = (sublime.KindId.VARIABLE, "b", "Boolean")
KIND_CLASS = (sublime.KindId.TYPE, "c", "Class")
KIND_COLOR = (sublime.KindId.MARKUP, "c", "Color")
KIND_CONSTANT = (sublime.KindId.VARIABLE, "c", "Constant")
KIND_CONSTRUCTOR = (sublime.KindId.FUNCTION, "c", "Constructor")
KIND_ENUM = (sublime.KindId.TYPE, "e", "Enum")
KIND_ENUMMEMBER = (sublime.KindId.VARIABLE, "e", "Enum Member")
KIND_EVENT = (sublime.KindId.FUNCTION, "e", "Event")
KIND_FIELD = (sublime.KindId.VARIABLE, "f", "Field")
KIND_FILE = (sublime.KindId.NAVIGATION, "f", "File")
KIND_FOLDER = (sublime.KindId.NAVIGATION, "f", "Folder")
KIND_FUNCTION = (sublime.KindId.FUNCTION, "f", "Function")
KIND_INTERFACE = (sublime.KindId.TYPE, "i", "Interface")
KIND_KEY = (sublime.KindId.NAVIGATION, "k", "Key")
KIND_KEYWORD = (sublime.KindId.KEYWORD, "k", "Keyword")
KIND_METHOD = (sublime.KindId.FUNCTION, "m", "Method")
KIND_MODULE = (sublime.KindId.NAMESPACE, "m", "Module")
KIND_NAMESPACE = (sublime.KindId.NAMESPACE, "n", "Namespace")
KIND_NULL = (sublime.KindId.VARIABLE, "n", "Null")
KIND_NUMBER = (sublime.KindId.VARIABLE, "n", "Number")
KIND_OBJECT = (sublime.KindId.TYPE, "o", "Object")
KIND_OPERATOR = (sublime.KindId.KEYWORD, "o", "Operator")
KIND_PACKAGE = (sublime.KindId.NAMESPACE, "p", "Package")
KIND_PROPERTY = (sublime.KindId.VARIABLE, "p", "Property")
KIND_REFERENCE = (sublime.KindId.NAVIGATION, "r", "Reference")
KIND_SNIPPET = (sublime.KindId.SNIPPET, "s", "Snippet")
KIND_STRING = (sublime.KindId.VARIABLE, "s", "String")
KIND_STRUCT = (sublime.KindId.TYPE, "s", "Struct")
KIND_TEXT = (sublime.KindId.MARKUP, "t", "Text")
KIND_TYPEPARAMETER = (sublime.KindId.TYPE, "t", "Type Parameter")
KIND_UNIT = (sublime.KindId.VARIABLE, "u", "Unit")
KIND_VALUE = (sublime.KindId.VARIABLE, "v", "Value")
KIND_VARIABLE = (sublime.KindId.VARIABLE, "v", "Variable")

KIND_ERROR = (sublime.KindId.COLOR_REDISH, "e", "Error")
KIND_WARNING = (sublime.KindId.COLOR_YELLOWISH, "w", "Warning")
KIND_INFORMATION = (sublime.KindId.COLOR_BLUISH, "i", "Information")
KIND_HINT = (sublime.KindId.COLOR_BLUISH, "h", "Hint")

KIND_QUICKFIX = (sublime.KindId.COLOR_YELLOWISH, "f", "QuickFix")
KIND_REFACTOR = (sublime.KindId.COLOR_CYANISH, "r", "Refactor")
KIND_SOURCE = (sublime.KindId.COLOR_PURPLISH, "s", "Source")

COMPLETION_KINDS: dict[CompletionItemKind, SublimeKind] = {
CompletionItemKind.Text: KIND_TEXT,
Expand Down
14 changes: 8 additions & 6 deletions plugin/core/input_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def __init__(self, command: sublime_plugin.WindowCommand, args: dict[str, Any])
self.listener: sublime_plugin.TextChangeListener | None = None
self.input_view: sublime.View | None = None

def attach_listener(self) -> None:
def _attach_listener(self) -> None:
for buffer in sublime._buffers(): # type: ignore
view = buffer.primary_view()
# This condition to find the input field view might not be sufficient if there is another command palette
Expand All @@ -129,6 +129,10 @@ def attach_listener(self) -> None:
selection.clear()
selection.add(len(self.text))

def _detach_listener(self) -> None:
if self.listener and self.listener.is_attached():
self.listener.detach()

@final
def list_items(self) -> list[sublime.ListInputItem]:
if not self.text: # Show initial items when the command was just invoked
Expand All @@ -149,7 +153,7 @@ def _select_first_row(self) -> None:

def initial_text(self) -> str:
setattr(self.command, '_text', '')
sublime.set_timeout(self.attach_listener)
sublime.set_timeout(self._attach_listener)
return self.text

def initial_selection(self) -> list[tuple[int, int]]:
Expand All @@ -160,12 +164,10 @@ def validate(self, text: str) -> bool:
return bool(text)

def cancel(self) -> None:
if self.listener and self.listener.is_attached():
self.listener.detach()
self._detach_listener()

def confirm(self, text: str) -> None:
if self.listener and self.listener.is_attached():
self.listener.detach()
self._detach_listener()

def on_modified(self, text: str) -> None:
""" Called after changes have been made to the input, with the text of the input field passed as argument. """
Expand Down
6 changes: 3 additions & 3 deletions plugin/core/open.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def _select_and_center(view: sublime.View | None, r: Range) -> sublime.View | No
def _return_existing_view(flags: int, existing_view_group: int, active_group: int, specified_group: int) -> bool:
if specified_group > -1:
return existing_view_group == specified_group
if bool(flags & (sublime.ADD_TO_SELECTION | sublime.REPLACE_MRU)):
if bool(flags & (sublime.NewFileFlags.ADD_TO_SELECTION | sublime.NewFileFlags.REPLACE_MRU)):
return False
if existing_view_group == active_group:
return True
return not bool(flags & sublime.FORCE_GROUP)
return not bool(flags & sublime.NewFileFlags.FORCE_GROUP)


def _find_open_file(window: sublime.Window, fname: str, group: int = -1) -> sublime.View | None:
Expand Down Expand Up @@ -101,7 +101,7 @@ def open_file(
was_already_open = view is not None
view = window.open_file(file, flags, group)
if not view.is_loading():
if was_already_open and (flags & sublime.SEMI_TRANSIENT):
if was_already_open and (flags & sublime.NewFileFlags.SEMI_TRANSIENT):
# workaround bug https://github.com/sublimehq/sublime_text/issues/2411 where transient view might not get
# its view listeners initialized.
sublime_plugin.check_view_event_listeners(view) # type: ignore
Expand Down
4 changes: 2 additions & 2 deletions plugin/core/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ def run(
modifier_keys = event.get('modifier_keys')
if modifier_keys:
if 'primary' in modifier_keys:
flags |= sublime.ADD_TO_SELECTION | sublime.SEMI_TRANSIENT | sublime.CLEAR_TO_RIGHT
flags |= sublime.NewFileFlags.ADD_TO_SELECTION | sublime.NewFileFlags.SEMI_TRANSIENT | sublime.NewFileFlags.CLEAR_TO_RIGHT # noqa: E501
elif 'shift' in modifier_keys:
flags |= sublime.ADD_TO_SELECTION | sublime.SEMI_TRANSIENT
flags |= sublime.NewFileFlags.ADD_TO_SELECTION | sublime.NewFileFlags.SEMI_TRANSIENT
sublime.set_timeout_async(lambda: self._run_async(location, session_name, flags, group))

def want_event(self) -> bool:
Expand Down
Loading