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

fix out-of-bounds checking for completions #892

Merged
merged 2 commits into from
Mar 12, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/FsAutoComplete.Core/KeywordList.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ module KeywordList =
|> Seq.toArray

let allKeywords : string list =
FSharpKeywords.KeywordsWithDescription
|> List.map fst
keywordDescriptions
|> Seq.map ((|KeyValue|) >> fst)
|> Seq.toList

let keywordCompletionItems =
allKeywords
Expand Down
17 changes: 6 additions & 11 deletions src/FsAutoComplete/FsAutoComplete.Lsp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ open CliWrap.Buffered
open FSharp.Compiler.Tokenization
open FSharp.Compiler.EditorServices
open FSharp.Compiler.Symbols
open FSharp.UMX

module FcsRange = FSharp.Compiler.Text.Range
type FcsRange = FSharp.Compiler.Text.Range
Expand Down Expand Up @@ -1041,18 +1042,12 @@ type FSharpLspServer(backgroundServiceEnabled: bool, state: State, lspClient: FS
commands.TryGetFileCheckerOptionsWithLines file
|> Result.mapError JsonRpc.Error.InternalErrorMessage

let line, col = p.Position.Line, p.Position.Character
let lineStr = lines.GetLineString line

let word =
lineStr.Substring(0, min col lineStr.Length)

do! ensureInBounds lines (line, col)
let lineSegmentLSPRange =
{ Start = { p.Position with Character = 0}; End = p.Position }
let lineSegmentFCSRange = protocolRangeToRange (UMX.untag file) lineSegmentLSPRange
let! lineStr = lines.GetText lineSegmentFCSRange |> Result.mapError JsonRpc.Error.InternalErrorMessage

if (lineStr.StartsWith "#"
&& (KeywordList.hashDirectives.Keys
|> Seq.exists (fun k -> k.StartsWith word)
|| word.Contains "\n")) then
if lineStr.StartsWith "#" then
let completionList =
{ IsIncomplete = false
Items = KeywordList.hashSymbolCompletionItems }
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 @@ -41,6 +41,46 @@ let tests state =
| Error e ->
failtestf "Got an error while retrieving completions: %A" e
})

testCaseAsync "completion at start of line" (async {
let! server, path = server
let completionParams : CompletionParams =
{
TextDocument = { Uri = Path.FilePathToUri path }
Position = { Line = 6; Character = 5 } // the '.' in 'List.'
Context = Some { triggerKind = CompletionTriggerKind.TriggerCharacter; triggerCharacter = Some '.' }
}
let! response = server.TextDocumentCompletion completionParams
match response with
| Ok (Some completions) ->
Expect.equal completions.Items.Length 106 "at time of writing the List module has 106 exposed members"
let firstItem = completions.Items.[0]
Expect.equal firstItem.Label "Empty" "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 at end of line" (async {
let! server, path = server
let completionParams : CompletionParams =
{
TextDocument = { Uri = Path.FilePathToUri path }
Position = { Line = 8; Character = 16 } // the '.' in 'List.'
Context = Some { triggerKind = CompletionTriggerKind.TriggerCharacter; triggerCharacter = Some '.' }
}
let! response = server.TextDocumentCompletion completionParams
match response with
| Ok (Some completions) ->
Expect.equal completions.Items.Length 106 "at time of writing the List module has 106 exposed members"
let firstItem = completions.Items.[0]
Expect.equal firstItem.Label "Empty" "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
})
]

///Tests for getting autocomplete
Expand Down Expand Up @@ -243,7 +283,7 @@ let autoOpenTests state =
return (edit, ns, openPos)
| Ok _ -> return failtest $"Quick fix on `{word}` doesn't contain open action"
}

let test (compareWithQuickFix: bool) (name: string option) (server: Async<FSharpLspServer * string>) (word: string, ns: string) (cursor: Position) (expectedOpen: Position) pending =
let name = name |> Option.defaultWith (fun _ -> sprintf "completion on `Regex` at (%i, %i) should `open System.Text.RegularExpressions` at (%i, %i) (0-based)" (cursor.Line) (cursor.Character) (expectedOpen.Line) (expectedOpen.Character))
let runner = if pending then ptestCaseAsync else testCaseAsync
Expand Down Expand Up @@ -369,7 +409,7 @@ let autoOpenTests state =
do! server.Shutdown()
})
]

let ptestScript name scriptName =
testList name [
let scriptPath = Path.Combine(dirPath, scriptName)
Expand Down
5 changes: 5 additions & 0 deletions test/FsAutoComplete.Tests.Lsp/TestCases/Completion/Script.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ async {
return 1
}
|> Async. // completion at this `.` should not have a billion suggestions


List.

let tail = List.