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

Address several (most?) cases of correctly triggering F# function application signature help in some nested contexts #10961

Merged
merged 7 commits into from
Feb 5, 2021
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
72 changes: 62 additions & 10 deletions src/fsharp/service/ServiceUntypedParse.fs
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,22 @@ type FSharpParseFileResults(errors: FSharpDiagnostic[], input: ParsedInput optio
match expr with
| SynExpr.Ident ident -> Some ident.idRange

| SynExpr.LongIdent(_, _, _, range) -> Some range
| SynExpr.LongIdent (_, _, _, range) -> Some range

| SynExpr.Paren(expr, _, _, range) when rangeContainsPos range pos ->
| SynExpr.Paren (expr, _, _, range) when rangeContainsPos range pos ->
getIdentRangeForFuncExprInApp traverseSynExpr expr pos

// This matches computation expressions like 'async { ... }'
| SynExpr.App(_, _, _, SynExpr.CompExpr (_, _, expr, range), _) when rangeContainsPos range pos ->
getIdentRangeForFuncExprInApp traverseSynExpr expr pos

| SynExpr.App(_, _, funcExpr, argExpr, _) ->
| SynExpr.App (_, _, funcExpr, argExpr, _) ->
match argExpr with
| SynExpr.App (_, _, _, _, range) when rangeContainsPos range pos ->
getIdentRangeForFuncExprInApp traverseSynExpr argExpr pos

| SynExpr.Paren(SynExpr.Lambda(_, _, _args, body, _, _), _, _, _) when rangeContainsPos body.Range pos ->
getIdentRangeForFuncExprInApp traverseSynExpr body pos
// Special case: `async { ... }` is actually a CompExpr inside of the argExpr of a SynExpr.App
| SynExpr.CompExpr (_, _, expr, range) when rangeContainsPos range pos ->
getIdentRangeForFuncExprInApp traverseSynExpr expr pos

| SynExpr.Paren (expr, _, _, range) when rangeContainsPos range pos ->
getIdentRangeForFuncExprInApp traverseSynExpr expr pos

| _ ->
match funcExpr with
Expand All @@ -236,7 +236,7 @@ type FSharpParseFileResults(errors: FSharpDiagnostic[], input: ParsedInput optio
// of the identifier of the function we're after
getIdentRangeForFuncExprInApp traverseSynExpr funcExpr pos

| SynExpr.LetOrUse(_, _, bindings, body, range) when rangeContainsPos range pos ->
| SynExpr.LetOrUse (_, _, bindings, body, range) when rangeContainsPos range pos ->
let binding =
bindings
|> List.tryFind (fun x -> rangeContainsPos x.RangeOfBindingAndRhs pos)
Expand All @@ -246,6 +246,58 @@ type FSharpParseFileResults(errors: FSharpDiagnostic[], input: ParsedInput optio
| None ->
getIdentRangeForFuncExprInApp traverseSynExpr body pos

| SynExpr.IfThenElse (ifExpr, thenExpr, elseExpr, _, _, _, range) when rangeContainsPos range pos ->
if rangeContainsPos ifExpr.Range pos then
getIdentRangeForFuncExprInApp traverseSynExpr ifExpr pos
elif rangeContainsPos thenExpr.Range pos then
getIdentRangeForFuncExprInApp traverseSynExpr thenExpr pos
else
match elseExpr with
| None -> None
| Some expr ->
getIdentRangeForFuncExprInApp traverseSynExpr expr pos

| SynExpr.Match (_, expr, clauses, range) when rangeContainsPos range pos ->
if rangeContainsPos expr.Range pos then
getIdentRangeForFuncExprInApp traverseSynExpr expr pos
else
let clause = clauses |> List.tryFind (fun clause -> rangeContainsPos clause.Range pos)
match clause with
| None -> None
| Some clause ->
match clause with
| SynMatchClause.Clause (_, whenExpr, resultExpr, _, _) ->
match whenExpr with
| None ->
getIdentRangeForFuncExprInApp traverseSynExpr resultExpr pos
| Some whenExpr ->
if rangeContainsPos whenExpr.Range pos then
getIdentRangeForFuncExprInApp traverseSynExpr whenExpr pos
else
getIdentRangeForFuncExprInApp traverseSynExpr resultExpr pos


// Ex: C.M(x, y, ...) <--- We want to find where in the tupled application the call is being made
| SynExpr.Tuple(_, exprs, _, tupRange) when rangeContainsPos tupRange pos ->
let expr = exprs |> List.tryFind (fun expr -> rangeContainsPos expr.Range pos)
match expr with
| None -> None
| Some expr ->
getIdentRangeForFuncExprInApp traverseSynExpr expr pos

// Capture the body of a lambda, often nested in a call to a collection function
| SynExpr.Lambda(_, _, _args, body, _, _) when rangeContainsPos body.Range pos ->
getIdentRangeForFuncExprInApp traverseSynExpr body pos

| SynExpr.Do(expr, range) when rangeContainsPos range pos ->
getIdentRangeForFuncExprInApp traverseSynExpr expr pos

| SynExpr.Assert(expr, range) when rangeContainsPos range pos ->
getIdentRangeForFuncExprInApp traverseSynExpr expr pos

| SynExpr.ArbitraryAfterError (_debugStr, range) when rangeContainsPos range pos ->
Some range

| expr ->
traverseSynExpr expr
|> Option.map (fun expr -> expr)
Expand Down
128 changes: 127 additions & 1 deletion tests/service/ServiceUntypedParseTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ async {
|> shouldEqual ((4, 11), (4, 16))

[<Test>]
let ``TryRangeOfFunctionOrMethodBeingApplied - inside lambda``() =
let ``TryRangeOfFunctionOrMethodBeingApplied - inside lambda - binding``() =
let source = """
let add n1 n2 = n1 + n2
let lst = [1; 2; 3]
Expand All @@ -855,6 +855,132 @@ let mapped =
|> tups
|> shouldEqual ((6, 18), (6, 21))

[<Test>]
let ``TryRangeOfFunctionOrMethodBeingApplied - inside lambda - if expression``() =
let source = """
let add n1 n2 = n1 + n2
let lst = [1; 2; 3]
let mapped =
lst |> List.map (fun n ->
if true then
add
else
match add 1 2 with
| 0 when 0 = 0 -> add 1 2
| _ -> add 1 2
)
"""
let parseFileResults, _ = getParseAndCheckResults source
let res = parseFileResults.TryRangeOfFunctionOrMethodBeingApplied (mkPos 7 15)
match res with
| None -> Assert.Fail("Expected 'add' but got nothing")
| Some range ->
range
|> tups
|> shouldEqual ((7, 12), (7, 15))

[<Test>]
let ``TryRangeOfFunctionOrMethodBeingApplied - inside lambda - match expression``() =
let source = """
let add n1 n2 = n1 + n2
let lst = [1; 2; 3]
let mapped =
lst |> List.map (fun n ->
if true then
add
else
match add 1 2 with
| 0 when 0 = 0 -> add 1 2
| _ -> add 1 2
)
"""
let parseFileResults, _ = getParseAndCheckResults source
let res = parseFileResults.TryRangeOfFunctionOrMethodBeingApplied (mkPos 7 15)
match res with
| None -> Assert.Fail("Expected 'add' but got nothing")
| Some range ->
range
|> tups
|> shouldEqual ((7, 12), (7, 15))

[<Test>]
let ``TryRangeOfFunctionOrMethodBeingApplied - inside lambda - match expr``() =
let source = """
let add n1 n2 = n1 + n2
let lst = [1; 2; 3]
let mapped =
lst |> List.map (fun n ->
if true then
add
else
match add with
| 0 when 0 = 0 -> add 1 2
| _ -> add 1 2
)
"""
let parseFileResults, _ = getParseAndCheckResults source
let res = parseFileResults.TryRangeOfFunctionOrMethodBeingApplied (mkPos 9 21)
match res with
| None -> Assert.Fail("Expected 'add' but got nothing")
| Some range ->
range
|> tups
|> shouldEqual ((9, 18), (9, 21))

[<Test>]
let ``TryRangeOfFunctionOrMethodBeingApplied - inside lambda - match case``() =
let source = """
let add n1 n2 = n1 + n2
let lst = [1; 2; 3]
let mapped =
lst |> List.map (fun n ->
if true then
add
else
match add 1 2 with
| 0 when 0 = 0 -> add 1 2
| _ -> add
)
"""
let parseFileResults, _ = getParseAndCheckResults source
let res = parseFileResults.TryRangeOfFunctionOrMethodBeingApplied (mkPos 11 22)
match res with
| None -> Assert.Fail("Expected 'add' but got nothing")
| Some range ->
range
|> tups
|> shouldEqual ((11, 19), (11, 22))

[<Test>]
let ``TryRangeOfFunctionOrMethodBeingApplied - inside method call``() =
let source = """
type C() = static member Yeet(x, y, z) = ()
C.Yeet(1, 2, sqrt)
"""
let parseFileResults, _ = getParseAndCheckResults source
let res = parseFileResults.TryRangeOfFunctionOrMethodBeingApplied (mkPos 3 17)
match res with
| None -> Assert.Fail("Expected 'sqrt' but got nothing")
| Some range ->
range
|> tups
|> shouldEqual ((3, 13), (3, 17))

[<Test>]
let ``TryRangeOfFunctionOrMethodBeingApplied - inside method call - parenthesized lambda``() =
let source = """
type C() = static member Yeet(x, y, z) = ()
C.Yeet(1, 2, (fun x -> sqrt))
"""
let parseFileResults, _ = getParseAndCheckResults source
let res = parseFileResults.TryRangeOfFunctionOrMethodBeingApplied (mkPos 3 27)
match res with
| None -> Assert.Fail("Expected 'sqrt' but got nothing")
| Some range ->
range
|> tups
|> shouldEqual ((3, 23), (3, 27))

module PipelinesAndArgs =
[<Test>]
let ``TryIdentOfPipelineContainingPosAndNumArgsApplied - No pipeline, no infix app``() =
Expand Down