def on_query_completions(self, view, prefix, locations):
        bs = time.time()
        start = time.time()
        if not self.is_supported_language(view):
            return []
        line = view.substr(
            sublime.Region(view.full_line(locations[0]).begin(), locations[0]))
        before = line
        if len(prefix) > 0:
            before = line[:-len(prefix)]
        if re.search("[ \t]+$", before):
            before = ""
        elif re.search("\.$", before):
            # Member completion
            data = view.substr(sublime.Region(0, locations[0] - len(prefix)))
            full_data = view.substr(sublime.Region(0, view.size()))
            typedef = parsehelp.get_type_definition(data)
            if typedef == None:
                return self.return_completions([])
            line, column, typename, var, tocomplete = typedef
            print typedef
            # TODO: doesn't understand arrays at the moment
            tocomplete = tocomplete.replace("[]", "")

            if typename is None:
                # This is for completing for example "System."
                # or "String." or other static calls/variables
                typename = var
                var = None
            start = time.time()
            template = parsehelp.solve_template(typename)
            if template[1]:
                template = template[1]
            else:
                template = ""
            template = self.patch_up_template(data, full_data, template)
            typename = re.sub("(<.*>)|(\[.*\])", "", typename)
            oldtypename = typename
            typename = self.find_absolute_of_type(data, full_data, typename,
                                                  template)
            if typename == "":
                # Possibly a member of the current class
                clazz = parsehelp.extract_class(data)
                if clazz != None:
                    var = "this"
                    typename = self.find_absolute_of_type(
                        data, full_data, clazz, template)
                    tocomplete = "." + oldtypename + tocomplete

            end = time.time()
            print "absolute is %s (%f ms)" % (typename, (end - start) * 1000)
            if typename == "":
                return self.return_completions([])

            tocomplete = tocomplete[1:]  # skip initial .
            if len(tocomplete):
                # Just to make sure that the var isn't "this"
                # because in the end it isn't "this" we are
                # completing, but something else
                var = None

            isstatic = False
            if len(tocomplete) == 0 and var == None:
                isstatic = True
            start = time.time()
            idx = tocomplete.find(".")
            while idx != -1:
                sub = tocomplete[:idx]
                idx2 = sub.find("(")
                if idx2 >= 0:
                    sub = sub[:idx2]
                    count = 1
                    for i in range(idx + 1, len(tocomplete)):
                        if tocomplete[i] == '(':
                            count += 1
                        elif tocomplete[i] == ')':
                            count -= 1
                            if count == 0:
                                idx = tocomplete.find(".", i)
                                break
                tempstring = ""
                if template:
                    for param in template:
                        if len(tempstring):
                            tempstring += ";;--;;"
                        tempstring += parsehelp.make_template(param)
                if "<" in sub and ">" in sub:
                    temp = parsehelp.solve_template(sub)
                    temp2 = self.patch_up_template(data, full_data, temp[1])
                    temp = (temp[0], temp2)
                    temp = parsehelp.make_template(temp)
                    sub = "%s%s" % (temp, sub[sub.rfind(">") + 1:])

                n = self.get_return_type(typename, sub, tempstring)
                print "%s%s.%s = %s" % (typename, "<%s>" % tempstring
                                        if len(tempstring) else "", sub, n)
                if len(n) == 0:
                    return self.return_completions([])
                n = parsehelp.get_base_type(n)
                template = parsehelp.solve_template(n)
                typename = template[0]
                if self.get_language() == "cs" and len(template) == 3:
                    typename += "`%d+%s" % (len(
                        template[1]), parsehelp.make_template(template[2]))
                template = template[1]
                tocomplete = tocomplete[idx + 1:]
                idx = tocomplete.find(".")
            end = time.time()
            print "finding what to complete took %f ms" % (
                (end - start) * 1000)

            template_args = ""
            if template:
                for param in template:
                    if len(template_args):
                        template_args += ";;--;;"
                    template_args += parsehelp.make_template(param)

            print "completing %s%s.%s" % (typename, "<%s>" % template_args if
                                          len(template_args) else "", prefix)
            start = time.time()
            ret = self.complete_class(typename, prefix, template_args)
            ret = self.filter(typename, var, isstatic, data, ret)
            end = time.time()
            print "completion took %f ms" % ((end - start) * 1000)
            be = time.time()
            print "total %f ms" % ((be - bs) * 1000)
            if self.get_setting("completioncommon_shorten_names", True):
                old = ret
                ret = []
                regex = re.compile("([\\w\\.]+\\.)*")
                for display, insert in old:
                    olddisplay = display
                    display = regex.sub("", display)
                    while olddisplay != display:
                        olddisplay = display
                        display = regex.sub("", display)
                    ret.append((display, insert))
            return self.return_completions(ret)
        return []
