From 946f19efc25bd36b9d5c01d3b97350f446d4ec39 Mon Sep 17 00:00:00 2001 From: Saul Rennison Date: Fri, 8 Nov 2019 11:48:14 +0000 Subject: [PATCH 1/2] Add empty line before insert 'open' statement --- .../FSharp.Psi.Features/src/Util/OpensUtil.fs | 17 ++++++++++++++--- .../features/completion/Open 01 - First open.fs | 3 +++ .../completion/Open 01 - First open.fs.gold | 6 ++++++ .../completion/Open 02 - Second open.fs | 6 ++++++ .../completion/Open 02 - Second open.fs.gold | 7 +++++++ .../Open 03 - Comment after namespace.fs | 4 ++++ .../Open 03 - Comment after namespace.fs.gold | 7 +++++++ .../completion/Open 04 - Inside module.fs | 6 ++++++ .../completion/Open 04 - Inside module.fs.gold | 9 +++++++++ .../features/completion/Open 05 - Empty file.fs | 2 ++ .../completion/Open 05 - Empty file.fs.gold | 5 +++++ .../src/FSharp.Tests/FSharpCompletionTest.fs | 11 ++++++++++- 12 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 ReSharper.FSharp/test/data/features/completion/Open 01 - First open.fs create mode 100644 ReSharper.FSharp/test/data/features/completion/Open 01 - First open.fs.gold create mode 100644 ReSharper.FSharp/test/data/features/completion/Open 02 - Second open.fs create mode 100644 ReSharper.FSharp/test/data/features/completion/Open 02 - Second open.fs.gold create mode 100644 ReSharper.FSharp/test/data/features/completion/Open 03 - Comment after namespace.fs create mode 100644 ReSharper.FSharp/test/data/features/completion/Open 03 - Comment after namespace.fs.gold create mode 100644 ReSharper.FSharp/test/data/features/completion/Open 04 - Inside module.fs create mode 100644 ReSharper.FSharp/test/data/features/completion/Open 04 - Inside module.fs.gold create mode 100644 ReSharper.FSharp/test/data/features/completion/Open 05 - Empty file.fs create mode 100644 ReSharper.FSharp/test/data/features/completion/Open 05 - Empty file.fs.gold diff --git a/ReSharper.FSharp/src/FSharp.Psi.Features/src/Util/OpensUtil.fs b/ReSharper.FSharp/src/FSharp.Psi.Features/src/Util/OpensUtil.fs index a678f82342..ca2ab0e9c5 100644 --- a/ReSharper.FSharp/src/FSharp.Psi.Features/src/Util/OpensUtil.fs +++ b/ReSharper.FSharp/src/FSharp.Psi.Features/src/Util/OpensUtil.fs @@ -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) diff --git a/ReSharper.FSharp/test/data/features/completion/Open 01 - First open.fs b/ReSharper.FSharp/test/data/features/completion/Open 01 - First open.fs new file mode 100644 index 0000000000..6835cc71df --- /dev/null +++ b/ReSharper.FSharp/test/data/features/completion/Open 01 - First open.fs @@ -0,0 +1,3 @@ +// ${COMPLETE_ITEM:IDisposable (in System)} +namespace SomeNamespace +{caret} diff --git a/ReSharper.FSharp/test/data/features/completion/Open 01 - First open.fs.gold b/ReSharper.FSharp/test/data/features/completion/Open 01 - First open.fs.gold new file mode 100644 index 0000000000..ca3bd0c34c --- /dev/null +++ b/ReSharper.FSharp/test/data/features/completion/Open 01 - First open.fs.gold @@ -0,0 +1,6 @@ +// ${COMPLETE_ITEM:IDisposable (in System)} +namespace SomeNamespace + +open System + +IDisposable{caret} diff --git a/ReSharper.FSharp/test/data/features/completion/Open 02 - Second open.fs b/ReSharper.FSharp/test/data/features/completion/Open 02 - Second open.fs new file mode 100644 index 0000000000..ea24fe1cd1 --- /dev/null +++ b/ReSharper.FSharp/test/data/features/completion/Open 02 - Second open.fs @@ -0,0 +1,6 @@ +// ${COMPLETE_ITEM:Task (in System.Threading.Tasks)} +namespace SomeNamespace + +open System + +{caret} diff --git a/ReSharper.FSharp/test/data/features/completion/Open 02 - Second open.fs.gold b/ReSharper.FSharp/test/data/features/completion/Open 02 - Second open.fs.gold new file mode 100644 index 0000000000..260abdb8f9 --- /dev/null +++ b/ReSharper.FSharp/test/data/features/completion/Open 02 - Second open.fs.gold @@ -0,0 +1,7 @@ +// ${COMPLETE_ITEM:Task (in System.Threading.Tasks)} +namespace SomeNamespace + +open System +open System.Threading.Tasks + +Task{caret} diff --git a/ReSharper.FSharp/test/data/features/completion/Open 03 - Comment after namespace.fs b/ReSharper.FSharp/test/data/features/completion/Open 03 - Comment after namespace.fs new file mode 100644 index 0000000000..e076d0ec0d --- /dev/null +++ b/ReSharper.FSharp/test/data/features/completion/Open 03 - Comment after namespace.fs @@ -0,0 +1,4 @@ +// ${COMPLETE_ITEM:IDisposable (in System)} +namespace SomeNamespace +// Some comment here +{caret} diff --git a/ReSharper.FSharp/test/data/features/completion/Open 03 - Comment after namespace.fs.gold b/ReSharper.FSharp/test/data/features/completion/Open 03 - Comment after namespace.fs.gold new file mode 100644 index 0000000000..1b21cfd780 --- /dev/null +++ b/ReSharper.FSharp/test/data/features/completion/Open 03 - Comment after namespace.fs.gold @@ -0,0 +1,7 @@ +// ${COMPLETE_ITEM:IDisposable (in System)} +namespace SomeNamespace + +open System + +// Some comment here +IDisposable{caret} diff --git a/ReSharper.FSharp/test/data/features/completion/Open 04 - Inside module.fs b/ReSharper.FSharp/test/data/features/completion/Open 04 - Inside module.fs new file mode 100644 index 0000000000..701ec4b8c9 --- /dev/null +++ b/ReSharper.FSharp/test/data/features/completion/Open 04 - Inside module.fs @@ -0,0 +1,6 @@ +// ${COMPLETE_ITEM:IDisposable (in System)} +namespace SomeNamespace + +module Module = + let foo () = + {caret} diff --git a/ReSharper.FSharp/test/data/features/completion/Open 04 - Inside module.fs.gold b/ReSharper.FSharp/test/data/features/completion/Open 04 - Inside module.fs.gold new file mode 100644 index 0000000000..2a67edf032 --- /dev/null +++ b/ReSharper.FSharp/test/data/features/completion/Open 04 - Inside module.fs.gold @@ -0,0 +1,9 @@ +// ${COMPLETE_ITEM:IDisposable (in System)} +namespace SomeNamespace + +module Module = + + open System + + let foo () = + IDisposable{caret} diff --git a/ReSharper.FSharp/test/data/features/completion/Open 05 - Empty file.fs b/ReSharper.FSharp/test/data/features/completion/Open 05 - Empty file.fs new file mode 100644 index 0000000000..13a9cb25a9 --- /dev/null +++ b/ReSharper.FSharp/test/data/features/completion/Open 05 - Empty file.fs @@ -0,0 +1,2 @@ +// ${COMPLETE_ITEM:IDisposable (in System)} +{caret} diff --git a/ReSharper.FSharp/test/data/features/completion/Open 05 - Empty file.fs.gold b/ReSharper.FSharp/test/data/features/completion/Open 05 - Empty file.fs.gold new file mode 100644 index 0000000000..23b8425c37 --- /dev/null +++ b/ReSharper.FSharp/test/data/features/completion/Open 05 - Empty file.fs.gold @@ -0,0 +1,5 @@ +// ${COMPLETE_ITEM:IDisposable (in System)} + +open System + +IDisposable{caret} diff --git a/ReSharper.FSharp/test/src/FSharp.Tests/FSharpCompletionTest.fs b/ReSharper.FSharp/test/src/FSharp.Tests/FSharpCompletionTest.fs index 1f6db1e9f9..7443f77fe1 100644 --- a/ReSharper.FSharp/test/src/FSharp.Tests/FSharpCompletionTest.fs +++ b/ReSharper.FSharp/test/src/FSharp.Tests/FSharpCompletionTest.fs @@ -1,10 +1,12 @@ namespace JetBrains.ReSharper.Plugins.FSharp.Tests.Features open JetBrains.ReSharper.FeaturesTestFramework.Completion +open JetBrains.ReSharper.Plugins.FSharp.Checker.Settings open JetBrains.ReSharper.Plugins.FSharp.Tests.Common +open JetBrains.ReSharper.TestFramework open NUnit.Framework -[] +[)>] type FSharpCompletionTest() = inherit CodeCompletionTestBase() @@ -21,3 +23,10 @@ type FSharpCompletionTest() = [] member x.``Wild 02 - Insert``() = x.DoNamedTest() [] member x.``Wild 03 - Replace before``() = x.DoNamedTest() [] member x.``Wild 04 - Insert before``() = x.DoNamedTest() + + [] member x.``Open 01 - First open``() = x.DoNamedTest() + [] member x.``Open 02 - Second open``() = x.DoNamedTest() + [] member x.``Open 03 - Comment after namespace``() = x.DoNamedTest() + + [] + [] member x.``Open 04 - Inside module``() = x.DoNamedTest() From 8e259f01e2fcd764e90d467e33192ef4819c8898 Mon Sep 17 00:00:00 2001 From: Saul Rennison Date: Fri, 8 Nov 2019 18:21:49 +0000 Subject: [PATCH 2/2] Fix build error --- ReSharper.FSharp/test/src/FSharp.Tests/FSharpCompletionTest.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ReSharper.FSharp/test/src/FSharp.Tests/FSharpCompletionTest.fs b/ReSharper.FSharp/test/src/FSharp.Tests/FSharpCompletionTest.fs index 7443f77fe1..1bed43af88 100644 --- a/ReSharper.FSharp/test/src/FSharp.Tests/FSharpCompletionTest.fs +++ b/ReSharper.FSharp/test/src/FSharp.Tests/FSharpCompletionTest.fs @@ -1,7 +1,7 @@ namespace JetBrains.ReSharper.Plugins.FSharp.Tests.Features open JetBrains.ReSharper.FeaturesTestFramework.Completion -open JetBrains.ReSharper.Plugins.FSharp.Checker.Settings +open JetBrains.ReSharper.Plugins.FSharp.Settings open JetBrains.ReSharper.Plugins.FSharp.Tests.Common open JetBrains.ReSharper.TestFramework open NUnit.Framework