|
| 1 | +""" |
| 2 | +Copyright (c) 2012 Fredrik Ehnbom |
| 3 | +
|
| 4 | +This software is provided 'as-is', without any express or implied |
| 5 | +warranty. In no event will the authors be held liable for any damages |
| 6 | +arising from the use of this software. |
| 7 | +
|
| 8 | +Permission is granted to anyone to use this software for any purpose, |
| 9 | +including commercial applications, and to alter it and redistribute it |
| 10 | +freely, subject to the following restrictions: |
| 11 | +
|
| 12 | + 1. The origin of this software must not be misrepresented; you must not |
| 13 | + claim that you wrote the original software. If you use this software |
| 14 | + in a product, an acknowledgment in the product documentation would be |
| 15 | + appreciated but is not required. |
| 16 | +
|
| 17 | + 2. Altered source versions must be plainly marked as such, and must not be |
| 18 | + misrepresented as being the original software. |
| 19 | +
|
| 20 | + 3. This notice may not be removed or altered from any source |
| 21 | + distribution. |
| 22 | +""" |
| 23 | +import sublime |
| 24 | +import sublime_plugin |
| 25 | +import re |
| 26 | +import subprocess |
| 27 | +import os.path |
| 28 | + |
| 29 | + |
| 30 | +class SublimeJava(sublime_plugin.EventListener): |
| 31 | + |
| 32 | + def find_type_of_variable(self, data, variable): |
| 33 | + print variable |
| 34 | + regex = "(\w[^( \t]+)[ \t]+%s[ \t]*(\;|,|\)|=|:).*$" % variable |
| 35 | + print regex |
| 36 | + match = re.search(regex, data, re.MULTILINE) |
| 37 | + if not match is None: |
| 38 | + return match.group(1) |
| 39 | + else: |
| 40 | + # Variable not defined in this class... |
| 41 | + return None |
| 42 | + |
| 43 | + def find_absolute_of_type(self, data, type): |
| 44 | + match = re.search("class %s" % type, data) |
| 45 | + if not match is None: |
| 46 | + # Class is defined in this file, return package of the file |
| 47 | + package = re.search("[ \t]*package (.*);", data) |
| 48 | + if package is None: |
| 49 | + return type |
| 50 | + return "%s.%s" % (package.group(1), type) |
| 51 | + regex = "[ \t]*import[ \t]+(.*)\.%s" % type |
| 52 | + match = re.search(regex, data) |
| 53 | + if not match is None: |
| 54 | + return "%s.%s" % (match.group(1), type) |
| 55 | + return type |
| 56 | + |
| 57 | + def run_java(self, cmd): |
| 58 | + scriptdir = os.path.dirname(os.path.abspath(__file__)) |
| 59 | + proc = subprocess.Popen( |
| 60 | + cmd, |
| 61 | + cwd=scriptdir, |
| 62 | + shell=True, |
| 63 | + stdout=subprocess.PIPE |
| 64 | + ) |
| 65 | + stdout, stderr = proc.communicate() |
| 66 | + return stdout |
| 67 | + |
| 68 | + def complete_class(self, absolute_classname, prefix): |
| 69 | + stdout = self.run_java("java -classpath .:/Users/quarnster/android/android-sdk-mac_86/platforms/android-14/android.jar SublimeJava -complete %s %s" % (absolute_classname, prefix)) |
| 70 | + ret = [tuple(line.split(";")) for line in stdout.split("\n")[:-1]] |
| 71 | + return sorted(ret, key=lambda a: a[0]) |
| 72 | + |
| 73 | + def get_return_type(self, absolute_classname, prefix): |
| 74 | + stdout = self.run_java("java -classpath .:/Users/quarnster/android/android-sdk-mac_86/platforms/android-14/android.jar SublimeJava -returntype %s %s" % (absolute_classname, prefix)) |
| 75 | + return stdout.strip() |
| 76 | + |
| 77 | + def on_query_completions(self, view, prefix, locations): |
| 78 | + line = view.substr(sublime.Region(view.full_line(locations[0]).begin(), locations[0])) |
| 79 | + before = line |
| 80 | + if len(prefix) > 0: |
| 81 | + before = line[:-len(prefix)] |
| 82 | + if re.search("[ \t]+$", before): |
| 83 | + before = "" |
| 84 | + elif re.search("\.$", before): |
| 85 | + # Member completion |
| 86 | + data = view.substr(sublime.Region(0, locations[0])) |
| 87 | + before = re.search("[^ \t]+\.$", before).group(0) |
| 88 | + |
| 89 | + idx = before.find(".") |
| 90 | + var = before[:idx].strip() |
| 91 | + before = before[idx+1:] |
| 92 | + print "var is %s" % var |
| 93 | + t = self.find_type_of_variable(data, var) |
| 94 | + print "type is %s" % t |
| 95 | + t = self.find_absolute_of_type(data, t) |
| 96 | + print "absolute is %s" % (t) |
| 97 | + |
| 98 | + idx = before.find(".") |
| 99 | + while idx != -1: |
| 100 | + sub = before[:idx] |
| 101 | + idx2 = sub.find("(") |
| 102 | + if idx2 >= 0: |
| 103 | + sub = sub[:idx2] |
| 104 | + |
| 105 | + n = self.get_return_type(t, sub) |
| 106 | + print "%s.%s = %s" % (t, sub, n) |
| 107 | + t = n |
| 108 | + before = before[idx+1:] |
| 109 | + idx = before.find(".") |
| 110 | + |
| 111 | + print "completing %s.%s" % (t, prefix) |
| 112 | + |
| 113 | + return self.complete_class(t, prefix) |
| 114 | + |
| 115 | + print "here" |
| 116 | + return [] |
0 commit comments