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

[Fix #445] def form with strings and docstrings #505

Merged
merged 1 commit into from
Feb 27, 2019

Conversation

carlosgeos
Copy link
Contributor

@carlosgeos carlosgeos commented Jan 20, 2019

I took the proposed solution here: #445 (thanks @oskarkv) and scoped it to only the def form.

Basically:

(def my_string_with_docstring "how to use" "just a string")

how to use and just a string are highlighted differently.

And:

(def im_a_string "hello")

hello is no longer highlighted as a docstring

I did what was said in these comments: #445 (comment) and #445 (comment). Now it works for defs, defprotocol, ns (whatever the form)...


  • The commits are consistent with our [contribution guidelines][1].
  • 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).

@carlosgeos
Copy link
Contributor Author

carlosgeos commented Jan 21, 2019

This also closes #469 #405 abo-abo/lispy#301

@bbatsov
Copy link
Member

bbatsov commented Feb 20, 2019

Please add some unit tests and update the change log as well. (This suggestions applies to the other PRs you’ve opened as well)

@carlosgeos carlosgeos force-pushed the fix-445 branch 2 times, most recently from 94f22b4 to 7174466 Compare February 20, 2019 17:18
@carlosgeos
Copy link
Contributor Author

done for this one, the others will come soon

(indent-according-to-mode)

(should (equal expected-state (buffer-string)))
(should (equal expected-state (buffer-substring-no-properties
Copy link
Member

Choose a reason for hiding this comment

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

Why is this change necessary?

Copy link
Contributor Author

@carlosgeos carlosgeos Feb 22, 2019

Choose a reason for hiding this comment

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

Because clojure-in-docstring-p checks for a font-lock-face, indent-according-to-mode works differently (in this case) if font-lock-mode is off, so I wrote font-lock-ensure since otherwise there is no font-lock information in the temp-buffer.

If this change wasn't there, this test:

(check-indentation no-indent-for-def-string
"(def foo \"hello|\")"
"(def foo \"hello|\")")

would always pass, both in the current version and my version. To make it fail in the current version (which is expected and wanted), the change had to be made. I found no other way to mimic what is really happening.

And about the line you highlighted: you are right, I'll compare the strings without removing the properties, no reason to do that.

@@ -68,6 +68,7 @@ values of customisable variables."
(search-forward "|")
(delete-char -1)
(clojure-mode)
(font-lock-ensure)
Copy link
Member

Choose a reason for hiding this comment

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

How can this affect an indentation test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it is because the indentation command behaves differently depending on whether font lock mode is ON or OFF, in the case of docstrings.

This branch is master with the test added: https://github.com/carlosgeos/clojure-mode/tree/temp-branch. The tests are fine, but if (font-lock-ensure) is added, the test with the docstring fails (like it should).

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, that’s odd. Turning on the mode should enable the font locking.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Usually yes, but apparently not when using temp-buffer:

https://stackoverflow.com/a/23569337
https://emacs.stackexchange.com/a/44301

@bbatsov bbatsov requested a review from cichli February 23, 2019 11:28
Copy link
Member

@cichli cichli left a comment

Choose a reason for hiding this comment

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

Thanks again! The changes look great to me. Just one question: should we still font lock the below string as a docstring?

(def foo
  "foo"
  )

It seems sexp-at-point removes that trailing whitespace, so we pass the wrong position to char-after.

@carlosgeos
Copy link
Contributor Author

True, that example is incorrectly font locked. I'll make some modifications.

@carlosgeos
Copy link
Contributor Author

The new solution handles whitespace and newlines better

If everything is ok, I will squash

;; In a def, at last position is not a docstring
(not (and (string= "def" firstsym)
(save-excursion
(goto-char startpos)
Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary given the goto-char below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The first goto-char looks redundant but I think it is not, because there is a (sexp-at-point) below, and it needs the point at the correct position beforehand.

Also, the (sexp-at-point) form does not take any arguments so I cannot pass startpos to it.

Anyway, I tried without it and it didn't work. Happy to remove it if we find the way to make it work.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, great point. I didn't think of that! Yeah, we definitely need it because of the (sexp-at-point).

Copy link
Member

Choose a reason for hiding this comment

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

Go ahead and squash if you like. I will merge once I have the rights to do so.

@cichli
Copy link
Member

cichli commented Feb 25, 2019

Apart from that one final comment, everything looks great now.

… forms

- def forms can now have docstrings and strings properly font-locked
- they will not be incorrectly indented

added tests as well
cichli added a commit to cichli/clojure-mode that referenced this pull request Feb 27, 2019
@cichli cichli merged commit a9a0276 into clojure-emacs:master Feb 27, 2019
@carlosgeos
Copy link
Contributor Author

Thanks, I also think you can close #469 and #405

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