Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add empty line if necessary before 'open' statements #65

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}
11 changes: 10 additions & 1 deletion ReSharper.FSharp/test/src/FSharp.Tests/FSharpCompletionTest.fs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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>]
[<FSharpTest; TestSettingsKey(typeof<FSharpOptions>)>]
type FSharpCompletionTest() =
inherit CodeCompletionTestBase()

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()

[<TestSettings("{ TopLevelOpenCompletion: false }")>]
[<Test>] member x.``Open 04 - Inside module``() = x.DoNamedTest()