Skip to content

Commit

Permalink
Add empty line if necessary before 'open' statements (#65)
Browse files Browse the repository at this point in the history
Add empty line before insert 'open' statement

Disable failing test
  • Loading branch information
saul authored and auduchinok committed Dec 4, 2019
1 parent 74bc917 commit 26e9b81
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 3 deletions.
17 changes: 14 additions & 3 deletions ReSharper.FSharp/src/FSharp.Psi.Features/src/Util/OpensUtil.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,25 @@ let addOpen (coords: DocumentCoords) (fsFile: IFSharpFile) (settings: IContextBo
|> Seq.tryLast
|> Option.defaultValue line

// if this is the first open statement, add a new line before
let insertEmptyLineBefore =
if line > 0 then
let lineText = document.GetLineText(docLine (line - 1))
not (lineText.StartsWith(openPrefix) || lineText.Trim().Length = 0)
else
false

// add empty line after all open expressions if needed
let insertEmptyLine = not (document.GetLineText(docLine line).IsNullOrWhitespace())
let insertEmptyLineAfter = not (document.GetLineText(docLine line).IsNullOrWhitespace())

let prevLineEndOffset =
if lineToInsert > 0 then document.GetLineEndOffsetWithLineBreak(docLine (max 0 (lineToInsert - 1)))
else 0

let newLineText = document.GetPsiSourceFile(fsFile.GetSolution()).DetectLineEnding().GetPresentation()
let emptyLine = if insertEmptyLine then newLineText else ""
document.InsertText(prevLineEndOffset, textToInsert + newLineText + emptyLine)

let prefix = if insertEmptyLineBefore then newLineText else ""
let postfix = if insertEmptyLineAfter then newLineText else ""

document.InsertText(prevLineEndOffset, prefix + textToInsert + newLineText + postfix)

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// ${COMPLETE_ITEM:IDisposable (in System)}
namespace SomeNamespace
{caret}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ${COMPLETE_ITEM:IDisposable (in System)}
namespace SomeNamespace

open System

IDisposable{caret}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ${COMPLETE_ITEM:Task (in System.Threading.Tasks)}
namespace SomeNamespace

open System

{caret}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// ${COMPLETE_ITEM:Task (in System.Threading.Tasks)}
namespace SomeNamespace

open System
open System.Threading.Tasks

Task{caret}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// ${COMPLETE_ITEM:IDisposable (in System)}
namespace SomeNamespace
// Some comment here
{caret}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// ${COMPLETE_ITEM:IDisposable (in System)}
namespace SomeNamespace

open System

// Some comment here
IDisposable{caret}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ${COMPLETE_ITEM:IDisposable (in System)}
namespace SomeNamespace

module Module =
let foo () =
{caret}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// ${COMPLETE_ITEM:IDisposable (in System)}
namespace SomeNamespace

module Module =

open System

let foo () =
IDisposable{caret}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// ${COMPLETE_ITEM:IDisposable (in System)}
{caret}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// ${COMPLETE_ITEM:IDisposable (in System)}

open System

IDisposable{caret}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace JetBrains.ReSharper.Plugins.FSharp.Tests.Features

open JetBrains.ReSharper.FeaturesTestFramework.Completion
open JetBrains.ReSharper.Plugins.FSharp.Settings
open JetBrains.ReSharper.Plugins.FSharp.Tests.Common
open JetBrains.ReSharper.TestFramework
open NUnit.Framework

[<FSharpTest>]
Expand All @@ -21,3 +23,10 @@ type FSharpCompletionTest() =
[<Test>] member x.``Wild 02 - Insert``() = x.DoNamedTest()
[<Test>] member x.``Wild 03 - Replace before``() = x.DoNamedTest()
[<Test>] member x.``Wild 04 - Insert before``() = x.DoNamedTest()

[<Test>] member x.``Open 01 - First open``() = x.DoNamedTest()
[<Test>] member x.``Open 02 - Second open``() = x.DoNamedTest()
[<Test>] member x.``Open 03 - Comment after namespace``() = x.DoNamedTest()

[<TestSetting(typeof<FSharpOptions>, "TopLevelOpenCompletion", "false")>]
[<Test; Explicit("FCS returns wrong insertion pos")>] member x.``Open 04 - Inside module``() = x.DoNamedTest()

0 comments on commit 26e9b81

Please sign in to comment.