Skip to content

Commit 218e046

Browse files
added prettier to tidy code
1 parent f3edbef commit 218e046

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

client/modules/IDE/components/Editor.jsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import PropTypes from 'prop-types';
22
import React from 'react';
33
import CodeMirror from 'codemirror';
44
import emmet from '@emmetio/codemirror-plugin';
5-
import beautifyJS from 'js-beautify';
5+
import prettier from 'prettier';
6+
import parserBabel from 'prettier/parser-babel';
7+
import htmlParser from 'prettier/parser-html';
8+
import cssParser from 'prettier/parser-postcss';
69
import { withTranslation } from 'react-i18next';
710
import 'codemirror/mode/css/css';
811
import 'codemirror/addon/selection/active-line';
@@ -59,14 +62,10 @@ import * as ConsoleActions from '../actions/console';
5962

6063
emmet(CodeMirror);
6164

62-
const beautifyCSS = beautifyJS.css;
63-
const beautifyHTML = beautifyJS.html;
64-
6565
window.JSHINT = JSHINT;
6666
window.CSSLint = CSSLint;
6767
window.HTMLHint = HTMLHint;
6868

69-
const IS_TAB_INDENT = false;
7069
const INDENTATION_AMOUNT = 2;
7170

7271
class Editor extends React.Component {
@@ -337,23 +336,28 @@ class Editor extends React.Component {
337336
}
338337

339338
tidyCode() {
340-
const beautifyOptions = {
341-
indent_size: INDENTATION_AMOUNT,
342-
indent_with_tabs: IS_TAB_INDENT
343-
};
344339
const mode = this._cm.getOption('mode');
345340
const currentPosition = this._cm.doc.getCursor();
346341
if (mode === 'javascript') {
347342
this._cm.doc.setValue(
348-
beautifyJS(this._cm.doc.getValue(), beautifyOptions)
343+
prettier.format(this._cm.doc.getValue(), {
344+
parser: 'babel',
345+
plugins: [parserBabel]
346+
})
349347
);
350348
} else if (mode === 'css') {
351349
this._cm.doc.setValue(
352-
beautifyCSS(this._cm.doc.getValue(), beautifyOptions)
350+
prettier.format(this._cm.doc.getValue(), {
351+
parser: 'css',
352+
plugins: [cssParser]
353+
})
353354
);
354355
} else if (mode === 'htmlmixed') {
355356
this._cm.doc.setValue(
356-
beautifyHTML(this._cm.doc.getValue(), beautifyOptions)
357+
prettier.format(this._cm.doc.getValue(), {
358+
parser: 'html',
359+
plugins: [htmlParser]
360+
})
357361
);
358362
}
359363
this._cm.focus();

0 commit comments

Comments
 (0)