Code Monkey home page Code Monkey logo

vscodebeautify's Introduction

js-beautify for VS Code

Build Status Build status Licence VS Code Marketplace Rating Downloads Installs Donate

Beautify javascript, JSON, CSS, Sass, and HTML in Visual Studio Code.

VS Code uses js-beautify internally, but it lacks the ability to modify the style you wish to use. This extension enables running js-beautify in VS Code, AND honouring any .jsbeautifyrc file in the open file's path tree to load your code styling. Run with F1 Beautify (to beautify a selection) or F1 Beautify file.

For help on the settings in the .jsbeautifyrc see Settings.md

How we determine what settings to use

  1. When not using a multi-root workspace:

    1. If there is a valid .jsbeautifyrc in the file's path tree, up to project root, these will be the only settings used.
    2. If an option is a file path or object of configuration specified in the user or workspace settings like this: "beautify.config" : "string|Object.<string,string|number|boolean>", these will be the only settings used. The file path is interpreted relative to the workspace's root folder.
    3. If there is a valid .jsbeautifyrc in the file's path tree, above project root, these will be the only settings used.
    4. If there is a valid .jsbeautifyrc in your home directory, these will be the only settings used.
  2. When using a multi-root workspace: Same as above, but the search ends at the nearest parent workspace root to the open file.

otherwise...

  1. Settings are translated from your VS Code workspace/user settings.
  2. Any open editor settings (indent spaces/tabs) for the specific file are merged in.
  3. Editorconfig settings are searched for (See http://editorconfig.org/) and are merged in.

VS Code | .jsbeautifyrc settings map

.jsbeautifyrc setting VS Code setting
eol files.eol
tab_size editor.tabSize
indent_with_tabs (inverted) editor.insertSpaces
wrap_line_length html.format.wrapLineLength
wrap_attributes html.format.wrapAttributes
unformatted html.format.unformatted
indent_inner_html html.format.indentInnerHtml
preserve_newlines html.format.preserveNewLines
max_preserve_newlines html.format.maxPreserveNewLines
indent_handlebars html.format.indentHandlebars
end_with_newline html.format.endWithNewline (html)
end_with_newline file.insertFinalNewLine (css, js)
extra_liners html.format.extraLiners
space_after_anon_function javascript.format
.insertSpaceAfterFunctionKeywordForAnonymousFunctions
space_in_paren javascript.format
.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis

Note that the html.format settings will ONLY be used when the document is html. javascript.format settings are included always.

Also runs html and css beautify from the same package, as determined by the file extension. The schema indicates which beautifier each of the settings pertains to.

The .jsbeautifyrc config parser accepts sub elements of html, js and css so that different settings can be used for each of the beautifiers (like sublime allows). Note that this will cause the config file to be incorrectly structured for running js-beautify from the command line.

Settings are inherited from the base of the file. Thus:

{
  "indent_size": 4,
  "indent_char": " ",
  "css": {
    "indent_size": 2
  }
}

Will result in the indent_size being set to 4 for JavaScript and HTML, but set to 2 for CSS. All will get the same indent_char setting.

If the file is unsaved, or the type is undetermined, you'll be prompted for which beautifier to use.

You can control which file types, extensions, or specific file names should be beautified with the beautify.language setting.

{
  "beautify.language": {
    "js": {
      "type": ["javascript", "json"],
      "filename": [".jshintrc", ".jsbeautifyrc"]
      // "ext": ["js", "json"]
      // ^^ to set extensions to be beautified using the javascript beautifier
    },
    "css": ["css", "scss"],
    "html": ["htm", "html"]
    // ^^ providing just an array sets the VS Code file type
  }
}

Beautify on save will be enabled when "editor.formatOnSave" is true.

Beautification on particular files using the built in Format Document (which includes formatting on save) can be skipped with the beautify.ignore option. Using the Beautify file and Beautify selection will still work. For files opened from within the workspace directory, the glob patterns will match from the workspace folder root. For files opened from elsewhere, or when no workspace is open, the patterns will match from the system root.

Examples:

/* ignore all files named 'test.js' not in the root folder,
   all files directly in any 'spec' directory, and
   all files in any 'test' directory at any depth
*/
"beautify.ignore": ["*/test.js", "**/spec/*", "**/test/**/*"]

/* ignore all files ending in '_test.js' anywhere */
"beautify.ignore": "**/*_test.js"

Note that the glob patterns are not used to test against the containing folder. You must match the filename as well.

Embedded version of js-beautify is v1.8.4

Keyboard Shortcut

Use the following to embed a beautify shortcut in keybindings.json. Replace with your preferred key bindings.

{
  "key": "cmd+b",
  "command": "HookyQR.beautify",
  "when": "editorFocus"
}

Contributing

For information on contributing see Contributing.md

vscodebeautify's People

Contributors

admosity avatar hookyqr avatar igornovozhilov avatar keale2 avatar sensehack avatar stephen-ullom avatar tallyb avatar wabisabit avatar wormeyman avatar zilioti avatar zsohnvlocity avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vscodebeautify's Issues

Typescript spaces with generic type

I don't know if this can be solved within the scope of this project, since it seems to be typescript specific. But whenever i place a use something like: let myArray :Array<string> = [] and i beatify it it changes to 'let myArray :Array < string > = []'

JS - commented out code is indented one extra time

I've run into a small issue with this extension regarding commented-out code. Say I have a block of code like this:

const a
const b
// const c
const d
const e

Upon running Beautify, it is formatted to look like this:

const a
const b
  // const c
const d
const e

I don't want the commented code to be indented, I want it to remain in line with the code surrounding it. Is there any way to prevent this behavior?

Add Beautify on Save

I know VS Code does some beautification as text it entered into the editor but a beautify on save feature would be great for files that are opened but not changed. Can this be added to this extension or is it more suited as a feature in the main VSCode app? If it is something easy to add then it would be great to have it in your extension.

Use file spacing preferences by default

I prefer tabs with everything I edit, but Beautify converts these to spaces when I beautify my file. Most of the time I am editing loose files around my filesystem, so I can't create a .jsbeautifyrc.

Is there a way/can there be a feature to use the spacing preferences set up within VS Code?

image

Either this, or a way to have default preferences set up in settings.json?

Unable to beautify the js code in html files

Versions:
beautify - 0.4.1
vsCode - 1.5.2

Settings:

"beautify.onSave": true,
    "beautify.JSfiles": [
        "html",
        "js",
        "json",
        "jsbeautifyrc",
        "jshintrc"
    ]

Before Formatting:
image

After Formatting:
image

As you can see, the alignment/formatting of js code inside the html file is not working. Where as if its in a .js file, its working like charm.

beautify.onSave saving loop

When in set
"beautify.onSave": true

on save vscode starts to constantly save and change the file repedeatly times. Only way to stop it, is to switch to another file.
This is really annoying since the file is constantly marked as saved/nonsaved and also clears the undo history.

Support for custom onSave values

I tried adding the .vue extension to beautify.onSave but this warns me that this isnt an acceptable value and it won't auto beautify vue files on save. Manually running beautify does work.

No handler found for the command: 'HookyQR.beautify'

I am using beautify 0.0.3 with VSCode 0.10.3 and getting
"No handler found for the command: 'HookyQR.beautify'" warning whenever trying to run Beautify. Extention isn't working as well through it works if I used js-beautify through cmdline.

function indent

15
the chekEmail function and checkPhone function in the same level, but have indent.
how can I do with it?

Unreadable content after beautify when using Vash (expressjs view engine)

Hi, thanks for your extension. it's really useful.

But there is a problem, I'm using Vash and I defined .vash extension to be an html file (because vscode doesn't support vash yet). also I using your extension as well.

