One-line heredoc for better syntax highlightning

php.internals

Olle Härstedt

3 years ago
Hi internals! Some editors can guess the domain-specific language inside heredoc, e.g. if you do $query = <<<MySQL SELECT * FROM foo MySQL; It would be nice if this feature could be used in single lines as well: $query = <<<MySQL SELECT * FROM foo MySQL; Any limitations in the parser that prevents this? Or just a convention or historical choice? Olle Härstedt Software Developer (Chief Software Architect) tel:+49 40 22660066 mailto:olle.hearstedt@limesurvey.org https://www.limesurvey.org LimeSurvey GmbH Papenreye 63 22453 Hamburg Sitz der Gesellschaft / trade register: Hamburg, Amtsgericht Hamburg HRB 137625 Steuernummer / tax id: 49 / 740 / 01441 https://www.limesurvey.org https://www.facebook.com/LimeSurvey/ https://twitter.com/LimeSurvey https://www.linkedin.com/company/limesurvey https://www.xing.com/companies/limesurveygmbh https://github.com/LimeSurvey/LimeSurvey

Rowan Collins

3 years ago
On 19 September 2022 15:24:26 BST, "Olle Härstedt" <olle.haerstedt@limesurvey.org> wrote:
>Hi internals! > >Some editors can guess the domain-specific language inside heredoc, e.g. if you do > >$query = <<<MySQL >SELECT * FROM foo >MySQL; > >It would be nice if this feature could be used in single lines as well: > >$query = <<<MySQL SELECT * FROM foo MySQL;
While I can't immediately think of a practical problem with this, I'm hesitant at the idea of adding yet more ways of quoting strings; it means more for users to learn, more for third party parsers to implement (including exactly the syntax highlighting tools you're going to help), and in the worst case, more edge cases that can lead to security issues. I don't know about other editors, but PhpStorm at least also recognises an in-line comment explicitly labelling any string, which if anything seems more explicit: $query = /** @lang mysql */"SELECT * FROM foo"; Regards,
-- Rowan Tommins [IMSoP]

Sara Golemon

3 years ago
On 19 September 2022 15:24:26 BST, "Olle Härstedt" < olle.haerstedt@limesurvey.org> wrote:
>Some editors can guess the domain-specific language inside heredoc, e.g.
if you do
> >$query = <<<MySQL >SELECT * FROM foo >MySQL; > >It would be nice if this feature could be used in single lines as well: > >$query = <<<MySQL SELECT * FROM foo MySQL; >
Good news! This feature exists and was introduced by Rasmus **checks notes** about 25 years ago. To use it, you'll want to hold down your shift key, then press the key just to the left of your enter key. This is called a "quote", and you can see it demonstrated here around the word quote. Hope that helps! Seriously though, I know you're looking to help your editor find something to syntax highlight, but this is an editor problem, not a PHP language problem. If your editor can detect SQL in heredoc, then it should be able to improve and find it in interpolated strings. Making the parser more complex for no benefit to the actual language (some detriment, I would argue) is not the fix here. -Sara P.S. - Yes, I'm assuming a US layout, you know where your quote key is.

Olle Härstedt

3 years ago
2022-09-20 17:00 GMT+02:00, Sara Golemon <pollita@php.net>:
> On 19 September 2022 15:24:26 BST, "Olle Härstedt" < > olle.haerstedt@limesurvey.org> wrote: >>Some editors can guess the domain-specific language inside heredoc, e.g. > if you do >> >>$query = <<<MySQL >>SELECT * FROM foo >>MySQL; >> >>It would be nice if this feature could be used in single lines as well: >> >>$query = <<<MySQL SELECT * FROM foo MySQL; >> > Good news! This feature exists and was introduced by Rasmus **checks > notes** about 25 years ago. > > To use it, you'll want to hold down your shift key, then press the key just > to the left of your enter key. This is called a "quote", and you can see > it demonstrated here around the word quote. > > Hope that helps! > > Seriously though, I know you're looking to help your editor find something > to syntax highlight, but this is an editor problem, not a PHP language > problem. If your editor can detect SQL in heredoc, then it should be able > to improve and find it in interpolated strings.
Nope, because you need to be explicit which type of SQL you're working with - MySQL, Postgres, SQL Server, etc. Would be hard to write a regexp to guess that for the editor. :) In the Vim case, it reads your delimiter name and applies syntax highlight from that (mysql, javascript, html).
> Making the parser more > complex for no benefit to the actual language (some detriment, I would > argue) is not the fix here. > > -Sara > > P.S. - Yes, I'm assuming a US layout, you know where your quote key is.
I you read my text more carefully, you'd see that I thought the parser would be simplified by my suggestion, by removing what I thought was an arbitrary limitation. I was wrong. :( Olle

Tim Düsterhus

3 years ago
Hi On 9/19/22 16:24, Olle Härstedt wrote:
> $query = <<<MySQL > SELECT * FROM foo > MySQL; > > It would be nice if this feature could be used in single lines as well: > > $query = <<<MySQL SELECT * FROM foo MySQL; >
I don't find that example particularly readable or convincing. It mushes together PHP and MySQL keywords too much. Once you add a WHERE clause that SQL query should be split across multiple multiple lines for readability anyway and even for that very simple example I personally would add a linebreak in front of the FROM: $query = <<<MySQL SELECT * FROM foo MySQL; Best regards Tim Düsterhus