Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow plugins to modify server response messages #1992

Merged
merged 4 commits into from
Jul 13, 2022

Conversation

jwortmann
Copy link
Member

@jwortmann jwortmann commented Jul 8, 2022

This method for AbstractPlugin allows LSP-* plugins to modify the "result" in (successful) response messages from the server, before it gets further processed by the response handler function in LSP.

It is useful especially for problematic content in hover responses, but the implementation is generic enough to allow plugins to do any shenanigans they like with the payload ;)

Examples:

Hover popups from LSP-pyright have some minor issues (sublimelsp/LSP-pyright#42), for example there is occasionally only a single \n between the end of a fenced code block and --- (horizontal rule). This is absolutely fine in CommonMark, but mdpopups (or the Python-Markdown parser or whatever it uses) mistakenly interprets it as a Setext header, i.e. currently the code will be bold and the horizontal rule is omitted:

before

Add the following lines to LSP-pyright in plugin.py, and it will be displayed correctly:

    def on_server_response(self, method: str, response: Response) -> None:
        if method == "textDocument/hover" and isinstance(response.result, dict) and response.result.get("contents", {}).get("kind") == "markdown":
            response.result["contents"]["value"] = re.sub("\n---", "\n\n---", response.result["contents"]["value"])

after


I would like to use it for the hover popups from the Julia language server, which have several similar issues in their Markdown content. In particular they often use links formatted in the form [`title`](@ref), and as soon as you click on the link, it will start a webbroser and try to open @ref as URL, which of course doesn't work. But with small modifications I could make these links to open a corresponding documentation page from the server in a HtmlSheet - https://github.com/sublimelsp/LSP-julia/blob/50f978826407faf0d1aaebcd3c2f8e5cb53f8136/plugin.py#L543-L562

plugin/core/sessions.py Outdated Show resolved Hide resolved
plugin/core/sessions.py Outdated Show resolved Hide resolved
plugin/core/sessions.py Outdated Show resolved Hide resolved
@jwortmann jwortmann marked this pull request as draft July 13, 2022 19:13
@jwortmann jwortmann marked this pull request as ready for review July 13, 2022 19:42
@rwols rwols merged commit 9fa725a into sublimelsp:main Jul 13, 2022
@jwortmann jwortmann deleted the modify-response-payload branch July 13, 2022 20:22
rchl added a commit that referenced this pull request Jul 14, 2022
* main:
  Allow plugins to modify server response messages (#1992)
  Keep active group when using Goto commands (#1994)
  Optionally fallback to goto_definition in lsp_symbol_definition (#1986)
  Don't use actual linebreaks in log panel if payload is string literal (#1993)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants