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(auto-edit): fix the false notification for auto-edit non eligibility #6899

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Changes from 1 commit
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
55 changes: 53 additions & 2 deletions vscode/src/autoedits/create-autoedits-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,28 @@ export function createAutoEditsProvider({
)
}

/**
* Displays an error notification to the user about non-eligibility for auto edits,
* but only if the user is currently in the Settings view (to avoid spamming them).
*
* This is because because of the flaky network issues we could evaluate the default feature flag value to false
* and show the non eligibility notification to the user even if they have access to the feature.
* Generally the users should see the notification only when they manully change the vscode config which could be either
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Generally the users should see the notification only when they manully change the vscode config which could be either
* Generally the users should see the notification only when they manually change the vscode config which could be either

nit :D

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

* through the settings UI or `settings.json` file.
*
* @param {string | undefined} nonEligibilityReason - The reason why the user is currently not eligible
* for auto edits. If not provided, no notification occurs.
*/
export async function handleAutoeditsNotificationForNonEligibleUser(
nonEligibilityReason?: string
): Promise<void> {
const switchToAutocompleteText = 'Switch to autocomplete'
if (!nonEligibilityReason || !isSettingsEditorOpen()) {
return
}

const switchToAutocompleteText = 'Switch to autocomplete'
const selection = await vscode.window.showErrorMessage(
`Error: ${nonEligibilityReason ?? AUTOEDITS_NON_ELIGIBILITY_MESSAGES.FEATURE_FLAG_NOT_ELIGIBLE}`,
`Error: ${nonEligibilityReason}`,
switchToAutocompleteText
)
if (selection === switchToAutocompleteText) {
Expand All @@ -122,6 +137,42 @@ export async function handleAutoeditsNotificationForNonEligibleUser(
}
}

/**
* Checks whether the current view in VS Code is the Settings editor (JSON or UI).
*
* This function performs two checks:
* 1. Detect if the active text editor points to a known settings file (e.g., settings.json, settings.jsonc).
* 2. If there's no text editor open, examine the "Tab" label to see if it's the built-in Settings UI.
*
* Note: Using the tab's label is locale-specific; if a user runs VS Code in a non-English locale,
* or if the label changes in future VS Code versions, this heuristic may fail.
*
* @returns {boolean} True if the user is most likely viewing the Settings editor (JSON or UI), false otherwise.
*/
function isSettingsEditorOpen(): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting idea

const activeEditor = vscode.window.activeTextEditor

// 1) If there's an active text editor, check if the file name matches typical settings files
if (activeEditor) {
const fsPath = activeEditor.document.uri.fsPath
if (fsPath.endsWith('settings.json') || fsPath.endsWith('settings.jsonc')) {
return true
}
return false
}

// 2) If there's no activeTextEditor, the user might be in the graphical Settings UI or have no editor at all
const activeTab = vscode.window.tabGroups.activeTabGroup?.activeTab
if (!activeTab) {
// No tab at all: definitely not a JSON settings file;
// could be just an empty Editor area, Start page, or something else
return false
}

// The built-in Settings UI tab typically has the label "Settings" (in English).
return activeTab.label === 'Settings'
}

export function isUserEligibleForAutoeditsFeature(
autoeditsFeatureFlagEnabled: boolean,
authStatus: AuthStatus,
Expand Down
Loading