-
-
Notifications
You must be signed in to change notification settings - Fork 766
Expand file tree
/
Copy pathlint.py
More file actions
77 lines (57 loc) · 2.2 KB
/
lint.py
File metadata and controls
77 lines (57 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Ruff integration for python-mode linting."""
from .environment import env
from .ruff_integration import run_ruff_check, check_ruff_available, validate_configuration
import os.path
def code_check():
"""Run ruff check on current file.
This function replaces the previous pylama integration with ruff.
It maintains compatibility with existing pymode configuration variables.
:return bool:
"""
if not env.curbuf.name:
return env.stop()
# Check if Ruff is enabled
if not env.var('g:pymode_ruff_enabled', silence=True, default=True):
return env.stop()
# Check if ruff is available
if not check_ruff_available():
env.error("Ruff is not available. Please install ruff: pip install ruff")
return env.stop()
# Validate configuration and show warnings
warnings = validate_configuration()
for warning in warnings:
env.message(f"Warning: {warning}")
# Get file content from current buffer
content = '\n'.join(env.curbuf) + '\n'
file_path = env.curbuf.name
# Use relpath if possible, but handle Windows drive letter differences
try:
path = os.path.relpath(file_path, env.curdir)
env.debug("Start ruff code check: ", path)
except ValueError:
# On Windows, relpath fails if paths are on different drives
# Fall back to absolute path in this case
env.debug("Start ruff code check (abs path): ", file_path)
path = file_path
# Run ruff check
errors = run_ruff_check(file_path, content)
env.debug("Find errors: ", len(errors))
# Apply sorting if configured
sort_rules = env.var('g:pymode_lint_sort', default=[])
def __sort(e):
try:
return sort_rules.index(e.get('type'))
except ValueError:
return 999
if sort_rules:
env.debug("Find sorting: ", sort_rules)
errors = sorted(errors, key=__sort)
# Convert to vim-compatible format
errors_list = []
for error in errors:
err_dict = error.to_dict()
err_dict['bufnr'] = env.curbuf.number
errors_list.append(err_dict)
# Add to location list
env.run('g:PymodeLocList.current().extend', errors_list)
# ruff: noqa