Skip to content

Support multiple consecutive reader comments (#_#_a b) #545

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

Merged
merged 1 commit into from
Nov 12, 2019

Conversation

sulami
Copy link
Contributor

@sulami sulami commented Oct 1, 2019

Modified the default regexps and the heuristic to find the end of the region to
comment out.

Previously Emacs would treat the second #_ as the commented form, and then
highlight the following two forms as usual. Now it (mostly) matches what Clojure
actually evaluates. Things get weird when you start mixing #_ and forms, but
this fixes the most common use cases, like key-value-pairs.

Tagging #404/#528 for visibility

Note: I couldn't get the tests to work locally, so I'd be grateful if someone wants to add a test case for the above.


Before submitting a PR mark the checkboxes for the items you've done (if you
think a checkbox does not apply, then leave it unchecked):

  • The commits are consistent with our contribution guidelines.
  • You've added tests (if possible) to cover your change(s). Bugfix, indentation, and font-lock tests are extremely important!
  • You've run M-x checkdoc and fixed any warnings in the code you've written.
  • You've updated the changelog (if adding/changing user-visible functionality).
  • You've updated the readme (if adding/changing user-visible functionality).

Thanks!

@bbatsov
Copy link
Member

bbatsov commented Oct 3, 2019

Note: I couldn't get the tests to work locally, so I'd be grateful if someone wants to add a test case for the above.

What's the problem you had with the tests? Make test works fine for me.

@sulami
Copy link
Contributor Author

sulami commented Oct 4, 2019

Note: I couldn't get the tests to work locally, so I'd be grateful if someone wants to add a test case for the above.

What's the problem you had with the tests? Make test works fine for me.

make: *** No rule to make target `/Users/sulami/.emacs.d/straight/repos/clojure-mode/.cask/26.3/elpa', needed by `test'.  Stop.

@bbatsov
Copy link
Member

bbatsov commented Oct 4, 2019

What happens when you do emacs --version in a terminal?

Perhaps you can run make elpa before make test and this will work?

@sulami
Copy link
Contributor Author

sulami commented Oct 4, 2019

Emacs 26.3 (+ licensing info)

make elpa && make test worked fine (and passed), thanks! I'll have a look if I can craft a test for my change.

Verified

This commit was signed with the committer’s verified signature.
sulami Robin Schroer
Modified the default regexps and the heuristic to find the end of the region to
comment out.

Previously Emacs would treat the second `#_` as the commented form, and then
highlight the following two forms as usual. Now it (mostly) matches what Clojure
actually evaluates. Things get weird when you start mixing `#_` and forms, but
this fixes the most common use cases, like key-value-pairs.
@sulami
Copy link
Contributor Author

sulami commented Oct 4, 2019

Okay, I've added some tests. It font-locks the second pair in #_#_ to comment if there's nothing following, which isn't strictly true in the "what the clojure reader sees" sense, but it's no like #_ by itself is valid anything anyway.

@sulami sulami force-pushed the multiple-reader-comments branch from 6711a03 to 0b770ce Compare October 4, 2019 09:59
@yuhan0
Copy link
Contributor

yuhan0 commented Oct 4, 2019

Hi, just as a point of comparison, I had my own implementation of this feature roughly as follows, which uses a "stack" in the search function to determine the discarded range instead of regex.

(defun clojure--search-comment-macro-internal (limit)
  "Search for a comment forward stopping at LIMIT."
  (when (search-forward-regexp clojure-comment-regexp limit t)
    (let* ((md (match-data))
           (start (match-beginning 1))
           (state (syntax-ppss start))
           (skip 1))
      ;; inside string or comment?
      (if (or (nth 3 state)
              (nth 4 state))
          (clojure--search-comment-macro-internal limit)
        (goto-char start)
        (save-match-data
          (while (< 0 skip)
            (if (looking-at clojure-comment-regexp)
                (progn (goto-char (match-beginning 1))
                       (incf skip))
              (clojure-forward-logical-sexp 1)
              (skip-chars-forward " \n")
              (decf skip))))
        ;; Data for (match-end 1).
        (setf (elt md 2) (elt md 0))
        (setf (elt md 3) (point))
        (set-match-data md)
        t))))

The (dubious) advantage of this is that it works when you have interleaving forms , better emulating how the clojure reader works.
image

However It might be much slower than this implementation which is good for 99.9% of use cases (not sure how font-lock performance could be benchmarked)

@bbatsov
Copy link
Member

bbatsov commented Oct 7, 2019

(not sure how font-lock performance could be benchmarked)

I guess you can just generate a huge Clojure buffer, start the Emacs profiler and see how much time will be spend in font-locking with both approaches.

@bbatsov bbatsov merged commit 51016fa into clojure-emacs:master Nov 12, 2019
@bbatsov
Copy link
Member

bbatsov commented Nov 12, 2019

@sulami Thanks for tackling this! Sorry about the slow turnaround.

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

3 participants