diff --git a/vsintegration/src/FSharp.Editor/Classification/ClassificationService.fs b/vsintegration/src/FSharp.Editor/Classification/ClassificationService.fs index 889ebec9718..afdb990489b 100644 --- a/vsintegration/src/FSharp.Editor/Classification/ClassificationService.fs +++ b/vsintegration/src/FSharp.Editor/Classification/ClassificationService.fs @@ -295,6 +295,7 @@ type internal FSharpClassificationService [] () = addSemanticClassification sourceText textSpan classificationData result } + |> CancellableTask.ifCanceledReturn () |> CancellableTask.startAsTask cancellationToken // Do not perform classification if we don't have project options (#defines matter) diff --git a/vsintegration/src/FSharp.Editor/Common/CancellableTasks.fs b/vsintegration/src/FSharp.Editor/Common/CancellableTasks.fs index 99eb3946bba..78a28eb0de2 100644 --- a/vsintegration/src/FSharp.Editor/Common/CancellableTasks.fs +++ b/vsintegration/src/FSharp.Editor/Common/CancellableTasks.fs @@ -19,7 +19,7 @@ // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - + namespace Microsoft.VisualStudio.FSharp.Editor // Don't warn about the resumable code invocation @@ -467,8 +467,8 @@ module CancellableTasks = // Low priority extensions type CancellableTaskBuilderBase with - [] - member inline _.Source(awaiter: CancellableTask) = + [] + member inline _.Source(awaiter: CancellableTask) = (fun (token) -> (awaiter token :> Task).GetAwaiter()) /// @@ -611,10 +611,10 @@ module CancellableTasks = fun sm -> if __useResumableCode then sm.Data.ThrowIfCancellationRequested() - + let mutable awaiter = getAwaiter let mutable __stack_fin = true - + if not (Awaiter.isCompleted awaiter) then let __stack_yield_fin = ResumableCode.Yield().Invoke(&sm) __stack_fin <- __stack_yield_fin @@ -706,7 +706,7 @@ module CancellableTasks = (task: 'Awaitable) : 'Awaiter = Awaitable.getAwaiter task - + /// Allows the computation expression to turn other types into CancellationToken -> 'Awaiter /// @@ -1119,6 +1119,17 @@ module CancellableTasks = let inline ignore ([] ctask: CancellableTask<_>) = toUnit ctask + /// If this CancellableTask gets canceled for another reason than the token being canceled, return the specified value. + let inline ifCanceledReturn value (ctask : CancellableTask<_>) = + cancellableTask { + let! ct = getCancellationToken () + + try + return! ctask + with :? OperationCanceledException when ct.IsCancellationRequested = false -> + return value + } + /// [] module MergeSourcesExtensions = diff --git a/vsintegration/src/FSharp.Editor/Structure/BlockStructureService.fs b/vsintegration/src/FSharp.Editor/Structure/BlockStructureService.fs index f96ea848c1f..d0326d84311 100644 --- a/vsintegration/src/FSharp.Editor/Structure/BlockStructureService.fs +++ b/vsintegration/src/FSharp.Editor/Structure/BlockStructureService.fs @@ -156,6 +156,8 @@ open CancellableTasks [)>] type internal FSharpBlockStructureService [] () = + let emptyValue = FSharpBlockStructure ImmutableArray.empty + interface IFSharpBlockStructureService with member _.GetBlockStructureAsync(document, cancellationToken) : Task = @@ -171,4 +173,5 @@ type internal FSharpBlockStructureService [] () = |> Seq.toImmutableArray |> FSharpBlockStructure } + |> CancellableTask.ifCanceledReturn emptyValue |> CancellableTask.start cancellationToken