Skip to content

Commit

Permalink
Move sublime.Kind tuple definitions into constants module
Browse files Browse the repository at this point in the history
  • Loading branch information
jwortmann committed Nov 27, 2023
1 parent 664ad3e commit e51c2cb
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 129 deletions.
2 changes: 1 addition & 1 deletion plugin/completion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .core.constants import COMPLETION_KINDS
from .core.edit import parse_text_edit
from .core.logging import debug
from .core.promise import Promise
Expand All @@ -19,7 +20,6 @@
from .core.sessions import Session
from .core.settings import userprefs
from .core.typing import Callable, List, Dict, Optional, Generator, Tuple, Union, cast, Any, TypeGuard
from .core.views import COMPLETION_KINDS
from .core.views import FORMAT_STRING, FORMAT_MARKUP_CONTENT
from .core.views import MarkdownLangMap
from .core.views import minihtml
Expand Down
128 changes: 126 additions & 2 deletions plugin/core/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# TODO: Move all constants which are shared by multiple modules into this file, so that they can be imported without
# causing import loops
from .protocol import CodeActionKind
from .protocol import CompletionItemKind
from .protocol import DiagnosticSeverity
from .protocol import SymbolKind
from .typing import Dict, Tuple
import sublime


SublimeKind = Tuple[int, str, str]


# Keys for View.add_regions
HOVER_HIGHLIGHT_KEY = 'lsp_hover_highlight'
Expand All @@ -9,3 +17,119 @@
HOVER_ENABLED_KEY = 'lsp_show_hover_popups'
HOVER_PROVIDER_COUNT_KEY = 'lsp_hover_provider_count'
SHOW_DEFINITIONS_KEY = 'show_definitions'

# 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")

COMPLETION_KINDS = {
CompletionItemKind.Text: KIND_TEXT,
CompletionItemKind.Method: KIND_METHOD,
CompletionItemKind.Function: KIND_FUNCTION,
CompletionItemKind.Constructor: KIND_CONSTRUCTOR,
CompletionItemKind.Field: KIND_FIELD,
CompletionItemKind.Variable: KIND_VARIABLE,
CompletionItemKind.Class: KIND_CLASS,
CompletionItemKind.Interface: KIND_INTERFACE,
CompletionItemKind.Module: KIND_MODULE,
CompletionItemKind.Property: KIND_PROPERTY,
CompletionItemKind.Unit: KIND_UNIT,
CompletionItemKind.Value: KIND_VALUE,
CompletionItemKind.Enum: KIND_ENUM,
CompletionItemKind.Keyword: KIND_KEYWORD,
CompletionItemKind.Snippet: KIND_SNIPPET,
CompletionItemKind.Color: KIND_COLOR,
CompletionItemKind.File: KIND_FILE,
CompletionItemKind.Reference: KIND_REFERENCE,
CompletionItemKind.Folder: KIND_FOLDER,
CompletionItemKind.EnumMember: KIND_ENUMMEMBER,
CompletionItemKind.Constant: KIND_CONSTANT,
CompletionItemKind.Struct: KIND_STRUCT,
CompletionItemKind.Event: KIND_EVENT,
CompletionItemKind.Operator: KIND_OPERATOR,
CompletionItemKind.TypeParameter: KIND_TYPEPARAMETER
} # type: Dict[CompletionItemKind, SublimeKind]

SYMBOL_KINDS = {
SymbolKind.File: KIND_FILE,
SymbolKind.Module: KIND_MODULE,
SymbolKind.Namespace: KIND_NAMESPACE,
SymbolKind.Package: KIND_PACKAGE,
SymbolKind.Class: KIND_CLASS,
SymbolKind.Method: KIND_METHOD,
SymbolKind.Property: KIND_PROPERTY,
SymbolKind.Field: KIND_FIELD,
SymbolKind.Constructor: KIND_CONSTRUCTOR,
SymbolKind.Enum: KIND_ENUM,
SymbolKind.Interface: KIND_INTERFACE,
SymbolKind.Function: KIND_FUNCTION,
SymbolKind.Variable: KIND_VARIABLE,
SymbolKind.Constant: KIND_CONSTANT,
SymbolKind.String: KIND_STRING,
SymbolKind.Number: KIND_NUMBER,
SymbolKind.Boolean: KIND_BOOLEAN,
SymbolKind.Array: KIND_ARRAY,
SymbolKind.Object: KIND_OBJECT,
SymbolKind.Key: KIND_KEY,
SymbolKind.Null: KIND_NULL,
SymbolKind.EnumMember: KIND_ENUMMEMBER,
SymbolKind.Struct: KIND_STRUCT,
SymbolKind.Event: KIND_EVENT,
SymbolKind.Operator: KIND_OPERATOR,
SymbolKind.TypeParameter: KIND_TYPEPARAMETER
} # type: Dict[SymbolKind, SublimeKind]

