88 removeExternalCommand ,
99 executeCommand as runCommand ,
1010} from "cm/commandRegistry" ;
11- import { addMode , removeMode } from "cm/modelist" ;
11+ import { addMode , getModeForPath , removeMode } from "cm/modelist" ;
1212import cmThemeRegistry from "cm/themes" ;
1313import Contextmenu from "components/contextmenu" ;
1414import inputhints from "components/inputhints" ;
@@ -57,30 +57,8 @@ export default class Acode {
5757 #modules = { } ;
5858 #pluginsInit = { } ;
5959 #pluginUnmount = { } ;
60- #formatter = [
61- {
62- id : "default" ,
63- name : "Default" ,
64- exts : [ "*" ] ,
65- format : async ( ) => {
66- // TODO: Implement code formatting for CodeMirror
67- // const { beautify } = ace.require("ace/ext/beautify");
68- // const cursorPos = editorManager.editor.getCursorPosition();
69- // beautify(editorManager.editor.session);
70- // editorManager.editor.gotoLine(cursorPos.row + 1, cursorPos.column);
71-
72- // Placeholder for CodeMirror formatting
73- // For now, we'll just maintain cursor position without formatting
74- const { editor } = editorManager ;
75- const head = editor . state . selection . main . head ;
76- const cursor = editor . state . doc . lineAt ( head ) ;
77- const line = cursor . number ;
78- const col = head - cursor . from ;
79- // Restore cursor position after any potential formatting
80- editor . gotoLine ( line , col ) ;
81- } ,
82- } ,
83- ] ;
60+ // Registered formatter implementations (populated by plugins)
61+ #formatter = [ ] ;
8462
8563 constructor ( ) {
8664 const encodingsModule = {
@@ -493,10 +471,20 @@ export default class Acode {
493471 delete appSettings . uiSettings [ `plugin-${ id } ` ] ;
494472 }
495473
496- registerFormatter ( id , extensions , format ) {
474+ registerFormatter ( id , extensions , format , displayName ) {
475+ let exts ;
476+ if ( Array . isArray ( extensions ) ) {
477+ exts = extensions . filter ( Boolean ) ;
478+ if ( ! exts . length ) exts = [ "*" ] ;
479+ } else if ( typeof extensions === "string" && extensions ) {
480+ exts = [ extensions ] ;
481+ } else {
482+ exts = [ "*" ] ;
483+ }
497484 this . #formatter. unshift ( {
498485 id,
499- exts : extensions ,
486+ name : displayName ,
487+ exts : exts ,
500488 format,
501489 } ) ;
502490 }
@@ -516,19 +504,42 @@ export default class Acode {
516504
517505 async format ( selectIfNull = true ) {
518506 const file = editorManager . activeFile ;
519- const name = ( file . session . getMode ( ) . $id || "" ) . split ( "/" ) . pop ( ) ;
520- const formatterId = appSettings . value . formatter [ name ] ;
507+ if ( ! file || file . type !== "editor" ) return false ;
508+
509+ let resolvedMode = file . currentMode ;
510+ if ( ! resolvedMode ) {
511+ try {
512+ resolvedMode = getModeForPath ( file . filename ) ?. name ;
513+ } catch ( _ ) {
514+ resolvedMode = null ;
515+ }
516+ }
517+ const modeName = resolvedMode || "text" ;
518+ const formatterMap = appSettings . value . formatter || { } ;
519+ const formatterId = formatterMap [ modeName ] ;
521520 const formatter = this . #formatter. find ( ( { id } ) => id === formatterId ) ;
522521
523- await formatter ?. format ( ) ;
522+ if ( ! formatter ) {
523+ if ( formatterId ) {
524+ delete formatterMap [ modeName ] ;
525+ await appSettings . update ( false ) ;
526+ }
524527
525- if ( ! formatter && selectIfNull ) {
526- formatterSettings ( name ) ;
527- this . #afterSelectFormatter( name ) ;
528- return ;
528+ if ( selectIfNull ) {
529+ formatterSettings ( modeName ) ;
530+ this . #afterSelectFormatter( modeName ) ;
531+ } else {
532+ toast ( strings [ "please select a formatter" ] ) ;
533+ }
534+ return false ;
529535 }
530- if ( ! formatter && ! selectIfNull ) {
531- toast ( strings [ "please select a formatter" ] ) ;
536+
537+ try {
538+ await formatter . format ( ) ;
539+ return true ;
540+ } catch ( error ) {
541+ helpers . error ( error ) ;
542+ return false ;
532543 }
533544 }
534545
0 commit comments