Skip to content

Commit d57d904

Browse files
committed
warn about unreferenced import/variable/function
1 parent 2261042 commit d57d904

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

pyls/plugins/importmagic_lint.py

+31-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def pyls_commands():
3838

3939
@hookimpl
4040
def pyls_lint(document):
41-
"""Build a diagnostics of unresolved symbols. Every entry follows this format:
41+
"""Build a diagnostics of unresolved and unreferenced symbols.
42+
Every entry follows this format:
4243
{
4344
'source': 'importmagic',
4445
'range': {
@@ -51,8 +52,8 @@ def pyls_lint(document):
5152
'character': end_column,
5253
},
5354
},
54-
'message': 'Unresolved import <symbol>',
55-
'severity': lsp.DiagnosticSeverity.Hint,
55+
'message': message_to_be_displayed,
56+
'severity': sevirity_level,
5657
}
5758
5859
Args:
@@ -61,12 +62,13 @@ def pyls_lint(document):
6162
A list of dictionaries.
6263
"""
6364
scope = importmagic.Scope.from_source(document.source)
64-
unresolved, _unreferenced = scope.find_unresolved_and_unreferenced_symbols()
65+
unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
6566

6667
diagnostics = []
6768

6869
# Annoyingly, we only get the text of an unresolved import, so we'll look for it ourselves
6970
for unres in unresolved:
71+
# TODO (youben): delete this test as it double execution time (next for loop will do the same)
7072
if unres not in document.source:
7173
continue
7274

@@ -85,6 +87,30 @@ def pyls_lint(document):
8587
'severity': lsp.DiagnosticSeverity.Hint,
8688
})
8789

90+
for unref in unreferenced:
91+
for line_no, line in enumerate(document.lines):
92+
pos = line.find(unref)
93+
if pos < 0:
94+
continue
95+
96+
# Find out if the unref is a module or a variable/func
97+
imports = importmagic.Imports(importmagic.SymbolIndex(), document.source)
98+
modules = [m.name for m in list(imports._imports)]
99+
if unref in modules:
100+
message = "Unreferenced import '%s'" % unref
101+
else:
102+
message = "Unreferenced variable/function '%s'" % unref
103+
104+
diagnostics.append({
105+
'source': SOURCE,
106+
'range': {
107+
'start': {'line': line_no, 'character': pos},
108+
'end': {'line': line_no, 'character': pos + len(unref)}
109+
},
110+
'message': message,
111+
'severity': lsp.DiagnosticSeverity.Warning,
112+
})
113+
88114
return diagnostics
89115

90116

@@ -121,7 +147,7 @@ def pyls_code_actions(config, document, context):
121147

122148
unres = m.group('unresolved')
123149
# Might be slow but is cached once built
124-
index = _get_index(sys.path)
150+
index = _get_index(sys.path) # TODO (youben): add project path for indexing
125151

126152
for score, module, variable in sorted(index.symbol_scores(unres)[:MAX_COMMANDS], reverse=True):
127153
if score < min_score:

0 commit comments

Comments
 (0)