-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinter.py
189 lines (147 loc) · 5.7 KB
/
linter.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#
# linter.py
# Linter for SublimeLinter3, a code checking framework for Sublime Text 3 (Java)
#
# Written by Ganesha <[email protected]>
# Copyright (c) 2013 Ganesha <[email protected]>
#
# License: MIT
#
"""This module exports the Java plugin class."""
import os
import glob
import shutil
import tempfile
import re
from SublimeLinter.lint import Linter, util
class Java(Linter):
"""Provides an interface to java."""
syntax = ('java')
# change to None to use fn
cmd = None
executable = 'javac'
# regex = r'^.+?:(?P<line>\d+):\s*(?:(?P<warning>warning)|(?P<error>error)):\s*(?P<message>.+)'
# multiline = False
head_regex = r'^.+?:(?P<line>\d+):\s*(?:(?P<warning>warning)|(?P<error>error)):\s*(?P<message>.+)$'
regex = r'''(?xi)
# First line is (lineno): type: error message
^.+?:(?P<line>\d+):\s*(?:(?P<warning>warning)|(?P<error>error)):\s*(?P<message>.+)$\r?\n
# Second line is the line of code
^.*$\r?\n
# Third line is a caret pointing to the position of the error
^(?P<col>[^\^]*)\^$
'''
multiline = True
line_col_base = (1, 1)
tempfile_suffix = 'java'
error_stream = util.STREAM_STDERR
selectors = {}
word_re = None
defaults = {}
inline_settings = None
inline_overrides = None
comment_re = None
def cmd(self):
"""
Return a string with the command line to execute.
We define this method because we want to use the .jshintrc files,
and we can't rely on jshint to find them, because we are using stdin.
"""
settings = self.get_view_settings()
directory = settings.get("directory")
target = settings.get("target")
source = settings.get("source")
bootclasspath = settings.get("bootclasspath")
# sometimes settings does not read from project settings and directory is empty
if directory == None:
return []
command = [self.executable_path, '-Xlint', '-d', directory]
if target != None:
command += ['-target', target]
if source != None:
command += ['-source', source]
if bootclasspath != None:
command += ['-bootclasspath', bootclasspath]
classpath = settings.get("classpath")
if isinstance(classpath, str):
classpath = classpath
elif classpath == None:
classpath = ""
elif all(isinstance(item, str) for item in classpath): # check iterable for stringness of all items. Will raise TypeError if some_object is not iterable
classpath = ":".join(classpath)
else:
classpath = ""
if classpath != "":
command += ['-cp', classpath]
command += ['@']
# print(command)
return command
def tmpfile(self, cmd, code, suffix=''):
"""Run an external executable using a temp file to pass code and return its output."""
filename = os.path.basename(self.filename)
try:
cmd = list(cmd)
if '@' in cmd:
cmd[cmd.index('@')] = self.filename
else:
cmd.append(f.name)
print("cmd", ' '.join(cmd))
out = util.popen(cmd, output_stream=self.error_stream)
if out:
out = out.communicate()
filtered = ''
skip = 0
lines = util.combine_output(out).split('\n')
errline = []
for line in lines:
# print("line:", line)
if line == '':
continue
match = re.match(r'warning: \[path\] bad path element', line)
if match:
filtered += self.filename + ":1: " + line + "\n\n\n"
continue
if skip > 0:
skip = skip - 1
else:
match = re.match(self.head_regex, line)
if match:
if len(errline) > 0:
print("errline:", errline)
filtered += errline[0];
if len(errline) > 3:
filtered += ", " + (errline[3].strip())
if len(errline) > 4:
filtered += ", " + (errline[4].strip())
filtered += "\n"
filtered += errline[1] + "\n"
filtered += errline[2] + "\n"
errline = []
if match and (not self.filename in line):
skip = 2
else:
print("line:", line)
errline.append(line)
print("errline:", len(errline), errline)
if len(errline) > 0:
filtered += errline[0];
if len(errline) > 3:
filtered += ", " + (errline[3].strip())
if len(errline) > 4:
filtered += ", " + (errline[4].strip())
filtered += "\n"
filtered += errline[1] + "\n"
if len(errline) > 2:
filtered += errline[2] + "\n"
print("-------------")
print(filtered)
print("-------------")
return filtered
# return util.combine_output(out)
else:
return ''
# except:
# pass
finally:
pass
# shutil.rmtree(d, True)