Skip to content

Commit

Permalink
fix: wrong "open" completion position with namespace
Browse files Browse the repository at this point in the history
Currently, "open" are sometimes inserted before namespace which is invalid + fix "Completion.AutoOpen" tests
  • Loading branch information
MrLuje committed May 23, 2024
1 parent 3f31b94 commit 58313e2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/FsAutoComplete.Core/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ module Commands =
// this only happens when there are no other `open`

// from insert position go up until first open OR namespace
ic.Pos.LinesToBeginning()
ic.Pos.IncLine().LinesToBeginning()
|> Seq.tryFind (fun l ->
let lineStr = getLine l
// namespace MUST be top level -> no indentation
Expand Down
8 changes: 7 additions & 1 deletion src/FsAutoComplete/LspServers/AdaptiveFSharpLspServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -717,10 +717,16 @@ type AdaptiveFSharpLspServer
{ (fcsPos |> fcsPosToLsp) with
Character = 0 }

let displayText =
match config.ExternalAutocomplete, ci.Label.Split(" (open ") with
| true, [| label; _ |] -> label
| true, [| label |] -> label
| _, _ -> ci.Label

Some
[| { TextEdit.NewText = text
TextEdit.Range = { Start = insertPos; End = insertPos } } |],
$"{ci.Label} (open {ns})"
$"{displayText} (open {ns})"

let d = Documentation.Markup(markdown comment)

Expand Down
11 changes: 6 additions & 5 deletions test/FsAutoComplete.Tests.Lsp/CompletionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,8 @@ let autoOpenTests state =
| Ok None -> failtest "Request none"
| Ok(Some res) ->
Expect.isFalse res.IsIncomplete "Result is incomplete"
let ci = res.Items |> Array.tryFind (fun c -> c.Label = word)
// with ExternalAutoComplete, completions are like "Regex (open System.Text.RegularExpressions)"
let ci = res.Items |> Array.tryFind (fun c -> c.Label.StartsWith word)

if ci = None then
failwithf
Expand Down Expand Up @@ -934,7 +935,7 @@ let autoOpenTests state =

yield! tests ]

let ptestScript name scriptName =
let _ptestScript name scriptName =
testList
name
[ let scriptPath = Path.Combine(dirPath, scriptName)
Expand All @@ -947,16 +948,16 @@ let autoOpenTests state =

yield! tests ]

ptestList
testList
"Completion.AutoOpen"
[
// NOTE: Positions are ZERO-based!: { Line = 3; Character = 9 } -> Line 4, Column 10 in editor display
testScript "with root module with new line" "ModuleWithNewLine.fsx"
testScript "with root module" "Module.fsx"
testScript "with root module with open" "ModuleWithOpen.fsx"
testScript "with root module with open and new line" "ModuleWithOpenAndNewLine.fsx"
ptestScript "with namespace with new line" "NamespaceWithNewLine.fsx"
ptestScript "with namespace" "Namespace.fsx"
testScript "with namespace with new line" "NamespaceWithNewLine.fsx"
testScript "with namespace" "Namespace.fsx"
testScript "with namespace with open" "NamespaceWithOpen.fsx"
testScript "with namespace with open and new line" "NamespaceWithOpenAndNewLine.fsx"
testScript "with implicit top level module with new line" "ImplicitTopLevelModuleWithNewLine.fsx"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ module Nested1 =

namespace OpenNamespace.OtherNamespace
type T = {
Value: Regex(*-1,|-2*)
Value: Regex(*-1,|-4*)
}
with
member _.Foo () =
Regex(*-5,|-4*)
Regex(*-5,|-6*)
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module Nested1 =
namespace OpenNamespace.OtherNamespace

type T = {
Value: Regex(*-2,|-2*)
Value: Regex(*-2,|-4*)
}
with
member _.Foo () =
Regex(*-6,|-4*)
Regex(*-6,|-6*)

0 comments on commit 58313e2

Please sign in to comment.