From aeefaaf87ffbf3df8266cf6bf0181ff7f01f50ec Mon Sep 17 00:00:00 2001 From: cartermp Date: Mon, 30 Oct 2017 18:21:50 -0700 Subject: [PATCH 1/2] Change brace matching to account for when the caret is at the end of the end of a brace --- .../Formatting/BraceMatchingService.fs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/vsintegration/src/FSharp.Editor/Formatting/BraceMatchingService.fs b/vsintegration/src/FSharp.Editor/Formatting/BraceMatchingService.fs index 7b795f0cc82..6d5f53b13e7 100644 --- a/vsintegration/src/FSharp.Editor/Formatting/BraceMatchingService.fs +++ b/vsintegration/src/FSharp.Editor/Formatting/BraceMatchingService.fs @@ -2,7 +2,6 @@ namespace Microsoft.VisualStudio.FSharp.Editor -open System open System.ComponentModel.Composition open Microsoft.CodeAnalysis.Editor open Microsoft.FSharp.Compiler.SourceCodeServices @@ -14,28 +13,33 @@ type internal FSharpBraceMatchingService checkerProvider: FSharpCheckerProvider, projectInfoManager: FSharpProjectOptionsManager ) = - static let defaultUserOpName = "BraceMatching" - static member GetBraceMatchingResult(checker: FSharpChecker, sourceText, fileName, parsingOptions: FSharpParsingOptions, position: int, userOpName: string) = + static member GetBraceMatchingResult(checker: FSharpChecker, sourceText, fileName, parsingOptions: FSharpParsingOptions, caretPosition: int, userOpName: string) = async { let! matchedBraces = checker.MatchBraces(fileName, sourceText.ToString(), parsingOptions, userOpName) - let isPositionInRange range = + let isPositionInRange range = match RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, range) with | None -> false | Some range -> - let length = position - range.Start - length >= 0 && length <= range.Length + // We need to see if the caret is contained within or on the outside of the span + // that we get back from the language service. + // + // Ex: let x = ((12))^ + // + // The caret can be on the outside of the last paren, but this is actually 1 position + // further to the right than the end of the span that we get back. + range.Contains(caretPosition) || range.End = caretPosition + 1 return matchedBraces |> Array.tryFind(fun (left, right) -> isPositionInRange left || isPositionInRange right) } interface IBraceMatcher with - member this.FindBracesAsync(document, position, cancellationToken) = + member __.FindBracesAsync(document, caretPosition, cancellationToken) = asyncMaybe { let! parsingOptions, _options = projectInfoManager.TryGetOptionsForEditingDocumentOrProject(document) let! sourceText = document.GetTextAsync(cancellationToken) - let! (left, right) = FSharpBraceMatchingService.GetBraceMatchingResult(checkerProvider.Checker, sourceText, document.Name, parsingOptions, position, defaultUserOpName) + let! (left, right) = FSharpBraceMatchingService.GetBraceMatchingResult(checkerProvider.Checker, sourceText, document.Name, parsingOptions, caretPosition, defaultUserOpName) let! leftSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, left) let! rightSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, right) return BraceMatchingResult(leftSpan, rightSpan) From c4d0f51cbcbc80db7fc9bcdf8355942e82c654ef Mon Sep 17 00:00:00 2001 From: cartermp Date: Tue, 31 Oct 2017 10:21:07 -0700 Subject: [PATCH 2/2] Just use span.Contains --- .../FSharp.Editor/Formatting/BraceMatchingService.fs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/vsintegration/src/FSharp.Editor/Formatting/BraceMatchingService.fs b/vsintegration/src/FSharp.Editor/Formatting/BraceMatchingService.fs index 6d5f53b13e7..9244f8619ad 100644 --- a/vsintegration/src/FSharp.Editor/Formatting/BraceMatchingService.fs +++ b/vsintegration/src/FSharp.Editor/Formatting/BraceMatchingService.fs @@ -22,22 +22,14 @@ type internal FSharpBraceMatchingService let isPositionInRange range = match RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, range) with | None -> false - | Some range -> - // We need to see if the caret is contained within or on the outside of the span - // that we get back from the language service. - // - // Ex: let x = ((12))^ - // - // The caret can be on the outside of the last paren, but this is actually 1 position - // further to the right than the end of the span that we get back. - range.Contains(caretPosition) || range.End = caretPosition + 1 + | Some span -> span.Contains(caretPosition) return matchedBraces |> Array.tryFind(fun (left, right) -> isPositionInRange left || isPositionInRange right) } interface IBraceMatcher with member __.FindBracesAsync(document, caretPosition, cancellationToken) = asyncMaybe { - let! parsingOptions, _options = projectInfoManager.TryGetOptionsForEditingDocumentOrProject(document) + let! parsingOptions, _ = projectInfoManager.TryGetOptionsForEditingDocumentOrProject(document) let! sourceText = document.GetTextAsync(cancellationToken) let! (left, right) = FSharpBraceMatchingService.GetBraceMatchingResult(checkerProvider.Checker, sourceText, document.Name, parsingOptions, caretPosition, defaultUserOpName) let! leftSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, left)