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

Use filterText as trigger, if it's present #990

Merged
merged 5 commits into from
Apr 28, 2020

Conversation

rwols
Copy link
Member

@rwols rwols commented Apr 27, 2020

The LSP-filterText is truly the suitable thing for the ST-trigger.

If an LSP-filterText is present, we have a bit of a problem on where
to stuff all of the interesting things.
I decided to put the LSP-label into the ST-annotation field.
That leaves us with only the ST-details to fill in. If there's an LSP-detail,
use that. Otherwise, use the LSP-documentation for the ST-details.

In case there's no LSP-filterText, we are supposed to use the LSP-label
for the ST-trigger. In that case there are enough fields: the LSP-detail
can go into the ST-annotation, and the LSP-documentation and go into the
ST-details.

The LSP-filterText is truly the suitable thing for the ST-trigger.

If an LSP-filterText is present, we have a bit of a problem on where
to stuff all of the interesting things.
I decided to put the LSP-label into the ST-annotation field.
That leaves us with only the ST-details to fill in. If there's an LSP-detail,
use that. Otherwise, use the LSP-documentation for the ST-details.

In case there's no LSP-filterText, we are supposed to use the LSP-label
for the ST-trigger. In that case there are enough fields: the LSP-detail
can go into the ST-annotation, and the LSP-documentation and go into the
ST-details.
@rwols
Copy link
Member Author

rwols commented Apr 27, 2020

I know we had some discussion about this in the past, and we ultimately decided to keep using the LSP-label for ST-trigger. We went back to the LSP-label because the completions looked too "bare". But now we can stuff the LSP-label in the ST-annotation field.

This fixes the clangd problem of decorated labels.

@ayoub-benali, could you try out this branch and see if metals is working better with this, now that we have an ST4 AC widget?

This PR is also inspired by findings and investigations in https://gist.github.com/jskinner/4dfa6d86f848e1e1e583883507369b67

@rwols
Copy link
Member Author

rwols commented Apr 27, 2020

Some screenshots:

pyls:

  • Has no filterText, so the label is used as trigger.
  • LSP-detail field is put into ST-annotation field.
  • LSP-documentation field is put into ST-details field.
    Screenshot from 2020-04-27 17-48-24

clangd:

  • Uses filterText.
  • LSP-label is put into ST-annotation field.
  • LSP-detail is put into ST-details field.
    Screenshot from 2020-04-27 17-48-48

lemminx: (XML server)

  • uses filterText.
  • LSP-label is put into ST-annotation field. This is absolutely necessary as "end with" does not match the prefix at all.
    Screenshot from 2020-04-27 17-49-13

@rwols
Copy link
Member Author

rwols commented Apr 27, 2020

I don't know Scala, but I'm guessing this works pretty well for metals, too:

Screenshot from 2020-04-27 18-28-53

What we could do in addition to this diff, is to check whether lsp_label.startswith(lsp_filter_text). If it does, then st_trigger=lsp_label is safe to do.

@predragnikolic
Copy link
Member

@rwols, can you check Implement all members for metals?

#771
based what I saw in the issue for "Implement all members",
"filterText": "e" and the "label": "Implement all members".

 "label": "Implement all members",
  "kind": 12,
  "sortText": "00002",
  "filterText": "e",
  "insertTextFormat": 2,
  "textEdit": {
    "range": {
      "start": {
        "line": 9,
        "character": 3
      },
      "end": {
        "line": 9,
        "character": 4
      }
    },
    "newText": "def foo: Int \u003d ${0:???}\n   def boo: Int \u003d ${0:???}"
  },
  "data": {
    "target": "file:/Users/ckipp/Documents/scala-workspace/test-project/?id\u003droot",
    "symbol": "local6"
  }

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of if/else maybe just one or?

st_details = html.escape(lsp_detail) or self.normalized_documentation(item)

@rwols
Copy link
Member Author

rwols commented Apr 27, 2020

So I'm pretty sure we came to the wrong conclusion in #771.
The filterText is supposed to be used for prefix matching. Read microsoft/language-server-protocol#898 (comment) very carefully.

