-
Notifications
You must be signed in to change notification settings - Fork 185
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
Refactor symbol and completion item kinds #2000
Conversation
77679d5
to
083e7f8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work. I do think we can optimize to use 1 dict lookup everywhere instead of 2.
|
||
|
||
def _enum_like_class_to_list(c: Type[object]) -> List[str]: | ||
def _enum_like_class_to_list(c: Type[object]) -> List[Union[int, str]]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type should be Union[List[str], List[int]]
, or am I misunderstanding the use case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently that isn't possible, because mypy can't infer which one of the list types the return value is, when it is used later. See https://github.com/sublimelsp/LSP/runs/7490372297
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I guess I can add a type hint in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can run mypy locally by running tox
by the way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, seems that Pyright and mypy disagree what to do with the type hint comment in f95c500.
So I guess I could revert to the initial return type List[Union[int, str]]
, or use # type: ignore
for the failing line. Or do you see a better solution?
Thanks I will try to figure out how to run mypy locally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, eventually it goes into some json anyway. For now it’s okay to use List[Union] :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, might not work with inheritance...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's postpone this to another PR and use List[Union] for now ;-)
Alternatively, there is also the enum module / IntEnum which could be used via https://github.com/packagecontrol/enum dependency.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EnumLike(Generic[T])
looks sweet, but like jwortmann I imagined something like https://docs.python.org/3/library/enum.html. Maybe postpone that until python 3.8 because I don't like more dependencies. Or use the EnumLike(Generic[T])
approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That more concise solution using EnumLike
didn't really work as it wouldn't be possible to access class method without instantiating an object. And @staticmethod
doesn't have access to cls
. So it would have to be either the more repetitive solution or some other smart approach.
KIND_VALUE = (sublime.KIND_ID_VARIABLE, "v", "Value") | ||
KIND_VARIABLE = (sublime.KIND_ID_VARIABLE, "v", "Variable") | ||
|
||
COMPLETION_KINDS = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the reason for using a List
was performance :) a list item lookup is an offset into a contiguous array. But, I guess the performance doesn't matter much. Maybe the dict is even faster, who knows? From what I understand dicts are implemented as hash maps. So a number is run through a hash function before doing an array offset index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. As an alternative to the dict, I could use lists and add the fallback KIND_UNSPECIFIED = (sublime.KIND_ID_AMBIGUOUS, "?", "???")
as a first entry in the list, then indexing with kind - 1
could also be avoided. But I guess the performance difference (if any) indeed shouldn't matter here and is orders of magnitude smaller than the display refresh time. So it probably doesn't have any impact and would only be a matter of style/preference. And for the lists, there was an additional check if 1 <= kind <= len(SYMBOL_KINDS)
, which might be even more overhead...
* main: Prevent failing to load all configs when one fails Refactor symbol and completion item kinds (#2000) Add Godot (GDScript) LSP instructions Add preview for resource files in LocationPicker
* main: Prevent failing to load all configs when one fails Refactor symbol and completion item kinds (#2000) Add Godot (GDScript) LSP instructions Add preview for resource files in LocationPicker
SymbolKind
s andCompletionItemKind
s, which have a lot of overlap and even had a few inconsistencies before.kind - 1
int
instead of a kind tuple for the kind insublime.CompletionItem.command_completion
, in case there is a kind id in the language server response, which is out of range for LSP's CompletionItemKind.Changes:
https://github.com/sublimehq/Packages/issues/1336
).onclick
oronchange
).