DIAGNOSTIC_KINDS = {
DiagnosticSeverity.Error: KIND_ERROR,
DiagnosticSeverity.Warning: KIND_WARNING,
DiagnosticSeverity.Information: KIND_INFORMATION,
DiagnosticSeverity.Hint: KIND_HINT
} # type: Dict[DiagnosticSeverity, SublimeKind]

CODE_ACTION_KINDS = {
CodeActionKind.QuickFix: KIND_QUICKFIX,
CodeActionKind.Refactor: KIND_REFACTOR,
CodeActionKind.Source: KIND_SOURCE
} # type: Dict[CodeActionKind, SublimeKind]
2 changes: 1 addition & 1 deletion plugin/core/tree_view.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .constants import SublimeKind
from .css import css
from .promise import Promise
from .registry import LspWindowCommand
from .registry import windows
from .typing import Dict, IntEnum, List, Optional, TypeVar
from .views import SublimeKind
from abc import ABCMeta
from abc import abstractmethod
from functools import partial
Expand Down
121 changes: 2 additions & 119 deletions plugin/core/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .constants import CODE_ACTION_KINDS
from .constants import SublimeKind
from .css import css as lsp_css
from .protocol import CodeAction
from .protocol import CodeActionKind
Expand All @@ -7,7 +9,6 @@
from .protocol import Color
from .protocol import ColorInformation
from .protocol import Command
from .protocol import CompletionItemKind
from .protocol import Diagnostic
from .protocol import DiagnosticRelatedInformation
from .protocol import DiagnosticSeverity
Expand All @@ -28,7 +29,6 @@
from .protocol import Range
from .protocol import Request
from .protocol import SelectionRangeParams
from .protocol import SymbolKind
from .protocol import TextDocumentContentChangeEvent
from .protocol import TextDocumentIdentifier
from .protocol import TextDocumentItem
Expand All @@ -52,7 +52,6 @@
import tempfile

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

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

Expand All @@ -66,122 +65,6 @@
("hint", "hints", "region.bluish markup.info.hint.lsp", "", _baseflags | sublime.DRAW_STIPPLED_UNDERLINE, sublime.DRAW_NO_FILL), # noqa: E501
] # type: List[Tuple[str, str, str, str, int, int]]

# 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")

COMPLETION_KINDS = {
CompletionItemKind.Text: KIND_TEXT,
CompletionItemKind.Method: KIND_METHOD,
CompletionItemKind.Function: KIND_FUNCTION,
CompletionItemKind.Constructor: KIND_CONSTRUCTOR,
CompletionItemKind.Field: KIND_FIELD,
CompletionItemKind.Variable: KIND_VARIABLE,
CompletionItemKind.Class: KIND_CLASS,
CompletionItemKind.Interface: KIND_INTERFACE,
CompletionItemKind.Module: KIND_MODULE,
CompletionItemKind.Property: KIND_PROPERTY,
CompletionItemKind.Unit: KIND_UNIT,
CompletionItemKind.Value: KIND_VALUE,
CompletionItemKind.Enum: KIND_ENUM,
CompletionItemKind.Keyword: KIND_KEYWORD,
CompletionItemKind.Snippet: KIND_SNIPPET,
CompletionItemKind.Color: KIND_COLOR,
CompletionItemKind.File: KIND_FILE,
CompletionItemKind.Reference: KIND_REFERENCE,
CompletionItemKind.Folder: KIND_FOLDER,
CompletionItemKind.EnumMember: KIND_ENUMMEMBER,
CompletionItemKind.Constant: KIND_CONSTANT,
CompletionItemKind.Struct: KIND_STRUCT,
CompletionItemKind.Event: KIND_EVENT,
CompletionItemKind.Operator: KIND_OPERATOR,
CompletionItemKind.TypeParameter: KIND_TYPEPARAMETER
} # type: Dict[CompletionItemKind, SublimeKind]

