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 ,
29+ ] ;
30+
31+ class Prettier {
32+
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 ;
63+ }
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 ;
79+ }
80+ return {
81+ row,
82+ column,
83+ } ;
84+ }
85+ }
86+
87+ if ( 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' , 'html' , 'css' , 'scss' , 'less' , 'json' , 'yml' , 'yaml' , 'xml' , 'vue' , 'hbs' , 'ejs' , 'md' ] ;
102+ acode . registerFormatter ( pluginId , extensions , prettier . run . bind ( prettier ) ) ;
103+ }
0 commit comments