Skip to content

Commit 0651ccf

Browse files
committed
Moves the tidier code and file mode out of editor file
1 parent 4d9b05f commit 0651ccf

File tree

3 files changed

+63
-61
lines changed

3 files changed

+63
-61
lines changed

client/modules/IDE/components/Editor/index.jsx

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import PropTypes from 'prop-types';
44
import React from 'react';
55
import CodeMirror from 'codemirror';
66
import emmet from '@emmetio/codemirror-plugin';
7-
import prettier from 'prettier/standalone';
8-
import babelParser from 'prettier/parser-babel';
9-
import htmlParser from 'prettier/parser-html';
10-
import cssParser from 'prettier/parser-postcss';
117
import { withTranslation } from 'react-i18next';
128
import StackTrace from 'stacktrace-js';
139
import 'codemirror/mode/css/css';
@@ -67,6 +63,8 @@ import { FolderIcon } from '../../../../common/icons';
6763
import IconButton from '../../../../common/IconButton';
6864

6965
import { showHint, hideHinter } from './hinter';
66+
import getFileMode from './utils';
67+
import tidyCode from './tidier';
7068

7169
emmet(CodeMirror);
7270

@@ -79,7 +77,6 @@ class Editor extends React.Component {
7977
currentLine: 1
8078
};
8179
this._cm = null;
82-
this.tidyCode = this.tidyCode.bind(this);
8380

