Repository: spyder-ide/spyder

Issue:
No code folding at startup
## Problem Description

After commit https://github.com/spyder-ide/spyder/commit/1f7911b9f64683d0514dbce001be4ebf95456fb6, code folding is not initiated automatically at startup. It is activated though as soon as a change is registered in the document.

![code_folding](https://user-images.githubusercontent.com/10170372/75462932-076d9080-5953-11ea-8b9b-cab9d86f6278.gif)


## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder 5.0.0.dev0 (Commit: f21d862d0) 
* Python 3.7.3 64-bit | Qt 5.9.6 | PyQt5 5.9.2 | Windows 10 

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
# Mandatory:
atomicwrites >=1.2.0          :  1.3.0 (OK)
chardet >=2.0.0               :  3.0.4 (OK)
cloudpickle >=0.5.0           :  1.2.2 (OK)
diff_match_patch >=20181111   :  20181111 (OK)
intervaltree                  :  None (OK)
IPython >=4.0                 :  7.9.0 (OK)
jedi =0.14.1                  :  0.14.1 (OK)
nbconvert >=4.0               :  5.6.0 (OK)
numpydoc >=0.6.0              :  0.9.1 (OK)
paramiko >=2.4.0              :  2.6.0 (OK)
parso =0.5.2                  :  0.5.2 (OK)
pexpect >=4.4.0               :  4.7.0 (OK)
pickleshare >=0.4             :  0.7.5 (OK)
psutil >=5.3                  :  5.6.3 (OK)
pygments >=2.0                :  2.4.2 (OK)
pylint >=0.25                 :  2.4.3 (OK)
pyls >=0.31.2;<0.32.0         :  0.31.2 (OK)
qdarkstyle >=2.7              :  2.8 (OK)
qtawesome >=0.5.7             :  0.6.0.dev0 (OK)
qtconsole >=4.6.0             :  4.7.0.dev0 (OK)
qtpy >=1.5.0                  :  1.9.0 (OK)
rtree >=0.8.3                 :  0.8.3 (OK)
sphinx >=0.6.6                :  2.2.1 (OK)
spyder_kernels >=1.8.1;<2.0.0 :  2.0.0.dev0 (OK)
watchdog                      :  None (OK)
zmq >=17                      :  18.1.0 (OK)

# Optional:
cython >=0.21                 :  0.29.13 (OK)
matplotlib >=2.0.0            :  3.1.3 (OK)
numpy >=1.7                   :  1.17.2 (OK)
pandas >=0.13.1               :  0.25.2 (OK)
scipy >=0.17.0                :  1.3.1 (OK)
sympy >=0.7.3                 :  1.4 (OK)
```



Discussion:
@andfoy, please take a look at this one.
There seems to be some kind of race condition when there are two or more tabs already open. Actually, there are sometimes that the folding is displayed at stratup, but others that not
@impact27, could you also help us with this one? This is a regression introduced by your work in PR #11488.
Sorry @impact27, I talked too soon.

@andfoy, the faulty commit is 1f7911b9f64683d0514dbce001be4ebf95456fb6. Now we have to understand why.

Resolving patch:
diff --git a/spyder/plugins/completion/plugin.py b/spyder/plugins/completion/plugin.py
--- a/spyder/plugins/completion/plugin.py
+++ b/spyder/plugins/completion/plugin.py
@@ -74,6 +74,10 @@ class CompletionManager(SpyderCompletionPlugin):
             ),
         })
 
+    SKIP_INTERMEDIATE_REQUESTS = {
+        LSPRequestTypes.DOCUMENT_COMPLETION
+    }
+
     def __init__(self, parent, plugins=['lsp', 'kite', 'fallback']):
         SpyderCompletionPlugin.__init__(self, parent)
         self.clients = {}
@@ -141,15 +145,21 @@ def _maybe_send_locked(self, req_id):
         def send():
             # Needed to prevent the send of completions for old requests
             # See spyder-ide/spyder#10798
-            max_req_id = max(
-                [key for key, item in self.requests.items()
-                 if item['req_type'] == self.requests[req_id]['req_type']]
-                or [-1])
+            req_type = self.requests[req_id]['req_type']
+            response_instance = id(self.requests[req_id]['response_instance'])
+            send = True
+            if req_type in self.SKIP_INTERMEDIATE_REQUESTS:
+                max_req_id = max(
+                    [key for key, item in self.requests.items()
+                     if item['req_type'] == req_type
+                     and id(item['response_instance']) == response_instance]
+                    or [-1])
+                send = req_id == max_req_id
 
             del self.requests[req_id]
 
             # Response only to recent requests.
-            if req_id == max_req_id:
+            if send:
                 self.gather_and_send(request_responses)
 
         wait_for = set(source for source


Test patch: