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

Use filter-buffer-substring to get buffer text #482

Merged
merged 1 commit into from
May 26, 2020

Conversation

muffinmad
Copy link
Collaborator

Python file:

int

textDocument/hover response:

(:id 44 :jsonrpc "2.0" :result
     (:contents
      (:kind "markdown" :value "```python\nint(x: Union[Text, bytes, SupportsInt, _SupportsIndex]=...)\nint(x: Union[Text, bytes, bytearray], base: int)\n```\n\n```\nint([x]) -> integer\nint(x, base=10) -> integer\n\nConvert a number or string to an integer, or return 0 if no arguments\nare given.  If x is a number, return x.__int__().  For floating point\nnumbers, this truncates towards zero.\n\nIf x is not a number or if base is given, then x must be a string,\nbytes, or bytearray instance representing an integer literal in the\ngiven base.  The literal can be preceded by '+' or '-' and be surrounded\nby whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.\nBase 0 means to interpret the base from the string as an integer literal.\n>>> int('0b100', base=0)\n4\n```")
      :range nil))

With default eglot settings the following message is shown in the echo area:

[eglot]
(...truncated. Full help is in `*eglot-help for int*')

Codeblock markers are invisible in gfm-view-mode and mostly useless after font faces are applied to the text.

This PR removes codeblock markers from the hover info.

Copy link
Owner

@joaotavora joaotavora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, sorry. Eglot should not be the receptacle of improvements that belong in other parts of Emacs or other packages, even if it's a kind of "soft depedency" such as Markdown. This is especially important as Eglot moves closer to the core of Emacs.

eglot.el Outdated
(ignore-errors (delay-mode-hooks (funcall mode)))
(font-lock-ensure)
(buffer-string))))
(when (eq major-mode 'gfm-view-mode)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too much for eglot.el, @muffinmad, sorry. IT's clearly something that is to be done in the server, in gfm-view-mode, or maybe in a user hook. But I think you should contribute this to gfm-view-mode.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Would it be fine for eglot to use filter-buffer-substring instead of buffer-string here? In this case gfm-view-mode (among other modes) can provide own filter-buffer-substring-function.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about that function, but that sounds very good in principle, yes.

This way modes used to represent hover info text can
e.g. filter out invisible text by providing
own `filter-buffer-substring-function'.

* eglot.el (eglot--format-markup): Use `filter-buffer-substring'.
@muffinmad muffinmad changed the title Remove invisible markdown text from hover info Use filter-buffer-substring to get buffer text May 18, 2020
@muffinmad
Copy link
Collaborator Author

This PR removes codeblock markers from the hover info.

This PR allows major modes, used to format the hover info text, to filter the text.

@joaotavora
Copy link
Owner

I think this PR now makes sense. However, it is too soon to merge it, probably. I'll make the most sense when gfm-view-mode starts supporting this: have you contacted its authors? Or are you planning to set your own filter from your .emacs? What would this filter look like?

@muffinmad
Copy link
Collaborator Author

I think this PR now makes sense. However, it is too soon to merge it, probably.

OK, no problem.

I'll make the most sense when gfm-view-mode starts supporting this: have you contacted its authors?

Not yet.

Or are you planning to set your own filter from your .emacs? What would this filter look like?

Something like this

(defun test/filter (start end &optional delete)
  (replace-regexp-in-string "^```.*\n*?" ""
                            (buffer-substring start end)))

(add-hook 'gfm-view-mode-hook
          (lambda ()
            (setq-local filter-buffer-substring-function
                        #'test/filter)))

But this will not work because of delay-mode-hooks, right?

@joaotavora
Copy link
Owner

But this will not work because of delay-mode-hooks, right?

You lost me :-)

@muffinmad
Copy link
Collaborator Author

You lost me :-)

Sorry :) My local .emacs solution add the hook for gfm-view-mode. That hook will set the filter-buffer-substring-function local variable to test/filter function that will be used to filter buffer text. But that hook will not be called because gfm-view-mode is enabled with delay-mode-hooks in the eglot--format-markup function.

Here is another way to make it solely in eglot without dependency on third-party modules :-) E.g.

(let ((filter-buffer-substring-function eglot-filter-help-buffer-function))
  (filter-buffer-substring (point-min) (point-max)))

Then the eglot-filter-help-buffer-function variable can be 'identity by default and users can write their own filter functions. Doesn't really like this solution bacouse in this case we need to add questionable user option to eglot.

@muffinmad
Copy link
Collaborator Author

Too bad there are no something like buffer-string-only-visible function in Emacs.

@joaotavora
Copy link
Owner

bah. vc-region-history worked perfectly:

commit 2ab8b59db44fd4dd9791ac203ac9f0915cc6824f
Author: Xu Chunyang <4550353+xuchunyang@users.noreply.github.com>
Date:   Sun Oct 27 23:41:53 2019 +0800

    Don't run mode hooks in eglot--format-markup
    
    * eglot.el (eglot--format-markup): Use delay-mode-hooks.
    
    Copyright-paperwork-exempt: yes

diff --git a/eglot.el b/eglot.el
--- a/eglot.el
+++ b/eglot.el
@@ -1086,1 +1086,1 @@
-      (ignore-errors (funcall mode)) (font-lock-ensure) (buffer-string))))
+      (ignore-errors (delay-mode-hooks (funcall mode)))

But smart old me forgot to include a reference to the issue that would have explained why delay-mode-hooks is there in the first place...

@joaotavora
Copy link
Owner

Got it, it's #327. The delay-mode-hooks is there because of "normal", programming major modes, which normally contains looks of user hooks. We could remove it for gfm-view-mode, and so your hook would work. But the correct version is to contact the authors of gfm-view-mode anyway, and if you really want a hook that works regardless of delay-mode-hooks, just use add-function and it will do the trick.

@muffinmad
Copy link
Collaborator Author

I'm mostly use vc-annotate for such cases ;-)

Call of delay-mode-hooks function is in it's place. We don't need to run all those mode hooks to just apply font faces.

@muffinmad
Copy link
Collaborator Author

But the correct version is to contact the authors of gfm-view-mode anyway, and if you really want a hook that works regardless of delay-mode-hooks, just use add-function and it will do the trick.

Absolutely agree. Thanks for the tips!

@joaotavora
Copy link
Owner

I'm mostly use vc-annotate for such cases ;-)

Yeah, but that annotates the whole buffer. I like vc-region-history much better, personally.

@muffinmad
Copy link
Collaborator Author

But the correct version is to contact the authors of gfm-view-mode

filter-buffer-substring-function is now implemented in both markdown-view-mode and gfm-view-mode. See jrblevin/markdown-mode#494.

@joaotavora
Copy link
Owner

filter-buffer-substring-function is now implemented in both markdown-view-mode and gfm-view-mode

Nice, but what does that mean for us?

@muffinmad
Copy link
Collaborator Author

filter-buffer-substring-function is now implemented in both markdown-view-mode and gfm-view-mode

Nice, but what does that mean for us?

That means to us that this PR must be merged immediately ;-) And make eglot respect filter-buffer-substring-function provided by the major mode used to present the hover info to happy eglot users!

@joaotavora joaotavora merged commit 9874456 into joaotavora:master May 26, 2020
@joaotavora
Copy link
Owner

joaotavora commented May 26, 2020 via email

@muffinmad muffinmad deleted the markdown-invisible branch May 27, 2020 08:28
astoff added a commit to astoff/eglot that referenced this pull request Mar 9, 2022
This was introduced in joaotavora#482 due to a bad interaction with a specific
server.  But this solution makes hyperlinks in Eldoc buffers
unclickable, because the markdown-mode function that visits a link
relies on the invisible text.

Fixes joaotavora#865.

* eglot.el (eglot--format-markup): Don't use
'filter-buffer-substring'; instead, keep invisible text.
astoff added a commit to astoff/eglot that referenced this pull request Mar 10, 2022
This was introduced in joaotavora#482 due to a bad interaction with a specific
server.  But this solution makes hyperlinks in Eldoc buffers
unclickable, because the markdown-mode function that visits a link
relies on the invisible text.

Fixes joaotavora#865.

* eglot.el (eglot--format-markup): Don't use
'filter-buffer-substring'; instead, keep invisible text.
joaotavora pushed a commit that referenced this pull request Mar 10, 2022
This was introduced in #482 due to a bad interaction with a specific
server.  But this solution makes hyperlinks in Eldoc buffers
unclickable, because the markdown-mode function that visits a link
relies on the invisible text.

Per #866 

* eglot.el (eglot--format-markup): Use buffer-string instead of 
filter-buffer-substring
bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 18, 2022
… text

This way modes used to represent hover info text, such as
gfm-view-mode can e.g. filter out invisible text by providing own
`filter-buffer-substring-function'.

* eglot.el (eglot--format-markup): Use `filter-buffer-substring'.
bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 18, 2022
…hover string

This was introduced in joaotavora/eglot#482 due to a bad interaction with a specific
server.  But this solution makes hyperlinks in Eldoc buffers
unclickable, because the markdown-mode function that visits a link
relies on the invisible text.

Per joaotavora/eglot#866 

* eglot.el (eglot--format-markup): Use buffer-string instead of 
filter-buffer-substring
bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 19, 2022
… text

This way modes used to represent hover info text, such as
gfm-view-mode can e.g. filter out invisible text by providing own
`filter-buffer-substring-function'.

* eglot.el (eglot--format-markup): Use `filter-buffer-substring'.
bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 19, 2022
…hover string

This was introduced in joaotavora/eglot#482 due to a bad interaction with a specific
server.  But this solution makes hyperlinks in Eldoc buffers
unclickable, because the markdown-mode function that visits a link
relies on the invisible text.

Per joaotavora/eglot#866 

* eglot.el (eglot--format-markup): Use buffer-string instead of 
filter-buffer-substring
bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 19, 2022
This way modes used to represent hover info text, such as
gfm-view-mode can e.g. filter out invisible text by providing own
`filter-buffer-substring-function'.

* eglot.el (eglot--format-markup): Use `filter-buffer-substring'.
#482: joaotavora/eglot#482
bhankas pushed a commit to bhankas/emacs that referenced this pull request Sep 19, 2022
This was introduced in #482 due to a bad interaction with a specific
server.  But this solution makes hyperlinks in Eldoc buffers
unclickable, because the markdown-mode function that visits a link
relies on the invisible text.

Per #866 

* eglot.el (eglot--format-markup): Use buffer-string instead of 
filter-buffer-substring
#865: joaotavora/eglot#865
#482: joaotavora/eglot#482
#866: joaotavora/eglot#866
jollaitbot pushed a commit to sailfishos-mirror/emacs that referenced this pull request Oct 12, 2022
This way modes used to represent hover info text, such as
gfm-view-mode can e.g. filter out invisible text by providing own
`filter-buffer-substring-function'.

* eglot.el (eglot--format-markup): Use `filter-buffer-substring'.

GitHub-reference: close joaotavora/eglot#482
jollaitbot pushed a commit to sailfishos-mirror/emacs that referenced this pull request Oct 12, 2022
This was introduced in joaotavora/eglot#482 due to a bad interaction with a specific
server.  But this solution makes hyperlinks in Eldoc buffers
unclickable, because the markdown-mode function that visits a link
relies on the invisible text.

Per joaotavora/eglot#866 

* eglot.el (eglot--format-markup): Use buffer-string instead of 
filter-buffer-substring

GitHub-reference: fix joaotavora/eglot#865
jollaitbot pushed a commit to sailfishos-mirror/emacs that referenced this pull request Oct 20, 2022
This was introduced in joaotavora/eglot#482 due to a bad interaction with a specific
server.  But this solution makes hyperlinks in Eldoc buffers
unclickable, because the markdown-mode function that visits a link
relies on the invisible text.

Per joaotavora/eglot#866 

* eglot.el (eglot--format-markup): Use buffer-string instead of 
filter-buffer-substring

GitHub-reference: fix joaotavora/eglot#865
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.

2 participants