@@ -38,7 +38,8 @@ def pyls_commands():
38
38
39
39
@hookimpl
40
40
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:
42
43
{
43
44
'source': 'importmagic',
44
45
'range': {
@@ -51,8 +52,8 @@ def pyls_lint(document):
51
52
'character': end_column,
52
53
},
53
54
},
54
- 'message': 'Unresolved import <symbol>' ,
55
- 'severity': lsp.DiagnosticSeverity.Hint ,
55
+ 'message': message_to_be_displayed ,
56
+ 'severity': sevirity_level ,
56
57
}
57
58
58
59
Args:
@@ -61,12 +62,13 @@ def pyls_lint(document):
61
62
A list of dictionaries.
62
63
"""
63
64
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 ()
65
66
66
67
diagnostics = []
67
68
68
69
# Annoyingly, we only get the text of an unresolved import, so we'll look for it ourselves
69
70
for unres in unresolved :
71
+ # TODO (youben): delete this test as it double execution time (next for loop will do the same)
70
72
if unres not in document .source :
71
73
continue
72
74
@@ -85,6 +87,30 @@ def pyls_lint(document):
85
87
'severity' : lsp .DiagnosticSeverity .Hint ,
86
88
})
87
89
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
+
88
114
return diagnostics
89
115
90
116
@@ -121,7 +147,7 @@ def pyls_code_actions(config, document, context):
121
147
122
148
unres = m .group ('unresolved' )
123
149
# 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
125
151
126
152
for score , module , variable in sorted (index .symbol_scores (unres )[:MAX_COMMANDS ], reverse = True ):
127
153
if score < min_score :
0 commit comments