@@ -4,10 +4,6 @@ import PropTypes from 'prop-types';
4
4
import React from 'react' ;
5
5
import CodeMirror from 'codemirror' ;
6
6
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' ;
11
7
import { withTranslation } from 'react-i18next' ;
12
8
import StackTrace from 'stacktrace-js' ;
13
9
import 'codemirror/mode/css/css' ;
@@ -67,6 +63,8 @@ import { FolderIcon } from '../../../../common/icons';
67
63
import IconButton from '../../../../common/IconButton' ;
68
64
69
65
import { showHint , hideHinter } from './hinter' ;
66
+ import getFileMode from './utils' ;
67
+ import tidyCode from './tidier' ;
70
68
71
69
emmet ( CodeMirror ) ;
72
70
@@ -79,7 +77,6 @@ class Editor extends React.Component {
79
77
currentLine : 1
80
78
} ;
81
79
this . _cm = null ;
82
- this . tidyCode = this . tidyCode . bind ( this ) ;
83
80
84
81
this . updateLintingMessageAccessibility = debounce ( ( annotations ) => {
85
82
this . props . clearLintMessage ( ) ;
@@ -157,7 +154,7 @@ class Editor extends React.Component {
157
154
[ `${ metaKey } -Enter` ] : ( ) => null ,
158
155
[ `Shift-${ metaKey } -Enter` ] : ( ) => null ,
159
156
[ `${ metaKey } -F` ] : 'findPersistent' ,
160
- [ `Shift-${ metaKey } -F` ] : this . tidyCode ,
157
+ [ `Shift-${ metaKey } -F` ] : ( ) => tidyCode ( this . _cm ) ,
161
158
[ `${ metaKey } -G` ] : 'findPersistentNext' ,
162
159
[ `Shift-${ metaKey } -G` ] : 'findPersistentPrev' ,
163
160
[ replaceCommand ] : 'replace' ,
@@ -206,7 +203,7 @@ class Editor extends React.Component {
206
203
] = `${ this . props . fontSize } px` ;
207
204
208
205
this . props . provideController ( {
209
- tidyCode : this . tidyCode ,
206
+ tidyCode : ( ) => tidyCode ( this . _cm ) ,
210
207
showFind : this . showFind ,
211
208
showReplace : this . showReplace ,
212
209
getContent : this . getContent
@@ -226,7 +223,7 @@ class Editor extends React.Component {
226
223
227
224
componentDidUpdate ( prevProps ) {
228
225
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 ) ;
230
227
if ( fileMode === 'javascript' ) {
231
228
// Define the new Emmet configuration based on the file mode
232
229
const emmetConfig = {
@@ -315,7 +312,7 @@ class Editor extends React.Component {
315
312
}
316
313
317
314
this . props . provideController ( {
318
- tidyCode : this . tidyCode ,
315
+ tidyCode : ( ) => tidyCode ( this . _cm ) ,
319
316
showFind : this . showFind ,
320
317
showReplace : this . showReplace ,
321
318
getContent : this . getContent
@@ -329,26 +326,6 @@ class Editor extends React.Component {
329
326
this . props . provideController ( null ) ;
330
327
}
331
328
332
- getFileMode ( fileName ) {
333
- let mode ;
334
- if ( fileName . match ( / .+ \. j s $ / i) ) {
335
- mode = 'javascript' ;
336
- } else if ( fileName . match ( / .+ \. c s s $ / i) ) {
337
- mode = 'css' ;
338
- } else if ( fileName . match ( / .+ \. ( h t m l | x m l ) $ / i) ) {
339
- mode = 'htmlmixed' ;
340
- } else if ( fileName . match ( / .+ \. j s o n $ / i) ) {
341
- mode = 'application/json' ;
342
- } else if ( fileName . match ( / .+ \. ( f r a g | g l s l ) $ / i) ) {
343
- mode = 'x-shader/x-fragment' ;
344
- } else if ( fileName . match ( / .+ \. ( v e r t | s t l | m t l ) $ / i) ) {
345
- mode = 'x-shader/x-vertex' ;
346
- } else {
347
- mode = 'text/plain' ;
348
- }
349
- return mode ;
350
- }
351
-
352
329
getContent ( ) {
353
330
const content = this . _cm . getValue ( ) ;
354
331
const updatedFile = Object . assign ( { } , this . props . file , { content } ) ;
@@ -368,44 +345,13 @@ class Editor extends React.Component {
368
345
this . _cm . execCommand ( 'replace' ) ;
369
346
}
370
347
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
-
402
348
initializeDocuments ( files ) {
403
349
this . _docs = { } ;
404
350
files . forEach ( ( file ) => {
405
351
if ( file . name !== 'root' ) {
406
352
this . _docs [ file . id ] = CodeMirror . Doc (
407
353
file . content ,
408
- this . getFileMode ( file . name )
354
+ getFileMode ( file . name )
409
355
) ; // eslint-disable-line
410
356
}
411
357
} ) ;
0 commit comments