2025年8月31日日曜日

Context Management in GitHub Copilot Chat (VS Code)

 

Using the right context helps Copilot understand what you're asking and where to look. You can add context implicitly (automatically) or explicitly using #-mentions, @-mentions, or instruction files.


✅ 1. Implicit Context (Automatic)

VS Code automatically includes context such as:

  • Selected text in the active editor

  • Current file or notebook name

  • The active file in Ask and Edit modes

  • Automatically determined relevant files in Agent mode


🏷️ 2. Use #-Mentions to Add Context

Type # in chat to bring up:

  • Files/folders/symbols

  • Tools

  • Context variables

🧾 Examples:

Explain what this function does #main.py Add a route to update address info #api #routes Where is the DB string configured? #codebase

🔎 Available Context Tags:

TagDescription
#codebaseSearches entire workspace
#<file>Adds a specific file (e.g. #app.js)
#<folder>Includes a folder
#<symbol>Refers to a function/class (opened in editor)
#changesIncludes current Git changes
#problemsIncludes unresolved workspace issues
#testFailureAdds test failures as context
#fetch <url>Fetches & references content from a webpage
#githubRepo <repo>Pulls code patterns from GitHub repos (e.g. #githubRepo vercel/next.js)

🖱️ You can also:

  • Drag & drop files into Chat view

  • Use Add ContextFiles & Folders / Symbols


🌐 3. Reference Web Content with Tools

#fetch <url>

Grab documentation or examples from the web:

How do I use React hooks? #fetch https://react.dev/learn

#githubRepo <user/repo>

Search code patterns in public GitHub repos:

Add routing like #githubRepo vercel/next.js

🛠️ 4. Reference Tools

Tools can be part of extensions, MCP servers, or built-in.

Examples:

Summarize open issues #github-mcp Generate schema from #postgres

💡 You can create tool sets and reference them as a group.


🧑‍💼 5. Use @-Mentions for Domain Experts

Mention a chat participant (domain expert assistant):

MentionDescription
@vscodeHelp with VS Code settings/config
@terminalAsk about terminal commands and usage
@workspaceWorkspace-specific insights

Examples:

@vscode how do I enable autosave? @terminal list top 5 largest files

🌍 6. Use the Simple Browser (Experimental)

  1. Run your app locally

  2. Open Simple Browser via Command Palette

  3. Enable element selection (chat.sendElementsToChat.enabled)

  4. Click elements to add them as context

  5. Optionally enable:

    • chat.sendElementsToChat.attachCSS

    • chat.sendElementsToChat.attachImages

Useful for: Debugging or inspecting live UI components.


🧠 7. Instruction Files (Custom AI Behavior)

Define coding preferences, formatting rules, or team standards in a .md instruction file:

  • Avoid repeating the same prompts

  • Keep AI behavior consistent across projects

Learn more: Customize AI with instruction files


🧱 8. Workspace Indexing

Improves search and context relevance:

Index TypeDescription
RemoteGitHub-hosted index (for large repos)
LocalStored on your machine, semantic search
BasicFallback for simpler local searching

Enable via settings or GitHub Copilot setup.


💬 9. Manage Chat History

  • Chat remembers the context from previous turns

  • Use follow-up prompts like:

    “Now make that async”
    “Add tests for this”

🔄 To start fresh:

  • Use New Chat (Ctrl+N)

  • Clears history and resets context


💡 Tips

  • Enable github.copilot.chat.codesearch.enabled for better #codebase results

  • Type /help in chat for built-in instructions

  • Right-click a file → “Add to Chat”

  • Use floating chat windows for multitasking

0 件のコメント:

コメントを投稿