griffe icon indicating copy to clipboard operation
griffe copied to clipboard

Improve Docstring location information

Open analog-cbarber opened this issue 3 years ago • 5 comments

The Docstring init method currently does this:

        self.value: str = inspect.cleandoc(value.rstrip())
        self.lineno: int | None = lineno
        self.endlineno: int | None = endlineno

The net result leading whitespace is removed from each line. This is what we want, but it now means that the there is no information about how much to add to get the correct column offset.

It would be nice if the class could provide enough information to be able to associate characters in the docstring with their line/column in the original source. Accordingly, I suggest:

  1. add a col_offset field indicating the size of the margin that was removed

  2. (bonus feature) add a method to convert an offset into the cleaned up doc string into original line,col. Something like:

    def offset_to_line_col(self, offset:int) -> Tuple[int,int]:
        line = self.lineno + self.value.count('\n', 0, offset)
        col = self.col_offset + offset - self.value.rfind('\n', 0, offset) - 1
        return line, col
    

Implementing this would either require pulling implementation of cleandoc into the module to expose the margin or comparing the cleaned up string with the original to infer the correct value.

analog-cbarber avatar Jun 27 '22 23:06 analog-cbarber

You can only infer the column offset for the line after the first based on this information alone. To get the offset of the first line, it would have to be passed in (from the AST node).

analog-cbarber avatar Jun 28 '22 00:06 analog-cbarber

And of course there are tabs. I wonder how many python projects actually allow tabs?

analog-cbarber avatar Jun 28 '22 00:06 analog-cbarber