profiq / docgen

IntelliJ plugin to generate Python documentation using OpenAI's GPT model
2 stars 4 forks source link

Insert the docstring with proper indentation, not always 4 spaces #15

Closed msvana closed 9 months ago

msvana commented 1 year ago

Fixes the docstring always being inserted with an indentation of 4 spaces from the line start. This causes trouble when documenting nested functions or class methods.

Old, incorrect behavior:

class IndexedText:
    def concordance(self, word: str, width=40):
    """
    Prints the concordance of a given word in the text.

    Args:
        self: The instance of the class.
        word (str): The word to find the concordance for.
        width (int): The width of the context to display. Default is 40.

    Returns:
        None

    Raises:
        None

    Example:
        >>> obj = ClassName()
        >>> obj.concordance('example', width=50)
        Context before the word: '...context before the word...'
        Context after the word: '...context after the word...'
    """
        key = wordnet.synsets(word)[0].offset()
        wc = int(width / 4)
        for i in self._index[key]:
            lcontext = ' '.join(self._text[i-wc:i])
            rcontext = ' '.join(self._text[i:i+wc])

New, correct behavior:

class IndexedText:
    def concordance(self, word: str, width=40):
        """
        Prints the concordance of a given word in the text.

        Args:
            self: The instance of the class.
            word (str): The word to find the concordance for.
            width (int): The width of the context to display. Default is 40.

        Returns:
            None

        Raises:
            None

        Example:
            >>> obj = ClassName()
            >>> obj.concordance('example', width=50)
            Context before the word: '...context before the word...'
            Context after the word: '...context after the word...'
        """
        key = wordnet.synsets(word)[0].offset()
        wc = int(width / 4)
        for i in self._index[key]:
            lcontext = ' '.join(self._text[i-wc:i])
            rcontext = ' '.join(self._text[i:i+wc])

In the preview window the docstring is now also displayed with no indentation. Correct indentation is added after user confirms the insert.

@petrvecera check this out