@@ -41,9 +41,7 @@ export class Formatter {
41
41
formatOptions ?: LSP . FormattingOptions | null ,
42
42
shfmtConfig ?: Record < string , string | boolean > | null ,
43
43
) : Promise < TextEdit [ ] > {
44
- const documentText = document . getText ( )
45
-
46
- const result = await this . runShfmt ( documentText , formatOptions , shfmtConfig )
44
+ const result = await this . runShfmt ( document , formatOptions , shfmtConfig )
47
45
48
46
if ( ! this . _canFormat ) {
49
47
return [ ]
@@ -61,7 +59,7 @@ export class Formatter {
61
59
}
62
60
63
61
private async runShfmt (
64
- documentText : string ,
62
+ document : TextDocument ,
65
63
formatOptions ?: LSP . FormattingOptions | null ,
66
64
shfmtConfig ?: Record < string , string | boolean > | null ,
67
65
) : Promise < string > {
@@ -74,6 +72,12 @@ export class Formatter {
74
72
if ( shfmtConfig ?. simplifyCode ) args . push ( '-s' ) // --simplify
75
73
if ( shfmtConfig ?. spaceRedirects ) args . push ( '-sr' ) // --space-redirects
76
74
75
+ // If we can determine a local filename, pass that to shfmt to aid language dialect detection
76
+ const filePathMatch = document . uri . match ( / ^ f i l e : \/ \/ ( .* ) $ / )
77
+ if ( filePathMatch ) {
78
+ args . push ( `--filename=${ filePathMatch [ 1 ] } ` )
79
+ }
80
+
77
81
logger . debug ( `Shfmt: running "${ this . executablePath } ${ args . join ( ' ' ) } "` )
78
82
79
83
let out = ''
@@ -90,7 +94,7 @@ export class Formatter {
90
94
// This is solved in Node >= 15.1 by the "on('spawn', ...)" event, but we need to
91
95
// support earlier versions.
92
96
} )
93
- proc . stdin . end ( documentText )
97
+ proc . stdin . end ( document . getText ( ) )
94
98
} )
95
99
96
100
let exit
0 commit comments