@@ -45,26 +45,47 @@ function getAutoFormatConfig() {
4545
4646function formatTemplate ( template , parser , extraIndentation = '' ) {
4747 const config = getAutoFormatConfig ( )
48+ template = template . replace ( / ^ \s * / gm, '' )
49+
50+ // process multiline comments
51+ template = modifyComments ( template )
4852 let formattedTemplate = prettier . format ( template , { parser, ...config } )
4953
5054 if ( extraIndentation ) {
51- formattedTemplate =
52- extraIndentation +
53- formattedTemplate
54- . replace ( / \n / g, `\n${ extraIndentation } ` )
55- . replace ( / [ \t ] + $ / , ' ' )
55+ formattedTemplate = extraIndentation + formattedTemplate . replace ( / \n / g, `\n${ extraIndentation } ` )
5656 }
5757
5858 return formattedTemplate
5959}
6060
61+ function modifyComments ( str ) {
62+ const regex = / ( \s * ) < ! - - [ \s \S ] * ?- - > / g
63+
64+ return str . replace ( regex , ( match ) => {
65+ const lines = match . split ( '\n' )
66+
67+ // If it's a single-line comment, return it unchanged
68+ if ( lines . length === 1 ) {
69+ return match
70+ }
71+
72+ // Process multi-line comments
73+ const modifiedLines = lines . map ( ( line , index ) => {
74+ if ( index === 0 ) {
75+ return line
76+ } else {
77+ return ' ' + line
78+ }
79+ } )
80+
81+ return modifiedLines . join ( '\n' )
82+ } )
83+ }
84+
6185function createEdit ( document , start , end , newText ) {
6286 const startPosition = document . positionAt ( start )
6387 const endPosition = document . positionAt ( end )
64- return new vscode . TextEdit (
65- new vscode . Range ( startPosition , endPosition ) ,
66- newText
67- )
88+ return new vscode . TextEdit ( new vscode . Range ( startPosition , endPosition ) , newText )
6889}
6990
7091function formatJS ( document , currentDoc , fileExtension ) {
@@ -74,11 +95,7 @@ function formatJS(document, currentDoc, fileExtension) {
7495 return templates . map ( ( { start, end, template } ) => {
7596 const stringChar = template . slice ( - 1 )
7697 const templateText = template . slice ( 1 , - 1 )
77- const formattedTemplate = formatTemplate (
78- templateText ,
79- 'angular' ,
80- ' ' . repeat ( 4 )
81- )
98+ const formattedTemplate = formatTemplate ( templateText , 'angular' , ' ' . repeat ( 4 ) )
8299 const newText = `${ stringChar } \n${ formattedTemplate } ${ stringChar } `
83100 return createEdit ( document , start , end , newText )
84101 } )
@@ -101,9 +118,7 @@ function formatBlits(document, currentDoc) {
101118}
102119
103120module . exports = vscode . workspace . onWillSaveTextDocument ( ( event ) => {
104- const autoFormatEnabled = vscode . workspace
105- . getConfiguration ( 'blits' )
106- . get ( 'autoFormat' )
121+ const autoFormatEnabled = vscode . workspace . getConfiguration ( 'blits' ) . get ( 'autoFormat' )
107122
108123 if ( ! autoFormatEnabled ) return
109124
0 commit comments