Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublime JSFormat: configure to not auto-format JSON

I have been looking around the web for a good Sublime (3) package to use to auto-format my source code in various languages, namely JavaScript. I came across this SOF post (Sublime Text 2: Auto fix indentation for javascript?) and thus decided to give JSFormat a try. So far, it seems to work pretty good...except for when it handles JSON objects in the JS code. For example, let's say that I have a function like this:

function foo() {
  return {name: 'Dave', score: 1000};
}

It returns a JavaScript object in JSON format, prettu much a hash object. I like writing such objects in one line because it is simple and easy to read, especially since it is just a small, ad-hoc object. But, if I were to format this with JSFormat, my function would now look like this:

function foo() {
  return {
    name: 'Dave',
    score: 1000
  };
}

Maybe this is just me, but I really don't like representing such simple JSON objects in multiple lines. Yes, normally JavaScript code that requires braces should have its contents on a separate lines from the braces, such as functions, if statements and loops. Maybe if the JSON object was a long object that contained functions inside of it, such as a jQuery Ajax class, then it makes sense to separate the attributes onto multiple lines.

Nonetheless, regardless whether my points about the braces makes sense, I know that JSFormat supports configuration and perhaps there is a way to configure JSFormat to not separate the attributes of a JSON object into multiple lines, if it is not desired. Any ideas?

like image 632
ecbrodie Avatar asked Oct 05 '13 16:10

ecbrodie


People also ask

Can sublime format JSON?

Syntax of Sublime Pretty JSONSublime Text is an easy option to Pretty format JSON, also we can prettyify JSON in our browser. Based on OS, installation steps might change a bit. If MAC OS, press CMD+SHIFT+P, then select 'Install Package'. Search for 'Pretty JSON' and install.

How do I save a JSON file in Sublime Text?

1 Answer. Show activity on this post. Using Command Palette Ctrl+Shift+P find "Pretty JSON: Minify (compress) JSON" (you can search for part of it like 'json minify') this will make selection or full buffer as single line JSON which later you can use in command lines (curl/httpie) or somewhere else...


1 Answers

Sorry for the bad news, but...

JSFormat uses js-beautify, which does not support single-line function definitions. Everything is broken into "beautified" lines, making it "more readable."

Look at the example given for js-beautify... the example itself is of a single-line function definition. There is no way to distinguish single-line function definitions you do want preserved from those you don't.

If you think about it, the ideal situation to use a beautifier is if you want to take minified code and make it readable... That's just one long line of code too.

I feel your pain, believe me.

like image 165
digitalextremist Avatar answered Nov 09 '22 06:11

digitalextremist