Ejemplo n.º 1
0
    def end_index(self):
        #there's a bug in astroid where it doesn't correctly detect the last
        #line of multiline enclosed blocks (parens, brackets, etc.) -- it gives
        #the last line with content, rather than the line containing the
        #terminating character

        #we have to work around this by scanning through the source ourselves to
        #find the terminating point

        #astroid bug report submitted:
        #https://bitbucket.org/logilab/astroid/issue/31/astroid-sometimes-reports-the-wrong

        file_source_lines = self.get_file_source_code().splitlines(True)

        #we start by finding the line/column at which the next 'sibling' of
        #this node begins. if the node is at the end of the file, we get the
        #end of the file instead
        next_sibling = self._astroid_object.next_sibling()
        if next_sibling:
            scan_from_line = next_sibling.fromlineno
            scan_from_column = next_sibling.col_offset - 1
        else:
            scan_from_line = len(file_source_lines) - 1
            scan_from_column = len(file_source_lines[scan_from_line]) - 1

        #this string doesn't have the right formatting, but it should be
        #otherwise correct -- so we can use it to see what character our
        #variable ends on
        terminating_char = self._astroid_value.as_string()[-1]

        return find_termination(file_source_lines, scan_from_line,
                                scan_from_column, terminating_char)
Ejemplo n.º 2
0
    def body_start_index(self):
        file_source = self.get_file_source_code()
        first_child = self._astroid_child_after_signature

        #see the safe_docstring function for details on why we do this
        docstring = self._astroid_object.doc
        if docstring and ':' in docstring:
            file_source = file_source.replace(
                docstring,
                safe_docstring(docstring))

        
        #first character AFTER the colon at the end of the signature
        after_colon_index = find_termination(
            file_source.splitlines(True),
            first_child.fromlineno - 1,
            first_child.col_offset,
            ':')

        #now that we've found the colon where the function signature ends,
        #search FORWARDS for the next newline. one after that is our start
        #index
        for newline_index, char in enumerate(file_source[after_colon_index:]):
            if char == '\n':
                return after_colon_index + newline_index + 1
Ejemplo n.º 3
0
    def end_index(self):
        #there's a bug in astroid where it doesn't correctly detect the last
        #line of multiline enclosed blocks (parens, brackets, etc.) -- it gives
        #the last line with content, rather than the line containing the
        #terminating character

        #we have to work around this by scanning through the source ourselves to
        #find the terminating point

        #astroid bug report submitted:
        #https://bitbucket.org/logilab/astroid/issue/31/astroid-sometimes-reports-the-wrong

        file_source_lines = self.get_file_source_code().splitlines(True)

        #we start by finding the line/column at which the next 'sibling' of
        #this node begins. if the node is at the end of the file, we get the
        #end of the file instead
        next_sibling = self._astroid_object.next_sibling()
        if next_sibling:
            scan_from_line = next_sibling.fromlineno
            scan_from_column = next_sibling.col_offset - 1
        else:
            scan_from_line = len(file_source_lines) - 1
            scan_from_column = len(file_source_lines[scan_from_line]) - 1

        #this string doesn't have the right formatting, but it should be
        #otherwise correct -- so we can use it to see what character our
        #variable ends on
        terminating_char = self._astroid_value.as_string()[-1]

        return find_termination(
            file_source_lines,
            scan_from_line,
            scan_from_column,
            terminating_char)