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

Simple completion in type hints/definitions #908

Merged
merged 3 commits into from
Apr 3, 2022
Merged
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
16 changes: 10 additions & 6 deletions src/FsAutoComplete.Core/ParseAndCheckResults.fs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ type ParseAndCheckResults
member x.TryGetToolTipEnhanced (pos: Position) (lineStr: LineStr) =
match Completion.atPos (pos, x.GetParseResults.ParseTree) with
| Completion.Context.StringLiteral -> Ok None
| Completion.Context.SynType
| Completion.Context.Unknown ->
match Lexer.findLongIdents (pos.Column, lineStr) with
| None -> Error "Cannot find ident for tooltip"
Expand Down Expand Up @@ -551,9 +552,11 @@ type ParseAndCheckResults

member x.TryGetCompletions (pos: Position) (lineStr: LineStr) filter (getAllSymbols: unit -> AssemblySymbol list) =
async {
match Completion.atPos (pos, x.GetParseResults.ParseTree) with
let completionContext = Completion.atPos (pos, x.GetParseResults.ParseTree)
match completionContext with
| Completion.Context.StringLiteral -> return None
| Completion.Context.Unknown ->
| Completion.Context.Unknown
| Completion.Context.SynType ->
try
let longName =
QuickParse.GetPartialLongNameEx(lineStr, pos.Column - 1)
Expand All @@ -565,10 +568,12 @@ type ParseAndCheckResults
>> Log.addContextDestructured "longName" longName
)

let getAllSymbols () =
let getSymbols () =
getAllSymbols ()
|> List.filter (fun entity ->
entity.FullName.Contains "."
// Attempt to filter to types when we know we're in a type and FCS uses all symbols
(completionContext <> Completion.Context.SynType || entity.Kind LookupType.Fuzzy = EntityKind.Type)
&& entity.FullName.Contains "."
&& not (PrettyNaming.IsOperatorDisplayName entity.Symbol.DisplayName))

let token = Lexer.getSymbol pos.Line (pos.Column - 1) lineStr SymbolLookupKind.Simple [||]
Expand All @@ -590,8 +595,7 @@ type ParseAndCheckResults
| _ ->

let results =
checkResults.GetDeclarationListInfo(Some parseResults, pos.Line, lineStr, longName, getAllSymbols)

checkResults.GetDeclarationListInfo(Some parseResults, pos.Line, lineStr, longName, getSymbols)
let getKindPriority =
function
| CompletionItemKind.CustomOperation -> -1
Expand Down
3 changes: 3 additions & 0 deletions src/FsAutoComplete.Core/UntypedAstUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,7 @@ module Completion =
type Context =
| StringLiteral
| Unknown
| SynType

let atPos (pos: Position, ast: ParsedInput): Context =
let visitor =
Expand All @@ -1930,6 +1931,8 @@ module Completion =
)
| _ -> defaultTraverse expr
else None

member x.VisitType(path, defaultTraverse, synType) : Context option = Some Context.SynType
}
SyntaxTraversal.Traverse(pos, ast, visitor)
|> Option.defaultValue Context.Unknown
33 changes: 33 additions & 0 deletions test/FsAutoComplete.Tests.Lsp/CompletionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,39 @@ let tests state =
"first member should be List.Empty, since properties are preferred over functions"
| Ok None -> failtest "Should have gotten some completion items"
| Error e -> failtestf "Got an error while retrieving completions: %A" e
})
testCaseAsync
"completion in record defn field type"
(async {
let! server, path = server

let completionParams: CompletionParams =
{ TextDocument = { Uri = Path.FilePathToUri path }
Position = { Line = 13; Character = 10 } // after Lis partial type name in Id record field declaration
Context =
Some
{ triggerKind = CompletionTriggerKind.Invoked
triggerCharacter = None } }

let! response = server.TextDocumentCompletion completionParams

match response with
| Ok (Some completions) ->
Expect.isLessThan
completions.Items.Length
300
"shouldn't have a very long list of completion items that are only types"
Expect.isGreaterThan
completions.Items.Length
100
"should have a reasonable number of completion items that are only types"

Expect.exists
completions.Items
(fun item -> item.Label = "list")
"completion should contain the list type"
| Ok None -> failtest "Should have gotten some completion items"
| Error e -> failtestf "Got an error while retrieving completions: %A" e
}) ]

///Tests for getting autocomplete
Expand Down
4 changes: 4 additions & 0 deletions test/FsAutoComplete.Tests.Lsp/TestCases/Completion/Script.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ async {
List.

let tail = List.

type A = {
Id : Lis
}