In that comment:

  • filterText: b
  • label: foo-text-range-replace

When the user types a b, the completion item with foo-text-range-replace appears.

In #771:

  • filterText e
  • label: Implement all members

When the user types an e, the completion item with Implement all members should appear.

Comment on lines 196 to 197
def normalized_documentation(self, lsp_item: Dict[str, Any]) -> str:
lsp_documentation = lsp_item.get("documentation")
Copy link
Member

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.

items = list(map(self.format_completion, response_items))

maybe the reason it is the combination of normalized_documentation and transform_region being applied to all items?

output

Copy link
Member

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 up

Copy link
Member Author

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 it LspShowDocumentationCommand). So that href will appear in the ST-details field at the bottom of the completion widget. The user can click it. The LspShowDocumentationCommand would then show the docs in a popup with mdpopups.show_popup and with the flag sublime.COOPERATE_WITH_AUTO_COMPLETE. This would allow us to do the lazy rendering as we can store the LSP-documentations of all items somewhere.

Copy link
Member

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.

Copy link
Contributor

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 ?

Copy link
Member Author

@rwols rwols Apr 28, 2020

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.

Copy link
Member Author

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.

Copy link
Member

@rchl rchl Apr 28, 2020

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't know which completion item is highlighted in the AC widget.

I got misled by this
https://discordapp.com/channels/280102180189634562/280102180189634562/704475737088065566

Copy link
Member Author

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.

Yes.

@rchl
Copy link
Member

rchl commented Apr 27, 2020

I've tried on lsp-typescript and lsp-vue and couldn't see anything out of place. It was a bit better generally.

LSP-typescript

Before:
before
After:
after

  • there is some duplication here so the startsWith optimization might help here

LSP-vue

Before:
vuebefore
After:
vueafter

  • I like extra documentation field

@ayoub-benali
Copy link
Contributor

ayoub-benali commented Apr 27, 2020

@rwols @predragnikolic I tested #771
complete

EDIT:
prefix completion is still working
complete-2

rwols added 2 commits April 28, 2020 10:15
Revisit some tests. I've modified the payloads to be "real" payloads.
One test fails now, a pure insertion text edit. This is to be expected
and we should be able to handle this in an upcoming ST API.
@rwols rwols merged commit fb209f6 into st4000-exploration Apr 28, 2020
@rwols rwols deleted the filter-text-as-trigger branch April 28, 2020 09:59
@FichteFoll
Copy link

FichteFoll commented Apr 29, 2020

I've tried on lsp-typescript and lsp-vue and couldn't see anything out of place.

The LSP-typescript example confuses me a bit, tbh. Should the ? not be included in the trigger? Does it make sense to show almost the same text as the trigger in the annotation but with a ? appended to represent what is being inserted? Sounds like a lot of duplication.

@rchl
Copy link
Member

rchl commented Apr 29, 2020

I agree it's kinda unnecessary and busy. I believe that duplication would be gone if the startsWith "optimization" would be applied (#990 (comment)).

@rwols
Copy link
Member Author

rwols commented Apr 29, 2020

It's not guaranteed that if one LSP-label starts with its associated LSP-filterText, then all LSP-labels start with their associated LSP-filterText.

For instance look at the metals language server. We have a funky e as filterText, but the label is Implement all members, something totally different.

However from some testing I found that most completion items from metals do have the "starts with" property.

But I fear that it might be confusing for users. We could definitely give it a try though. Also, carefully look at the behavior of ST's fuzzy string matching and see if it doesn't deteriorate when using the LSP-label for the trigger.

@rwols
Copy link
Member Author

rwols commented Apr 29, 2020

See also: sublimehq/sublime_text#819

Using filterText is safer to use as it should contain only alphanumeric characters (PowershellEditorServices language server seems to have a different opinion on this).

@FichteFoll
Copy link

Indeed, I was reminded about sublimehq/sublime_text#819 earlier today and this is definitely a show stopper for non-alphanumerics in the trigger. Trying to find a general algorhithm to remove redundant duplication when it makes sense, but leave it when it wouldn't sounds like a doable but tough endeavour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants