Repository: spyder-ide/spyder

Issue:
TypeError when inserting text in code snippet
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

I got TypeError while typing a serveral lines snippets under indentation.


The snippet I used is as follows.

```python
fig = figure()
ax1 = fig.add_subplot(1, 1, 1)
ax1.plot($1)
ax1.set_xlabel('$2')
ax1.set_ylabel('$3')
ax1.set_title('$4')
ax1.grid(True)
fig.tight_layout()
$0
```

![스크린샷, 2020-11-03 06-33-35](https://user-images.githubusercontent.com/22970578/97923094-cf9bd000-1da0-11eb-98aa-2882a33411b9.png)

The location where the snippet was entered is as follows.

```python
# -*- coding: utf-8 -*-
"""Example."""
# Third party imports
from matplotlib.pyplot import figure
from numpy import linspace

data = linspace(1, 2, 3)

if __name__ == '__main__':
    here
```

![Peek 2020-11-03 06-42](https://user-images.githubusercontent.com/22970578/97923181-ee01cb80-1da0-11eb-8ce1-cd1e2f0518d3.gif)


Thank you.

### Traceback
```python-traceback
  File "/home/ok97465/codepy/spyder/spyder/plugins/editor/extensions/snippets.py", line 229, in _on_key_pressed
    self._process_text(text)
  File "/home/ok97465/codepy/spyder/spyder/plugins/editor/extensions/snippets.py", line 52, in wrapper
    return f(self, *args, **kwargs)
  File "/home/ok97465/codepy/spyder/spyder/plugins/editor/extensions/snippets.py", line 237, in _process_text
    self.insert_text(text, line, column)
  File "/home/ok97465/codepy/spyder/spyder/plugins/editor/extensions/snippets.py", line 379, in insert_text
    position = ((x, y), (x, y + 1))
TypeError: can only concatenate tuple (not "int") to tuple
```

## Versions

* Spyder version: 4.2.0.dev0 7224013fc
* Python version: 3.8.5
* Qt version: 5.12.5
* PyQt5 version: 5.12.3
* Operating System: Linux 5.4.0-52-generic

### Dependencies

```

# Mandatory:
atomicwrites >=1.2.0           :  1.4.0 (OK)
chardet >=2.0.0                :  3.0.4 (OK)
cloudpickle >=0.5.0            :  1.6.0 (OK)
diff_match_patch >=20181111    :  20200713 (OK)
intervaltree >=3.0.2           :  3.1.0 (OK)
IPython >=4.0                  :  7.18.1 (OK)
jedi =0.17.2                   :  0.17.2 (OK)
jsonschema >=3.2.0             :  3.2.0 (OK)
keyring >=17.0.0               :  21.4.0 (OK)
nbconvert >=4.0                :  6.0.7 (OK)
numpydoc >=0.6.0               :  1.1.0 (OK)
parso =0.7.0                   :  0.7.0 (OK)
pexpect >=4.4.0                :  4.8.0 (OK)
pickleshare >=0.4              :  0.7.5 (OK)
psutil >=5.3                   :  5.7.2 (OK)
pygments >=2.0                 :  2.7.2 (OK)
pylint >=1.0                   :  2.6.0 (OK)
pyls >=0.35.0;<1.0.0           :  v4.1.4+782.g7224013fc (OK)
pyls_black >=0.4.6             :  0.4.6 (OK)
pyls_spyder >=0.1.1            :  0.1.1 (OK)
qdarkstyle >=2.8               :  2.8.1 (OK)
qtawesome >=0.5.7              :  1.0.1 (OK)
qtconsole >=4.7.7              :  4.7.7 (OK)
qtpy >=1.5.0                   :  1.9.0 (OK)
rtree >=0.8.3                  :  0.9.4 (OK)
setuptools >=39.0.0            :  50.3.0.post20201006 (OK)
sphinx >=0.6.6                 :  3.2.1 (OK)
spyder_kernels >=1.9.4;<1.10.0 :  1.10.0.dev0 (OK)
three_merge >=0.1.1            :  0.1.1 (OK)
watchdog >=0.10.3              :  0.10.3 (OK)
xdg >=0.26                     :  0.27 (OK)
zmq >=17                       :  19.0.2 (OK)

# Optional:
cython >=0.21                  :  0.29.21 (OK)
matplotlib >=2.0.0             :  3.3.2 (OK)
numpy >=1.7                    :  1.19.2 (OK)
pandas >=1.1.1                 :  1.1.3 (OK)
scipy >=0.17.0                 :  1.5.2 (OK)
sympy >=0.7.3                  :  1.6.2 (OK)
```



Discussion:
Thanks for reporting @ok97465. @andfoy will take a look at this problem.
@ok97465, do you insert another character besides `x` on the snippet placeholder? I'm unable to reproduce the error
@andfoy When I tried to type xlabel, I got TypeError. I tried it after deleting spyder-py3-dev.
@ok97465, I could reproduce it. We'll try to fix it with @andfoy tomorrow.

Resolving patch:
diff --git a/spyder/plugins/editor/extensions/snippets.py b/spyder/plugins/editor/extensions/snippets.py
--- a/spyder/plugins/editor/extensions/snippets.py
+++ b/spyder/plugins/editor/extensions/snippets.py
@@ -675,16 +675,19 @@ def _find_node_by_position(self, line, col):
         point = (line, col) * 2
         node_numbers = list(self.index.intersection(point))
         current_node, nearest_text, nearest_snippet = None, None, None
+
         if len(node_numbers) > 0:
             for node_number in node_numbers:
                 current_node = self.node_position[node_number][-1]
                 if isinstance(current_node, nodes.SnippetASTNode):
                     nearest_snippet = current_node
                 elif isinstance(current_node, nodes.TextNode):
-                    nearest_text = current_node
+                    if node_number != 0:
+                        nearest_text = current_node
                 elif isinstance(current_node, nodes.LeafNode):
                     if current_node.name == 'EPSILON':
                         break
+
         if nearest_text is not None:
             node_id = id(current_node)
             text_ids = set([id(token) for token in nearest_text.tokens])


Test patch: