Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Use different syntax table for JSX text #24

Closed
felipeochoa opened this Issue Dec 27, 2016 · 18 comments

Comments

2 participants
Owner

felipeochoa commented Dec 27, 2016

We should use put-text-property to set a different syntax table on stretches of JSX text. Not sure which syntax table is best, it should be more text-like. One issue in particular is single quotes messing up the syntax parser when they're unmatched (e.g., in a contraction). Probably should use an SGML syntax table, but maybe just fundamental itself?

Any workarounds for this? I noticed my JSX block lost highlighting capabilities with rainbow-delimeters. (it was a single quote).

Not the end of the world by any means.

Owner

felipeochoa commented Jul 27, 2017

Here's some advice you could play around with. I haven't looked into the correct syntax table to use, but if this works for you feel free to send a PR or leave a note and I'll incorporate this into master.

(defun rjsx-advice-text-syntax-table (orig-fn &rest args)
  (let ((token-type (apply orig-fn args)))
    (when (eq token-type rjsx-JSX-TEXT)
      (let* ((token (js2-current-token))
             (beg (js2-token-beg token))
             (end (js2-token-end token)))
        (js2-record-text-property beg end 'syntax-table (standard-syntax-table))))
    token-type))

(advice-add 'rjsx-get-next-xml-token :around 'rjsx-advice-text-syntax-table)
;; (advice-remove 'rjsx-get-next-xml-token 'rjsx-advice-text-syntax-table)

Hmm didnt seem to make a change. I switched the above code snippet to be text-mode-syntax-table, but rjsx mode wouldn't come on when I did that.

Owner

felipeochoa commented Jul 28, 2017

Can you turn on debug-on-error and paste the stack trace in here?

Not these errors don't happen with your code. I was just switching it to other modes to see if any gave the right result.

The code I'm viewing in JSX is:

import React, {Component} from 'react';

export default class MyComp extends Component{
  render(){
    return (
      <div>
        I'm a contraction
        </div>
    )
  }
}

Which stops highlighting at the div with contraction.

Here the output, trying the different text-mode-syntax-table:
Error running timer ‘js2-mode-idle-reparse’: (void-function text-mode-syntax-table)

The full stack trace is:
Debugger entered--Lisp error: (void-function text-mode-syntax-table) (text-mode-syntax-table) (js2-record-text-property beg end (quote syntax-table) (text-mode-syntax-table)) (let* ((token (js2-current-token)) (beg (progn nil (or (and (memq (aref token 0) cl-struct-js2-token-tags)) (signal (quote wrong-type-argument) (list (quote js2-token) token))) (aref token 2))) (end (progn nil (or (and (memq (aref token 0) cl-struct-js2-token-tags)) (signal (quote wrong-type-argument) (list (quote js2-token) token))) (aref token 3)))) (js2-record-text-property beg end (quote syntax-table) (text-mode-syntax-table))) (progn (let* ((token (js2-current-token)) (beg (progn nil (or (and (memq ... cl-struct-js2-token-tags)) (signal (quote wrong-type-argument) (list ... token))) (aref token 2))) (end (progn nil (or (and (memq ... cl-struct-js2-token-tags)) (signal (quote wrong-type-argument) (list ... token))) (aref token 3)))) (js2-record-text-property beg end (quote syntax-table) (text-mode-syntax-table)))) (if (eq token-type rjsx-JSX-TEXT) (progn (let* ((token (js2-current-token)) (beg (progn nil (or (and ...) (signal ... ...)) (aref token 2))) (end (progn nil (or (and ...) (signal ... ...)) (aref token 3)))) (js2-record-text-property beg end (quote syntax-table) (text-mode-syntax-table))))) (let ((token-type (apply orig-fn args))) (if (eq token-type rjsx-JSX-TEXT) (progn (let* ((token (js2-current-token)) (beg (progn nil (or ... ...) (aref token 2))) (end (progn nil (or ... ...) (aref token 3)))) (js2-record-text-property beg end (quote syntax-table) (text-mode-syntax-table))))) token-type) rjsx-advice-text-syntax-table(#[0 "\306� \307V\203#

Owner

felipeochoa commented Jul 28, 2017 edited

I think you have text-mode-syntax-table in the parenthesis (called as a function). Try unwrapping it to use it as a variable and see what that gives

Owner

felipeochoa commented Aug 2, 2017

In other words, use this:

(defun rjsx-advice-text-syntax-table (orig-fn &rest args)
  (let ((token-type (apply orig-fn args)))
    (when (eq token-type rjsx-JSX-TEXT)
      (let* ((token (js2-current-token))
             (beg (js2-token-beg token))
             (end (js2-token-end token)))
        (js2-record-text-property beg end 'syntax-table text-mode-syntax-table)))
    token-type))

Here's what I get after using that:
image
(I get the same result with either piece of advice though)

Owner

felipeochoa commented Aug 2, 2017

Maybe the problem is parse-sexp-lookup-properties. Can you try setting that to t?

Yes, I can try that this afternoon, thanks for the feedback

I'm getting the same result.
Though I might add it might be a problem with Rainbow Delimiter since other things do highlight properly. Just no more brackets and such after the contraction

Owner

felipeochoa commented Aug 7, 2017

That screenshot was with rainbow delimiters. What if you try running emacs with -Q and just installing those two packages?

vidjuheffex commented Aug 8, 2017 edited

@felipeochoa well what do you know... working highlighting. Thanks. I guess I have to pinpoint the problem package now.

Edit: maybe I was seeing things. But I can no longer recreate this working situation.

Now here's a stranger thing. (Or maybe not, I'm kind of new to emacs)
I have my customize settings in a separate file. So I unlinked those. Still had the issue, so I knew it wasn't that. Then I commented out my entire init.el file, I open it up to vanilla looking emacs. I open, testfile.jsx.. and still the same problem.

I know -Q and commenting out aren't the same (for one, .jsx files still opened in rjsx-mode) but I was wondering if you (or any other experiences emacs users) had some knowledge onhow to best troubleshoot this.

Owner

felipeochoa commented Aug 8, 2017

I'm not sure -- you may have better luck on Stack Exchange or Reddit for debugging this. I don't use customize, so don't really know how those interact, but I would suggest you simply try adding packages back one at a time until it breaks. Then you'll have a strong clue

Owner

felipeochoa commented Aug 8, 2017

Ah just saw you'd already posted on reddit

vidjuheffex commented Aug 8, 2017 edited

EDIT: ignore the following message. Highlighting doesnt work on either. I've managed to see it right once on each machine. I think I'm just going to turn off rainbow delimeter.

Yeah, I'll get to the bottom of it eventually.

I have two windows boxes, both with the same version of emacs from the same source, the same packages installed with use-package :ensure t, and on my Surface everything is fine, on my desktop highlighting breaks. It's the strangest thing.

@felipeochoa I spoke too soon! It's a combination of things!

Okay, I have to use the first snippet you posted here. BUT. I have to toggle rainbow delimiters on and off as per the response here: https://www.reddit.com/r/emacs/comments/6sa6jt/troubleshooting_conflicting_packages/

Thanks so much and I apologize for such a long back and forth.

Owner

felipeochoa commented Aug 8, 2017

Glad you got it fixed. Sounds like an issue of colliding font-lock handling

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment