This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 218421 - Please see JSBeautify project for formatting options and example(Javascript 2 formatter)
Summary: Please see JSBeautify project for formatting options and example(Javascript 2...
Status: NEW
Alias: None
Product: javascript
Classification: Unclassified
Component: Formatting & Indentation (show other bugs)
Version: 7.3
Hardware: All All
: P2 normal with 1 vote (vote)
Assignee: Petr Pisl
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-09-16 15:43 UTC by drewhjava
Modified: 2017-08-20 21:36 UTC (History)
3 users (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description drewhjava 2012-09-16 15:43:25 UTC
https://github.com/einars/js-beautify

I could list out the problems with the upcoming Javascript2 formatter, but it's probably easier if you look at jsbeautify project.  Again, let me know if you need any help.
Comment 1 Petr Hejl 2012-09-16 16:05:29 UTC
(In reply to comment #0)
> https://github.com/einars/js-beautify
> 
> I could list out the problems with the upcoming Javascript2 formatter
Please do so. It is impossible to guess what you consider to be wrong.

> , but it's
> probably easier if you look at jsbeautify project.  Again, let me know if you
> need any help.
Comment 2 drewhjava 2012-09-16 16:15:33 UTC
I just wanted to say excellent work so far:


var Trip = "test"
			Airport = "test",
                        a = "blah";


Should be


var Trip = "test",
      Airport = "test",
      a  = "blah";


There should possibly be an option to preserve empty lines.

So this 

var viewModel = {

		trips: ko.observableArray([]),
		addTrip: function () {

                }
};

Could be 

var viewModel = {

		trips: ko.observableArray([]),

		addTrip: function () {
                
                }
};

I'll have to get the other ones while I'm working.  Thanks for your help.
Comment 3 drewhjava 2012-09-16 16:17:26 UTC
I'm sorry, here is the gist.

https://gist.github.com/3733037
Comment 4 Petr Hejl 2012-09-16 16:44:48 UTC
Thanks for the samples. I'm not sure whether you use bleeding edge dev build.
For me the first sample is formatted this way, so I'm not sure about the differnece:
var Trip = "test",
        Airport = "test",
        a  = "blah";
It is properly idented with configured continuation (8 spaces) in your case it is 6. Is this what you report?

I'm bit unsure about the second sample and empty line preservation. In dev the lines are preserved for me and the result is:


var viewModel = {

    trips: ko.observableArray([]),
    addTrip: function() {

    }
};
Comment 5 drewhjava 2012-09-16 16:49:28 UTC
Hey Petr thanks for your time looking into this.  In the first example, setting my continuation to 6 fixed the issue.  Is this the correct way to fix it?

In the second example, a preserve empty lines would turn this.

var viewModel = {

    trips: ko.observableArray([]),
    addTrip: function() {

    }
};


into


var viewModel = {

    trips: ko.observableArray([]),  //if i put a space after this

    addTrip: function() {

    }
};

Right now if I have a space there it removes it.

var viewModel = {

    trips: ko.observableArray([]),  //if i put a space after this

    addTrip: function() {

    },//removes this space too
    deleteTrip: function() {

    }
};

It may just be an object issue.  I'll need to test in other cases.
Comment 6 drewhjava 2012-09-16 16:53:09 UTC
BTW, I'm using nightly build(9/15).  I usually do a an hg pull, hg update, and build the ide too.
Comment 7 Petr Hejl 2012-09-16 17:05:32 UTC
(In reply to comment #5)
> Hey Petr thanks for your time looking into this.  In the first example, setting
> my continuation to 6 fixed the issue.  Is this the correct way to fix it?
Well I'm not really sure what is the problem. The continuation indentation is also used at other places, so if you want a different value then this is the correct fix. However if you want a var statement to be indented in special way we would have to introduce a special option.

> 
> In the second example, a preserve empty lines would turn this.
> 
> var viewModel = {
> 
>     trips: ko.observableArray([]),
>     addTrip: function() {
> 
>     }
> };
> 
> 
> into
> 
> 
> var viewModel = {
> 
>     trips: ko.observableArray([]),  //if i put a space after this
> 
>     addTrip: function() {
> 
>     }
> };
> 
> Right now if I have a space there it removes it.
> 
> var viewModel = {
> 
>     trips: ko.observableArray([]),  //if i put a space after this
> 
>     addTrip: function() {
> 
>     },//removes this space too
>     deleteTrip: function() {
> 
>     }
> };
> 
> It may just be an object issue.  I'll need to test in other cases.

Sorry, but I'm not sure I understand try to provide just sample and the expected result. Perhaps you mean "preserve indentation of empty lines" not "preserve empty lines", don't you?
Comment 8 drewhjava 2012-09-16 18:08:35 UTC
The formatting shouldn't turn this.

var view = {

	c : [],
		
	a : function () {

	},
			
	v : function () {

	}

}

into this.

var view = {

	c : [],		
	a : function () {

	},			
	v : function () {

	}

}

It should preserve the empty lines i put there. 


(In reply to comment #7)
> (In reply to comment #5)
> > Hey Petr thanks for your time looking into this.  In the first example, setting
> > my continuation to 6 fixed the issue.  Is this the correct way to fix it?
> Well I'm not really sure what is the problem. The continuation indentation is
> also used at other places, so if you want a different value then this is the
> correct fix. However if you want a var statement to be indented in special way
> we would have to introduce a special option.
> 
> > 
> > In the second example, a preserve empty lines would turn this.
> > 
> > var viewModel = {
> > 
> >     trips: ko.observableArray([]),
> >     addTrip: function() {
> > 
> >     }
> > };
> > 
> > 
> > into
> > 
> > 
> > var viewModel = {
> > 
> >     trips: ko.observableArray([]),  //if i put a space after this
> > 
> >     addTrip: function() {
> > 
> >     }
> > };
> > 
> > Right now if I have a space there it removes it.
> > 
> > var viewModel = {
> > 
> >     trips: ko.observableArray([]),  //if i put a space after this
> > 
> >     addTrip: function() {
> > 
> >     },//removes this space too
> >     deleteTrip: function() {
> > 
> >     }
> > };
> > 
> > It may just be an object issue.  I'll need to test in other cases.
> 
> Sorry, but I'm not sure I understand try to provide just sample and the
> expected result. Perhaps you mean "preserve indentation of empty lines" not
> "preserve empty lines", don't you?
Comment 9 Petr Hejl 2012-09-16 18:18:55 UTC
(In reply to comment #8)
> The formatting shouldn't turn this.
> 
> var view = {
> 
>     c : [],
> 
>     a : function () {
> 
>     },
> 
>     v : function () {
> 
>     }
> 
> }
> 
> into this.
> 
> var view = {
> 
>     c : [],        
>     a : function () {
> 
>     },            
>     v : function () {
> 
>     }
> 
> }
> 
> It should preserve the empty lines i put there. 
Got it. Would that be solved by (not yet implemented) specification of number of blank lines similar to Java/PHP? So you could configure number of empty lines after/before object property? Or do you insist on preserving possibly different number of lines (does not make much sense, I think it should be unified - that a reason to use formatter)?
Comment 10 drewhjava 2012-09-16 18:30:06 UTC
I agree that it should be done similar to Java/Php.  Yea, it doesn't make sense on preserving an arbitrary amount of lines.  As far as specification of blank lines, would this include after functions and other scenarios?

A this point the formatter preserves the number of spaces in this situation.

var defineMappedCustomers = function (viewModel) {

};

Just as a recommendation, it should be one space by default(for object properties).  This is what most JS developers use(not everyone but most)


> Got it. Would that be solved by (not yet implemented) specification of number
> of blank lines similar to Java/PHP? So you could configure number of empty
> lines after/before object property? Or do you insist on preserving possibly
> different number of lines (does not make much sense, I think it should be
> unified - that a reason to use formatter)?
Comment 11 CaptainStarbuck 2017-08-20 21:35:54 UTC
Working extensively with NetBeans v8.2 and Nashorn = JavaScript, I've been finding that the default Format destroys the formatting in comments. Looking into the Tools>Options for formatting, we see the current release doesn't support the same options as Java.

So in response to the initial discussion here, where @drewhjava says "I could list out the problems with the upcoming Javascript2 formatter" and @ppisl responds "Please do so", my simple answer would be to look at the features available for Java which are not available for JavaScript. That's a large list of missing options, and targets for enhacements.

Drew published what would seem to be a great plugin which would replace the default and provide JS-Beautify options as he suggested here:
https://github.com/drewhamlett/netbeans-jsbeautify
That was created for v7.2 and I will test to see if it works with 8.2. It needs to be refitted to use the latest CDN version of the JS-Beautify code.

With JS-Beautify, "meta comments" are required around code, comments in my case, to preserve code segments from formatting. If the default code formatting included the option to uncheck "Enable Comments Formatting" and "Format Block Comments", we/I wouldn't need to add utility-specific metadata like that into code.

But it would be nice to see that functionality built-in for JavaScript to be more of a first-class citizen in the NetBeans ecosystem, or again for JavaScript support to be enhanced with more of the Java formatting options.

In the project I'm working on, markup is embedded in the block comments, and ant build tasks pull all of these comments to generate .md docs. Yes, JSDoc does a similar form of doc build, but that's not a replacement for what is being done.

Thanks.