@@ -52,11 +52,7 @@ public static async Task<TextEdit[]> GetUsingStatementEditsAsync(RazorCodeDocume
52
52
using var edits = new PooledArrayBuilder < TextEdit > ( ) ;
53
53
foreach ( var usingStatement in newUsings . Except ( oldUsings ) )
54
54
{
55
- // This identifier will be eventually thrown away.
56
- Debug . Assert ( codeDocument . Source . FilePath != null ) ;
57
- var identifier = new OptionalVersionedTextDocumentIdentifier { Uri = new Uri ( codeDocument . Source . FilePath , UriKind . Relative ) } ;
58
- var workspaceEdit = CreateAddUsingWorkspaceEdit ( usingStatement , additionalEdit : null , codeDocument , codeDocumentIdentifier : identifier ) ;
59
- edits . AddRange ( workspaceEdit . DocumentChanges ! . Value . First . First ( ) . Edits . Select ( e => ( TextEdit ) e ) ) ;
55
+ edits . Add ( CreateAddUsingTextEdit ( usingStatement , codeDocument ) ) ;
60
56
}
61
57
62
58
return edits . ToArray ( ) ;
@@ -92,7 +88,7 @@ public static bool TryExtractNamespace(string csharpAddUsing, out string @namesp
92
88
return true ;
93
89
}
94
90
95
- public static WorkspaceEdit CreateAddUsingWorkspaceEdit ( string @namespace , TextDocumentEdit ? additionalEdit , RazorCodeDocument codeDocument , OptionalVersionedTextDocumentIdentifier codeDocumentIdentifier )
91
+ public static TextEdit CreateAddUsingTextEdit ( string @namespace , RazorCodeDocument codeDocument )
96
92
{
97
93
/* The heuristic is as follows:
98
94
*
@@ -109,34 +105,15 @@ public static WorkspaceEdit CreateAddUsingWorkspaceEdit(string @namespace, TextD
109
105
* that now I can come up with a more sophisticated heuristic (something along the lines of checking if
110
106
* there's already an ordering, etc.).
111
107
*/
112
- using var documentChanges = new PooledArrayBuilder < TextDocumentEdit > ( ) ;
113
-
114
- // Need to add the additional edit first, as the actual usings go at the top of the file, and would
115
- // change the ranges needed in the additional edit if they went in first
116
- if ( additionalEdit is not null )
117
- {
118
- documentChanges . Add ( additionalEdit ) ;
119
- }
120
108
121
109
using var usingDirectives = new PooledArrayBuilder < RazorUsingDirective > ( ) ;
122
110
CollectUsingDirectives ( codeDocument , ref usingDirectives . AsRef ( ) ) ;
123
111
if ( usingDirectives . Count > 0 )
124
112
{
125
- // Interpolate based on existing @using statements
126
- var edits = GenerateSingleUsingEditsInterpolated ( codeDocument , codeDocumentIdentifier , @namespace , in usingDirectives ) ;
127
- documentChanges . Add ( edits ) ;
128
- }
129
- else
130
- {
131
- // Just throw them at the top
132
- var edits = GenerateSingleUsingEditsAtTop ( codeDocument , codeDocumentIdentifier , @namespace ) ;
133
- documentChanges . Add ( edits ) ;
113
+ return GetInsertUsingTextEdit ( codeDocument , @namespace , in usingDirectives ) ;
134
114
}
135
115
136
- return new WorkspaceEdit ( )
137
- {
138
- DocumentChanges = documentChanges . ToArray ( ) ,
139
- } ;
116
+ return GetInsertUsingTextEdit ( codeDocument , @namespace ) ;
140
117
}
141
118
142
119
public static async Task < ImmutableArray < string > > FindUsingDirectiveStringsAsync ( SyntaxTree syntaxTree , CancellationToken cancellationToken )
@@ -165,15 +142,16 @@ static string GetNamespaceFromDirective(UsingDirectiveSyntax usingDirectiveSynta
165
142
}
166
143
}
167
144
168
- private static TextDocumentEdit GenerateSingleUsingEditsInterpolated (
145
+ /// <summary>
146
+ /// Generates a <see cref="TextEdit"/> to insert a new using directive into the Razor code document, at the right spot among existing using directives.
147
+ /// </summary>
148
+ private static TextEdit GetInsertUsingTextEdit (
169
149
RazorCodeDocument codeDocument ,
170
- OptionalVersionedTextDocumentIdentifier codeDocumentIdentifier ,
171
150
string newUsingNamespace ,
172
151
ref readonly PooledArrayBuilder < RazorUsingDirective > existingUsingDirectives )
173
152
{
174
153
Debug . Assert ( existingUsingDirectives . Count > 0 ) ;
175
154
176
- using var edits = new PooledArrayBuilder < SumType < TextEdit , AnnotatedTextEdit > > ( ) ;
177
155
var newText = $ "@using { newUsingNamespace } { Environment . NewLine } ";
178
156
179
157
foreach ( var usingDirective in existingUsingDirectives )
@@ -188,31 +166,21 @@ private static TextDocumentEdit GenerateSingleUsingEditsInterpolated(
188
166
if ( string . CompareOrdinal ( newUsingNamespace , usingDirectiveNamespace ) < 0 )
189
167
{
190
168
var usingDirectiveLineIndex = codeDocument . Source . Text . GetLinePosition ( usingDirective . Node . Span . Start ) . Line ;
191
- var edit = LspFactory . CreateTextEdit ( line : usingDirectiveLineIndex , character : 0 , newText ) ;
192
- edits . Add ( edit ) ;
193
- break ;
169
+ return LspFactory . CreateTextEdit ( line : usingDirectiveLineIndex , character : 0 , newText ) ;
194
170
}
195
171
}
196
172
197
173
// If we haven't actually found a place to insert the using directive, do so at the end
198
- if ( edits . Count == 0 )
199
- {
200
- var endIndex = existingUsingDirectives [ ^ 1 ] . Node . Span . End ;
201
- var lineIndex = GetLineIndexOrEnd ( codeDocument , endIndex - 1 ) + 1 ;
202
- var edit = LspFactory . CreateTextEdit ( line : lineIndex , character : 0 , newText ) ;
203
- edits . Add ( edit ) ;
204
- }
205
-
206
- return new TextDocumentEdit ( )
207
- {
208
- TextDocument = codeDocumentIdentifier ,
209
- Edits = edits . ToArray ( )
210
- } ;
174
+ var endIndex = existingUsingDirectives [ ^ 1 ] . Node . Span . End ;
175
+ var lineIndex = GetLineIndexOrEnd ( codeDocument , endIndex - 1 ) + 1 ;
176
+ return LspFactory . CreateTextEdit ( line : lineIndex , character : 0 , newText ) ;
211
177
}
212
178
213
- private static TextDocumentEdit GenerateSingleUsingEditsAtTop (
179
+ /// <summary>
180
+ /// Generates a <see cref="TextEdit"/> to insert a new using directive into the Razor code document, at the top of the file.
181
+ /// </summary>
182
+ private static TextEdit GetInsertUsingTextEdit (
214
183
RazorCodeDocument codeDocument ,
215
- OptionalVersionedTextDocumentIdentifier codeDocumentIdentifier ,
216
184
string newUsingNamespace )
217
185
{
218
186
var insertPosition = ( 0 , 0 ) ;
@@ -229,12 +197,7 @@ private static TextDocumentEdit GenerateSingleUsingEditsAtTop(
229
197
insertPosition = ( lineIndex , 0 ) ;
230
198
}
231
199
232
- // Insert all usings at the given point
233
- return new TextDocumentEdit
234
- {
235
- TextDocument = codeDocumentIdentifier ,
236
- Edits = [ LspFactory . CreateTextEdit ( insertPosition , newText : $ "@using { newUsingNamespace } { Environment . NewLine } ") ]
237
- } ;
200
+ return LspFactory . CreateTextEdit ( insertPosition , newText : $ "@using { newUsingNamespace } { Environment . NewLine } ") ;
238
201
}
239
202
240
203
private static int GetLineIndexOrEnd ( RazorCodeDocument codeDocument , int endIndex )
0 commit comments