Esempio n. 2
0
    def run(self, edit):
        view = self.view
        tu = get_translation_unit(view)
        if tu == None:
            return
        tu.lock()
        target = ""
        try:
            row, col = view.rowcol(view.sel()[0].a)
            cursor = cindex.Cursor.get(tu.var, view.file_name().encode("utf-8"),
                                       row + 1, col + 1)

            word = view.word(view.sel()[0].a)
            spelling = view.substr(word)
            if cursor is None or cursor.kind.is_invalid() or (cursor.displayname != spelling and cursor.kind != cindex.CursorKind.INCLUSION_DIRECTIVE):
                # Try to determine what we're supposed to be looking for
                data = view.substr(sublime.Region(0, view.line(view.sel()[0].a).end()))
                chars = r"[\[\]\(\)&|.+-/*,<>;]"
                for match in re.finditer(r"(^|\w+|=|%s|\s)\s*(%s)\s*($|==|%s)" % (chars, spelling, chars), data):
                    if (match.start(2), match.end(2)) == (word.begin(), word.end()):
                        if match.group(3) == "(":
                            # Probably a function
                            ExtensiveSearch(None, spelling, self.view, self.view.window(), impl=False)
                        else:
                            # A variable perhaps?
                            data = data[:match.end(2)] + "."
                            typedef = parsehelp.get_type_definition(data)
                            if typedef:
                                line, column, name, var, extra = typedef
                                if line > 0 and column > 0:
                                    open(view, "%s:%d:%d" % (view.file_name().encode("utf-8"), line, column))
                                elif name != None and name == parsehelp.get_base_type(name):
                                    search_re = re.compile(r"(^|\s|\})\s*(class|struct)(\s+%s\s*)(;|\{)" % name)
                                    ExtensiveSearch(None, name, self.view, self.view.window(), impl=False, search_re=search_re)
                        break
                return
            ref = cursor.get_reference()
            target = ""

            if not ref is None and cursor == ref:
                can = cursor.get_canonical_cursor()
                if not can is None and can != cursor:
                    target = format_cursor(can)
                else:
                    o = cursor.get_overridden()
                    if len(o) == 1:
                        target = format_cursor(o[0])
                    elif len(o) > 1:
                        self.o = o
                        opts = []
                        for i in range(len(o)):
                            opts.append(self.quickpanel_format(o[i]))
                        view.window().show_quick_panel(opts,
                                                       self.quickpanel_on_done)
                    elif (cursor.kind == cindex.CursorKind.VAR_DECL or \
                            cursor.kind == cindex.CursorKind.PARM_DECL or \
                            cursor.kind == cindex.CursorKind.FIELD_DECL):
                        for child in cursor.get_children():
                            if child.kind == cindex.CursorKind.TYPE_REF:
                                d = child.get_definition()
                                if not d is None:
                                    target = format_cursor(d)
                                break
                    elif cursor.kind == cindex.CursorKind.CLASS_DECL:
                        for child in cursor.get_children():
                            if child.kind == cindex.CursorKind.CXX_BASE_SPECIFIER:
                                d = child.get_definition()
                                if not d is None:
                                    target = format_cursor(d)
            elif not ref is None:
                target = format_cursor(ref)
            elif cursor.kind == cindex.CursorKind.INCLUSION_DIRECTIVE:
                f = cursor.get_included_file()
                if not f is None:
                    target = f.name
        finally:
            tu.unlock()
        if len(target) > 0:
            open(self.view, target)
        else:
            sublime.status_message("No parent to go to!")
    def on_query_completions(self, view, prefix, locations):
        bs = time.time()
        start = time.time()
        if not self.is_supported_language(view):
            return []
        line = view.substr(sublime.Region(view.full_line(locations[0]).begin(), locations[0]))
        before = line
        if len(prefix) > 0:
            before = line[:-len(prefix)]
        if re.search("[ \t]+$", before):
            before = ""
        elif re.search("\.$", before):
            # Member completion
            data = view.substr(sublime.Region(0, locations[0]-len(prefix)))
            full_data = view.substr(sublime.Region(0, view.size()))
            typedef = parsehelp.get_type_definition(data)
            if typedef == None:
                return self.return_completions([])
            line, column, typename, var, tocomplete = typedef
            print typedef
            # TODO: doesn't understand arrays at the moment
            tocomplete = tocomplete.replace("[]", "")

            if typename is None:
                # This is for completing for example "System."
                # or "String." or other static calls/variables
                typename = var
                var = None
            start = time.time()
            template = parsehelp.solve_template(typename)
            if template[1]:
                template = template[1]
            else:
                template = ""
            template = self.patch_up_template(data, full_data, template)
            typename = re.sub("(<.*>)|(\[.*\])", "", typename)
            oldtypename = typename
            typename = self.find_absolute_of_type(data, full_data, typename, template)
            if typename == "":
                # Possibly a member of the current class
                clazz = parsehelp.extract_class(data)
                if clazz != None:
                    var = "this"
                    typename = self.find_absolute_of_type(data, full_data, clazz, template)
                    tocomplete = "." + oldtypename + tocomplete

            end = time.time()
            print "absolute is %s (%f ms)" % (typename, (end-start)*1000)
            if typename == "":
                return self.return_completions([])

            tocomplete = tocomplete[1:]  # skip initial .
            if len(tocomplete):
                # Just to make sure that the var isn't "this"
                # because in the end it isn't "this" we are
                # completing, but something else
                var = None

            isstatic = False
            if len(tocomplete) == 0 and var == None:
                isstatic = True
            start = time.time()
            idx = tocomplete.find(".")
            while idx != -1:
                sub = tocomplete[:idx]
                idx2 = sub.find("(")
                if idx2 >= 0:
                    sub = sub[:idx2]
                    count = 1
                    for i in range(idx+1, len(tocomplete)):
                        if tocomplete[i] == '(':
                            count += 1
                        elif tocomplete[i] == ')':
                            count -= 1
                            if count == 0:
                                idx = tocomplete.find(".", i)
                                break
                tempstring = ""
                if template:
                    for param in template:
                        if len(tempstring):
                            tempstring += ";;--;;"
                        tempstring += parsehelp.make_template(param)
                if "<" in sub and ">" in sub:
                    temp = parsehelp.solve_template(sub)
                    temp2 = self.patch_up_template(data, full_data, temp[1])
                    temp = (temp[0], temp2)
                    temp = parsehelp.make_template(temp)
                    sub = "%s%s" % (temp, sub[sub.rfind(">")+1:])

                n = self.get_return_type(typename, sub, tempstring)
                print "%s%s.%s = %s" % (typename, "<%s>" % tempstring if len(tempstring) else "", sub, n)
                if len(n) == 0:
                    return self.return_completions([])
                n = parsehelp.get_base_type(n)
                template = parsehelp.solve_template(n)
                typename = template[0]
                if self.get_language() == "cs" and len(template) == 3:
                    typename += "`%d+%s" % (len(template[1]), parsehelp.make_template(template[2]))
                template = template[1]
                tocomplete = tocomplete[idx+1:]
                idx = tocomplete.find(".")
            end = time.time()
            print "finding what to complete took %f ms" % ((end-start) * 1000)

            template_args = ""
            if template:
                for param in template:
                    if len(template_args):
                        template_args += ";;--;;"
                    template_args += parsehelp.make_template(param)

            print "completing %s%s.%s" % (typename, "<%s>" % template_args if len(template_args) else "", prefix)
            start = time.time()
            ret = self.complete_class(typename, prefix, template_args)
            ret = self.filter(typename, var, isstatic, data, ret)
            end = time.time()
            print "completion took %f ms" % ((end-start)*1000)
            be = time.time()
            print "total %f ms" % ((be-bs)*1000)
            if self.get_setting("completioncommon_shorten_names", True):
                old = ret
                ret = []
                regex = re.compile("([\\w\\.]+\\.)*")
                for display, insert in old:
                    olddisplay = display
                    display = regex.sub("", display)
                    while olddisplay != display:
                        olddisplay = display
                        display = regex.sub("", display)
                    ret.append((display, insert))
            return self.return_completions(ret)
        return []