In vash file when I try to beautify, the js blocks combines together. (I know it's not a real bug, but not really good for me), btw when I try to beautify the same content using jsbeautifier.org, the result is too better and more readable (of course the result is not perfect).

Is there any way to prevent combining vash blocks on beautify?

Example of vash (that I wish after beautify still I see this structure)

@html.extend('layout',function(model){
    @html.block('body',function(model){
        <h1>@model.title</h1>
        <p>@model.content</p>
    })
})

Result (using VSCodeBeautify)

@html.extend('layout',function(model){ @html.block('body',function(model){
<h1>@model.title</h1>
<p>@model.content</p>
}) })

Result (using jsbeautifier.org)

@html.extend('layout', function(model) {
    @html.block('body', function(model) { <
        h1 > @model.title < /h1> <
            p > @model.content < /p>
    })
})

For more complex structure the result will be really unreadable.

comments tag into a new line

Hi, when I use beautify in visual studio code on mac the comment tag goes into the line, I would like to have inline as the current code. Is possible to change it? Any ideas?

current:

</li><!-- /Dropdown -->

after beautify

</li>
<!-- /Dropdown -->

Beautify removes new lines between LESS rules

Hey there,

although I defined "newline_between_rules": true in my .jsbeautifyrc, the Beautify extension removes those new lines and thus would transform this

.state:hover {
    .btn-variant();

    &.state-lead {
        .btn-variant(warning);
    }

    &.state-proposed {
        .btn-variant(info);
    }
}

into this:

.state:hover {
    .btn-variant();
    &.state-lead {
        .btn-variant(warning);
    }
    &.state-proposed {
        .btn-variant(info);
    }
}

… which is not what I wanted, of course. Did I configure something wrong or is this a bug?

Here's my complete .jsbeautifyrc:

{
    "end_with_newline": true,
    "indent_level": 0,
    "indent_with_tabs": true,
    "max_preserve_newlines": 2,
    "preserve_newlines": true,
    "wrap_line_length": 0,
    "js": {
        "brace_style": "collapse",
        "break_chained_methods": false,
        "indent_with_tabs": true,
        "jslint_happy": false,
        "jslint_happy_align_switch_case": false,
        "space_after_anon_function": false,
        "space_before_conditional": true,
        "space_in_empty_paren": false,
        "space_in_paren": false
    },
    "html": {
        "brace_style": "collapse",
        "wrap_attributes_indent_size": 4,
        "unformatted": ["a", "sub", "sup", "b", "i", "u"]
    },
    "css": {
        "convert_quotes": "single",
        "newline_between_rules": true,
        "selector_separator_newline": true,
        "space_around_selector_separator": true
    }
}

Mulitline fomating in import statement

Hello,
I wirte example before and after code formating:
before:
import { Mongo } from 'meteor/mongo;
after:

import { 
  Mongo
} from 'meteor/mongo;

This problem occurred also available for atom verssion.

Best Regards,
Paul

License

Can you add your license file to the project root directory

Extra spaces are not applied same way as jsbeautifier.org

From previous editors I always got a space before and after > in css.

This works on http://jsbeautifier.org/ :

.main.row > .column.segment {
  width: 100 % ;
  flex: 1;
  overflow: auto;
  margin - bottom: 0!important;
}

but not with beautifier in vscode:

.main.row>.column .segment {
  width: 100%;
  flex: 1;
  overflow: auto;
  margin-bottom: 0 !important;
}

I tried with only the "indent_size": 2, and also all default settings from js-beautify git hub instruction.

How does this work with SCSS files? Is .jsbeautifyrc ignored?

I'm trying to figure out why this extension seems to ignore my .jsbeautifyrc file settings for scss files. Right now, I have this:

{
    "end_with_newline": true,
    "indent_level": 0,
    "indent_with_tabs": true,
    "max_preserve_newlines": 2,
    "preserve_newlines": true,
    "wrap_line_length": 0,
    "newline_between_rules": true,
    "convert_quotes": "single",
    "js": {
        "brace_style": "collapse",
        "break_chained_methods": false,
        "indent_with_tabs": true,
        "jslint_happy": false,
        "jslint_happy_align_switch_case": false,
        "space_after_anon_function": false,
        "space_before_conditional": true,
        "space_in_empty_paren": false,
        "space_in_paren": false
    },
    "html": {
        "brace_style": "collapse",
        "wrap_attributes_indent_size": 4,
        "unformatted": ["a", "sub", "sup", "b", "i", "u"]
    },
    "css": {
        "convert_quotes": "single",
        "newline_between_rules": true,
        "selector_separator_newline": true,
        "space_around_selector_separator": true,
        "indent_size": 16
    }
}

So I have newline_between_rules set to true by default. But when I save my scss file, it removes all the new lines. Also it doesn't convert my quotes to single quotes. I have the .jsbeautifyrc file in the same directory as my .scss file. What am I doing wrong?

Also, this extension seems to be Beautifying my .scss files on save even if I remove them from the array:

"beautify.CSSfiles": [
    "css"
],

Also I tried this:

"beautify.onSaveIgnore": [
        //don't beautify any file in any 'minified' directory, at any depth:
        "**/minified/**",
        //don't minify any file that contains '.min.', '_min.', '-min.' in the filename (This is the default ignore setting):
        "**/*+(.|_|-)min.*",
        "scss"
    ]

And my SCSS files are STILL being Beautified on save.

Can anyone give me some insight here? Am I hitting some bugs or just completely misunderstanding how this thing works?

Duplication

When formatting code via context menu > 'Format Code', the last line of the file is being duplicated.

This was occurring in both html and js files.

Had to uninstall the extension so can't provide any examples

[Feature request] Add support for C# code.

Basically, it's the same as JS, with some exceptions:

private const int SPIF_SENDWININICHANGE = 2; is beautifyied to

private
const int SPIF_SENDWININICHANGE = 2;

but should remain the same.

Same thing for @"Control Panel\Desktop" beautifyied as @ "Control Panel\Desktop"

There might be more, but I didn't found them.

code is broken and uglified onSave

Why does my code get uglified and broken on auto save?

This:

return ( 
    <div>
      <SearchBar />
    </div>
);

becomes this:

return ( <
    div >
    <
    SearchBar / >
    <
    /div>
  );
{
    "beautify.onSave": true
}

Am I missing something in the settings? Or does it not work with jsx?

Manually formatting with Shift + Opt + F works fine though...

Can't identify xml file even if defined in .jsbeautifyrc file

If I have this section in my .jsbeautifyrc file:

    "html": {
        "allowed_file_extensions": ["htm", "html", "xhtml", "shtml", "xml", "svg"]
         }

I still get prompted every time I try to format an xml file:

Couldn't determine type to beautify, please choose.

.jsbeautifyrc file not being used

I am trying to make VSCodeBeautify take my .jsbeautifyrc file into account inside a Node.JS project.
I cannot seem to find the right place.
I've tried putting it at the root of the folder opened in VSCode.
I've tried putting it inside the .vscode folder.
I've tried putting it at the same level as the package.json.
I've tried restarting VSCode couple times.

None of theses locations seems to do anything.

Here's my .jsbeautifyrc file

// Documentation: https://github.com/einars/js-beautify/
{
    "indent_size": 2,
    "indent_char": " ",
    "eol": "\n",
    "indent_level": 0,
    "indent_with_tabs": false,
    "preserve_newlines": true,
    "max_preserve_newlines": 10,
    "jslint_happy": false,
    "space_after_anon_function": false,
    "brace_style": "collapse",
    "keep_array_indentation": false,
    "keep_function_indentation": false,
    "space_before_conditional": true,
    "break_chained_methods": false,
    "eval_code": false,
    "unescape_strings": false,
    "wrap_line_length": 0,
    "wrap_attributes": "auto",
    "wrap_attributes_indent_size": 4,
    "end_with_newline": false
}

.vscode folder

It would be good if I could place the .jsbeautifyrc-file in the .vscode-folder together with other extension files. The root would be cleaner.

However I'm not sure if .vscode is a standard folder but "Spelling and Grammar Checking" by Sean McBreen is using that path.

Global .jsbeautifyrc file?

I've had a look through the documentation and the issues here but I can't seem to find how to add a single global .jsbeautifyrc file for all projects. Is this possible? In particular I prefer no space between an anonymous function and its parentheses. Thank you

No support for SASS/SCSS

Hi,

Seems like your extension doesn't support SASS/SCSS files, right, or I missed something?

I'm using latest version of VSCode & VSCodeBeautify

Thanks !

Beautify deletes necessary spaces in escaped LESS that brings to compilation error

Dear developer,

Could you pleae try to beautify the following less file:

@sbtChatMessageDialogFooterHeight: 80px;
.sbtChatMessagePopupList {
    overflow: auto;
    height: ~"calc( 100% - @{sbtChatMessageDialogFooterHeight} )";
}

Once saved it removes very important space here:
height:~"calc(

that leads to LESS compiler error.

Could you please solve that?

I had to deactive your extension temporarily.

Looking forward to having it back

Thanks!

Petr.

Keyboard shortcut.

Why I haven't any keyboard shortcut (Alt+Shift+F doesn't work) in my VSCode?

Using js formatting for typescript

I was testing js beautify on typescript files .
the results were pretty good.

in the pr #37 i solved the issue of manually selecting js for formatting every time.

without this ,it would always popup a selector menu to select from js , html & css.

Beautify on save isn't triggered with additional configured extensions

let refType = doc.languageId;

if (refType === 'javascript') refType = 'js';
if (['json', 'js', 'html', 'css'].indexOf(refType) === -1) return;

It looks like the above lines of code need to be altered to used the "beautify.HTMLfiles", "beautify.CSSfiles", "beautify.JSfiles" user settings. I have the following in my settings file but the saving the handlebars file does not run the beautifier:

"beautify.HTMLfiles": [
    "htm",
    "html",
    "hbs"
]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.