Skip to content

Commit

Permalink
[JavaScript] Formatter Fixes and Updates #31 #28
Browse files Browse the repository at this point in the history
Code templates are now formatter and indented properly.
Updated JSBeautifier options to match standard JS coding format.
Updated License on JSBeautify to be more clear.
Fixed issue where in empty blocks the closing parenthesis had an extra indent.
Fixed Enumerable Range, where sometimes new lines were fewer than 2.
  • Loading branch information
harsimranb committed Jul 13, 2014
1 parent 4539a6f commit c59fe06
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
//
// JSBeautifier.cs
//
// Author:
// rekna <https://jsbeautifylib.codeplex.com/>
//
// Copyright (c) 2014 Rekna
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
/*
JSBeautifier.cs
Author:
Einar Lielmanis and contributor <https://github.com/beautify-web/js-beautify>
ghost6991 <https://github.com/ghost6991/Jsbeautifier>
Harsimran Bath
The MIT License (MIT)
Copyright (c) 2007-2013 Einar Lielmanis and contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Notes: Originally written in JS by Einar Lielmanis and contributors. Converted to C# by ghost6991. I, Harsimran, made a few changes here and there.
*/

using System;
using System.Collections.Generic;
using System.Text;
Expand Down Expand Up @@ -259,10 +252,10 @@ public string Beautify (string s, JSBeautifierOptions opts = null)
s = s.Remove (0, 1);
}

var defaultIndent = Opts.DefaultIndent * Opts.IndentSize;
while (defaultIndent != 0) {
this.PreindentString += this.Opts.IndentChar;
defaultIndent--;
int tempDefSize = Opts.DefaultIndent;
while (tempDefSize != 0) {
this.PreindentString += this.IndentString;
tempDefSize--;
}

this.Input = s;
Expand Down Expand Up @@ -878,7 +871,8 @@ private void HandleEndBlock (string tokenText)
this.RestoreMode ();
if (this.Opts.BraceStyle == JSBraceStyle.Expand) {
if (this.LastText != "{")
this.AppendNewline ();
RemoveIndent ();
this.AppendNewline ();
} else {
if (this.LastType == "TK_START_BLOCK") {
if (this.JustAddedNewline)
Expand Down Expand Up @@ -918,8 +912,10 @@ private void HandleWord (string tokenText)
haveNewlines = 0;
if (!this.Opts.PreserveNewlines)
haveNewlines = 1;
foreach (int i in Enumerable.Range(0, 2 - haveNewlines))
this.AppendNewline (false);

if (2 - haveNewlines > 0)
foreach (int i in Enumerable.Range(0, 2 - haveNewlines))
this.AppendNewline (false);
}
if ((this.LastText == "get" || this.LastText == "set" || this.LastText == "new") || this.LastType == "TK_WORD")
this.Append (" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public override void CorrectIndenting (Projects.Policies.PolicyContainer policyP

public override void OnTheFlyFormat (Ide.Gui.Document doc, int startOffset, int endOffset)
{
string textToFormat = doc.Editor.GetTextBetween (startOffset, endOffset);
string textToFormat = doc.Editor.GetTextBetween (startOffset, endOffset).Trim ();
DocumentLine currentLine = doc.Editor.GetLineByOffset (startOffset);
int indentLevel = 0;
const int defaultIndentSize = 4;
const int defaultIndentSize = 1;

if(currentLine != null && currentLine.PreviousLine != null) {
string indentString = doc.Editor.GetIndentationString (currentLine.PreviousLine.Offset);
Expand All @@ -62,16 +62,11 @@ public override void OnTheFlyFormat (Ide.Gui.Document doc, int startOffset, int
}
}

// var options = new JSBeautifyOptions {
// IndentSize = defaultIndentSize,
// IndentChar = ' ',
// IndentLevel = indentLevel,
// PreserveNewlines = true
// };
var beautifier = new JSBeautifier (new JSBeautifierOptions{
BraceStyle = JSBraceStyle.Expand,
BraceStyle = JSBraceStyle.Collapse,
IndentSize = defaultIndentSize,
IndentWithTabs = true,
DefaultIndent = indentLevel
});
string formattedText = beautifier.Beautify (textToFormat);

Expand Down

0 comments on commit c59fe06

Please sign in to comment.