Repository: spyder-ide/spyder

Issue:
code autocomplete inserts extra characters
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

### Issue Report Checklist

* [x] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [x] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [x] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [ ] Tried basic troubleshooting (if a bug/error)
    * [x] Restarted Spyder
    * [x] Reset preferences with ``spyder --reset``
    * [ ] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [x] Tried the other applicable steps from the Troubleshooting Guide
* [x] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description
code autocomplete inserts additional characters that look like markup:
"def __init__(${1:self})$0" instead of "def __init__(self)"
tested on two machines with miniconda (windows and ubuntu)

### What steps reproduce the problem?

1.  type "def __i" under class definition 
2.  press ctrl+space
3.  "def __init__(self)" is correctly suggested
4.  when suggestion is selected, "def __init__(${1:self})$0" is inserted instead of "def __init__(self)"

### What is the expected output? What do you see instead?
when suggestion is selected, "def __init__(${1:self})$0" is inserted instead of "def __init__(self)"


### Paste Traceback/Error Below (if applicable)
<!--- Copy from error dialog or View > Panes > Internal Console --->

```python-traceback

PASTE TRACEBACK HERE

```

## 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 version: 4.0.0
* Python version: 3.6
* Qt version: 
* PyQt version: 
* Operating System name/version: 

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
PASTE DEPENDENCIES HERE
```



Discussion:
The Kite team saw a similar piece of feedback. Pasting it below:
"If you have typed more than the minimum number of characters required for Kite to suggest a completion, Kite will insert the suggestion before the characters typed after the minimum count. This results in errors most of the time and using completions slows down my typing speed rather than speeding it up and simplifying it."
I encountered the same problem, when the function starting with an underscore and this function had parameters, there was a problem with function code completion.

See #10924
I also can report having the same problem in Spyder 4.0.1.

I tried the  simple code below. Instead of getting the proper function signature after accepting the suggestion, I also got some unwanted text.
```
class Test():
    def __init__(${1:self})$0:        
    def __format__(${1:self}, format_spec):
    def __sizeof__(${1:self})$0:
    def __str__(${1:self})$0:    
    def __reduce__(${1:self})$0:
```
@andfoy, please take a look at this one.

Resolving patch:
diff --git a/spyder/utils/snippets/lexer.py b/spyder/utils/snippets/lexer.py
--- a/spyder/utils/snippets/lexer.py
+++ b/spyder/utils/snippets/lexer.py
@@ -32,8 +32,8 @@
     'slash': r'^/$',
     'comma': r'^,$',
     'left_curly_name': r'^\{[a-zA-Z_]\w*$',
-    'name': r'^(?=[\w])(?=[^_\d])\w*$',
-    'symbol': r'^(?=[^\w]$)(?=[^\s]$)|_'
+    'name': r'^(?=[\w])(?=[^\d])\w*$',
+    'symbol': r'^(?=[^_\w]$)(?=[^\s]$)'
 })
 
 token_regex = {name: re.compile(r) for name, r in token_regex.items()}


Test patch: