Skip to content

Add TM_FULLNAME and TM_EMAIL snippet variables#315156

Draft
Copilot wants to merge 10 commits into
mainfrom
copilot/add-my-name-variable-snippet
Draft

Add TM_FULLNAME and TM_EMAIL snippet variables#315156
Copilot wants to merge 10 commits into
mainfrom
copilot/add-my-name-variable-snippet

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 8, 2026

Description

This adds first-class snippet variables for per-user identity so shared snippets can stamp author info without hardcoding names. It introduces user name/email expansion with cross-platform env fallbacks and keeps behavior deterministic with resolver ordering.

  • What’s added

    • New snippet variables:
      • TM_FULLNAME
      • TM_EMAIL
    • Registered in known snippet variable names so they are recognized as built-ins.
  • Resolution behavior

    • TM_FULLNAME resolves in priority order:
      1. GIT_AUTHOR_NAME
      2. GIT_COMMITTER_NAME
      3. USER
      4. USERNAME
      5. LOGNAME
    • TM_EMAIL resolves in priority order:
      1. GIT_AUTHOR_EMAIL
      2. GIT_COMMITTER_EMAIL
      3. EMAIL
    • Environment lookup checks exact key first, then lowercase key, and ignores empty/whitespace values.
    • Values are computed once per resolver instance (no extra static cache layer).
  • Editor integration

    • Wired UserBasedVariableResolver into snippet expansion paths used by snippet session insertion.
  • Docs and coverage

    • Updated snippet variable documentation to include both new variables.
    • Added focused unit tests covering precedence, key-casing variants, and empty-value behavior.
  • Follow-up from review feedback

    • Refactored UserBasedVariableResolver.resolve to use switch with default.
    • Inlined env lookup helper logic into _resolveFromEnv.
    • Removed placeholder constructor comment/body noise.
    • Renamed variables from CURRENT_USER / CURRENT_USER_EMAIL to TM_FULLNAME / TM_EMAIL for TextMate naming consistency.
    • Removed redundant toUpperCase() env lookup.
    • Removed static _cached resolver fields so only one caching level remains.
    • Reverted changes to extensions/json/syntaxes/snippets.tmLanguage.json per review feedback.
{
  "Comment todo": {
    "prefix": "//",
    "body": [
      "// TODO (${TM_FULLNAME}) [${CURRENT_YEAR}-${CURRENT_MONTH}-${CURRENT_DATE}]: $1",
      "// Contact: ${TM_EMAIL}"
    ]
  }
}

Testing

  • npm run compile-check-ts-native
  • npm run transpile-client
  • node test/unit/browser/index.js --run src/vs/editor/contrib/snippet/test/browser/snippetVariables.test.ts --browser chromium
  • npm run test-node -- --grep "Snippet Variables Resolver"

Copilot AI requested review from Copilot and removed request for Copilot May 8, 2026 01:52
Copilot AI linked an issue May 8, 2026 that may be closed by this pull request
Copilot AI requested review from Copilot and removed request for Copilot May 8, 2026 01:58
Copilot AI changed the title [WIP] Add variable for OS or git user name in code snippets Add snippet variables for current user name and email May 8, 2026
Copilot AI requested a review from dmitrivMS May 8, 2026 01:59
Copy link
Copy Markdown
Contributor

@dmitrivMS dmitrivMS left a comment

Choose a reason for hiding this comment

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

See comments.
What happens on MacOS?

Comment thread src/vs/editor/contrib/snippet/browser/snippetVariables.ts Outdated
Comment thread src/vs/editor/contrib/snippet/browser/snippetVariables.ts Outdated
Comment thread src/vs/editor/contrib/snippet/browser/snippetVariables.ts Outdated
Copilot AI requested review from Copilot and removed request for Copilot May 8, 2026 02:08
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 8, 2026

See comments.
What happens on MacOS?

On macOS this resolves via the same fallback chain: GIT_AUTHOR_NAMEGIT_COMMITTER_NAMEUSERUSERNAMELOGNAME (and similarly for email via git vars then EMAIL). In practice macOS shells usually provide USER and LOGNAME, so it resolves correctly there as well. (Current implementation in 525ad4f.)

Copilot AI requested a review from dmitrivMS May 8, 2026 02:10
@dmitrivMS
Copy link
Copy Markdown
Contributor

@copilot There are other places variables need to be registered - find and update them all.

Copilot AI review requested due to automatic review settings May 8, 2026 03:37
Copy link
Copy Markdown
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 adds two new built-in snippet variables (CURRENT_USER, CURRENT_USER_EMAIL) that resolve from environment variables, and wires the new resolver into snippet insertion so snippets can stamp per-user identity information.

Changes:

  • Added UserBasedVariableResolver and registered CURRENT_USER / CURRENT_USER_EMAIL as known built-in snippet variables.
  • Integrated the new resolver into SnippetSession variable resolution so the variables expand during snippet insertion.
  • Updated snippet documentation and added unit tests for precedence, casing, and empty/whitespace handling.
