Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 4, 2025

Completions within ternary conditionals inside function arguments weren't preselecting the expected type. For example:

function bar(z: string, x: Foo) { return x; }
const a = '';

bar(a, a == '' ? ); // Foo not preselected after ?
bar(a, a == '' ? : ); // Foo not preselected after :

Changes

  • Added getContextualTypeForConditionalExpression helper to get contextual type from parent conditional expression
  • Handle KindQuestionToken and KindColonToken cases when parent is ConditionalExpression
  • Fallthrough to default case for KindColonToken when not in conditional (preserves object literal, type annotation behavior)

The fix uses the parent ConditionalExpression node to locate the containing call expression, enabling proper contextual type inference.

Original prompt

This section details on the original issue you should resolve

<issue_title>No "recommended" completion entry when outer context is incomplete ternary conditional operator</issue_title>
<issue_description>From microsoft/TypeScript#59517

For the code:

export enum Bar { }
export enum Foo { }


function foo(x: Foo) { return x; }
function bar(z: string, x: Foo) { return x; }

const a = '';

// Trigger suggestions here
foo(); // Selects `Foo` first
bar(a, a == '' ? ); // doesn't select `Foo` first
  1. Trigger suggestions after the incomplete ternary

Expected
Foo is selected first

Actual
I see Bar selected first. It works correctly in the Foo function

cc @bpasero</issue_description>

<agent_instructions>To test this, create a fourslash test which does completions with markers within the foo call, and after the ? in the bar call. Something like:

export enum Bar { }
export enum Foo { }


function foo(x: Foo) { return x; }
function bar(z: string, x: Foo) { return x; }

const a = '';

foo(/*1*/);
bar(a, a == '' ? /*2*/);

Use print debugging with go test -v to figure out a fix.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Fix recommended completion for incomplete ternary operator Fix completion preselection in ternary conditional expressions Dec 4, 2025
Copilot AI requested a review from jakebailey December 4, 2025 05:16
Copilot finished work on behalf of jakebailey December 4, 2025 05:16
@jakebailey jakebailey marked this pull request as ready for review December 4, 2025 17:51
Copilot AI review requested due to automatic review settings December 4, 2025 17:51
Copilot finished reviewing on behalf of jakebailey December 4, 2025 17:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes completion preselection when the cursor is positioned within a ternary conditional expression inside function arguments. Previously, completions after ? or : in ternary conditionals weren't preselecting the expected type.

Key Changes

  • Added contextual type handling for ternary conditionals by implementing getContextualTypeForConditionalExpression helper
  • Enhanced getContextualType switch statement to handle KindQuestionToken and KindColonToken when parent is a conditional expression
  • Added comprehensive test coverage for all ternary completion scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
internal/ls/completions.go Implements contextual type resolution for completions in ternary conditional expressions by adding a new helper function and handling question/colon tokens
internal/fourslash/tests/manual/completionInTernaryConditional_test.go Adds comprehensive test coverage for completion preselection in ternary conditionals, including incomplete ternaries and both branches

@jakebailey jakebailey added this pull request to the merge queue Dec 4, 2025
Merged via the queue into main with commit a37a257 Dec 4, 2025
48 checks passed
@jakebailey jakebailey deleted the copilot/fix-ternary-completion-issue branch December 4, 2025 18:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No "recommended" completion entry when outer context is incomplete ternary conditional operator

3 participants