Repository: spyder-ide/spyder

Issue:
AttributeError: 'QTextBlock' object has no attribute '_selection'
## Description
If enabling "underline error" in the latest commit, an error occurs.

Congratulations on the release of spyder 4.0. :tada:

### What steps will reproduce the problem?
![kk](https://user-images.githubusercontent.com/22970578/70804465-eb4d5f00-1df9-11ea-82cc-ceee15b1c82f.gif)

<!--- You can use Markdown here --->

1. type invalid line
  .       a = 1
 2. Enable underline errors and warnings

### Traceback
```python-traceback
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 1096, in process_diagnostics
    self.process_code_analysis(params['params'])
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 2300, in process_code_analysis
    block._selection(),
AttributeError: 'QTextBlock' object has no attribute '_selection'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 1034, in emit
    msg = self.format(record)
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 880, in format
    return fmt.format(record)
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 619, in format
    record.message = record.getMessage()
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 380, in getMessage
    msg = msg % self.args
ValueError: incomplete format
Call stack:
  File "bootstrap.py", line 186, in <module>
    start.main()
  File "/home/ok97465/python/spyder/spyder/app/start.py", line 204, in main
    mainwindow.main()
  File "/home/ok97465/python/spyder/spyder/app/mainwindow.py", line 3718, in main
    mainwindow = run_spyder(app, options, args)
  File "/home/ok97465/python/spyder/spyder/app/mainwindow.py", line 3600, in run_spyder
    app.exec_()
  File "/home/ok97465/python/spyder/spyder/plugins/completion/languageserver/client.py", line 369, in on_msg_received
    handler(resp['params'])
  File "/home/ok97465/python/spyder/spyder/plugins/completion/languageserver/providers/document.py", line 43, in process_document_diagnostics
    {'params': diagnostics})
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 940, in handle_response
    handler(params)
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 1102, in process_diagnostics
    self.log_lsp_handle_errors("Error when processing linting")
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 971, in log_lsp_handle_errors
    logger.error('%', 1, stack_info=True)
Message: '%'
Arguments: (1,)
--- Logging error ---
Traceback (most recent call last):
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 1096, in process_diagnostics
    self.process_code_analysis(params['params'])
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 2300, in process_code_analysis
    block._selection(),
AttributeError: 'QTextBlock' object has no attribute '_selection'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 1034, in emit
    msg = self.format(record)
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 880, in format
    return fmt.format(record)
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 619, in format
    record.message = record.getMessage()
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 380, in getMessage
    msg = msg % self.args
ValueError: incomplete format
Call stack:
  File "bootstrap.py", line 186, in <module>
    start.main()
  File "/home/ok97465/python/spyder/spyder/app/start.py", line 204, in main
    mainwindow.main()
  File "/home/ok97465/python/spyder/spyder/app/mainwindow.py", line 3718, in main
    mainwindow = run_spyder(app, options, args)
  File "/home/ok97465/python/spyder/spyder/app/mainwindow.py", line 3600, in run_spyder
    app.exec_()
  File "/home/ok97465/python/spyder/spyder/plugins/completion/languageserver/client.py", line 369, in on_msg_received
    handler(resp['params'])
  File "/home/ok97465/python/spyder/spyder/plugins/completion/languageserver/providers/document.py", line 43, in process_document_diagnostics
    {'params': diagnostics})
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 940, in handle_response
    handler(params)
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 1102, in process_diagnostics
    self.log_lsp_handle_errors("Error when processing linting")
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 971, in log_lsp_handle_errors
    logger.error('%', 1, stack_info=True)
Message: '%'
Arguments: (1,)
```

## Versions

* Spyder version: 5.0.0.dev0 a492a39d2
* Python version: 3.7.3
* Qt version: 5.12.5
* PyQt5 version: 5.12.3
* Operating System: Linux 5.4.2-1-MANJARO

### Dependencies

```
cloudpickle >=0.5.0          :  1.2.2 (OK)
pygments >=2.0               :  2.4.2 (OK)
qtconsole >=4.6.0            :  4.6.0 (OK)
nbconvert >=4.0              :  5.6.0 (OK)
sphinx >=0.6.6               :  2.2.1 (OK)
pylint >=0.25                :  2.4.3 (OK)
psutil >=0.3                 :  5.6.3 (OK)
qtawesome >=0.5.7            :  0.6.0 (OK)
qtpy >=1.5.0                 :  1.9.0 (OK)
pickleshare >=0.4            :  0.7.5 (OK)
zmq >=17                     :  18.1.0 (OK)
chardet >=2.0.0              :  3.0.4 (OK)
numpydoc >=0.6.0             :  0.9.1 (OK)
spyder_kernels >=1.8.1;<2.0.0:  1.8.1 (OK)
qdarkstyle >=2.7             :  2.7 (OK)
atomicwrites >=1.2.0         :  1.3.0 (OK)
diff_match_patch >=20181111  :  20181111 (OK)
intervaltree                 :  None (OK)
watchdog                     :  None (OK)
keyring                      :  None (OK)
pexpect >=4.4.0              :  4.7.0 (OK)
sympy >=0.7.3                :  1.4 (OK)
cython >=0.21                :  0.29.13 (OK)
IPython >=4.0                :  7.9.0 (OK)
matplotlib >=2.0.0           :  3.1.2 (OK)
pandas >=0.13.1              :  0.25.2 (OK)
numpy >=1.7                  :  1.17.3 (OK)
scipy >=0.17.0               :  1.3.2 (OK)
pyls >=0.31.2;<0.32.0        :  0.31.2 (OK)
xdg >=0.26                   :  0.26 (OK)
rtree >=0.8.3                :  0.8.3 (OK)
```

AttributeError: 'QTextBlock' object has no attribute '_selection'
## Description
If enabling "underline error" in the latest commit, an error occurs.

Congratulations on the release of spyder 4.0. :tada:

### What steps will reproduce the problem?
![kk](https://user-images.githubusercontent.com/22970578/70804465-eb4d5f00-1df9-11ea-82cc-ceee15b1c82f.gif)

<!--- You can use Markdown here --->

1. type invalid line
  .       a = 1
 2. Enable underline errors and warnings

### Traceback
```python-traceback
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 1096, in process_diagnostics
    self.process_code_analysis(params['params'])
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 2300, in process_code_analysis
    block._selection(),
AttributeError: 'QTextBlock' object has no attribute '_selection'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 1034, in emit
    msg = self.format(record)
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 880, in format
    return fmt.format(record)
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 619, in format
    record.message = record.getMessage()
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 380, in getMessage
    msg = msg % self.args
ValueError: incomplete format
Call stack:
  File "bootstrap.py", line 186, in <module>
    start.main()
  File "/home/ok97465/python/spyder/spyder/app/start.py", line 204, in main
    mainwindow.main()
  File "/home/ok97465/python/spyder/spyder/app/mainwindow.py", line 3718, in main
    mainwindow = run_spyder(app, options, args)
  File "/home/ok97465/python/spyder/spyder/app/mainwindow.py", line 3600, in run_spyder
    app.exec_()
  File "/home/ok97465/python/spyder/spyder/plugins/completion/languageserver/client.py", line 369, in on_msg_received
    handler(resp['params'])
  File "/home/ok97465/python/spyder/spyder/plugins/completion/languageserver/providers/document.py", line 43, in process_document_diagnostics
    {'params': diagnostics})
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 940, in handle_response
    handler(params)
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 1102, in process_diagnostics
    self.log_lsp_handle_errors("Error when processing linting")
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 971, in log_lsp_handle_errors
    logger.error('%', 1, stack_info=True)
Message: '%'
Arguments: (1,)
--- Logging error ---
Traceback (most recent call last):
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 1096, in process_diagnostics
    self.process_code_analysis(params['params'])
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 2300, in process_code_analysis
    block._selection(),
AttributeError: 'QTextBlock' object has no attribute '_selection'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 1034, in emit
    msg = self.format(record)
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 880, in format
    return fmt.format(record)
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 619, in format
    record.message = record.getMessage()
  File "/home/ok97465/anaconda3/lib/python3.7/logging/__init__.py", line 380, in getMessage
    msg = msg % self.args
ValueError: incomplete format
Call stack:
  File "bootstrap.py", line 186, in <module>
    start.main()
  File "/home/ok97465/python/spyder/spyder/app/start.py", line 204, in main
    mainwindow.main()
  File "/home/ok97465/python/spyder/spyder/app/mainwindow.py", line 3718, in main
    mainwindow = run_spyder(app, options, args)
  File "/home/ok97465/python/spyder/spyder/app/mainwindow.py", line 3600, in run_spyder
    app.exec_()
  File "/home/ok97465/python/spyder/spyder/plugins/completion/languageserver/client.py", line 369, in on_msg_received
    handler(resp['params'])
  File "/home/ok97465/python/spyder/spyder/plugins/completion/languageserver/providers/document.py", line 43, in process_document_diagnostics
    {'params': diagnostics})
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 940, in handle_response
    handler(params)
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 1102, in process_diagnostics
    self.log_lsp_handle_errors("Error when processing linting")
  File "/home/ok97465/python/spyder/spyder/plugins/editor/widgets/codeeditor.py", line 971, in log_lsp_handle_errors
    logger.error('%', 1, stack_info=True)
Message: '%'
Arguments: (1,)
```

## Versions

* Spyder version: 5.0.0.dev0 a492a39d2
* Python version: 3.7.3
* Qt version: 5.12.5
* PyQt5 version: 5.12.3
* Operating System: Linux 5.4.2-1-MANJARO

### Dependencies

```
cloudpickle >=0.5.0          :  1.2.2 (OK)
pygments >=2.0               :  2.4.2 (OK)
qtconsole >=4.6.0            :  4.6.0 (OK)
nbconvert >=4.0              :  5.6.0 (OK)
sphinx >=0.6.6               :  2.2.1 (OK)
pylint >=0.25                :  2.4.3 (OK)
psutil >=0.3                 :  5.6.3 (OK)
qtawesome >=0.5.7            :  0.6.0 (OK)
qtpy >=1.5.0                 :  1.9.0 (OK)
pickleshare >=0.4            :  0.7.5 (OK)
zmq >=17                     :  18.1.0 (OK)
chardet >=2.0.0              :  3.0.4 (OK)
numpydoc >=0.6.0             :  0.9.1 (OK)
spyder_kernels >=1.8.1;<2.0.0:  1.8.1 (OK)
qdarkstyle >=2.7             :  2.7 (OK)
atomicwrites >=1.2.0         :  1.3.0 (OK)
diff_match_patch >=20181111  :  20181111 (OK)
intervaltree                 :  None (OK)
watchdog                     :  None (OK)
keyring                      :  None (OK)
pexpect >=4.4.0              :  4.7.0 (OK)
sympy >=0.7.3                :  1.4 (OK)
cython >=0.21                :  0.29.13 (OK)
IPython >=4.0                :  7.9.0 (OK)
matplotlib >=2.0.0           :  3.1.2 (OK)
pandas >=0.13.1              :  0.25.2 (OK)
numpy >=1.7                  :  1.17.3 (OK)
scipy >=0.17.0               :  1.3.2 (OK)
pyls >=0.31.2;<0.32.0        :  0.31.2 (OK)
xdg >=0.26                   :  0.26 (OK)
rtree >=0.8.3                :  0.8.3 (OK)
```



Discussion:

Resolving patch:
diff --git a/spyder/plugins/editor/widgets/codeeditor.py b/spyder/plugins/editor/widgets/codeeditor.py
--- a/spyder/plugins/editor/widgets/codeeditor.py
+++ b/spyder/plugins/editor/widgets/codeeditor.py
@@ -2280,9 +2280,6 @@ def process_code_analysis(self, results):
             block = document.findBlockByNumber(start['line'])
             error = severity == DiagnosticSeverity.ERROR
             color = self.error_color if error else self.warning_color
-
-            block.selection_start = start
-            block.selection_end = end
             color = QColor(color)
             color.setAlpha(255)
 
@@ -2290,6 +2287,8 @@ def process_code_analysis(self, results):
             if not data:
                 data = BlockUserData(self)
 
+            data.selection_start = start
+            data.selection_end = end
             data.code_analysis.append((source, code, severity, message))
             block.setUserData(data)
             block.color = color
@@ -2297,7 +2296,7 @@ def process_code_analysis(self, results):
             # Underline errors and warnings in this editor.
             if self.underline_errors_enabled:
                 self.highlight_selection('code_analysis_underline',
-                                         block._selection(),
+                                         data._selection(),
                                          underline_color=block.color)
 
         self.sig_process_code_analysis.emit()


Test patch: