microsoft/node-jsonc-parser

Support multi line strings

KoyamaSohei opened this issue · 3 comments

related issue:
microsoft/vscode#15140

My proposal

Support multi line strings with python-like syntax

{
  "prefix": "foo",
  "body": """ 
int main() {
  return 0;
}"""
}
// Equals to
/*
{
  "prefix": "foo",
  "body": "\nint main() {\n  return 0;\n}"
}
*/

End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line.

{
  "prefix": "foo",
  "body": """\
int main() {
  return 0;
}"""
}
// Equals to
/*
{
  "prefix": "foo",
  "body": "int main() {\n  return 0;\n}"
}
*/

Motivation

When we create a snippet with multi line, we have 2 choices.

  1. wrap the string of "body" every line. (it's bothersome)
{
  "prefix": "foo",
  "body": [
    "int main() {",
    "  return 0;",
    "}"
  ]
}
  1. join with \n (the longer the code, the harder it is to read)
{
  "prefix": "foo",
  "body": "int main() {\n  return 0;\n}"
}

There is no problem if the number of lines is small or if there are few opportunities to create snippets,
but there is room for improvement in terms of labor and readability for snippets heavy use.

I hope this proposal is taken into account. Thanks!

@aeschli

I read this issue json5/json5#190

json5 is not the best solution to solve my issue. ( because json5 line break does not include the meaning of '\n'. I want to copy code & paste it directly into config file).

// we need '\n\' end of every line ...
{
  prefix: "foo",
  body: "\
int main() {\n\
  return 0;\n\
}"
}

However, jsonc seems better to integrate with json5.

Do you have any plans to integrate jsonc into json5 in the future? If there is, my proposal was not in line with the integration process and may be inappropriate.

Hi @KoyamaSohei, thanks for the suggestion, but I would like to keep JSONC just as JSON with comments. That's what's in use for many existing the VSCode configuration files but e.g. also in Sublime, for eslint.json, tsconfig.json which to this with other parser libraries.

Adding multi-line strings would introduce another flavor of JSON and make things more complex.

If the snippet declarations syntax is too limiting, I'd suggest filing an issue against snippets in VSCode to also support JSON5 or YAML or any of ther already existing formats.

We have no plans to integrate jsonc into json5 .