Skip to content

Commit

Permalink
Fix completion on first character (#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
tboby authored Apr 4, 2022
1 parent fca7b16 commit e837dfc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/FsAutoComplete.Core/Lexer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ type LexerSymbol =
[<RequireQualifiedAccess>]
type SymbolLookupKind =
| Fuzzy
| ByRightColumn
| ByLongIdent
| Simple
| ForCompletion

type private DraftToken =
{ Kind: SymbolKind
Expand Down Expand Up @@ -135,8 +135,8 @@ module Lexer =
match lookupKind with
| SymbolLookupKind.Simple | SymbolLookupKind.Fuzzy ->
tokens |> List.filter (fun x -> x.Token.LeftColumn <= col && x.RightColumn + 1 >= col)
| SymbolLookupKind.ByRightColumn ->
tokens |> List.filter (fun x -> x.RightColumn = col)
| SymbolLookupKind.ForCompletion ->
tokens |> List.filter (fun x -> x.Token.LeftColumn <= col && x.RightColumn >= col)
| SymbolLookupKind.ByLongIdent ->
tokens |> List.filter (fun x -> x.Token.LeftColumn <= col)

Expand Down Expand Up @@ -172,8 +172,7 @@ module Lexer =
LeftColumn = leftCol
RightColumn = first.RightColumn + 1
Text = lineStr.[leftCol..first.RightColumn] })
| SymbolLookupKind.Fuzzy
| SymbolLookupKind.ByRightColumn ->
| SymbolLookupKind.Fuzzy ->
// Select IDENT token. If failed, select OPERATOR token.
tokensUnderCursor
|> List.tryFind (fun { DraftToken.Kind = k } ->
Expand All @@ -189,6 +188,7 @@ module Lexer =
LeftColumn = token.Token.LeftColumn
RightColumn = token.RightColumn + 1
Text = lineStr.Substring(token.Token.LeftColumn, token.Token.FullMatchedLength) })
| SymbolLookupKind.ForCompletion
| SymbolLookupKind.Simple ->
tokensUnderCursor
|> List.tryLast
Expand Down
2 changes: 1 addition & 1 deletion src/FsAutoComplete.Core/ParseAndCheckResults.fs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ type ParseAndCheckResults
&& entity.FullName.Contains "."
&& not (PrettyNaming.IsOperatorDisplayName entity.Symbol.DisplayName))

let token = Lexer.getSymbol pos.Line (pos.Column - 1) lineStr SymbolLookupKind.Simple [||]
let token = Lexer.getSymbol pos.Line (pos.Column - 1) lineStr SymbolLookupKind.ForCompletion [||]

logger.info (
Log.setMessage "TryGetCompletions - token: {token}"
Expand Down
44 changes: 42 additions & 2 deletions test/FsAutoComplete.Tests.Lsp/CompletionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let tests state =

let completionParams: CompletionParams =
{ TextDocument = { Uri = Path.FilePathToUri path }
Position = { Line = 3; Character = 10 } // the '.' in 'Async.'
Position = { Line = 3; Character = 9 } // the '.' in 'Async.'
Context =
Some
{ triggerKind = CompletionTriggerKind.TriggerCharacter
Expand Down Expand Up @@ -146,7 +146,47 @@ let tests state =
"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
}) ]
})

testCaseAsync "completion before first character of expression" (async {
let! server, path = server
let completionParams : CompletionParams =
{
TextDocument = { Uri = Path.FilePathToUri path }
Position = { Line = 8; Character = 12 } // after the 'L' in 'List.'
Context = Some { triggerKind = CompletionTriggerKind.Invoked; triggerCharacter = None }
}
let! response = server.TextDocumentCompletion completionParams
match response with
| Ok (Some completions) ->
Expect.isGreaterThan completions.Items.Length 100 "should have a very long list of all symbols"
let firstItem = completions.Items.[0]
Expect.equal firstItem.Label "async" "first member should be async, alphabetically first in the full symbol list"
| Ok None ->
failtest "Should have gotten some completion items"
| Error e ->
failtestf "Got an error while retrieving completions: %A" e
})

testCaseAsync "completion after first character of expression" (async {
let! server, path = server
let completionParams : CompletionParams =
{
TextDocument = { Uri = Path.FilePathToUri path }
Position = { Line = 8; Character = 11 } // before the 'L' in 'List.'
Context = Some { triggerKind = CompletionTriggerKind.Invoked; triggerCharacter = None }
}
let! response = server.TextDocumentCompletion completionParams
match response with
| Ok (Some completions) ->
Expect.isGreaterThan completions.Items.Length 100 "should have a very long list of all symbols"
let firstItem = completions.Items.[0]
Expect.equal firstItem.Label "async" "first member should be async, alphabetically first in the full symbol list"
| Ok None ->
failtest "Should have gotten some completion items"
| Error e ->
failtestf "Got an error while retrieving completions: %A" e
}) ]

///Tests for getting autocomplete
let autocompleteTest state =
Expand Down

0 comments on commit e837dfc

Please sign in to comment.