0

I am trying to write some SQL code as a string within Node.js (Javascript) and each time I press the enter key IntelliJ is splitting the string like such:

This:

db.query('SELECT insertRecord($id, $name, $location, $title)');

Becomes this:

db.query('SELECT insertRecord(' +
'$id,' +
'$name,' +
'$location,' + 
'$title)');

Which is insanely annoying to the point of being unworkable. I just want it to do this:

db.query('SELECT insertRecord(
  $id, 
  $name, 
  $location, 
  $title)'
);

This is not SQL string splitting which can be turned on/off in File > Settings > Editor > Smart Keys > SQL. This seems to be Javascript string splitting for which I can't find an on/off option. Is there a way to stop this behaviour?

volume one
  • 6,800
  • 13
  • 67
  • 146
  • 1
    `db.query('SELECT insertRecord(` [line break here] `$id,` [another line break] `$name,` etc. won't be valid JS, though. – mbojko Jan 28 '21 at 20:26
  • @mbojko why is that? i dont actually want a line break to happen in the string, it is for my ease of reading. I have hundreds of variables and cant work with them all on a single line. – volume one Jan 28 '21 at 20:28
  • 3
    @volumeone because string literals cannot span multiple lines *except* if you add a backslash before the newline. But I wouldn't advise that as it's exceptionally easy to turn it into invalid code by adding an extra space after the backslash. See [Creating multiline strings in JavaScript](https://stackoverflow.com/q/805107) – VLAZ Jan 28 '21 at 20:48
  • 2
    Use backticks instead of single quotes around the string. That will create a template literal, which does allow embedded newlines. – Barmar Jan 28 '21 at 21:27
  • Thank you all - backticks solved the issue nicely. – volume one Jan 29 '21 at 12:44

1 Answers1

1

JavaScript doesn't allow line breaks in string literals (in double/single quotes), the IDE adds concatenation to keep your code valid. If you need adding line breaks to your strings for better readability, turn your string literals into template literals by changing quotes to backticks. This can be done using intention available on Alt+Enter:

enter image description here

lena
  • 90,154
  • 11
  • 145
  • 150