Skip to content

clojure-in-docstring-p is wrong when string contains backslash (\) #544

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

Closed
Frozenlock opened this issue Sep 24, 2019 · 2 comments · Fixed by #563
Closed

clojure-in-docstring-p is wrong when string contains backslash (\) #544

Frozenlock opened this issue Sep 24, 2019 · 2 comments · Fixed by #563

Comments

@Frozenlock
Copy link

Expected behavior

(def test-string
  "this
is
my
string")

Highlighted as a normal string.

Actual behavior

(def test-string
  "this\n
  is
  my
  string")

Automatic indentation (unwanted in a string!) AND highlighted as a docstring.

Steps to reproduce the problem

Insert an escape character (\) to see the string switch to a docstring highlighting.

Eval (clojure-in-docstring-p) while the cursor is on the string to see that the result is wrong.

Environment & Version information

clojure-mode version information

clojure-mode (version 5.11.0)

Emacs version

Emacs 26.2

Operating system

Ubuntu 18.04.3 LTS

@isdanni
Copy link

isdanni commented Oct 11, 2019

I'm having the same problem. Same clojure & OS version, Emacs 26.3.

@kylc
Copy link
Contributor

kylc commented Jan 26, 2020

I've done some digging and I've narrowed it down to these lines in the clojure-font-lock-syntactic-face-function function:

(not (and (string= "def" firstsym)
        (save-excursion
            (goto-char startpos)
            (goto-char (+ startpos (length (sexp-at-point)) 2)) ;; <=====
            (looking-at "[ \r\n\t]*\)"))))

(length (sexp-at-point)) does not return useful value in this context for strings with escape characters in them, e.g.:

(length "abc")   ;=> 3
(length "abc\n") ;=> 4

In the example string "abc\n", the function needs to move over 5 characters to find the closing parenthesis it's looking for, but instead it only moves over 4, landing on the closing quote, and determining that this is not the last item in the def form and is therefore a docstring.

Is there a function that can be used to escape the string or otherwise find its "length including escape characters"? string-width nor string-bytes seem to be what we're after. Additionally, bounds-of-thing-at-point seems to be affected by the same logic error and so can't be used here.

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 a pull request may close this issue.

3 participants