@@ -152,164 +152,156 @@ type internal FSharpClassificationService [<ImportingConstructor>] () =
152
152
member _.AddLexicalClassifications ( _ : SourceText , _ : TextSpan , _ : List < ClassifiedSpan >, _ : CancellationToken ) = ()
153
153
154
154
member _.AddSyntacticClassificationsAsync
155
- (
156
- document : Document ,
157
- textSpan : TextSpan ,
158
- result : List < ClassifiedSpan >,
159
- cancellationToken : CancellationToken
160
- ) =
155
+ ( document : Document , textSpan : TextSpan , result : List < ClassifiedSpan >, cancellationToken : CancellationToken )
156
+ =
161
157
162
158
if not ( document |> shouldProduceClassification) then
163
159
System.Threading.Tasks.Task.CompletedTask
164
- else
165
-
166
- cancellableTask {
167
- use _logBlock = Logger.LogBlock( LogEditorFunctionId.Classification_ Syntactic)
168
-
169
- let! cancellationToken = CancellableTask.getCancellationToken ()
170
-
171
- let defines , langVersion , strictIndentation = document.GetFsharpParsingOptions()
172
-
173
- let! sourceText = document.GetTextAsync( cancellationToken)
174
-
175
- // For closed documents, only get classification for the text within the span.
176
- // This may be inaccurate for multi-line tokens such as string literals, but this is ok for now
177
- // as it's better than having to tokenize a big part of a file which in return will allocate a lot and hurt find all references performance.
178
- let isOpenDocument = document.Project.Solution.Workspace.IsDocumentOpen document.Id
179
-
180
- let eventProps : ( string * obj ) array =
181
- [|
182
- " context.document.project.id" , document.Project.Id.Id.ToString()
183
- " context.document.id" , document.Id.Id.ToString()
184
- " isOpenDocument" , isOpenDocument
185
- " textSpanLength" , textSpan.Length
186
- |]
187
-
188
- use _eventDuration =
189
- TelemetryReporter.ReportSingleEventWithDuration( TelemetryEvents.AddSyntacticClassifications, eventProps)
190
-
191
- if not isOpenDocument then
192
- let classifiedSpans =
193
- getLexicalClassifications ( document.FilePath, defines, sourceText, textSpan, cancellationToken)
194
-
195
- result.AddRange( classifiedSpans)
196
- else
197
- Tokenizer.classifySpans (
198
- document.Id,
199
- sourceText,
200
- textSpan,
201
- Some( document.FilePath),
202
- defines,
203
- Some langVersion,
204
- strictIndentation,
205
- result,
206
- cancellationToken
207
- )
208
- }
209
- |> CancellableTask.startAsTask cancellationToken
160
+ else
161
+
162
+ cancellableTask {
163
+ use _logBlock = Logger.LogBlock( LogEditorFunctionId.Classification_ Syntactic)
164
+
165
+ let! cancellationToken = CancellableTask.getCancellationToken ()
166
+
167
+ let defines , langVersion , strictIndentation = document.GetFsharpParsingOptions()
168
+
169
+ let! sourceText = document.GetTextAsync( cancellationToken)
170
+
171
+ // For closed documents, only get classification for the text within the span.
172
+ // This may be inaccurate for multi-line tokens such as string literals, but this is ok for now
173
+ // as it's better than having to tokenize a big part of a file which in return will allocate a lot and hurt find all references performance.
174
+ let isOpenDocument = document.Project.Solution.Workspace.IsDocumentOpen document.Id
175
+
176
+ let eventProps : ( string * obj ) array =
177
+ [|
178
+ " context.document.project.id" , document.Project.Id.Id.ToString()
179
+ " context.document.id" , document.Id.Id.ToString()
180
+ " isOpenDocument" , isOpenDocument
181
+ " textSpanLength" , textSpan.Length
182
+ |]
183
+
184
+ use _eventDuration =
185
+ TelemetryReporter.ReportSingleEventWithDuration( TelemetryEvents.AddSyntacticClassifications, eventProps)
186
+
187
+ if not isOpenDocument then
188
+ let classifiedSpans =
189
+ getLexicalClassifications ( document.FilePath, defines, sourceText, textSpan, cancellationToken)
190
+
191
+ result.AddRange( classifiedSpans)
192
+ else
193
+ Tokenizer.classifySpans (
194
+ document.Id,
195
+ sourceText,
196
+ textSpan,
197
+ Some( document.FilePath),
198
+ defines,
199
+ Some langVersion,
200
+ strictIndentation,
201
+ result,
202
+ cancellationToken
203
+ )
204
+ }
205
+ |> CancellableTask.startAsTask cancellationToken
210
206
211
207
member _.AddSemanticClassificationsAsync
212
- (
213
- document : Document ,
214
- textSpan : TextSpan ,
215
- result : List < ClassifiedSpan >,
216
- cancellationToken : CancellationToken
217
- ) =
208
+ ( document : Document , textSpan : TextSpan , result : List < ClassifiedSpan >, cancellationToken : CancellationToken )
209
+ =
218
210
219
211
if not ( document |> shouldProduceClassification) then
220
212
System.Threading.Tasks.Task.CompletedTask
221
- else
222
-
223
- cancellableTask {
224
- use _logBlock = Logger.LogBlock( LogEditorFunctionId.Classification_ Semantic)
225
-
226
- let! sourceText = document.GetTextAsync( cancellationToken)
227
-
228
- // If we are trying to get semantic classification for a document that is not open, get the results from the background and cache it.
229
- // We do this for find all references when it is populating results.
230
- // We cache it temporarily so we do not have to continuously call into the checker and perform a background operation.
231
- let isOpenDocument = document.Project.Solution.Workspace.IsDocumentOpen document.Id
232
-
233
- if not isOpenDocument then
234
- match ! unopenedDocumentsSemanticClassificationCache.TryGetValueAsync document with
235
- | ValueSome classificationDataLookup ->
236
- let eventProps : ( string * obj ) array =
237
- [|
238
- " context.document.project.id" , document.Project.Id.Id.ToString()
239
- " context.document.id" , document.Id.Id.ToString()
240
- " isOpenDocument" , isOpenDocument
241
- " textSpanLength" , textSpan.Length
242
- " cacheHit" , true
243
- |]
244
-
245
- use _eventDuration =
246
- TelemetryReporter.ReportSingleEventWithDuration( TelemetryEvents.AddSemanticClassifications, eventProps)
247
-
248
- addSemanticClassificationByLookup sourceText textSpan classificationDataLookup result
249
- | ValueNone ->
250
- let eventProps : ( string * obj ) array =
251
- [|
252
- " context.document.project.id" , document.Project.Id.Id.ToString()
253
- " context.document.id" , document.Id.Id.ToString()
254
- " isOpenDocument" , isOpenDocument
255
- " textSpanLength" , textSpan.Length
256
- " cacheHit" , false
257
- |]
258
-
259
- use _eventDuration =
260
- TelemetryReporter.ReportSingleEventWithDuration( TelemetryEvents.AddSemanticClassifications, eventProps)
261
-
262
- let! classificationData = document.GetFSharpSemanticClassificationAsync( nameof ( FSharpClassificationService))
263
-
264
- let classificationDataLookup = toSemanticClassificationLookup classificationData
265
- do ! unopenedDocumentsSemanticClassificationCache.SetAsync( document, classificationDataLookup)
266
- addSemanticClassificationByLookup sourceText textSpan classificationDataLookup result
267
- else
268
-
269
- match ! openedDocumentsSemanticClassificationCache.TryGetValueAsync document with
270
- | ValueSome classificationDataLookup ->
271
- let eventProps : ( string * obj ) array =
272
- [|
273
- " context.document.project.id" , document.Project.Id.Id.ToString()
274
- " context.document.id" , document.Id.Id.ToString()
275
- " isOpenDocument" , isOpenDocument
276
- " textSpanLength" , textSpan.Length
277
- " cacheHit" , true
278
- |]
279
-
280
- use _eventDuration =
281
- TelemetryReporter.ReportSingleEventWithDuration( TelemetryEvents.AddSemanticClassifications, eventProps)
282
-
283
- addSemanticClassificationByLookup sourceText textSpan classificationDataLookup result
284
- | ValueNone ->
285
-
286
- let eventProps : ( string * obj ) array =
287
- [|
288
- " context.document.project.id" , document.Project.Id.Id.ToString()
289
- " context.document.id" , document.Id.Id.ToString()
290
- " isOpenDocument" , isOpenDocument
291
- " textSpanLength" , textSpan.Length
292
- " cacheHit" , false
293
- |]
294
-
295
- use _eventDuration =
296
- TelemetryReporter.ReportSingleEventWithDuration( TelemetryEvents.AddSemanticClassifications, eventProps)
297
-
298
- let! _ , checkResults = document.GetFSharpParseAndCheckResultsAsync( nameof ( IFSharpClassificationService))
299
-
300
- let targetRange =
301
- RoslynHelpers.TextSpanToFSharpRange( document.FilePath, textSpan, sourceText)
302
-
303
- let classificationData = checkResults.GetSemanticClassification( Some targetRange)
304
-
305
- if classificationData.Length > 0 then
306
- let classificationDataLookup = itemToSemanticClassificationLookup classificationData
213
+ else
214
+
215
+ cancellableTask {
216
+ use _logBlock = Logger.LogBlock( LogEditorFunctionId.Classification_ Semantic)
217
+
218
+ let! sourceText = document.GetTextAsync( cancellationToken)
219
+
220
+ // If we are trying to get semantic classification for a document that is not open, get the results from the background and cache it.
221
+ // We do this for find all references when it is populating results.
222
+ // We cache it temporarily so we do not have to continuously call into the checker and perform a background operation.
223
+ let isOpenDocument = document.Project.Solution.Workspace.IsDocumentOpen document.Id
224
+
225
+ if not isOpenDocument then
226
+ match ! unopenedDocumentsSemanticClassificationCache.TryGetValueAsync document with
227
+ | ValueSome classificationDataLookup ->
228
+ let eventProps : ( string * obj ) array =
229
+ [|
230
+ " context.document.project.id" , document.Project.Id.Id.ToString()
231
+ " context.document.id" , document.Id.Id.ToString()
232
+ " isOpenDocument" , isOpenDocument
233
+ " textSpanLength" , textSpan.Length
234
+ " cacheHit" , true
235
+ |]
236
+
237
+ use _eventDuration =
238
+ TelemetryReporter.ReportSingleEventWithDuration( TelemetryEvents.AddSemanticClassifications, eventProps)
239
+
240
+ addSemanticClassificationByLookup sourceText textSpan classificationDataLookup result
241
+ | ValueNone ->
242
+ let eventProps : ( string * obj ) array =
243
+ [|
244
+ " context.document.project.id" , document.Project.Id.Id.ToString()
245
+ " context.document.id" , document.Id.Id.ToString()
246
+ " isOpenDocument" , isOpenDocument
247
+ " textSpanLength" , textSpan.Length
248
+ " cacheHit" , false
249
+ |]
250
+
251
+ use _eventDuration =
252
+ TelemetryReporter.ReportSingleEventWithDuration( TelemetryEvents.AddSemanticClassifications, eventProps)
253
+
254
+ let! classificationData = document.GetFSharpSemanticClassificationAsync( nameof ( FSharpClassificationService))
255
+
256
+ let classificationDataLookup = toSemanticClassificationLookup classificationData
307
257
do ! unopenedDocumentsSemanticClassificationCache.SetAsync( document, classificationDataLookup)
258
+ addSemanticClassificationByLookup sourceText textSpan classificationDataLookup result
259
+ else
308
260
309
- addSemanticClassification sourceText textSpan classificationData result
310
- }
311
- |> CancellableTask.ifCanceledReturn ()
312
- |> CancellableTask.startAsTask cancellationToken
261
+ match ! openedDocumentsSemanticClassificationCache.TryGetValueAsync document with
262
+ | ValueSome classificationDataLookup ->
263
+ let eventProps : ( string * obj ) array =
264
+ [|
265
+ " context.document.project.id" , document.Project.Id.Id.ToString()
266
+ " context.document.id" , document.Id.Id.ToString()
267
+ " isOpenDocument" , isOpenDocument
268
+ " textSpanLength" , textSpan.Length
269
+ " cacheHit" , true
270
+ |]
271
+
272
+ use _eventDuration =
273
+ TelemetryReporter.ReportSingleEventWithDuration( TelemetryEvents.AddSemanticClassifications, eventProps)
274
+
275
+ addSemanticClassificationByLookup sourceText textSpan classificationDataLookup result
276
+ | ValueNone ->
277
+
278
+ let eventProps : ( string * obj ) array =
279
+ [|
280
+ " context.document.project.id" , document.Project.Id.Id.ToString()
281
+ " context.document.id" , document.Id.Id.ToString()
282
+ " isOpenDocument" , isOpenDocument
283
+ " textSpanLength" , textSpan.Length
284
+ " cacheHit" , false
285
+ |]
286
+
287
+ use _eventDuration =
288
+ TelemetryReporter.ReportSingleEventWithDuration( TelemetryEvents.AddSemanticClassifications, eventProps)
289
+
290
+ let! _ , checkResults = document.GetFSharpParseAndCheckResultsAsync( nameof ( IFSharpClassificationService))
291
+
292
+ let targetRange =
293
+ RoslynHelpers.TextSpanToFSharpRange( document.FilePath, textSpan, sourceText)
294
+
295
+ let classificationData = checkResults.GetSemanticClassification( Some targetRange)
296
+
297
+ if classificationData.Length > 0 then
298
+ let classificationDataLookup = itemToSemanticClassificationLookup classificationData
299
+ do ! unopenedDocumentsSemanticClassificationCache.SetAsync( document, classificationDataLookup)
300
+
301
+ addSemanticClassification sourceText textSpan classificationData result
302
+ }
303
+ |> CancellableTask.ifCanceledReturn ()
304
+ |> CancellableTask.startAsTask cancellationToken
313
305
314
306
// Do not perform classification if we don't have project options (#defines matter)
315
307
member _.AdjustStaleClassification ( _ : SourceText , classifiedSpan : ClassifiedSpan ) : ClassifiedSpan = classifiedSpan
0 commit comments