Show a summary per file
File Description
src/vs/editor/contrib/snippet/test/browser/snippetVariables.test.ts Adds tests validating resolution behavior for the new user-based snippet variables.
src/vs/editor/contrib/snippet/browser/snippetVariables.ts Introduces UserBasedVariableResolver and registers the new built-in variable names.
src/vs/editor/contrib/snippet/browser/snippetSession.ts Wires UserBasedVariableResolver into the composite resolver used by snippet insertion paths.
src/vs/editor/contrib/snippet/browser/snippet.md Documents the new snippet variables and their environment-variable fallbacks.

Copilot's findings

  • Files reviewed: 4/4 changed files
  • Comments generated: 2

Comment thread src/vs/editor/contrib/snippet/browser/snippetVariables.ts Outdated
Comment on lines +387 to +392
private _resolveFromEnv(...keys: string[]): string | undefined {
for (const key of keys) {
const value = this._env[key] ?? this._env[key.toLowerCase()] ?? this._env[key.toUpperCase()];
if (!isFalsyOrWhitespace(value)) {
return value;
}
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 8, 2026

@copilot There are other places variables need to be registered - find and update them all.

Fixed in 7f4852d — I audited the registrations and updated all hard-coded snippet variable regex lists in extensions/json/syntaxes/snippets.tmLanguage.json to include CURRENT_USER and CURRENT_USER_EMAIL everywhere.

Copilot AI changed the title Add snippet variables for current user name and email Add snippet variables for current user name and email and register them across snippet grammar May 8, 2026
@dmitrivMS
Copy link
Copy Markdown
Contributor

@copilot

  1. Rename to TM_FULLNAME and TM_EMAIL to stay consistent with TM
  2. How hard would it be to get TM_ORGANIZATION_NAME added? E.g. from auth service or something? Could be env:ORGANIZATION but it's probably unset most of the time. Is there Git equivalent?

@dmitrivMS
Copy link
Copy Markdown
Contributor

@copilot Remove the redundant toUpperCase() in the env lookup.

Comment thread src/vs/editor/contrib/snippet/browser/snippetVariables.ts Outdated
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 8, 2026

@copilot

  1. Rename to TM_FULLNAME and TM_EMAIL to stay consistent with TM
  2. How hard would it be to get TM_ORGANIZATION_NAME added? E.g. from auth service or something? Could be env:ORGANIZATION ...

Done in e448a9b — renamed to TM_FULLNAME and TM_EMAIL across resolver/known vars/docs/tests/grammar.

For TM_ORGANIZATION_NAME: there isn’t a reliable git equivalent (git standard identity is name/email, not org). Pulling from auth service would be significantly heavier because snippet variable resolution is local/synchronous and currently env/git-based. The practical low-friction option would be best-effort env lookup (e.g. ORGANIZATION) as a separate follow-up, with expectation it’s often unset.

Comment thread extensions/json/syntaxes/snippets.tmLanguage.json Outdated
Copilot AI changed the title Add snippet variables for current user name and email and register them across snippet grammar Add TM_FULLNAME and TM_EMAIL snippet variables and register them across snippet grammar May 8, 2026
Copilot AI requested a review from dmitrivMS May 8, 2026 03:49
Copilot AI and others added 2 commits May 8, 2026 03:53
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 8, 2026

@copilot Remove the redundant toUpperCase() in the env lookup.

Done in 8639604 — removed the redundant toUpperCase() lookup from env resolution. No UI changes in this PR, so no screenshot applies.

return this._email;
}

return undefined;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@copilot this can go into default:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 58c183e — moved the fallback into the default branch of the switch in resolve. No UI changes in this PR, so no screenshot applies.


private static readonly _userNameEnvVariables = ['GIT_AUTHOR_NAME', 'GIT_COMMITTER_NAME', 'USER', 'USERNAME', 'LOGNAME'];
private static readonly _userEmailEnvVariables = ['GIT_AUTHOR_EMAIL', 'GIT_COMMITTER_EMAIL', 'EMAIL'];
private static _cachedFullName: string | undefined;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@copilot remove these _cached variables - one level of caching is enough

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in aef7942 — removed the static _cached variables and kept only the resolver-level computed values. No UI changes in this PR, so no screenshot applies.

@dmitrivMS
Copy link
Copy Markdown
Contributor

@copilot revert changes to extensions/json/syntaxes/snippets.tmLanguage.json file

@RedCMD
Copy link
Copy Markdown
Contributor

RedCMD commented May 8, 2026

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 8, 2026

@copilot revert changes to extensions/json/syntaxes/snippets.tmLanguage.json file

Done in aef7942 — reverted extensions/json/syntaxes/snippets.tmLanguage.json to the pre-change version. No UI changes in this PR, so no screenshot applies.

Copilot AI changed the title Add TM_FULLNAME and TM_EMAIL snippet variables and register them across snippet grammar Add TM_FULLNAME and TM_EMAIL snippet variables May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using OS user name / git user name inside code snippets

4 participants