Skip to content

Commit

Permalink
Make matched brace highlighting work exactly as in C# editor (dotnet#…
Browse files Browse the repository at this point in the history
…5049)

* make matched brace highlighting work exactly as in C# editor

* fix tests

* Add optional param for formatting

* revert matching braces logic in FSharpEditorFormattingService

* add missing test attributes

* Revert "revert matching braces logic in FSharpEditorFormattingService"

This reverts commit 5ddaba3.
  • Loading branch information
vasily-kirichenko authored and brettfo committed Jun 5, 2018
1 parent 6542cca commit 05d3884
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
14 changes: 8 additions & 6 deletions Formatting/BraceMatchingService.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Microsoft.VisualStudio.FSharp.Editor

open System
open System.ComponentModel.Composition
open Microsoft.CodeAnalysis.Editor
open Microsoft.FSharp.Compiler.SourceCodeServices
open System.Runtime.InteropServices

[<ExportBraceMatcher(FSharpConstants.FSharpLanguageName)>]
type internal FSharpBraceMatchingService
Expand All @@ -14,19 +14,21 @@ 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, position: int, userOpName: string, [<Optional; DefaultParameterValue(false)>] forFormatting: bool) =
async {
let! matchedBraces = checker.MatchBraces(fileName, sourceText.ToString(), parsingOptions, userOpName)
let isPositionInRange range =
match RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, range) with
| None -> false
| Some range ->
let length = position - range.Start
length >= 0 && length <= range.Length
| Some span ->
if forFormatting then
let length = position - span.Start
length >= 0 && length <= span.Length
else
span.Contains position
return matchedBraces |> Array.tryFind(fun (left, right) -> isPositionInRange left || isPositionInRange right)
}

Expand Down
2 changes: 1 addition & 1 deletion Formatting/EditorFormattingService.fs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type internal FSharpEditorFormattingService
x.Tag <> FSharpTokenTag.LINE_COMMENT)

let! (left, right) =
FSharpBraceMatchingService.GetBraceMatchingResult(checker, sourceText, filePath, parsingOptions, position, "FormattingService")
FSharpBraceMatchingService.GetBraceMatchingResult(checker, sourceText, filePath, parsingOptions, position, "FormattingService", forFormatting=true)

if right.StartColumn = firstMeaningfulToken.LeftColumn then
// Replace the indentation on this line with the indentation of the left bracket
Expand Down

0 comments on commit 05d3884

Please sign in to comment.