11import { EditorAction , EditorMenuItem , EditorPlugin } from "./index" ;
2- import { Ace } from "ace-builds" ;
32import { LanguageName } from "../../../../domain/SupportedLanguage" ;
43import xmlFormat from "xml-formatter" ;
54import os from "os" ;
5+ import { EditorWrapper } from "../../../../domain/EditorWrapper" ;
66
77enum Keys {
88 FORMAT = "beautify.format" ,
@@ -28,7 +28,7 @@ export default class BeautifyPlugin implements EditorPlugin{
2828
2929 ] ;
3030
31- editor ?: Ace . Editor ;
31+ editor ?: EditorWrapper ;
3232
3333 actionMap ?: Map < string , EditorAction > ;
3434
@@ -47,7 +47,7 @@ export default class BeautifyPlugin implements EditorPlugin{
4747 return Array . from ( this . actionMap . keys ( ) ) ;
4848 }
4949
50- initializePlugin ( editor : Ace . Editor ) : void {
50+ initializePlugin ( editor : EditorWrapper ) : void {
5151 this . editor = editor ;
5252
5353 this . initializeActionMap ( ) ;
@@ -60,32 +60,18 @@ export default class BeautifyPlugin implements EditorPlugin{
6060 label : "Format" ,
6161 code : Keys . FORMAT ,
6262 perform : ( ) => {
63- let langName = this . editor ?. getOption ( "mode" ) . split ( "/" ) . pop ( ) || LanguageName . TEXT ;
63+ let langName = this . editor ?. getLanguage ( ) || LanguageName . TEXT ;
6464
65- // multi selection
66- const multiCursorRanges = this . editor ?. getSelection ( ) . getAllRanges ( ) ;
67- if ( multiCursorRanges && multiCursorRanges . length > 0 && ( this . editor ?. getSelectedText ( ) || "" ) . length > 0 ) {
68- for ( let range of multiCursorRanges ) {
69- const target = this . editor ?. session . getTextRange ( range ) || "" ;
65+ const selectionRanges = this . editor ?. getAllSelectionRanges ( ) ;
66+ if ( selectionRanges && selectionRanges . length > 0 && ( this . editor ?. getSelectedText ( ) || "" ) . length > 0 ) {
67+ for ( let range of selectionRanges ) {
68+ const target = this . editor ?. getTextRange ( range ) || "" ;
7069
71- this . editor ?. session . replace ( range , format ( langName , target ) ) ;
70+ this . editor ?. replaceRange ( range , format ( langName , target ) ) ;
7271 }
7372
7473 return ;
7574 }
76-
77- // single selection
78- const singleCursorRange = this . editor ?. getSelection ( ) . getRange ( ) ;
79- if ( singleCursorRange && ( this . editor ?. getSelectedText ( ) || "" ) . length > 0 ) {
80- const target = this . editor ?. session . getTextRange ( singleCursorRange ) || "" ;
81-
82- this . editor ?. session . replace ( singleCursorRange , format ( langName , target ) ) ;
83-
84- return ;
85- }
86-
87- // no selection
88- this . editor ?. setValue ( format ( langName , this . editor ?. getValue ( ) ) ) ;
8975 }
9076 } ) ;
9177 }
0 commit comments