clojure-mode icon indicating copy to clipboard operation
clojure-mode copied to clipboard

' (apostrophe) within/at end of symbol not counted as part of symbol

Open tanzoniteblack opened this issue 9 years ago • 17 comments

Currently clojure-mode appears to be treating dec' as the symbol dec with a ' following it.

This is made most obvious when I call cider-find-var on dec' at it finds the symbol at point to be dec and takes me to that function definition instead of dec'.

Care should be taken in resolving this bug that the prefixed ' (such as in 'clojure.core/dec) isn't treated as part of the symbol.

tanzoniteblack avatar Apr 29 '15 18:04 tanzoniteblack

Note that I'm only aware of examples from clojure.core where the apostrophe appears at the end of the name, but theoretically count's, or something similar where the apostrophe is within the symbol, is a perfectly valid identifier in Clojure.

tanzoniteblack avatar Apr 29 '15 18:04 tanzoniteblack

@tanzoniteblack dec' is not valid clojure symbol, but the reader does accept it.

Cognitect takes breakage very seriously so I don't think they'll remove this undocumented 'feature' anytime soon, but I wouldn't rely on it in my own code if I were you.

See the relevant section in the reader documentation specifically this section:

Symbols begin with a non-numeric character and can contain alphanumeric characters and *, +, !, -, _, and ? (other characters will be allowed eventually, but not all macro characters have been determined)

fwiw there's a single function in core breaking the rules in the reader docs: clojure.core/+'

expez avatar Apr 29 '15 18:04 expez

I don't use ' in my own code, as it I've never found a case where it makes sense to do so, and while the the count's example I made up just for the purpose of the issue, the dec' example is actually pulled from clojure.core https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#L1098 and came up because I was looking at the difference between dec and dec'; there's also inc', +', *', and -' .

tanzoniteblack avatar Apr 29 '15 18:04 tanzoniteblack

Interesting, I had no idea there were more. Guess that makes it more likely that this will be promoted to a reader feature.

expez avatar Apr 29 '15 18:04 expez

We should probably support this properly in clojure-mode, but I'm not sure what's the best option, as ' is also the quote reader macro, therefore I can't just mark it as something that can be part of a symbol name as things like "'something" will be treated as symbols. Ideas/suggestions are welcome!

bbatsov avatar May 16 '15 06:05 bbatsov

@Malabarba any idea how to tackle this?

bbatsov avatar Nov 09 '15 20:11 bbatsov

Nothing in particular.

I could imagine adding several clauses to the code to account for this, but I wouldn't wanna do that for something that's used so little.

We could also change the syntax-class of ' to symbol, but I'm afraid this would break something elsewhere.

Malabarba avatar Nov 10 '15 10:11 Malabarba

We could also change the syntax-class of ' to symbol, but I'm afraid this would break something elsewhere.

That would affect the case of ' at the beginning. Otherwise I would have done this by now. We need this to be a symbol char only when it's not at the beginning. Or something should fix Clojure, which is not going to happen.

bbatsov avatar Nov 10 '15 10:11 bbatsov

I could write some code that prevents users from typing ' at the end of a symbol. :-D How's that for fixing Clojure?

Malabarba avatar Nov 10 '15 10:11 Malabarba

The problem is that there are already symbols with such names and we're not handling them properly now (even in the standard library). If this was only a theoretical possibility I wouldn't give a fuck about it.

bbatsov avatar Nov 10 '15 10:11 bbatsov

Well, emacs syntax classes are supposed to be very versatile. I think you can define a function to determine the syntax of a char instead of having it hardcoded. But I've no idea how to do that. :-)

Malabarba avatar Nov 10 '15 10:11 Malabarba

This really plays into one of the few surface areas of Emacs that I've never dipped my toes: Writing a major-mode from scratch.

Malabarba avatar Nov 10 '15 10:11 Malabarba

Few people have done this. Most start by copy pasting a similar mode. Which is obviously what happened here. :-)

On Tuesday, 10 November 2015, Artur Malabarba [email protected] wrote:

This really plays into one of the few surface areas of Emacs that I've never dipped my toes: Writing a programming major-mode from scratch.

— Reply to this email directly or view it on GitHub https://github.com/clojure-emacs/clojure-mode/issues/290#issuecomment-155388834 .

Best Regards, Bozhidar Batsov

http://www.batsov.com

bbatsov avatar Nov 10 '15 10:11 bbatsov

I've just learned this might be possible with (some generalisation of) the following Font-lock rule

("[0-9a-z]\\('\\)" 1 "_")

Malabarba avatar Jan 12 '16 16:01 Malabarba

Guess we'll have to test this. :-)

bbatsov avatar Jan 12 '16 17:01 bbatsov

That's a negative. I've found we can set the syntax-table property of a character with a font-lock rule like this:

  (,clojure--sym-regexp 0 '(face default syntax-table (3 . nil)) append)

However, in order for that to have any effect, we need to set (setq-local parse-sexp-lookup-properties t), and for some reason this makes my Emacs go crazy (might be an Emacs bug).

Malabarba avatar Jan 16 '16 09:01 Malabarba

autogenerated with https://github.com/MalloZup/doghub: issue inactive since 450 days. Please update the issue or close it

MalloZup avatar Aug 05 '19 21:08 MalloZup