|
| 1 | +# |
| 2 | +# linter.py |
| 3 | +# Linter for SublimeLinter3, a code checking framework for Sublime Text 3 |
| 4 | +# |
| 5 | +# Written by Jafar Shadiq,,, |
| 6 | +# Copyright (c) 2013 Jafar Shadiq,,, |
| 7 | +# |
| 8 | +# License: MIT |
| 9 | +# |
| 10 | + |
| 11 | +"""This module exports the Java plugin class.""" |
| 12 | + |
| 13 | +import os |
| 14 | +import glob |
| 15 | +import shutil |
| 16 | +import tempfile |
| 17 | +from SublimeLinter.lint import Linter, util, persist |
| 18 | + |
| 19 | + |
| 20 | +class Java(Linter): |
| 21 | + |
| 22 | + """Provides an interface to java.""" |
| 23 | + |
| 24 | + syntax = ('java') |
| 25 | + # change to None to use fn |
| 26 | + cmd = None |
| 27 | + executable = 'javac' |
| 28 | + # regex = r'^.+?:(?P<line>\d+):\s*(?:(?P<warning>warning)|(?P<error>error)):\s*(?P<message>.+)' |
| 29 | + # multiline = False |
| 30 | + regex = r'''(?xi) |
| 31 | + # First line is (lineno): type: error message |
| 32 | + ^.+?:(?P<line>\d+):\s*(?:(?P<warning>warning)|(?P<error>error)):\s*(?P<message>.+)$\r?\n |
| 33 | +
|
| 34 | + # Second line is the line of code |
| 35 | + ^.*$\r?\n |
| 36 | +
|
| 37 | + # Third line is a caret pointing to the position of the error |
| 38 | + ^(?P<col>[^\^]*)\^$ |
| 39 | + ''' |
| 40 | + multiline = True |
| 41 | + line_col_base = (1, 1) |
| 42 | + tempfile_suffix = 'java' |
| 43 | + error_stream = util.STREAM_STDERR |
| 44 | + selectors = {} |
| 45 | + word_re = None |
| 46 | + defaults = {} |
| 47 | + inline_settings = None |
| 48 | + inline_overrides = None |
| 49 | + comment_re = None |
| 50 | + |
| 51 | + def cmd(self): |
| 52 | + """ |
| 53 | + Return a string with the command line to execute. |
| 54 | +
|
| 55 | + We define this method because we want to use the .jshintrc files, |
| 56 | + and we can't rely on jshint to find them, because we are using stdin. |
| 57 | +
|
| 58 | + """ |
| 59 | + |
| 60 | + d = tempfile.mkdtemp('', 'sublimelinter-java-') |
| 61 | + listing = glob.glob(tempfile.tempdir + '/sublimelinter-java-*') |
| 62 | + for filename in listing: |
| 63 | + shutil.rmtree(filename, True) |
| 64 | + |
| 65 | + d = tempfile.mkdtemp('', 'sublimelinter-java-') |
| 66 | + |
| 67 | + command = [self.executable_path, '-d', d] |
| 68 | + |
| 69 | + settings = self.get_view_settings() |
| 70 | + classpath = settings.get("classpath") |
| 71 | + if isinstance(classpath, str): |
| 72 | + classpath = classpath |
| 73 | + 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 |
| 74 | + classpath = ":".join(classpath) |
| 75 | + else: |
| 76 | + classpath = "" |
| 77 | + |
| 78 | + if classpath != "": |
| 79 | + command += ['-cp', classpath] |
| 80 | + |
| 81 | + command += ['@'] |
| 82 | + # print(command) |
| 83 | + return command |
| 84 | + |
| 85 | + def tmpfile(self, cmd, code, suffix=''): |
| 86 | + """Run an external executable using a temp file to pass code and return its output.""" |
| 87 | + filename = os.path.basename(self.filename) |
| 88 | + |
| 89 | + try: |
| 90 | + d = tempfile.mkdtemp('', 'sublimelinter-javasource-') |
| 91 | + |
| 92 | + f = open(d + '/' + filename,'w') |
| 93 | + f.write(code) |
| 94 | + f.close() |
| 95 | + |
| 96 | + cmd = list(cmd) |
| 97 | + |
| 98 | + if '@' in cmd: |
| 99 | + cmd[cmd.index('@')] = f.name |
| 100 | + else: |
| 101 | + cmd.append(f.name) |
| 102 | + |
| 103 | + # print(cmd) |
| 104 | + |
| 105 | + out = util.popen(cmd, output_stream=self.error_stream) |
| 106 | + if out: |
| 107 | + out = out.communicate() |
| 108 | + return util.combine_output(out) |
| 109 | + else: |
| 110 | + return '' |
| 111 | + |
| 112 | + finally: |
| 113 | + shutil.rmtree(d, True) |
0 commit comments