Пример #1
0
    def get_signature(self, signature, file_path, row):
        # handle languages that don't use parenthesizes
        if '(' not in signature:
            return signature, row

        while '{' not in signature and ')' not in signature:
            row += 1
            signature += ' ' + get_line(self.view, file_path, row).strip()
        return signature, row
Пример #2
0
    def _get_docs(self, find_comment_params):
        file_path = find_comment_params['file_path']

        # extract docs above function
        row = find_comment_params['row_above_signiture']
        docs = get_line(self.view, file_path, row).strip()
        # go in reverse
        if '*/' in docs:
            while '/*' not in docs:
                row -= 1
                docs = get_line(self.view, file_path,
                                row).strip() + '<br>' + docs
            return docs

        # extract docs below function
        row = find_comment_params['row_below_signiture']
        docs = get_line(self.view, file_path, row).strip()
        if re.match('(\'\'\'|""")', docs):
            while not re.match('(\'\'\'|""").*?(\'\'\'|""")', docs,
                               re.MULTILINE):
                row += 1
                docs += '<br>' + get_line(self.view, file_path, row).strip()
            return docs
        return ''
Пример #3
0
    def run(self, edit, locations=None, point=None, index=None):
        """ index - when specified show the current location. """
        global MAX_LEN
        global PREVIOUS_INDEX
        # restart the counter of signature help to 0
        PREVIOUS_INDEX = 0

        if point is None:
            point = self.view.sel()[0].begin()

        word = None
        if locations is None:
            word = get_function_name(self.view, point)
            locations = definition(word, self.view)

        if len(locations) == 0:
            return

        MAX_LEN = len(locations)

        # display the reference count
        if len(locations) > 1 and index is None:
            self.view.run_command('side_show_signature', {
                'locations': locations,
                'point': point,
                'index': 0
            })
            return

        location = None
        if index:
            location = locations[index]
        else:
            location = locations[0]

        file_path, relative_file_path, row_col = location
        row, _col = row_col  # signature row

        function_line = get_line(self.view, file_path, row).strip()

        signature, row = self.get_signature(function_line, file_path, row)
        # prettify signature
        signature = signature.strip('{').strip(':')

        docs = self.get_docs(file_path, row_col)

        if docs:
            docs = """
            <div style="padding: 7px; 
                        border-top: 1px solid color(var(--foreground) alpha(0.1))">
                {}
            </div>""".format(docs)

        # prettify file origin
        if len(relative_file_path) > 70:
            relative_file_path = '...' + relative_file_path[-50:]

        if len(locations) == 1:
            range_count = ''
        else:
            current_index = 1 if index is None else index + 1
            range_count = "[{}-{}]".format(current_index, len(locations))
        origin = """
        <div style="padding: 7px; 
                    border-top: 1px solid color(var(--foreground) alpha(0.1));
                    color: color(var(--foreground) alpha(0.7))">
            {} {}
        </div>""".format(range_count, relative_file_path)

        content = """
        <body id="side-hover" style="margin:0">
            <div style="color: var(--orangish); 
                        padding: 7px;">
                {}
            </div>
            {} 
            {}
        </body>""".format(escape(signature, False), origin, docs)
        if index is not None:
            self.view.update_popup(content)

        self._show_popup(content, point)
        # end of command execution
        # perfect place to clear the linecache
        linecache.clearcache()
Пример #4
0
 def get_signature(self, signature, file_path, row):
     while '{' not in signature and ')' not in signature:
         row += 1
         signature += ' ' + get_line(self.view, file_path, row).strip()
     return signature, row
Пример #5
0
    def run(self, edit, find_all=True):
        window = sublime.active_window()
        word = get_word(self.view)

        locations = reference(word, self.view, find_all)

        if len(locations) == 0:
            window.destroy_output_panel('references')
            self.view.show_popup('No References',
                                 sublime.HIDE_ON_MOUSE_MOVE_AWAY)
            return

        panel = window.find_output_panel("references")
        if len(locations) == 1:
            open_view(locations[0], self.view)
            if panel is not None:
                window.destroy_output_panel('references')
            return

        if len(locations) > 1:
            if not find_all:
                chose_one_location_from_many(locations, self.view)
                return

            references_by_rel_file_path = {}  # type: Dict[str, List[line]]
            for location in locations:
                file_path, rel_file_path, row_col = location

                # create an array if it doesn't exist
                if references_by_rel_file_path.get(rel_file_path) is None:
                    references_by_rel_file_path[rel_file_path] = []

                row, col = row_col
                line = get_line(self.view, file_path, row).strip()
                ref = {'row': row, 'col': col, 'line': line}
                references_by_rel_file_path[rel_file_path].append(ref)

            # this string will be rendered in the panel
            find_result = ''
            for rel_file_path in references_by_rel_file_path:
                find_result += "{}:\n".format(rel_file_path)
                references = references_by_rel_file_path.get(rel_file_path)

                for ref in references:
                    find_result += "    {}:{}\t\t{}\n".format(
                        ref['row'], ref['col'], ref['line'])

                find_result += "\n"

            panel = window.create_output_panel("references")

            base_dir = get_project_path(window)
            panel.settings().set("result_base_dir", base_dir)

            panel.settings().set("gutter", False)
            panel.settings().set("result_file_regex", r"^(\S.*):$")
            panel.settings().set("result_line_regex",
                                 r"^\s+([0-9]+):([0-9]+).*$")
            panel.settings().set("draw_white_space", "none")
            panel.assign_syntax(
                'Packages/Default/Find Results.hidden-tmLanguage')

            panel = window.create_output_panel("references")

            window.run_command("show_panel", {"panel": "output.references"})
            panel.run_command(
                'append', {
                    'characters':
                    "{} references for '{}'\n\n{}".format(
                        len(locations), word, find_result),
                    'force':
                    True,
                    'scroll_to_end':
                    False
                })
            panel.set_read_only(True)

            # highlight region
            regions = panel.find_all(r"\b{}\b".format(word))
            panel.add_regions('highlight_references',
                              regions,
                              'comment',
                              flags=sublime.DRAW_OUTLINED)

            # perfect place to clear the linecache
            linecache.clearcache()