SYMBOL_KINDS = {
SymbolKind.File: KIND_FILE,
SymbolKind.Module: KIND_MODULE,
SymbolKind.Namespace: KIND_NAMESPACE,
SymbolKind.Package: KIND_PACKAGE,
SymbolKind.Class: KIND_CLASS,
SymbolKind.Method: KIND_METHOD,
SymbolKind.Property: KIND_PROPERTY,
SymbolKind.Field: KIND_FIELD,
SymbolKind.Constructor: KIND_CONSTRUCTOR,
SymbolKind.Enum: KIND_ENUM,
SymbolKind.Interface: KIND_INTERFACE,
SymbolKind.Function: KIND_FUNCTION,
SymbolKind.Variable: KIND_VARIABLE,
SymbolKind.Constant: KIND_CONSTANT,
SymbolKind.String: KIND_STRING,
SymbolKind.Number: KIND_NUMBER,
SymbolKind.Boolean: KIND_BOOLEAN,
SymbolKind.Array: KIND_ARRAY,
SymbolKind.Object: KIND_OBJECT,
SymbolKind.Key: KIND_KEY,
SymbolKind.Null: KIND_NULL,
SymbolKind.EnumMember: KIND_ENUMMEMBER,
SymbolKind.Struct: KIND_STRUCT,
SymbolKind.Event: KIND_EVENT,
SymbolKind.Operator: KIND_OPERATOR,
SymbolKind.TypeParameter: KIND_TYPEPARAMETER
} # type: Dict[SymbolKind, SublimeKind]

DIAGNOSTIC_KINDS = {
DiagnosticSeverity.Error: KIND_ERROR,
DiagnosticSeverity.Warning: KIND_WARNING,
DiagnosticSeverity.Information: KIND_INFORMATION,
DiagnosticSeverity.Hint: KIND_HINT
} # type: Dict[DiagnosticSeverity, SublimeKind]

CODE_ACTION_KINDS = {
CodeActionKind.QuickFix: KIND_QUICKFIX,
CodeActionKind.Refactor: KIND_REFACTOR,
CodeActionKind.Source: KIND_SOURCE
} # type: Dict[CodeActionKind, SublimeKind]

# Symbol scope to kind mapping, based on https://github.com/sublimetext-io/docs.sublimetext.io/issues/30
SUBLIME_KIND_SCOPES = {
sublime.KIND_KEYWORD: "keyword | storage.modifier | storage.type | keyword.declaration | variable.language | constant.language", # noqa: E501
Expand Down
2 changes: 1 addition & 1 deletion plugin/diagnostics.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from .core.constants import DIAGNOSTIC_KINDS
from .core.protocol import Diagnostic
from .core.protocol import DiagnosticSeverity
from .core.settings import userprefs
from .core.typing import List, Tuple
from .core.views import DIAGNOSTIC_KINDS
from .core.views import diagnostic_severity
from .core.views import format_diagnostics_for_annotation
import sublime
Expand Down
2 changes: 1 addition & 1 deletion plugin/goto_diagnostic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .core.constants import DIAGNOSTIC_KINDS
from .core.diagnostics_storage import is_severity_included
from .core.diagnostics_storage import ParsedUri
from .core.paths import project_base_dir
Expand All @@ -13,7 +14,6 @@
from .core.types import ClientConfig
from .core.typing import Any, Dict, Iterator, List, Optional, Tuple, Union
from .core.url import parse_uri, unparse_uri
from .core.views import DIAGNOSTIC_KINDS
from .core.views import diagnostic_severity
from .core.views import format_diagnostic_for_html
from .core.views import format_diagnostic_source_and_code
Expand Down
2 changes: 1 addition & 1 deletion plugin/hierarchy.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .core.constants import SYMBOL_KINDS
from .core.paths import simple_path
from .core.promise import Promise
from .core.protocol import CallHierarchyIncomingCall
Expand All @@ -20,7 +21,6 @@
from .core.typing import Callable, List, Optional, TypedDict, Union
from .core.typing import cast
from .core.views import make_command_link
from .core.views import SYMBOL_KINDS
from .core.views import text_document_position_params
from abc import ABCMeta
from abc import abstractmethod
Expand Down
2 changes: 1 addition & 1 deletion plugin/locationpicker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .core.constants import SublimeKind
from .core.logging import debug
from .core.protocol import DocumentUri
from .core.protocol import Location
Expand All @@ -7,7 +8,6 @@
from .core.typing import Union, List, Optional, Tuple
from .core.views import get_uri_and_position_from_location
from .core.views import location_to_human_readable
from .core.views import SublimeKind
from .core.views import to_encoded_filename
from urllib.request import url2pathname
import functools
Expand Down
4 changes: 2 additions & 2 deletions plugin/symbols.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import weakref
from .core.constants import SublimeKind
from .core.constants import SYMBOL_KINDS
from .core.protocol import DocumentSymbol
from .core.protocol import DocumentSymbolParams
from .core.protocol import Point
Expand All @@ -11,8 +13,6 @@
from .core.typing import Any, List, Optional, Tuple, Dict, Union, cast
from .core.views import offset_to_point
from .core.views import range_to_region
from .core.views import SublimeKind
from .core.views import SYMBOL_KINDS
from .core.views import text_document_identifier
from .goto_diagnostic import PreselectedListInputHandler
import os
Expand Down

0 comments on commit e51c2cb

Please sign in to comment.