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

Extended support for function shorthand notation #1282

Merged
merged 8 commits into from Apr 10, 2024

Conversation

maxecharel
Copy link
Contributor

Adds font-locking support for R function shorthand notation \() and for names of functions defined using this shorthand notation. Also makes the shorthand notation a recognized function pattern.

Maxime Pettinger added 3 commits March 3, 2024 11:18
using the backslash shorthand notation. Ensure extensibility by
creating variables `ess-R-keystrings' and `ess-r--non-fn-kstrs' as
complements to `ess-R-keywords' and `ess-r--non-fn-kwds'. Adjust
`ess-r--find-fl-keyword' accordingly. Fixes emacs-ess#1278.

Add question mark (shortcut to help) to both `ess-R-keystrings' and
`ess-r--non-fn-kstrs'.
`ess--r-s-function-pattern'. Allows function defined with the
shorthand notation to be recognized as functions by e.g.
`ess-r-beginning-of-function'.
Copy link
Member

@lionel- lionel- left a comment

Choose a reason for hiding this comment

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

Would you mind taking a look and add to the existing unit tests for font-locking in https://github.com/emacs-ess/ESS/blob/master/test/ess-test-r-fontification.el? If you run into trouble please let me know.

lisp/ess-custom.el Outdated Show resolved Hide resolved
@@ -2088,7 +2098,7 @@ See also function `ess-create-object-name-db'.")
(defvar ess-R-function-name-regexp
(concat "\\(" "\\sw+" "\\)"
"[ \t]*" "\\(<-\\)"
"[ \t\n]*" "function\\b"))
"[ \t\n]*" "\\(function\\b\\|\\\\\\)"))
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we want to encourage such usage but I guess it makes sense to have a baseline support and let linters/reformatters correct this usage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Personally I only use the shorthand notation for lambdas, and I agree with you but since the options has been made available by R Core, I think it is a good thing to have it supported by ESS. I see your point concerning the 'encouragement', but I do not consider this as a real one (including snippet templates would have been a different story^^)

@maxecharel
Copy link
Contributor Author

Hi @lionel- , sorry for the delay before answer. I will add to the existing unit tests for font-locking end of this week. I will get back to you. Thanks for the great work with ESS !

Maxime Pettinger added 2 commits March 23, 2024 23:02
…hens' convention and remove reference made to it from the docstring of `ess-R-keywords'.

Set it to lowercase.
… tests, excluding test related to backquoted function definition fontification.
@maxecharel
Copy link
Contributor Author

maxecharel commented Mar 24, 2024

Hi @lionel- , your feedback would be welcome concerning the two following issues I encountered when adding to the unit tests:

  1. while \() passes the ess-test-r-fontification-keywords-fn-test test, I had to go for a 'trial and error' approach that I describe below. To investigate the reasons why it failed in the first place, I would like to get access to the ERT test buffer (not just the results) to see how the string is printed. Any idea how to do so?
  2. when function name is backquoted, it is not highlighted (I realized that when trying to add to ess-test-r-fontification-keywords-disabled-backquoted-defun), i.e.:
    backquoted_name_for_shorthand_notation_no_fl
    I failed to identify the problem. I will carry on investigating in the following days, but maybe you have some hints on the nature of the issue?

Concerning (1), at first ess-test-r-fontification-keywords-fn-test failed. It was rather surprising given that manually executing (face-at-point) in my minimal Emacs testing config worked as expected:
etest_manual_testing_ok
Anyway, ERT indicated a failure whenever the relevant string was set (in the :case string) to \\\\(), \\() or even \()
(with the pilcrow character located at the relevant place depending on :case or :result).

My first guess was that the problem comes from the way ERT, based on the :case of ess-test-r-fontification-keywords-fn-test, prints the string representing \() in a test buffer. After some trials and errors, I ended up using group delimiters --- i.e. I went for \\(\\\\\\)() --- and it passed the test. Yet I am not satisfied with this, and I would like to get a deeper understanding of what happened exactly. Is there any way I can get access to the actual test buffer in which the expressions contained in :case are printed and actually tested? Or am I missing the point and there is no creation of such buffer?

Thanks.
Best,
Max

@lionel-
Copy link
Member

lionel- commented Mar 25, 2024

@maxecharel I will try to find time to take a look this week.

Maxime Pettinger added 2 commits March 25, 2024 18:33
…R function shorthand notation. Allows e.g. backquoted function names to be font-locked.
@maxecharel
Copy link
Contributor Author

@lionel- some updates (to keep you posted and as notes for myself):

  1. I understood why backquoted 'names' of functions defined with \() were not fontified; solved with commit 5c24a72 by modifying ess-r-font-lock-syntactic-face-function
    after_modifying_ess-r-font-lock-syntactic-face-function
  2. I discovered an issue with my initial modification of ess-R-function-name-regexp while I was investigating the previous issue and fixed it with commit 49bcd58:
    before_additional_adjustment_ess-R-function-name-regexp
    after_additional_adjustment_ess-R-function-name-regexp

One question remains, and one has been added (this one will certainly require your feedback at some point, sorry for that)

  1. how to get access to the ERT test buffer (nothing new here; I'll try to answer that later this week while reruning the tests, but if and only if you find time, your feedback is more than welcome)
  2. definitions of ess-r-mode and ess-r-transcript-mode (as well as inferior-ess-r-mode) all reference ess-r-font-lock-syntactic-face-function, but they (except the latter mode) also set a local add-log-current-defun-header-regexp including a string in the spirit of "<- function". However, here it looks like we enter a world that goes beyond font-locking (and therefore the scope of this PR). When you have the opportunity, could you just confirm this? Anyway I will set that aside for now, and will try to find the time to dig into it later.
    Cheers!

@lionel-
Copy link
Member

lionel- commented Apr 10, 2024

Sorry I don't have any bandwidth for this :/

Let's just merge your work.

@lionel- lionel- merged commit 8030e29 into emacs-ess:master Apr 10, 2024
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.

None yet

2 participants