@@ -21,10 +21,12 @@ describe(`Formatting Tests`, () => {
21
21
} ) ;
22
22
23
23
beforeEach ( async ( ) => {
24
+ await setOrganizeImportsOnFormat ( undefined ) ;
24
25
await openFileInWorkspaceAsync ( path . join ( 'src' , 'app' , 'Formatting.cs' ) ) ;
25
26
} ) ;
26
27
27
28
afterAll ( async ( ) => {
29
+ await setOrganizeImportsOnFormat ( undefined ) ;
28
30
await testAssetWorkspace . cleanupWorkspace ( ) ;
29
31
} ) ;
30
32
@@ -36,6 +38,8 @@ describe(`Formatting Tests`, () => {
36
38
await formatDocumentAsync ( ) ;
37
39
38
40
const expectedText = [
41
+ 'using Options;' ,
42
+ 'using System;' ,
39
43
'namespace Formatting;' ,
40
44
'class DocumentFormatting' ,
41
45
'{' ,
@@ -54,9 +58,11 @@ describe(`Formatting Tests`, () => {
54
58
} ) ;
55
59
56
60
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 ) ) ;
58
62
59
63
const expectedText = [
64
+ 'using Options;' ,
65
+ 'using System;' ,
60
66
'namespace Formatting;' ,
61
67
'class DocumentFormatting' ,
62
68
'{' ,
@@ -75,9 +81,11 @@ describe(`Formatting Tests`, () => {
75
81
76
82
test ( 'Document on type formatting formats the typed location' , async ( ) => {
77
83
// 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 ) , ';' ) ;
79
85
80
86
const expectedText = [
87
+ 'using Options;' ,
88
+ 'using System;' ,
81
89
'namespace Formatting;' ,
82
90
'class DocumentFormatting' ,
83
91
'{' ,
@@ -91,4 +99,60 @@ describe(`Formatting Tests`, () => {
91
99
] ;
92
100
await expectText ( vscode . window . activeTextEditor ! . document , expectedText ) ;
93
101
} ) ;
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
+ }
94
158
} ) ;
0 commit comments