-
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
Use filterText as trigger, if it's present #990
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cfb2256
Use filterText as trigger, if it's present
rwols 4f12d24
Update completion tests
rwols 676d629
I somehow removed a test
rwols 933db94
Don't present documentation, let's work on that later
rwols aae2fb6
Fix textEdit range in tests
rwols File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import html | ||
import mdpopups | ||
import sublime | ||
import sublime_plugin | ||
|
||
|
@@ -191,6 +193,19 @@ def on_query_completions(self, prefix: str, locations: List[int]) -> Optional[su | |
lambda res: self.handle_error(res, completion_list)) | ||
return completion_list | ||
|
||
def normalized_documentation(self, lsp_item: Dict[str, Any]) -> str: | ||
lsp_documentation = lsp_item.get("documentation") | ||
if isinstance(lsp_documentation, str): | ||
return html.escape(lsp_documentation.replace('\n', ' ')) | ||
elif isinstance(lsp_documentation, dict): | ||
value = lsp_documentation.get("value", '') | ||
if lsp_documentation.get("kind") == "markdown": | ||
return mdpopups.md2html(self.view, value) | ||
else: | ||
return html.escape(value.replace('\n', ' ')) | ||
else: | ||
return '' | ||
|
||
def format_completion(self, item: dict) -> sublime.CompletionItem: | ||
item_kind = item.get("kind") | ||
if item_kind: | ||
|
@@ -220,12 +235,29 @@ def format_completion(self, item: dict) -> sublime.CompletionItem: | |
convert = self.view.text_point_utf16 | ||
item["native_region"] = (convert(row, start_col_utf16), convert(row, end_col_utf16)) | ||
|
||
lsp_label = item["label"] | ||
lsp_filter_text = item.get("filterText") | ||
lsp_detail = item.get("detail", "").replace('\n', ' ') | ||
if lsp_filter_text: | ||
st_trigger = lsp_filter_text | ||
st_annotation = lsp_label | ||
# Try to keep fields non-empty. If there's no `detail` field, attempt to use the `documentation` field. | ||
if lsp_detail: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of
|
||
st_details = html.escape(lsp_detail) | ||
else: | ||
st_details = self.normalized_documentation(item) | ||
else: | ||
st_trigger = lsp_label | ||
st_annotation = lsp_detail | ||
st_details = self.normalized_documentation(item) | ||
|
||
return sublime.CompletionItem.command_completion( | ||
trigger=item["label"], | ||
trigger=st_trigger, | ||
command="lsp_select_completion_item", | ||
args=item, | ||
annotation=item.get('detail') or "", | ||
kind=kind | ||
annotation=st_annotation, | ||
kind=kind, | ||
details=st_details | ||
) | ||
|
||
def handle_response(self, response: Optional[Union[dict, List]], completion_list: sublime.CompletionList) -> None: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this function expensive?
I tried the PR locally,
When I trigger the AC in LSP-elm it takes >20s for this line finish. The AC doesn't show up at the end.
maybe the reason it is the combination of
normalized_documentation
andtransform_region
being applied to all items?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.
transform_region is not that expensive,
but normalized_documentation is. If I comment out the
normalized_documentation
calls, the AC show upThere 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.
These are some good points. Waiting potentially 20 seconds before completions show up is of course unacceptable. Performance from mdpopups.md2html is apparently not good enough (perhaps @facelessuser is willing to improve it, if that it even possible). I think the problem is ultimately on our side since we're rendering potentially >200 markdown strings to html.
Ideally, documentation would only be rendered once the user highlights a particular completion item (lazy rendering). But right now we're forced to render all markdown docs to html before presenting completions (cc @jskinner)
That said, from some initial experience with this PR I think putting the documentation into the ST-details is not a good fit. This is because we don't know how many lines/sentences a server will put into the docs. And there is only place for at most one sentence in the ST-details field.
So either we'll have to come up with a way to extract the "most important sentence" from the returned markdown string, or we have to think of something else.
One idea that's been floating in my head is as follows:
Use the new
subl://
scheme in the ST-details to make a href to a command (let's call itLspShowDocumentationCommand
). So that href will appear in the ST-details field at the bottom of the completion widget. The user can click it. TheLspShowDocumentationCommand
would then show the docs in a popup withmdpopups.show_popup
and with the flagsublime.COOPERATE_WITH_AUTO_COMPLETE
. This would allow us to do the lazy rendering as we can store the LSP-documentations of all items somewhere.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.
Because we currently don't know the what the highlighted completion item is in the AC,
Just to chain on your idea.
Instead of just calling LspShowDocumentationCommand,
the documentation field may not be there unless that completion item is resolved.
I would just suggest first calling the
completionItem/resolve
before showing the documentation.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.
It is the same for Metals, client is expected to call
completionItem/resolve
while scrolling through the completion list because the documentation is expensive to compute.Is it possible to request
completionItem/resolve
via the new AC API ?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.
We can't do that, because we don't know which completion item is highlighted in the AC widget.
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.
Yes, we know up-front whether server can resolve docs through the
resolveProvider
server capability. So if that's not present or set to false then we won't show the hyperlink.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.
But if that's a server capability and not per-completion then I suppose some completions might still return nothing.
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 got misled by this
https://discordapp.com/channels/280102180189634562/280102180189634562/704475737088065566
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.
Yes.