How can I get gg=G in vim to ignore a comma? - json

I have autoindent and smartindent in vim turned on, but nocindent turned off. I'm trying to indent some JSON text without pretty-printing the whole thing, which would be too intrusive:
{
"a" : "value1",
"b": "value2",
"c": "value3",
...
Gets formatted by gg=G as:
{
"a" : "value1",
"b": "value2",
"c": "value3",
...
What's the logic going on there, and what options can I set to fix it, if it's possible? I tried toggling options like autoindent, smartindent, and cindent (with their "no" counterparts), but it doesn't have an effect on the = command. My latest attempt had these options:
autoindent
smartindent
nocindent
cinoptions=
indentexpr=
indentkeys=0{,0},:,0#,!^F,o,O,e

I can explain the logic, but I'm not sure of an easy fix. Vim's internal indenter is following C-style syntax, so since the "a" : "value1", line doesn't end with a ; it assumes that the following lines are a continuation of that statement and they should be indented to show that.
:help C-indenting goes into great depth discussing the various indent options and how they interact. I skimmed it and nothing jumped out at me, but it's worth a read.
If you have an external formatter that better recognizes the structure of your code, you can always set equalprg to run that instead of using the internal formatter.
Edit: On second thought, set cinoptions+=+0 will disable indenting for line continuation. This will also affect regular code, but it might be a reasonable tradeoff depending which annoys you more. You can also set it per filetype if you're editing standalone .json files.

The built-in indent settings won't totally cover a complex, non-C language like JSON. Better use a tailored indent setting, like the indent/json.vim indent plugin that is part of vim-json.

Related

How to force prettier html formatting to format tags in one line?

I use prettier in my VSC, so how to force prettier HTML formatting to format tags in one line, not multiple lines?
I want to format something like this all in one line
<v-navigation-drawer :clipped="$vuetify.breakpoint.lgAndUp" v-model="drawer" fixed app>
Does exist any config for prettier HTML formatter?
Prettier has the option printWidth. If you set that option to a high number, it will break fewer lines.
In your .prettierrc.json file, you can override this option for your HTML files:
{
// Other options...
"overrides": [
{
// change .html with .vue if you are using Vue files instead of HTML
"files": "src/**/*.html",
"options": {
"printWidth": 140
}
}
]
}
Is not recommendable to use a line-lenght higher than 140. Prettier is opitionated, so it should be used without too many customizations.
First check if you have a filename .prettierrc in the root. If no, create it and then put these values in it.
{
"trailingComma": "es5",
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"printWidth": 3000,
"bracketSpacing": true
}
Set printWidth to something large value and toggle wrapping text with your editor whenever you want; for example, in VS Code: Alt+Z.
I know this topic is old but maybe others still have this issue. In the Prettier setting you can adjust the width of the Print Width. 120 width does the job for me.
It depends on your IDE settings but as documentation states, you should have a config file where you should look for the html.format.wrapLineLength property and apply the line length value which satisfy you. This will prevent Prettier to break your lines until assigned line length to the property is reached. This will only apply for HTML.
For overall purposes you should use prettier.printWidth property which
Fit code within this line limit
Mind that tabulation / indention spaces also count in the line length.
Unfortunately when code line exceed the above numbers it will be spited to multi-lines, each property per line. So far I did not find solution to just wrap to extra line, without spiting to multi.
UPDATE
Mentioned and desired by me behavior it is not affordable with Prettier, but the line length trick still can do the job ... partially.

Extract and Create Property in JSON file with RegEx

I have the following JSON file. Dotted across the file is the following:
"properties": {
"Name": "Darlington",
"Description": "<br><br><br> <table border=\"1\" padding=\"0\"> <tr><td>CCGcode</td><td>00C</td></tr> <tr><td>CCGname_short</td><td>Darlington</td></tr>"
}
Using RegEx, I would like to extract the CCG Code property and add it back in so that the above becomes:
"properties": {
"Name": "Darlington",
"CCGcode": "00C",
"Description": "<br><br><br> <table border=\"1\" padding=\"0\"> <tr><td>CCGcode</td><td>00C</td></tr> <tr><td>CCGname_short</td><td>Darlington</td></tr>"
}
I've tried all sorts and I just can't get it to work. I am using Sublime Text.
^("Description":").*?<td>CCGcode<\/td><td>([^<>\n]*).*$
The above selects the code, but not sure how I can get it to create the property.
Try this
( *)"Description".*?CCGcode.*?<td>([^<]+)
Regex demo
This one for sublimetext3
Find what:
( *)("Description".*?CCGcode.*?<td>)([^<]+)
Replace with:
\1"CCGcode": "\3",\n\1\2
Demo
There's a very simpel, but not so elegant, solution. Replace
"Description":.*?<td>CCGcode<\/td><td>([A-Z\d]*)<\/td>
with
"CCGCode":"\1",\n \0
Don't know how Sublime handles replacements, but you may have to change the replacing \0 and \1 to something else - e.g. $0 and $1.
What it does is to find the Description entry and the following CCGCode entry, capturing the code into capture group 1.
Then replace capture group 0 - the whole matched text, with the new CCGCode JSON tag plus the original text.
It's a pretty fragile solution, but it works for your sample case.
Check out example at regex101.
Regards

quick formatting shortcut sublime text

I was wondering if someone had the sequence of steps to accomplish this task, inline, without copy and pasting.
Imagine you have a block of code where things are not cleanly justified
{
foo: "foo value",
bar: "bar value",
reallylongvariable: "reallylongvariable value",
shortname: "shortname value"
}
is there a very quick way to transform it in this, justifying the spaces and ragged spacing to a unified formatting
{
foo: "foo value",
bar: "bar value",
reallylongvariable: "reallylongvariable value",
shortname: "shortname value"
}
I know about option selection. Generally, I follow these steps
option select a column,
paste it on a new set of lines,
reselect pasted content
use command + [ to remove indention,
option reselect column of unindented content
past back in place in the original column.
But this process feels very manual, and was wonder if there was way to clean up the indentation inline, without copy and paste. Perhaps a helper utility that can do this automatically within a highlighted selection.
Hopefully my question makes sense.
There are multiple plugins for this task, for example:
https://github.com/wbond/sublime_alignment
https://github.com/randy3k/AlignTab
Here you can search for all of them: https://sublime.wbond.net/search/align
I personally recommend the AlignTab. It may seem complex, but if you know how to use regular expressions it is the most powerful you will find. It may do some crazy magic if you get used to it.

JSON string data display with breaks?

So I'm creating a game for work, which grabs question and answer data from a JSON file that someone helped me create. All I want to figure out, is how to make the string that is returned from the data below display with line breaks for each of the multiple choice answers:
{
"question": "1",
"text" : "How many times has the Actuarial computer lab been moved? A. Once B. Twice C. Six times D. Fifteen times",
"answer" : "1,1"
},
I've been googling for a while (lots of Stackoverflow threads) but every solution appears to be something different or slightly more complex than what I want to do.
Here is how I'm displaying the string into my div:
var displayDiv = $("#textdisplay");
displayDiv.text(question.text);
You probably want to add <br/> or <p> tags to your text in the JSON file itself. If you've got a text editor that can do regex string replacement, then you want to do something like
Find: ([ABCD]\.\s)
Replace: <br/>$1
Or if you're on a machine with sed, you can use
sed -E 's/([ABCD]\. )/<br\/>\1/g' test.txt
That'd be my recommendation, at least. If you can't change the JSON file, use the same Regex to add breaks in JavaScript, like so:
question.text.replace(/([ABCD]\. )/g, '<br/>$1')

Structured text in JSON

I have been looking for a way to capture structured text (sections, paragraphs, emphasis, lists, etc.) in JSON, but I haven't found anything yet. Any suggestions? (Markdown crossed my mind, but there might be something better out there.)
How about something like this:
[ { "heading": "Foobar Example" },
{ "paragraph":
[
"This is normal text, followed by... ",
{ "bold": "some bold text" },
"etc."
]
}
]
That is:
use a string for plain text without formatting or other mark-up;
use an array whenever you want to indicate an ordered sequence of certain text elements;
use an object where the key indicates the mark-up and the value the text element to which the formatting is applied.
HTML is a well-established way to describe structured text, in a plain-text format(!). Markdown (as you mentioned) would work as well.
My view is that your best bet is probably going to be using some sort of plain-text markup such as those choices, and place your text in a single JSON string variable. Depending on your application, it may make sense to have an array of sections, containing an array of paragraphs, containing an array of normal/bold/list sections etc. However, in the general case I think good old-fashioned blocks are markup will ironically be cleaner and more scalable, due to the ease of passing them around, and the well-developed libraries for full-blown parsing if/when required.
There also seems to be a specification that might accomplish this Markdown Syntax for Object Notation (MSON)
Not sure if for you it's worth the trouble of implementing the spec, but it seems to be an option.