-
Notifications
You must be signed in to change notification settings - Fork 424
fix: avoid spurious 'restart terminal' prompts on every window reload #1648
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. | ||
|
|
||
| import * as vscode from "vscode"; | ||
|
|
||
| /** | ||
| * Helpers that update a {@link vscode.EnvironmentVariableCollection} only when | ||
| * the resulting mutation would actually change the collection. | ||
| * | ||
| * Background: VS Code shows a "Restart terminal to apply environment variable | ||
| * changes" prompt whenever an extension's collection differs from what has | ||
| * already been applied to running terminals. Calling `replace()` / `append()` | ||
| * with the same value, or calling `clear()` followed by re-adding identical | ||
| * entries, still counts as a change and re-triggers the prompt on every | ||
| * window reload. See issue #1647. | ||
| * | ||
| * These helpers compare the existing mutator (type + value + options) against | ||
| * the desired one and skip the write entirely when they match. | ||
| */ | ||
|
|
||
| const DEFAULT_OPTIONS: Required<vscode.EnvironmentVariableMutatorOptions> = { | ||
| applyAtProcessCreation: true, | ||
| applyAtShellIntegration: false, | ||
| }; | ||
|
|
||
| function normalizeOptions( | ||
| options?: vscode.EnvironmentVariableMutatorOptions, | ||
| ): Required<vscode.EnvironmentVariableMutatorOptions> { | ||
| return { ...DEFAULT_OPTIONS, ...options }; | ||
| } | ||
|
|
||
| function sameOptions( | ||
| existing: vscode.EnvironmentVariableMutator, | ||
| desired?: vscode.EnvironmentVariableMutatorOptions, | ||
| ): boolean { | ||
| const e = normalizeOptions(existing.options); | ||
| const d = normalizeOptions(desired); | ||
| return e.applyAtProcessCreation === d.applyAtProcessCreation | ||
| && e.applyAtShellIntegration === d.applyAtShellIntegration; | ||
| } | ||
|
|
||
| /** | ||
| * Calls `collection.replace(variable, value, options)` only when the existing | ||
| * mutator (if any) does not already match. | ||
| * | ||
| * @returns `true` if the collection was actually written to. | ||
| */ | ||
| export function applyReplaceIfChanged( | ||
| collection: vscode.EnvironmentVariableCollection, | ||
| variable: string, | ||
| value: string, | ||
| options?: vscode.EnvironmentVariableMutatorOptions, | ||
| ): boolean { | ||
| const existing = collection.get(variable); | ||
| if (existing | ||
| && existing.type === vscode.EnvironmentVariableMutatorType.Replace | ||
| && existing.value === value | ||
| && sameOptions(existing, options)) { | ||
| return false; | ||
| } | ||
| if (options) { | ||
| collection.replace(variable, value, options); | ||
| } else { | ||
| collection.replace(variable, value); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Calls `collection.append(variable, value, options)` only when the existing | ||
| * mutator (if any) does not already match. | ||
| * | ||
| * @returns `true` if the collection was actually written to. | ||
| */ | ||
| export function applyAppendIfChanged( | ||
| collection: vscode.EnvironmentVariableCollection, | ||
| variable: string, | ||
| value: string, | ||
| options?: vscode.EnvironmentVariableMutatorOptions, | ||
| ): boolean { | ||
| const existing = collection.get(variable); | ||
| if (existing | ||
| && existing.type === vscode.EnvironmentVariableMutatorType.Append | ||
| && existing.value === value | ||
| && sameOptions(existing, options)) { | ||
| return false; | ||
| } | ||
| if (options) { | ||
| collection.append(variable, value, options); | ||
| } else { | ||
| collection.append(variable, value); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Deletes the mutator for `variable` only when one is currently present. | ||
| * | ||
| * @returns `true` if a mutator was deleted. | ||
| */ | ||
| export function deleteIfPresent( | ||
| collection: vscode.EnvironmentVariableCollection, | ||
| variable: string, | ||
| ): boolean { | ||
| if (collection.get(variable)) { | ||
| collection.delete(variable); | ||
| return true; | ||
| } | ||
| return false; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.