8481
this.updateLintingMessageAccessibility = debounce((annotations) => {
8582
this.props.clearLintMessage();
@@ -157,7 +154,7 @@ class Editor extends React.Component {
157154
[`${metaKey}-Enter`]: () => null,
158155
[`Shift-${metaKey}-Enter`]: () => null,
159156
[`${metaKey}-F`]: 'findPersistent',
160-
[`Shift-${metaKey}-F`]: this.tidyCode,
157+
[`Shift-${metaKey}-F`]: () => tidyCode(this._cm),
161158
[`${metaKey}-G`]: 'findPersistentNext',
162159
[`Shift-${metaKey}-G`]: 'findPersistentPrev',
163160
[replaceCommand]: 'replace',
@@ -206,7 +203,7 @@ class Editor extends React.Component {
206203
] = `${this.props.fontSize}px`;
207204

208205
this.props.provideController({
209-
tidyCode: this.tidyCode,
206+
tidyCode: () => tidyCode(this._cm),
210207
showFind: this.showFind,
211208
showReplace: this.showReplace,
212209
getContent: this.getContent
@@ -226,7 +223,7 @@ class Editor extends React.Component {
226223

227224
componentDidUpdate(prevProps) {
228225
if (this.props.file.id !== prevProps.file.id) {
229-
const fileMode = this.getFileMode(this.props.file.name);
226+
const fileMode = getFileMode(this.props.file.name);
230227
if (fileMode === 'javascript') {
231228
// Define the new Emmet configuration based on the file mode
232229
const emmetConfig = {
@@ -315,7 +312,7 @@ class Editor extends React.Component {
315312
}
316313

317314
this.props.provideController({
318-
tidyCode: this.tidyCode,
315+
tidyCode: () => tidyCode(this._cm),
319316
showFind: this.showFind,
320317
showReplace: this.showReplace,
321318
getContent: this.getContent
@@ -329,26 +326,6 @@ class Editor extends React.Component {
329326
this.props.provideController(null);
330327
}
331328

332-
getFileMode(fileName) {
333-
let mode;
334-
if (fileName.match(/.+\.js$/i)) {
335-
mode = 'javascript';
336-
} else if (fileName.match(/.+\.css$/i)) {
337-
mode = 'css';
338-
} else if (fileName.match(/.+\.(html|xml)$/i)) {
339-
mode = 'htmlmixed';
340-
} else if (fileName.match(/.+\.json$/i)) {
341-
mode = 'application/json';
342-
} else if (fileName.match(/.+\.(frag|glsl)$/i)) {
343-
mode = 'x-shader/x-fragment';
344-
} else if (fileName.match(/.+\.(vert|stl|mtl)$/i)) {
345-
mode = 'x-shader/x-vertex';
346-
} else {
347-
mode = 'text/plain';
348-
}
349-
return mode;
350-
}
351-
352329
getContent() {
353330
const content = this._cm.getValue();
354331
const updatedFile = Object.assign({}, this.props.file, { content });
@@ -368,44 +345,13 @@ class Editor extends React.Component {
368345
this._cm.execCommand('replace');
369346
}
370347

371-
prettierFormatWithCursor(parser, plugins) {
372-
try {
373-
const { formatted, cursorOffset } = prettier.formatWithCursor(
374-
this._cm.doc.getValue(),
375-
{
376-
cursorOffset: this._cm.doc.indexFromPos(this._cm.doc.getCursor()),
377-
parser,
378-
plugins
379-
}
380-
);
381-
const { left, top } = this._cm.getScrollInfo();
382-
this._cm.doc.setValue(formatted);
383-
this._cm.focus();
384-
this._cm.doc.setCursor(this._cm.doc.posFromIndex(cursorOffset));
385-
this._cm.scrollTo(left, top);
386-
} catch (error) {
387-
console.error(error);
388-
}
389-
}
390-
391-
tidyCode() {
392-
const mode = this._cm.getOption('mode');
393-
if (mode === 'javascript') {
394-
this.prettierFormatWithCursor('babel', [babelParser]);
395-
} else if (mode === 'css') {
396-
this.prettierFormatWithCursor('css', [cssParser]);
397-
} else if (mode === 'htmlmixed') {
398-
this.prettierFormatWithCursor('html', [htmlParser]);
399-
}
400-
}
401-
402348
initializeDocuments(files) {
403349
this._docs = {};
404350
files.forEach((file) => {
405351
if (file.name !== 'root') {
406352
this._docs[file.id] = CodeMirror.Doc(
407353
file.content,
408-
this.getFileMode(file.name)
354+
getFileMode(file.name)
409355
); // eslint-disable-line
410356
}
411357
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import prettier from 'prettier/standalone';
2+
import babelParser from 'prettier/parser-babel';
3+
import htmlParser from 'prettier/parser-html';
4+
import cssParser from 'prettier/parser-postcss';
5+
6+
function prettierFormatWithCursor(parser, plugins, cmInstance) {
7+
try {
8+
const { formatted, cursorOffset } = prettier.formatWithCursor(
9+
cmInstance.doc.getValue(),
10+
{
11+
cursorOffset: cmInstance.doc.indexFromPos(cmInstance.doc.getCursor()),
12+
parser,
13+
plugins
14+
}
15+
);
16+
const { left, top } = cmInstance.getScrollInfo();
17+
cmInstance.doc.setValue(formatted);
18+
cmInstance.focus();
19+
cmInstance.doc.setCursor(cmInstance.doc.posFromIndex(cursorOffset));
20+
cmInstance.scrollTo(left, top);
21+
} catch (error) {
22+
console.error(error);
23+
}
24+
}
25+
26+
/** Runs prettier on the codemirror instance, depending on the mode. */
27+
export default function tidyCode(cmInstance) {
28+
const mode = cmInstance.getOption('mode');
29+
if (mode === 'javascript') {
30+
prettierFormatWithCursor('babel', [babelParser], cmInstance);
31+
} else if (mode === 'css') {
32+
prettierFormatWithCursor('css', [cssParser], cmInstance);
33+
} else if (mode === 'htmlmixed') {
34+
prettierFormatWithCursor('html', [htmlParser], cmInstance);
35+
}
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/** Detects what mode the file is based on the name. */
2+
export default function getFileMode(fileName) {
3+
let mode;
4+
if (fileName.match(/.+\.js$/i)) {
5+
mode = 'javascript';
6+
} else if (fileName.match(/.+\.css$/i)) {
7+
mode = 'css';
8+
} else if (fileName.match(/.+\.(html|xml)$/i)) {
9+
mode = 'htmlmixed';
10+
} else if (fileName.match(/.+\.json$/i)) {
11+
mode = 'application/json';
12+
} else if (fileName.match(/.+\.(frag|glsl)$/i)) {
13+
mode = 'x-shader/x-fragment';
14+
} else if (fileName.match(/.+\.(vert|stl|mtl)$/i)) {
15+
mode = 'x-shader/x-vertex';
16+
} else {
17+
mode = 'text/plain';
18+
}
19+
return mode;
20+
}

0 commit comments

Comments
 (0)