Esempio n. 4
0
    def run(self, edit):
        view = self.view
        tu = get_translation_unit(view)
        if tu == None:
            return
        tu.lock()
        target = ""
        try:
            row, col = view.rowcol(view.sel()[0].a)
            cursor = cindex.Cursor.get(tu.var, view.file_name(), row + 1,
                                       col + 1)

            word = view.word(view.sel()[0].a)
            spelling = view.substr(word)
            if cursor is None or cursor.kind.is_invalid(
            ) or cursor.displayname != spelling:
                # Try to determine what we're supposed to be looking for
                data = view.substr(
                    sublime.Region(0,
                                   view.line(view.sel()[0].a).end()))
                chars = r"[\[\]\(\)&|.+-/*,<>;]"
                for match in re.finditer(
                        r"(^|\w+|=|%s|\s)\s*(%s)\s*($|==|%s)" %
                    (chars, spelling, chars), data):
                    if (match.start(2), match.end(2)) == (word.begin(),
                                                          word.end()):
                        if match.group(3) == "(":
                            # Probably a function
                            ExtensiveSearch(None,
                                            spelling,
                                            self.view,
                                            self.view.window(),
                                            impl=False)
                        else:
                            # A variable perhaps?
                            data = data[:match.end(2)] + "."
                            typedef = parsehelp.get_type_definition(data)
                            if typedef:
                                line, column, name, var, extra = typedef
                                if line > 0 and column > 0:
                                    open(
                                        view, "%s:%d:%d" %
                                        (view.file_name(), line, column))
                                elif name != None and name == parsehelp.get_base_type(
                                        name):
                                    search_re = re.compile(
                                        r"(^|\s|\})\s*(class|struct)(\s+%s\s*)(;|\{)"
                                        % name)
                                    ExtensiveSearch(None,
                                                    name,
                                                    self.view,
                                                    self.view.window(),
                                                    impl=False,
                                                    search_re=search_re)
                        break
                return
            ref = cursor.get_reference()
            target = ""

            if not ref is None and cursor == ref:
                can = cursor.get_canonical_cursor()
                if not can is None and can != cursor:
                    target = format_cursor(can)
                else:
                    o = cursor.get_overridden()
                    if len(o) == 1:
                        target = format_cursor(o[0])
                    elif len(o) > 1:
                        self.o = o
                        opts = []
                        for i in range(len(o)):
                            opts.append(self.quickpanel_format(o[i]))
                        view.window().show_quick_panel(opts,
                                                       self.quickpanel_on_done)
                    elif (cursor.kind == cindex.CursorKind.VAR_DECL or \
                            cursor.kind == cindex.CursorKind.PARM_DECL or \
                            cursor.kind == cindex.CursorKind.FIELD_DECL):
                        for child in cursor.get_children():
                            if child.kind == cindex.CursorKind.TYPE_REF:
                                d = child.get_definition()
                                if not d is None:
                                    target = format_cursor(d)
                                break
                    elif cursor.kind == cindex.CursorKind.CLASS_DECL:
                        for child in cursor.get_children():
                            if child.kind == cindex.CursorKind.CXX_BASE_SPECIFIER:
                                d = child.get_definition()
                                if not d is None:
                                    target = format_cursor(d)
            elif not ref is None:
                target = format_cursor(ref)
            elif cursor.kind == cindex.CursorKind.INCLUSION_DIRECTIVE:
                f = cursor.get_included_file()
                if not f is None:
                    target = f.name
        finally:
            tu.unlock()
        if len(target) > 0:
            open(self.view, target)
        else:
            sublime.status_message("No parent to go to!")