@@ -21,10 +21,12 @@ describe(`Formatting Tests`, () => {
2121 } ) ;
2222
2323 beforeEach ( async ( ) => {
24+ await setOrganizeImportsOnFormat ( undefined ) ;
2425 await openFileInWorkspaceAsync ( path . join ( 'src' , 'app' , 'Formatting.cs' ) ) ;
2526 } ) ;
2627
2728 afterAll ( async ( ) => {
29+ await setOrganizeImportsOnFormat ( undefined ) ;
2830 await testAssetWorkspace . cleanupWorkspace ( ) ;
2931 } ) ;
3032
@@ -36,6 +38,8 @@ describe(`Formatting Tests`, () => {
3638 await formatDocumentAsync ( ) ;
3739
3840 const expectedText = [
41+ 'using Options;' ,
42+ 'using System;' ,
3943 'namespace Formatting;' ,
4044 'class DocumentFormatting' ,
4145 '{' ,
@@ -54,9 +58,11 @@ describe(`Formatting Tests`, () => {
5458 } ) ;
5559
5660 test ( 'Document range formatting formats only the range' , async ( ) => {
57- await formatRangeAsync ( new vscode . Range ( 3 , 0 , 5 , 0 ) ) ;
61+ await formatRangeAsync ( new vscode . Range ( 5 , 0 , 7 , 0 ) ) ;
5862
5963 const expectedText = [
64+ 'using Options;' ,
65+ 'using System;' ,
6066 'namespace Formatting;' ,
6167 'class DocumentFormatting' ,
6268 '{' ,
@@ -75,9 +81,11 @@ describe(`Formatting Tests`, () => {
7581
7682 test ( 'Document on type formatting formats the typed location' , async ( ) => {
7783 // The server expects the position to be the position after the inserted character `;`
78- await formatOnTypeAsync ( new vscode . Position ( 7 , 37 ) , ';' ) ;
84+ await formatOnTypeAsync ( new vscode . Position ( 9 , 37 ) , ';' ) ;
7985
8086 const expectedText = [
87+ 'using Options;' ,
88+ 'using System;' ,
8189 'namespace Formatting;' ,
8290 'class DocumentFormatting' ,
8391 '{' ,
@@ -91,4 +99,60 @@ describe(`Formatting Tests`, () => {
9199 ] ;
92100 await expectText ( vscode . window . activeTextEditor ! . document , expectedText ) ;
93101 } ) ;
102+
103+ test ( 'Document formatting can organize imports' , async ( ) => {
104+ await setOrganizeImportsOnFormat ( true ) ;
105+ await activateCSharpExtension ( ) ;
106+
107+ await formatDocumentAsync ( ) ;
108+
109+ const expectedText = [
110+ 'using System;' ,
111+ 'using Options;' ,
112+ 'namespace Formatting;' ,
113+ 'class DocumentFormatting' ,
114+ '{' ,
115+ ' public int Property1' ,
116+ ' {' ,
117+ ' get; set;' ,
118+ ' }' ,
119+ '' ,
120+ ' public void Method1()' ,
121+ ' {' ,
122+ ' System.Console.Write("");' ,
123+ ' }' ,
124+ '}' ,
125+ ] ;
126+ await expectText ( vscode . window . activeTextEditor ! . document , expectedText ) ;
127+ } ) ;
128+
129+ test ( 'Document range formatting does not organize imports' , async ( ) => {
130+ await setOrganizeImportsOnFormat ( true ) ;
131+ await activateCSharpExtension ( ) ;
132+
133+ await formatRangeAsync ( new vscode . Range ( 0 , 0 , 7 , 0 ) ) ;
134+
135+ const expectedText = [
136+ 'using Options;' ,
137+ 'using System;' ,
138+ 'namespace Formatting;' ,
139+ 'class DocumentFormatting' ,
140+ '{' ,
141+ ' public int Property1' ,
142+ ' {' ,
143+ ' get; set;' ,
144+ ' }' ,
145+ '' ,
146+ ' public void Method1() {' ,
147+ ' System.Console.Write("");' ,
148+ ' }' ,
149+ '}' ,
150+ ] ;
151+ await expectText ( vscode . window . activeTextEditor ! . document , expectedText ) ;
152+ } ) ;
153+
154+ async function setOrganizeImportsOnFormat ( value : boolean | undefined ) {
155+ const dotnetConfig = vscode . workspace . getConfiguration ( 'dotnet' ) ;
156+ await dotnetConfig . update ( 'formatting.organizeImportsOnFormat' , value , true ) ;
157+ }
94158} ) ;
0 commit comments