1+ import { FormattingOptions } from 'vscode-languageserver/node'
12import { TextDocument } from 'vscode-languageserver-textdocument'
23
34import { FIXTURE_DOCUMENT , FIXTURE_FOLDER } from '../../../../testing/fixtures'
@@ -17,14 +18,16 @@ function textToDoc(txt: string) {
1718async function getFormattingResult ( {
1819 document,
1920 executablePath = 'shfmt' ,
21+ formatOptions,
2022} : {
2123 document : TextDocument
2224 executablePath ?: string
25+ formatOptions ?: FormattingOptions
2326} ) : Promise < [ Awaited < ReturnType < Formatter [ 'format' ] > > , Formatter ] > {
2427 const formatter = new Formatter ( {
2528 executablePath,
2629 } )
27- const result = await formatter . format ( document )
30+ const result = await formatter . format ( document , formatOptions )
2831 return [ result , formatter ]
2932}
3033
@@ -90,4 +93,94 @@ describe('formatter', () => {
9093 ]
9194 ` )
9295 } )
96+
97+ it ( 'should format using tabs when insertSpaces is false' , async ( ) => {
98+ const [ result ] = await getFormattingResult ( {
99+ document : FIXTURE_DOCUMENT . SHFMT ,
100+ formatOptions : { tabSize : 4 , insertSpaces : false } ,
101+ } )
102+ expect ( result ) . toMatchInlineSnapshot ( `
103+ [
104+ {
105+ "newText": "#!/bin/bash
106+ set -ueo pipefail
107+
108+ if [ -z "$arg" ]; then
109+ echo indent
110+ fi
111+
112+ echo binary &&
113+ echo next line
114+
115+ case "$arg" in
116+ a)
117+ echo case indent
118+ ;;
119+ esac
120+
121+ echo space redirects >/dev/null
122+
123+ function next() {
124+ echo line
125+ }
126+ ",
127+ "range": {
128+ "end": {
129+ "character": 2147483647,
130+ "line": 2147483647,
131+ },
132+ "start": {
133+ "character": 0,
134+ "line": 0,
135+ },
136+ },
137+ },
138+ ]
139+ ` )
140+ } )
141+
142+ it ( 'should format using spaces when insertSpaces is true' , async ( ) => {
143+ const [ result ] = await getFormattingResult ( {
144+ document : FIXTURE_DOCUMENT . SHFMT ,
145+ formatOptions : { tabSize : 3 , insertSpaces : true } ,
146+ } )
147+ expect ( result ) . toMatchInlineSnapshot ( `
148+ [
149+ {
150+ "newText": "#!/bin/bash
151+ set -ueo pipefail
152+
153+ if [ -z "$arg" ]; then
154+ echo indent
155+ fi
156+
157+ echo binary &&
158+ echo next line
159+
160+ case "$arg" in
161+ a)
162+ echo case indent
163+ ;;
164+ esac
165+
166+ echo space redirects >/dev/null
167+
168+ function next() {
169+ echo line
170+ }
171+ ",
172+ "range": {
173+ "end": {
174+ "character": 2147483647,
175+ "line": 2147483647,
176+ },
177+ "start": {
178+ "character": 0,
179+ "line": 0,
180+ },
181+ },
182+ },
183+ ]
184+ ` )
185+ } )
93186} )
0 commit comments