1- import prettier from 'prettier/standalone' ;
2- import prettierParserBabel from 'prettier/parser-babel' ;
3- import prettierParserGraphql from 'prettier/parser-graphql' ;
4- import prettierParserAngular from 'prettier/parser-angular' ;
5- import prettierParserAspree from 'prettier/parser-espree' ;
6- import prettierParserFlow from 'prettier/parser-flow' ;
7- import prettierParserGlimmer from 'prettier/parser-glimmer' ;
8- import prettierParserHtml from 'prettier/parser-html' ;
9- import prettierParserMd from 'prettier/parser-markdown' ;
10- import prettierParserMeriyah from 'prettier/parser-meriyah' ;
11- import prettierParserPostcss from 'prettier/parser-postcss' ;
12- import prettierParserTypescript from 'prettier/parser-typescript' ;
13- import prettierParserYaml from 'prettier/parser-yaml' ;
14-
15- const pluginId = 'acode.plugin.prettier' ;
16- const poluginList = [
17- prettierParserBabel ,
18- prettierParserGraphql ,
19- prettierParserAngular ,
20- prettierParserAspree ,
21- prettierParserFlow ,
22- prettierParserGlimmer ,
23- prettierParserHtml ,
24- prettierParserMd ,
25- prettierParserMeriyah ,
26- prettierParserPostcss ,
27- prettierParserTypescript ,
28- prettierParserYaml ,
1+ import prettier from "prettier/standalone" ;
2+ import pretterParserHTML from "prettier/parser-html" ;
3+ import prettierParserBabel from "prettier/parser-babel" ;
4+ import prettierParserGraphql from "prettier/parser-graphql" ;
5+ import prettierParserAngular from "prettier/parser-angular" ;
6+ import prettierParserAspree from "prettier/parser-espree" ;
7+ import prettierParserFlow from "prettier/parser-flow" ;
8+ import prettierParserGlimmer from "prettier/parser-glimmer" ;
9+ import prettierParserMd from "prettier/parser-markdown" ;
10+ import prettierParserMeriyah from "prettier/parser-meriyah" ;
11+ import prettierParserPostcss from "prettier/parser-postcss" ;
12+ import prettierParserTypescript from "prettier/parser-typescript" ;
13+ import prettierParserYaml from "prettier/parser-yaml" ;
14+
15+ const plugins = [
16+ prettierParserBabel ,
17+ prettierParserGraphql ,
18+ prettierParserAngular ,
19+ prettierParserAspree ,
20+ prettierParserFlow ,
21+ prettierParserGlimmer ,
22+ pretterParserHTML ,
23+ prettierParserMd ,
24+ prettierParserMeriyah ,
25+ prettierParserPostcss ,
26+ prettierParserTypescript ,
27+ prettierParserYaml ,
2928] ;
3029
30+ const pluginId = "acode.plugin.prettier" ;
31+
3132class Prettier {
33+ static inferParser ( filename ) {
34+ switch ( filename . slice ( filename . lastIndexOf ( "." ) + 1 ) ) {
35+ case "html" :
36+ case "htm" :
37+ return "html" ;
3238
33- async init ( ) {
34- const config = appSettings . value [ pluginId ] ;
35- }
36-
37- async run ( ) {
38- const { editor, activeFile } = editorManager ;
39- const code = editor . getValue ( ) ;
40- const cursorPos = editor . getCursorPosition ( ) ;
41- const res = prettier . formatWithCursor ( code , {
42- cursorOffset : this . #cursorPosTocursorOffset( cursorPos ) ,
43- filepath : activeFile . name ,
44- plugins : poluginList ,
45- } ) ;
46- editor . setValue ( res . formatted ) ;
47- const { row, column } = this . #cursorOffsetTocursorPos( res . cursorOffset ) ;
48- setTimeout ( ( ) => {
49- editor . gotoLine ( row + 1 , column - 1 ) ;
50- } , 100 ) ;
51- }
52-
53- destroy ( ) {
54-
55- }
56-
57- #cursorPosTocursorOffset( cursorPos ) {
58- let { row, column } = cursorPos ;
59- const { editor } = editorManager ;
60- const lines = editor . getValue ( ) . split ( '\n' ) ;
61- for ( let i = 0 ; i < row - 1 ; i ++ ) {
62- column += lines [ i ] . length ;
39+ case "css" :
40+ return "css" ;
41+
42+ case "scss" :
43+ return "scss" ;
44+
45+ case "less" :
46+ return "less" ;
47+
48+ case "js" :
49+ case "cjs" :
50+ case "es" :
51+ case "mjs" :
52+ case "jsx" :
53+ return "babel" ;
54+
55+ case "ts" :
56+ case "tsx" :
57+ return "typescript" ;
58+
59+ case "vue" :
60+ return "vue" ;
61+
62+ case "json" :
63+ return "json" ;
64+
65+ case "hbs" :
66+ case "handlebars" :
67+ return "glimmer" ;
68+
69+ case "md" :
70+ return "markdown" ;
71+
72+ case "yaml" :
73+ case "yml" :
74+ return "yaml" ;
75+
76+ default :
77+ return null ;
78+ }
6379 }
64- return column ;
65- }
66-
67- #cursorOffsetTocursorPos( cursorOffset ) {
68- const { editor } = editorManager ;
69- const lines = editor . getValue ( ) . split ( '\n' ) ;
70- let row = 0 ;
71- let column = 0 ;
72- for ( let i = 0 ; i < lines . length ; i ++ ) {
73- if ( column + lines [ i ] . length >= cursorOffset ) {
74- row = i ;
75- column = cursorOffset - column ;
76- break ;
77- }
78- column += lines [ i ] . length ;
80+
81+ async init ( ) {
82+ const config = appSettings . value [ pluginId ] ;
83+ }
84+
85+ async run ( ) {
86+ const { editor, activeFile } = editorManager ;
87+ const code = editor . getValue ( ) ;
88+ const cursorPos = editor . getCursorPosition ( ) ;
89+ const parser = Prettier . inferParser ( activeFile . name ) ;
90+ console . log ( "biraj's parser is" , parser ) ;
91+ const res = prettier . formatWithCursor ( code , {
92+ parser,
93+ cursorOffset : this . #cursorPosTocursorOffset( cursorPos ) ,
94+ filepath : activeFile . name ,
95+ plugins,
96+ } ) ;
97+ editor . setValue ( res . formatted ) ;
98+ const { row, column } = this . #cursorOffsetTocursorPos( res . cursorOffset ) ;
99+ setTimeout ( ( ) => {
100+ editor . gotoLine ( row + 1 , column - 1 ) ;
101+ } , 100 ) ;
102+ }
103+
104+ destroy ( ) { }
105+
106+ #cursorPosTocursorOffset( cursorPos ) {
107+ let { row, column } = cursorPos ;
108+ const { editor } = editorManager ;
109+ const lines = editor . getValue ( ) . split ( "\n" ) ;
110+ for ( let i = 0 ; i < row - 1 ; i ++ ) {
111+ column += lines [ i ] . length ;
112+ }
113+ return column ;
114+ }
115+
116+ #cursorOffsetTocursorPos( cursorOffset ) {
117+ const { editor } = editorManager ;
118+ const lines = editor . getValue ( ) . split ( "\n" ) ;
119+ let row = 0 ;
120+ let column = 0 ;
121+ for ( let i = 0 ; i < lines . length ; i ++ ) {
122+ if ( column + lines [ i ] . length >= cursorOffset ) {
123+ row = i ;
124+ column = cursorOffset - column ;
125+ break ;
126+ }
127+ column += lines [ i ] . length ;
128+ }
129+ return {
130+ row,
131+ column,
132+ } ;
79133 }
80- return {
81- row,
82- column,
83- } ;
84- }
85134}
86135
87136if ( window . acode ) {
88- const prettier = new Prettier ( ) ;
89- acode . setPluginInit ( pluginId , ( baseUrl , $page , { cacheFileUrl, cacheFile } ) => {
90- if ( ! baseUrl . endsWith ( '/' ) ) {
91- baseUrl += '/' ;
92- }
93- prettier . baseUrl = baseUrl ;
94- prettier . init ( $page , cacheFile , cacheFileUrl ) ;
95- console . log ( 'Python plugin initialized' ) ;
96- } ) ;
97- acode . setPluginUnmount ( pluginId , ( ) => {
98- prettier . destroy ( ) ;
99- console . log ( 'Python plugin unmounted' ) ;
100- } ) ;
101- const extensions = [ 'js' , 'jsx' , 'ts' , 'tsx' , 'css' , 'scss' , 'less' , 'json' , 'yml' , 'yaml' , 'xml' , 'md' ] ;
102- // const extensions = ['js', 'jsx', 'ts', 'tsx', 'html', 'css', 'scss', 'less', 'json', 'yml', 'yaml', 'xml', 'vue', 'hbs', 'ejs', 'md'];
103- acode . registerFormatter ( pluginId , extensions , prettier . run . bind ( prettier ) ) ;
104- }
137+ const prettier = new Prettier ( ) ;
138+ acode . setPluginInit ( pluginId , ( baseUrl , $page , { cacheFileUrl, cacheFile } ) => {
139+ if ( ! baseUrl . endsWith ( "/" ) ) {
140+ baseUrl += "/" ;
141+ }
142+ prettier . baseUrl = baseUrl ;
143+ prettier . init ( $page , cacheFile , cacheFileUrl ) ;
144+ } ) ;
145+ acode . setPluginUnmount ( pluginId , ( ) => {
146+ prettier . destroy ( ) ;
147+ } ) ;
148+
149+ const extensions = [
150+ "html" ,
151+ "htm" ,
152+ "css" ,
153+ "scss" ,
154+ "less" ,
155+ "js" ,
156+ "cjs" ,
157+ "es" ,
158+ "mjs" ,
159+ "jsx" ,
160+ "ts" ,
161+ "tsx" ,
162+ "vue" ,
163+ "json" ,
164+ "hbs" ,
165+ "handlebars" ,
166+ "md" ,
167+ "yaml" ,
168+ "yml" ,
169+ ] ;
170+
171+ acode . registerFormatter ( pluginId , extensions , prettier . run . bind ( prettier ) ) ;
172+ }
0 commit comments