用脈絡引導 Copilot

使用 GitHub Copilot 進行軟體開發

Thalia Barrera

AI Engineering Curriculum Manager, DataCamp

脈絡的重要性

使用 GitHub Copilot 進行軟體開發

脈絡的重要性

使用 GitHub Copilot 進行軟體開發
使用 GitHub Copilot 進行軟體開發

#codebase 變數

$$

#codebase: 在整個專案中進行語意程式碼搜尋

範例提示:

How is user authentication handled? #codebase

結果:

  • 🔍 掃描整個 codebase
  • 💻 在回答中納入相關邏輯、函式呼叫或設定
使用 GitHub Copilot 進行軟體開發

Copilot 如何理解你的 codebase

索引類型:

  • 本機索引: 儲存在你的機器上
  • 遠端索引: 用於託管在 GitHub 的版本庫
使用 GitHub Copilot 進行軟體開發

檢查你的索引類型

使用 GitHub Copilot 進行軟體開發

檢查你的索引類型

使用 GitHub Copilot 進行軟體開發

#changes 變數

$$

#changes: 參照最近的原始碼控制變更

範例提示:

Will any of these changes break the login flow? #changes

使用時機:

  • 💾 評估程式碼修改
使用 GitHub Copilot 進行軟體開發

#selection 變數

$$

#selection: 參照你反白選取的程式碼區塊

範例提示:

Can you refactor this? #selection

使用時機:

  • 🔧 快速編修
  • 📝 建立聚焦的提示
使用 GitHub Copilot 進行軟體開發

#fetch 變數

  • 說明文件 📚、API 參考 💻、部落格文章 📰

$$

#fetch: 擷取網頁內容

範例提示:

Summarize this documentation for me #fetch https://example.com/docs

結果:

  • 🌐 取得網頁並用作脈絡
使用 GitHub Copilot 進行軟體開發

#files 變數

$$

#files: 將特定檔案附加到提示

範例提示:

Can you check this file for error handling issues? #files:utils/data_loader.py

結果:

  • 🔍 分析被參照檔案的內容
  • 📄 將其納入提示脈絡
使用 GitHub Copilot 進行軟體開發

其他聊天變數與工具

$$

Variable/tool Description
#search 在目前工作區中啟用檔案搜尋。
#testFailure 取得單元測試失敗資訊。用於執行與診斷測試時。
#problems 將「Problems」面板的工作區問題加入為脈絡。修正或除錯程式碼時很有用。
#terminalLastCommand 取得上次在終端機執行的指令及其輸出。
#githubRepo 在 GitHub 版本庫中進行程式碼搜尋。
1 https://code.visualstudio.com/docs/copilot/reference/copilot-vscode-features#_chat-tools
使用 GitHub Copilot 進行軟體開發
使用 GitHub Copilot 進行軟體開發

建立漸進式的聊天互動

單一且複雜的提示:

Add PostgreSQL integration with authentication, persistence, sessions, error handling, and connection pooling

漸進式複雜度流程:

  1. What's the best approach for adding database persistence? #codebase
  2. Implement the database connection using the suggested approach
  3. Help me with the schema design for the Task and the User classes
  4. Now add session management and basic error handling
  5. ...
使用 GitHub Copilot 進行軟體開發

開新聊天以清除脈絡

使用 GitHub Copilot 進行軟體開發

開新聊天以清除脈絡

使用 GitHub Copilot 進行軟體開發

使用 GitHub Copilot 進行軟體開發

一起來練習吧!

使用 GitHub Copilot 進行軟體開發

Preparing Video For Download...