diff --git a/apps/remix-ide/src/app/plugins/code-format.ts b/apps/remix-ide/src/app/plugins/code-format.ts index a6846d3c18..0a01e08769 100644 --- a/apps/remix-ide/src/app/plugins/code-format.ts +++ b/apps/remix-ide/src/app/plugins/code-format.ts @@ -22,46 +22,51 @@ export class CodeFormat extends Plugin { super(profile) } - async format() { - const file = await this.call('fileManager', 'getCurrentFile') - const content = await this.call('fileManager', 'readFile', file) - let parserName = '' - let options: Options = { - } - switch (path.extname(file)) { - case '.sol': - parserName = 'solidity-parse' - break - case '.ts': - parserName = 'typescript' - options = { - ...options, - trailingComma: 'all', - semi: false, - singleQuote: true, - quoteProps: 'as-needed', - bracketSpacing: true, - arrowParens: 'always', - } - break - case '.js': - parserName = "espree" - options = { - ...options, - semi: false, - singleQuote: true, - } - break - case '.json': - parserName = 'json' - break + async format(file: string) { + + try { + const content = await this.call('fileManager', 'readFile', file) + if (!content) return + let parserName = '' + let options: Options = { + } + switch (path.extname(file)) { + case '.sol': + parserName = 'solidity-parse' + break + case '.ts': + parserName = 'typescript' + options = { + ...options, + trailingComma: 'all', + semi: false, + singleQuote: true, + quoteProps: 'as-needed', + bracketSpacing: true, + arrowParens: 'always', + } + break + case '.js': + parserName = "espree" + options = { + ...options, + semi: false, + singleQuote: true, + } + break + case '.json': + parserName = 'json' + break + } + const result = prettier.format(content, { + plugins: [sol as any, ts, babel, espree], + parser: parserName, + ...options + }) + await this.call('fileManager', 'writeFile', file, result) + } catch (e) { + // do nothing } - const result = prettier.format(content, { - plugins: [sol as any, ts, babel, espree], - parser: parserName, - ...options - }) - await this.call('fileManager', 'writeFile', file, result) } } diff --git a/libs/remix-ui/editor/src/lib/remix-ui-editor.tsx b/libs/remix-ui/editor/src/lib/remix-ui-editor.tsx index eb76950e3e..715ecf9c7a 100644 --- a/libs/remix-ui/editor/src/lib/remix-ui-editor.tsx +++ b/libs/remix-ui/editor/src/lib/remix-ui-editor.tsx @@ -122,9 +122,11 @@ export const EditorUI = (props: EditorUIProps) => { \t\t\t\t\t\t\t|_| \\_\\ |_____| |_| |_| |___| /_/\\_\\ |___| |____/ |_____|\n\n \t\t\t\t\t\t\tKeyboard Shortcuts:\n \t\t\t\t\t\t\t\tCTRL + S: Compile the current contract\n - \t\t\t\t\t\t\t\tCtrl + Shift + F : Open the File Explorer\n - \t\t\t\t\t\t\t\tCtrl + Shift + A : Open the Plugin Manager\n - \t\t\t\t\t\t\t\tCTRL + SHIFT + S: Compile the current contract & Run an associated script\n\n + \t\t\t\t\t\t\t\tCTRL + Shift + F : Open the File Explorer\n + \t\t\t\t\t\t\t\tCTRL + Shift + A : Open the Plugin Manager\n + \t\t\t\t\t\t\t\tCTRL + SHIFT + S: Compile the current contract & Run an associated script\n + \t\t\t\t\t\t\tEditor Keyboard Shortcuts:\n + \t\t\t\t\t\t\t\tCTRL + Alt + F : Format the code in the current file\n \t\t\t\t\t\t\tImportant Links:\n \t\t\t\t\t\t\t\tOfficial website about the Remix Project: https://remix-project.org/\n \t\t\t\t\t\t\t\tOfficial documentation: https://remix-ide.readthedocs.io/en/latest/\n @@ -578,7 +580,8 @@ export const EditorUI = (props: EditorUIProps) => { monacoRef.current.KeyMod.Shift | monacoRef.current.KeyMod.Alt | monacoRef.current.KeyCode.KeyF, ], run: async () => { - await props.plugin.call('codeFormatter', 'format') + const file = await props.plugin.call('fileManager', 'getCurrentFile') + await props.plugin.call('codeFormatter', 'format', file) }, } editor.addAction(formatAction)