Skip to content
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

Add enabled setting to control linting #475

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Pylint extension for Visual Studio Code

A Visual Studio Code extension with support for the Pylint linter. This extension ships with `pylint=3.0.2`.
A Visual Studio Code extension with support for the Pylint linter. This extension ships with `pylint=3.0.3`.

> **Note**: The minimum version of Pylint this extension supports is `2.12.2`.

Expand Down Expand Up @@ -32,13 +32,14 @@ There are several settings you can configure to customize the behavior of this e
| ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| pylint.args | `[]` | Arguments passed to Pylint for linting Python files. Each argument should be provided as a separate string in the array. <br> Examples: <br>- `"pylint.args": ["--rcfile=<file>"]` <br> - `"pylint.args": ["--disable=C0111", "--max-line-length=120"]` |
| pylint.cwd | `${workspaceFolder}` | Sets the current working directory used to lint Python files with Pylint. By default, it uses the root directory of the workspace `${workspaceFolder}`. You can set it to `${fileDirname}` to use the parent folder of the file being linted as the working directory for Pylint. |
| pylint.enabled | `true` | Enable/disable linting Python files with Pylint. This setting can be applied globally or at workspace level. The server itself will continue to moitor events but will not perform linting or code actions if linting is disabled. |
| pylint.severity | `{ "convention": "Information", "error": "Error", "fatal": "Error", "refactor": "Hint", "warning": "Warning", "info": "Information" }` | Mapping of Pylint's message types to VS Code's diagnostic severity levels as displayed in the Problems window. You can also use it to override specific Pylint error codes. E.g. `{ "convention": "Information", "error": "Error", "fatal": "Error", "refactor": "Hint", "warning": "Warning", "W0611": "Error", "undefined-variable": "Warning" }` |
| pylint.path | `[]` | "Path or command to be used by the extension to lint Python files with Pylint. Accepts an array of a single or multiple strings. If passing a command, each argument should be provided as a separate string in the array. If set to `["pylint"]`, it will use the version of Pylint available in the `PATH` environment variable. Note: Using this option may slowdown linting. <br>Examples: <br>- `"pylint.path" : ["~/global_env/pylint"]` <br>- `"pylint.path" : ["conda", "run", "-n", "lint_env", "python", "-m", "pylint"]` <br>- `"pylint.path" : ["pylint"]` <br>- `"pylint.path" : ["${interpreter}", "-m", "pylint"]` |
| pylint.interpreter | `[]` | Path to a Python executable or a command that will be used to launch the Pylint server and any subprocess. Accepts an array of a single or multiple strings. When set to `[]`, the extension will use the path to the selected Python interpreter. If passing a command, each argument should be provided as a separate string in the array. |
| pylint.importStrategy | `useBundled` | Defines which Pylint binary to be used to lint Python files. When set to `useBundled`, the extension will use the Pylint binary that is shipped with the extension. When set to `fromEnvironment`, the extension will attempt to use the Pylint binary and all dependencies that are available in the currently selected environment. Note: If the extension can't find a valid Pylint binary in the selected environment, it will fallback to using the Pylint binary that is shipped with the extension. This setting will be overriden if `pylint.path` is set. |
| pylint.showNotification | `off` | Controls when notifications are shown by this extension. Accepted values are `onError`, `onWarning`, `always` and `off`. |
| pylint.lintOnChange | `false` | Enable linting Python files with Pylint as you type. |
| pylint.ignorePatterns | `[]` | Configure [glob patterns](https://docs.python.org/3/library/fnmatch.html) as supported by the fnmatch Python library to exclude files or folders from being linted with Pylint. |
| pylint.ignorePatterns | `[]` | Configure [glob patterns](https://docs.python.org/3/library/fnmatch.html) as supported by the fnmatch Python library to exclude files or folders from being linted with Pylint. |

The following variables are supported for substitution in the `pylint.args`, `pylint.cwd`, `pylint.path`, `pylint.interpreter` and `pylint.ignorePatterns` settings:

Expand Down
16 changes: 12 additions & 4 deletions bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,15 @@ def solutions(
)
def code_action(params: lsp.CodeActionParams) -> List[lsp.CodeAction]:
"""LSP handler for textDocument/codeAction request."""
diagnostics = list(
d for d in params.context.diagnostics if d.source == TOOL_DISPLAY
)

document = LSP_SERVER.workspace.get_document(params.text_document.uri)

settings = copy.deepcopy(_get_settings_by_document(document))
code_actions = []
if not settings["enabled"]:
return code_actions

diagnostics = (d for d in params.context.diagnostics if d.source == TOOL_DISPLAY)

for diagnostic in diagnostics:
func = QUICK_FIXES.solutions(diagnostic.code)
if func:
Expand Down Expand Up @@ -568,6 +570,7 @@ def _log_version_info() -> None:
# *****************************************************
def _get_global_defaults():
return {
"enabled": GLOBAL_SETTINGS.get("enabled", True),
"path": GLOBAL_SETTINGS.get("path", []),
"interpreter": GLOBAL_SETTINGS.get("interpreter", [sys.executable]),
"args": GLOBAL_SETTINGS.get("args", []),
Expand Down Expand Up @@ -687,6 +690,11 @@ def _run_tool_on_document(
# deep copy here to prevent accidentally updating global settings.
settings = copy.deepcopy(_get_settings_by_document(document))

if not settings["enabled"]:
log_warning(f"Skipping file [Linting Disabled]: {document.path}")
log_warning("See `pylint.enabled` in settings.json to enabling linting.")
return None

if str(document.uri).startswith("vscode-notebook-cell"):
log_warning(f"Skipping notebook cells [Not Supported]: {str(document.uri)}")
return None
Expand Down
Loading
Loading