Repository: sublimelsp/LSP

Issue:
[AC] AC not showing up with a "decorated" trigger
Note: check if only DYNAMIC_COMPLETIONS are affected.

When I supplement the completion by typing more characters, the completion widget goes away.

![Peek 2020-03-28 13-19](https://user-images.githubusercontent.com/2431823/77822869-db326480-70f6-11ea-82fa-207d26bdb433.gif)
<hr>
I don't get a completion widget at all sometimes. In the gif below I expect to at least see a completion for the "time" variable:

![Peek 2020-03-28 13-31](https://user-images.githubusercontent.com/2431823/77823050-76780980-70f8-11ea-806a-009f3d56e58a.gif)

I'm guessing this is due to incomplete completions. I'm not sure what the dynamic is here.

_Originally posted by @rwols in https://github.com/sublimelsp/LSP/pull/866#issuecomment-605441312_


Discussion:
As it turns out this is due to clangd putting a space in the label. We can remove the space with the command-line option `--header-insertion-decorators`.

With `--header-insertion-decorators=0`: (looks like there's a bug in the insertion logic)

![Peek 2020-04-04 15-57](https://user-images.githubusercontent.com/2431823/78452741-582c8380-768d-11ea-84b2-e38a92567d39.gif)

Without `--header-insertion-decorators=0`:

![Peek 2020-04-04 15-58](https://user-images.githubusercontent.com/2431823/78452765-7eeaba00-768d-11ea-8927-4783eeca8c75.gif)

Currently, we're mapping the `label` of an LSP completion item to the `trigger` of a sublime completion item. But as can be seen with clangd, this is wrong.

As I mentioned way back, the concept of `trigger` in SublimeText seems to map better to the `filterText` of an LSP completion item. However, that makes the completions look a bit bare.

This is with `trigger=item["label"]` and `--header-insertion-decorators=0`:

![Peek 2020-04-04 16-27](https://user-images.githubusercontent.com/2431823/78453358-74caba80-7691-11ea-8125-0b36451fefb5.gif)

This is with `trigger=item.get("filterText") or item["label"]` and `--header-insertion-decorators=0`:

![Peek 2020-04-04 16-28](https://user-images.githubusercontent.com/2431823/78453367-785e4180-7691-11ea-85cc-9fcf518a8825.gif)

What we need is a distinction between **label** and **trigger** in SublimeText. cc @jskinner
I wonder if "autocomplete_preserve_order": "none" would help in this case?
Indeed, setting auto_complete_preserve_order to "none" seems to fix the problem. Below is without `--header-insertion-decorators=0`.

![Peek 2020-04-04 17-01](https://user-images.githubusercontent.com/2431823/78454052-eefd3e00-7695-11ea-925b-af9d9da1e166.gif)

Based on this I am inclined to say that we should put `self.view.settings().set("auto_complete_preserve_order", "none")` in completion.py.

Still, what the user sees in the AC widget, and what the user is expected to type to trigger the completion are two different concepts.
After googling about `filterText` I cannot find much information about it other than "it is filter text".
I thought that `filterText` would be a substring of `label`, but some language servers actually seem to be making the `label` a substring of the `filterText`. Some language servers decide that there's no substring relation between `filterText` and `label` at all. At this point I'm beginning to wonder what the point of `filterText` is or whether it is just a vscode implementation detail leaking into the protocol. 

Resolving patch:
diff --git a/plugin/completion.py b/plugin/completion.py
--- a/plugin/completion.py
+++ b/plugin/completion.py
@@ -224,6 +224,8 @@ def initialize(self) -> None:
                 'triggerCharacters') or []
             if trigger_chars:
                 self.register_trigger_chars(session, trigger_chars)
+            # This is to make ST match with labels that have a weird prefix like a space character.
+            self.view.settings().set("auto_complete_preserve_order", "none")
 
     def _view_language(self, config_name: str) -> Optional[str]:
         languages = self.view.settings().get('lsp_language')


Test patch:
diff --git a/tests/setup.py b/tests/setup.py
--- a/tests/setup.py
+++ b/tests/setup.py
@@ -170,9 +170,6 @@ def init_view_settings(self) -> None:
         s("tab_size", 4)
         s("translate_tabs_to_spaces", False)
         s("word_wrap", False)
-        # ST4 removes completion items when "auto_complete_preserve_order" is not "none",
-        # see https://github.com/sublimelsp/LSP/pull/866#discussion_r380249385
-        s("auto_complete_preserve_order", "none")
 
     def get_view_event_listener(self, unique_attribute: str) -> 'Optional[ViewEventListener]':
         for listener in view_event_listeners[self.view.id()]: