Subject: bug#24896: JSX prop indentation after fat arrow

From: Jackson Ray Hamilton <[email-masked]>
Date: Mon, 23 Jan 2017 09:07:17 -0800

Hi Felipe,

Regarding,

> This is based on a rough heuristic that essentially backtracks looking for "[(,]\n *<" (it also handles comments). This misses any JSX which is not at the start of the line, and it only tells us the start of the tag, not the end or where the body ends. In js2 and rjsx there is of course the full parser to give us this information.

Please note Dmitry's comment:
https://github.com/mooz/js2-mode/issues/140#issuecomment-40887172,

> As for indentation . . . there's a question how one would determine whether point is inside an XML expression (and expression's bounds) without using the AST (using it was rejected in the past on the grounds of that being slow).

And see here for my explanation of that design:
https://github.com/mooz/js2-mode/issues/140#issuecomment-145325361

Feel free the improve upon this algorithm, although do take care to
benchmark the code before and after your changes, with buffers of
various sizes.  Large files won't hold up well if using an AST for
indentation.  Probably better to extend the current heuristic to be more
accurate.

Jackson

On 01/23/2017 01:26 AM, Felipe Ochoa wrote:
>>> There are still issues with greater-than and less-than 
>>> as binary operators.
>> Inside XML literals, you mean?
> 
> Yes, exactly.
> 
>> How's your experience so far?
> 
> It's actually worked very well. I had an issue once where indenting an entire region took several passes to get right, but now I'm not able to reproduce it :( 
> 
>> Here's the problem: js-indent-line uses syntax-ppss. 
>> sgml-indent-line doesn't (for now), but js-jsx-indent-line 
>> calls js-indent-line in certain contexts. And this is a problem
>> because calling syntax-ppss in different contexts with 
>> incompatible (paren-wise) syntax tables will make 
>> syntax-ppss cache broken, and lead to likewise broken
>> behaviors.
> 
> I'm not sure I'm grasping this part entirely. I understand conceptually that using syntax-ppss with incompatible syntax tables could lead to cache problems. But it seems to me that the js*-mode and sgml-*-mode syntax tables are already incompatible (namely, "<" and ">", which are causing all this grief!). Would introducing this additional incompatibility cause more problems? 
> 
>> So, one thing we could do here is let-bind the variables that 
>> constitute syntax-ppss cache around the call to orig-fun 
>> (i.e. around the context where we modify the syntax table).
>> ... (the cache is not really a public API)
> 
> This sounds like a bit of a headache. E.g., indenting a region would require binding and unbinding the cache carefully as you stepped into and out of JSX. What if we just scrap the syntax-ppss cache altogether? Would the performance penalty be too great?
> 
>> Another, somewhat more difficult approach, would be to try
>> to apply the "<" and ">" syntax classes in 
>> syntax-propertize-function, only to occurrences of "{" and "}" 
>> inside XML literals. That would require knowing where the said
>> literals begin and end, but we do know that somehow already,
>> seeing as we know which indentation function to choose, right?
> 
> This is based on a rough heuristic that essentially backtracks looking for "[(,]\n *<" (it also handles comments). This misses any JSX which is not at the start of the line, and it only tells us the start of the tag, not the end or where the body ends. In js2 and rjsx there is of course the full parser to give us this information. 
> 
>> This way we don't depend on syntax-ppss internals, and reindenting
>> the whole buffer might be faster, because we would keep syntax-ppss 
>> cache around more. Still, not sure how much faster that would be in 
>> practice.
> 
> I think we could use a regex like the following to identify JSX start tokens:
> 
> (rx (seq (or (any "-+*/%=><?:&")
>              (seq (or "return" "typeof" "delete" "instanceof") whitespace)
>              (any "([{,;"))
>          (* whitespace) ; Should also skip over comments
>          "<"))
> 
> I.e., any "<" after an operator or at the beginning of an expression or statement. We'd have to filter out some false positives (postfix ++ and --, strings, and comments, possibly others), but this would get all the JSX start tags, I think. We could use a similar regex to find the ">" that close JSX tags:
> 
> (rx (seq ">"
>          (* whitespace) ; Should also skip over comments
>          (or (any "-+*/%=><?:&")
>              (seq (or "return" "typeof" "delete" "instanceof") whitespace)
>              (any "(}],;"))
> 
> Not sure how to go from there to the "{" and "}" tokens though. Is it possible to run syntax-ppss using different tables for different parts of the buffer?
>