0

I was trying to define a polymode so python docstrings have markdown-mode in them. My code for now is:

(use-package polymode
    :ensure t
    :config
    (define-hostmode poly-python-hostmode
      :mode 'python-mode)

    (define-innermode poly-python-markdown-innermode
      :mode 'markdown-mode
      :head-matcher "^[ \t\n]*(def|class)*.+\n[ \t\n]*\"\"\"\n"
      :tail-matcher "^[ \t\n]*\"\"\"\n"
      :head-mode 'host
      :tail-mode 'host)

    (define-polymode poly-python-mode
      :hostmode 'poly-python-hostmode
      :innermodes '(poly-python-markdown-innermode)))

This, however, doesn't work. The first issue is that while sites like RegExr correctly detect (simple) function or class cases, Emacs fails to do so (possibly due to different regex syntax) but the more pressing issue is that function definitions like this:

class MyClass:
    """
    This is a docstring
    """

    def __init__(
        self,
    ):
        """More doc"""
        pass

won't get detected, because arguments go in more than 1 line. So I would like to know if someone has already done this and/or how to make my regexes work and be more robust in detecting docstrings (and only docstrings). It would be grand if there was a way to somehow leverage python-mode (and lsp for that matter) to detect docstrings and send that to polymode, although I don't have many hopes in that.

Edit

Changing the code to:

      ...
      :head-matcher "\)?:\n[ \t\n]*\"\"\"\n?"
      :tail-matcher "[ \t\n]*\"\"\"\n"
      ...
)

Makes it work decently well. The question stands if there is some way to interact with the host mode/make it more robust (just in case).

This site is temporarily in read only mode and not accepting new answers.

Browse other questions tagged .