-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves the tidier code and file mode out of editor file
- Loading branch information
Showing
3 changed files
with
63 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import prettier from 'prettier/standalone'; | ||
import babelParser from 'prettier/parser-babel'; | ||
import htmlParser from 'prettier/parser-html'; | ||
import cssParser from 'prettier/parser-postcss'; | ||
|
||
function prettierFormatWithCursor(parser, plugins, cmInstance) { | ||
try { | ||
const { formatted, cursorOffset } = prettier.formatWithCursor( | ||
cmInstance.doc.getValue(), | ||
{ | ||
cursorOffset: cmInstance.doc.indexFromPos(cmInstance.doc.getCursor()), | ||
parser, | ||
plugins | ||
} | ||
); | ||
const { left, top } = cmInstance.getScrollInfo(); | ||
cmInstance.doc.setValue(formatted); | ||
cmInstance.focus(); | ||
cmInstance.doc.setCursor(cmInstance.doc.posFromIndex(cursorOffset)); | ||
cmInstance.scrollTo(left, top); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
/** Runs prettier on the codemirror instance, depending on the mode. */ | ||
export default function tidyCode(cmInstance) { | ||
const mode = cmInstance.getOption('mode'); | ||
if (mode === 'javascript') { | ||
prettierFormatWithCursor('babel', [babelParser], cmInstance); | ||
} else if (mode === 'css') { | ||
prettierFormatWithCursor('css', [cssParser], cmInstance); | ||
} else if (mode === 'htmlmixed') { | ||
prettierFormatWithCursor('html', [htmlParser], cmInstance); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** Detects what mode the file is based on the name. */ | ||
export default function getFileMode(fileName) { | ||
let mode; | ||
if (fileName.match(/.+\.js$/i)) { | ||
mode = 'javascript'; | ||
} else if (fileName.match(/.+\.css$/i)) { | ||
mode = 'css'; | ||
} else if (fileName.match(/.+\.(html|xml)$/i)) { | ||
mode = 'htmlmixed'; | ||
} else if (fileName.match(/.+\.json$/i)) { | ||
mode = 'application/json'; | ||
} else if (fileName.match(/.+\.(frag|glsl)$/i)) { | ||
mode = 'x-shader/x-fragment'; | ||
} else if (fileName.match(/.+\.(vert|stl|mtl)$/i)) { | ||
mode = 'x-shader/x-vertex'; | ||
} else { | ||
mode = 'text/plain'; | ||
} | ||
return mode; | ||
} |