-
Notifications
You must be signed in to change notification settings - Fork 37
Depend on python-lsp-server #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1489e85
80ac5fa
4399194
817c4ef
06cb3c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
File that contains the pyls plugin mypy-ls. | ||
File that contains the pylsp plugin mypy-ls. | ||
|
||
Created on Fri Jul 10 09:53:57 2020 | ||
|
||
|
@@ -12,9 +12,9 @@ | |
import os.path | ||
import logging | ||
from mypy import api as mypy_api | ||
from pyls import hookimpl | ||
from pyls.workspace import Document, Workspace | ||
from pyls.config.config import Config | ||
from pylsp import hookimpl | ||
from pylsp.workspace import Document, Workspace | ||
from pylsp.config.config import Config | ||
from typing import Optional, Dict, Any, IO, List | ||
import atexit | ||
|
||
|
@@ -27,7 +27,9 @@ | |
tmpFile: Optional[IO[str]] = None | ||
|
||
|
||
def parse_line(line: str, document: Optional[Document] = None) -> Optional[Dict[str, Any]]: | ||
def parse_line( | ||
line: str, document: Optional[Document] = None | ||
) -> Optional[Dict[str, Any]]: | ||
""" | ||
Return a language-server diagnostic from a line of the Mypy error report. | ||
|
||
|
@@ -54,51 +56,53 @@ def parse_line(line: str, document: Optional[Document] = None) -> Optional[Dict[ | |
if file_path != "<string>": # live mode | ||
# results from other files can be included, but we cannot return | ||
# them. | ||
if document and document.path and not document.path.endswith( | ||
file_path): | ||
log.warning("discarding result for %s against %s", file_path, | ||
document.path) | ||
if document and document.path and not document.path.endswith(file_path): | ||
log.warning( | ||
"discarding result for %s against %s", file_path, document.path | ||
) | ||
return None | ||
|
||
lineno = int(linenoStr or 1) - 1 # 0-based line number | ||
offset = int(offsetStr or 1) - 1 # 0-based offset | ||
errno = 2 | ||
if severity == 'error': | ||
if severity == "error": | ||
errno = 1 | ||
diag: Dict[str, Any] = { | ||
'source': 'mypy', | ||
'range': { | ||
'start': {'line': lineno, 'character': offset}, | ||
"source": "mypy", | ||
"range": { | ||
"start": {"line": lineno, "character": offset}, | ||
# There may be a better solution, but mypy does not provide end | ||
'end': {'line': lineno, 'character': offset + 1} | ||
"end": {"line": lineno, "character": offset + 1}, | ||
}, | ||
'message': msg, | ||
'severity': errno | ||
"message": msg, | ||
"severity": errno, | ||
} | ||
if document: | ||
# although mypy does not provide the end of the affected range, we | ||
# can make a good guess by highlighting the word that Mypy flagged | ||
word = document.word_at_position(diag['range']['start']) | ||
word = document.word_at_position(diag["range"]["start"]) | ||
if word: | ||
diag['range']['end']['character'] = ( | ||
diag['range']['start']['character'] + len(word)) | ||
diag["range"]["end"]["character"] = diag["range"]["start"][ | ||
"character" | ||
] + len(word) | ||
|
||
return diag | ||
return None | ||
|
||
|
||
@hookimpl | ||
def pyls_lint(config: Config, workspace: Workspace, document: Document, | ||
is_saved: bool) -> List[Dict[str, Any]]: | ||
def pylsp_lint( | ||
config: Config, workspace: Workspace, document: Document, is_saved: bool | ||
) -> List[Dict[str, Any]]: | ||
""" | ||
Lints. | ||
|
||
Parameters | ||
---------- | ||
config : Config | ||
The pyls config. | ||
The pylsp config. | ||
workspace : Workspace | ||
The pyls workspace. | ||
The pylsp workspace. | ||
document : Document | ||
The document to be linted. | ||
is_saved : bool | ||
|
@@ -110,27 +114,25 @@ def pyls_lint(config: Config, workspace: Workspace, document: Document, | |
List of the linting data. | ||
|
||
""" | ||
settings = config.plugin_settings('mypy-ls') | ||
live_mode = settings.get('live_mode', True) | ||
args = ['--incremental', | ||
'--show-column-numbers', | ||
'--follow-imports', 'silent'] | ||
settings = config.plugin_settings("mypy-ls") | ||
live_mode = settings.get("live_mode", True) | ||
args = ["--incremental", "--show-column-numbers", "--follow-imports", "silent"] | ||
|
||
global tmpFile | ||
if live_mode and not is_saved and tmpFile: | ||
tmpFile = open(tmpFile.name, "w") | ||
tmpFile.write(document.source) | ||
tmpFile.close() | ||
args.extend(['--shadow-file', document.path, tmpFile.name]) | ||
args.extend(["--shadow-file", document.path, tmpFile.name]) | ||
elif not is_saved: | ||
return [] | ||
|
||
if mypyConfigFile: | ||
args.append('--config-file') | ||
args.append("--config-file") | ||
args.append(mypyConfigFile) | ||
args.append(document.path) | ||
if settings.get('strict', False): | ||
args.append('--strict') | ||
if settings.get("strict", False): | ||
args.append("--strict") | ||
|
||
report, errors, _ = mypy_api.run(args) | ||
|
||
|
@@ -144,14 +146,14 @@ def pyls_lint(config: Config, workspace: Workspace, document: Document, | |
|
||
|
||
@hookimpl | ||
def pyls_settings(config: Config) -> Dict[str, Dict[str, Dict[str, str]]]: | ||
def pylsp_settings(config: Config) -> Dict[str, Dict[str, Dict[str, str]]]: | ||
""" | ||
Read the settings. | ||
|
||
Parameters | ||
---------- | ||
config : Config | ||
The pyls config. | ||
The pylsp config. | ||
|
||
Returns | ||
------- | ||
|
@@ -187,10 +189,11 @@ def init(workspace: str) -> Dict[str, str]: | |
configuration = eval(file.read()) | ||
global mypyConfigFile | ||
mypyConfigFile = findConfigFile(workspace, "mypy.ini") | ||
if (("enabled" not in configuration or configuration["enabled"]) | ||
and ("live_mode" not in configuration or configuration["live_mode"])): | ||
if ("enabled" not in configuration or configuration["enabled"]) and ( | ||
"live_mode" not in configuration or configuration["live_mode"] | ||
): | ||
global tmpFile | ||
tmpFile = tempfile.NamedTemporaryFile('w', delete=False) | ||
tmpFile = tempfile.NamedTemporaryFile("w", delete=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't realize that my editor automatically blackens all Python files, I didn't mean to push these changes. I will revert them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some uniform formatting would probably be good and I did a very poor job in that regard. Therefore, this is fine with me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh well I already pushed a revert. If you are OK with formatting with black I will open an issue and do it properly, with a pre-commit hook probably. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would be nice |
||
tmpFile.close() | ||
return configuration | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
python-language-server>=0.34.0 | ||
mypy | ||
future;python_version < '3' | ||
flake8 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where do the additional requirements come from? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I think they came from the old pyls-mypy branch I based my changes on (see tomv564#52). I will fix it. |
||
configparser | ||
python-lsp-server | ||
mypy;python_version >= '3.2' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was my editor replacing the tabs with spaces, sorry about that.