Compare commits

..

1 Commits

Author SHA1 Message Date
Cole Miller
bc2ceb0f1a WIP 2024-12-09 23:20:10 -05:00
253 changed files with 8262 additions and 18833 deletions

View File

@@ -26,8 +26,8 @@ body:
required: true required: true
- type: textarea - type: textarea
attributes: attributes:
label: If applicable, add screenshots or screencasts of the incorrect state / behavior label: If applicable, add mockups / screenshots to help explain present your vision of the feature
description: Drag images / videos into the text input below description: Drag issues into the text input below
validations: validations:
required: false required: false
- type: textarea - type: textarea

View File

@@ -1,4 +1,3 @@
# yaml-language-server: $schema=https://json.schemastore.org/github-issue-config.json
blank_issues_enabled: false blank_issues_enabled: false
contact_links: contact_links:
- name: Language Request - name: Language Request

View File

@@ -8,7 +8,7 @@ on:
jobs: jobs:
update_top_ranking_issues: update_top_ranking_issues:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.repository == 'zed-industries/zed' if: github.repository_owner == 'zed-industries'
steps: steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Set up uv - name: Set up uv

View File

@@ -8,7 +8,7 @@ on:
jobs: jobs:
update_top_ranking_issues: update_top_ranking_issues:
runs-on: ubuntu-latest runs-on: ubuntu-latest
if: github.repository == 'zed-industries/zed' if: github.repository_owner == 'zed-industries'
steps: steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Set up uv - name: Set up uv

View File

@@ -140,7 +140,7 @@ jobs:
name: Create a Linux *.tar.gz bundle for ARM name: Create a Linux *.tar.gz bundle for ARM
if: github.repository_owner == 'zed-industries' if: github.repository_owner == 'zed-industries'
runs-on: runs-on:
- buildjet-16vcpu-ubuntu-2204-arm - hosted-linux-arm-1
needs: tests needs: tests
env: env:
DIGITALOCEAN_SPACES_ACCESS_KEY: ${{ secrets.DIGITALOCEAN_SPACES_ACCESS_KEY }} DIGITALOCEAN_SPACES_ACCESS_KEY: ${{ secrets.DIGITALOCEAN_SPACES_ACCESS_KEY }}

1317
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -142,7 +142,6 @@ members = [
"crates/zed", "crates/zed",
"crates/zed_actions", "crates/zed_actions",
"crates/zeta", "crates/zeta",
"crates/git_ui",
# #
# Extensions # Extensions
@@ -228,7 +227,6 @@ fs = { path = "crates/fs" }
fsevent = { path = "crates/fsevent" } fsevent = { path = "crates/fsevent" }
fuzzy = { path = "crates/fuzzy" } fuzzy = { path = "crates/fuzzy" }
git = { path = "crates/git" } git = { path = "crates/git" }
git_ui = { path = "crates/git_ui" }
git_hosting_providers = { path = "crates/git_hosting_providers" } git_hosting_providers = { path = "crates/git_hosting_providers" }
go_to_line = { path = "crates/go_to_line" } go_to_line = { path = "crates/go_to_line" }
google_ai = { path = "crates/google_ai" } google_ai = { path = "crates/google_ai" }
@@ -362,6 +360,7 @@ cargo_metadata = "0.19"
cargo_toml = "0.20" cargo_toml = "0.20"
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
clap = { version = "4.4", features = ["derive"] } clap = { version = "4.4", features = ["derive"] }
clickhouse = "0.11.6"
cocoa = "0.26" cocoa = "0.26"
cocoa-foundation = "0.2.0" cocoa-foundation = "0.2.0"
convert_case = "0.6.0" convert_case = "0.6.0"

View File

@@ -1,4 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-eraser">
<path d="m7 21-4.3-4.3c-1-1-1-2.5 0-3.4l9.6-9.6c1-1 2.5-1 3.4 0l5.6 5.6c1 1 1 2.5 0 3.4L13 21"/>
<path d="M22 21H7"/><path d="m5 11 9 9"/>
</svg>

Before

Width:  |  Height:  |  Size: 365 B

View File

@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-file-diff"><path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/><path d="M9 10h6"/><path d="M12 13V7"/><path d="M9 17h6"/></svg>

Before

Width:  |  Height:  |  Size: 348 B

View File

@@ -59,11 +59,6 @@
"gitignore": "vcs", "gitignore": "vcs",
"gitkeep": "vcs", "gitkeep": "vcs",
"gitmodules": "vcs", "gitmodules": "vcs",
"TAG_EDITMSG": "vcs",
"MERGE_MSG": "vcs",
"COMMIT_EDITMSG": "vcs",
"NOTES_EDITMSG": "vcs",
"EDIT_DESCRIPTION": "vcs",
"gleam": "gleam", "gleam": "gleam",
"go": "go", "go": "go",
"gql": "graphql", "gql": "graphql",
@@ -113,7 +108,6 @@
"mdf": "storage", "mdf": "storage",
"mdx": "document", "mdx": "document",
"metadata": "code", "metadata": "code",
"metal": "metal",
"mjs": "javascript", "mjs": "javascript",
"mka": "audio", "mka": "audio",
"mkv": "video", "mkv": "video",
@@ -323,9 +317,6 @@
"lua": { "lua": {
"icon": "icons/file_icons/lua.svg" "icon": "icons/file_icons/lua.svg"
}, },
"metal": {
"icon": "icons/file_icons/metal.svg"
},
"nim": { "nim": {
"icon": "icons/file_icons/nim.svg" "icon": "icons/file_icons/nim.svg"
}, },

View File

@@ -1 +0,0 @@
<svg width="16" height="16" fill="none" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" clip-rule="evenodd" d="M4.56 4.502 3.25 3.027V11.5h1.5V6.973l2.69 3.025 1.31 1.475V7.918l3.306 3.582h2.042L8.55 5.491 7.25 4.081V7.528L4.56 4.502Z" fill="#000"/></svg>

Before

Width:  |  Height:  |  Size: 269 B

View File

@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-git-branch"><line x1="6" x2="6" y1="3" y2="15"/><circle cx="18" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="M18 9a9 9 0 0 1-9 9"/></svg>

Before

Width:  |  Height:  |  Size: 348 B

View File

@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-panel-left"><rect width="18" height="18" x="3" y="3" rx="2"/><path d="M9 3v18"/></svg>

Before

Width:  |  Height:  |  Size: 289 B

View File

@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-panel-right"><rect width="18" height="18" x="3" y="3" rx="2"/><path d="M15 3v18"/></svg>

Before

Width:  |  Height:  |  Size: 291 B

View File

@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-square-dot"><rect width="18" height="18" x="3" y="3" rx="2"/><circle cx="12" cy="12" r="1"/></svg>

Before

Width:  |  Height:  |  Size: 301 B

View File

@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-square-minus"><rect width="18" height="18" x="3" y="3" rx="2"/><path d="M8 12h8"/></svg>

Before

Width:  |  Height:  |  Size: 291 B

View File

@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-square-plus"><rect width="18" height="18" x="3" y="3" rx="2"/><path d="M8 12h8"/><path d="M12 8v8"/></svg>

Before

Width:  |  Height:  |  Size: 309 B

View File

@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-thumbs-down"><path d="M17 14V2"/><path d="M9 18.12 10 14H4.17a2 2 0 0 1-1.92-2.56l2.33-8A2 2 0 0 1 6.5 2H20a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2h-2.76a2 2 0 0 0-1.79 1.11L12 22a3.13 3.13 0 0 1-3-3.88Z"/></svg>

Before

Width:  |  Height:  |  Size: 405 B

View File

@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-thumbs-up"><path d="M7 10v12"/><path d="M15 5.88 14 10h5.83a2 2 0 0 1 1.92 2.56l-2.33 8A2 2 0 0 1 17.5 22H4a2 2 0 0 1-2-2v-8a2 2 0 0 1 2-2h2.76a2 2 0 0 0 1.79-1.11L12 2a3.13 3.13 0 0 1 3 3.88Z"/></svg>

Before

Width:  |  Height:  |  Size: 404 B

View File

@@ -1,4 +0,0 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M8 8.9V11C5.93097 11 5.06903 11 3 11V10.4L8 5.6V5H3V7.1" stroke="black" stroke-width="1.5"/>
<path d="M11 5L13 8L11 11" stroke="black" stroke-width="1.5"/>
</svg>

Before

Width:  |  Height:  |  Size: 268 B

View File

@@ -468,21 +468,13 @@
}, },
{ {
"context": "Editor && showing_completions", "context": "Editor && showing_completions",
"use_key_equivalents": true,
"bindings": {
"enter": "editor::ConfirmCompletion"
}
},
{
"context": "Editor && !inline_completion && showing_completions",
"use_key_equivalents": true,
"bindings": { "bindings": {
"enter": "editor::ConfirmCompletion",
"tab": "editor::ComposeCompletion" "tab": "editor::ComposeCompletion"
} }
}, },
{ {
"context": "Editor && inline_completion", "context": "Editor && inline_completion && !showing_completions",
"use_key_equivalents": true,
"bindings": { "bindings": {
"tab": "editor::AcceptInlineCompletion" "tab": "editor::AcceptInlineCompletion"
} }

View File

@@ -66,7 +66,6 @@
"cmd-v": "editor::Paste", "cmd-v": "editor::Paste",
"cmd-z": "editor::Undo", "cmd-z": "editor::Undo",
"cmd-shift-z": "editor::Redo", "cmd-shift-z": "editor::Redo",
"ctrl-shift-z": "zeta::RateCompletions",
"up": "editor::MoveUp", "up": "editor::MoveUp",
"ctrl-up": "editor::MoveToStartOfParagraph", "ctrl-up": "editor::MoveToStartOfParagraph",
"pageup": "editor::MovePageUp", "pageup": "editor::MovePageUp",
@@ -230,7 +229,7 @@
"context": "MessageEditor > Editor", "context": "MessageEditor > Editor",
"use_key_equivalents": true, "use_key_equivalents": true,
"bindings": { "bindings": {
"enter": "assistant2::Chat" "cmd-enter": "assistant2::Chat"
} }
}, },
{ {
@@ -541,18 +540,12 @@
"context": "Editor && showing_completions", "context": "Editor && showing_completions",
"use_key_equivalents": true, "use_key_equivalents": true,
"bindings": { "bindings": {
"enter": "editor::ConfirmCompletion" "enter": "editor::ConfirmCompletion",
}
},
{
"context": "Editor && !inline_completion && showing_completions",
"use_key_equivalents": true,
"bindings": {
"tab": "editor::ComposeCompletion" "tab": "editor::ComposeCompletion"
} }
}, },
{ {
"context": "Editor && inline_completion", "context": "Editor && inline_completion && !showing_completions",
"use_key_equivalents": true, "use_key_equivalents": true,
"bindings": { "bindings": {
"tab": "editor::AcceptInlineCompletion" "tab": "editor::AcceptInlineCompletion"
@@ -795,24 +788,5 @@
"ctrl-k left": "pane::SplitLeft", "ctrl-k left": "pane::SplitLeft",
"ctrl-k right": "pane::SplitRight" "ctrl-k right": "pane::SplitRight"
} }
},
{
"context": "RateCompletionModal",
"use_key_equivalents": true,
"bindings": {
"cmd-enter": "zeta::ThumbsUp",
"shift-down": "zeta::NextEdit",
"shift-up": "zeta::PreviousEdit",
"right": "zeta::PreviewCompletion"
}
},
{
"context": "RateCompletionModal > Editor",
"use_key_equivalents": true,
"bindings": {
"escape": "zeta::FocusCompletions",
"cmd-shift-enter": "zeta::ThumbsUpActiveCompletion",
"cmd-shift-backspace": "zeta::ThumbsDownActiveCompletion"
}
} }
] ]

View File

@@ -15,10 +15,8 @@
"ctrl-b": "editor::MoveLeft", "ctrl-b": "editor::MoveLeft",
"ctrl-n": "editor::MoveDown", "ctrl-n": "editor::MoveDown",
"ctrl-p": "editor::MoveUp", "ctrl-p": "editor::MoveUp",
"home": ["editor::MoveToBeginningOfLine", { "stop_at_soft_wraps": false }], "ctrl-a": "editor::MoveToBeginningOfLine",
"end": ["editor::MoveToEndOfLine", { "stop_at_soft_wraps": false }], "ctrl-e": "editor::MoveToEndOfLine",
"ctrl-a": ["editor::MoveToBeginningOfLine", { "stop_at_soft_wraps": false }],
"ctrl-e": ["editor::MoveToEndOfLine", { "stop_at_soft_wraps": false }],
"alt-f": "editor::MoveToNextSubwordEnd", "alt-f": "editor::MoveToNextSubwordEnd",
"alt-b": "editor::MoveToPreviousSubwordStart", "alt-b": "editor::MoveToPreviousSubwordStart",
"ctrl-d": "editor::Delete", "ctrl-d": "editor::Delete",
@@ -55,14 +53,6 @@
"shift shift": "file_finder::Toggle" "shift shift": "file_finder::Toggle"
} }
}, },
{
"context": "BufferSearchBar > Editor",
"bindings": {
"ctrl-s": "search::SelectNextMatch",
"ctrl-r": "search::SelectPrevMatch",
"ctrl-g": "buffer_search::Dismiss"
}
},
{ {
"context": "Pane", "context": "Pane",
"bindings": { "bindings": {

View File

@@ -12,7 +12,7 @@
"ctrl->": "zed::IncreaseBufferFontSize", "ctrl->": "zed::IncreaseBufferFontSize",
"ctrl-<": "zed::DecreaseBufferFontSize", "ctrl-<": "zed::DecreaseBufferFontSize",
"ctrl-shift-j": "editor::JoinLines", "ctrl-shift-j": "editor::JoinLines",
"ctrl-d": "editor::DuplicateSelection", "ctrl-d": "editor::DuplicateLineDown",
"ctrl-y": "editor::DeleteLine", "ctrl-y": "editor::DeleteLine",
"ctrl-m": "editor::ScrollCursorCenter", "ctrl-m": "editor::ScrollCursorCenter",
"ctrl-pagedown": "editor::MovePageDown", "ctrl-pagedown": "editor::MovePageDown",

View File

@@ -15,7 +15,7 @@
"ctrl-shift-m": "editor::SelectLargerSyntaxNode", "ctrl-shift-m": "editor::SelectLargerSyntaxNode",
"ctrl-shift-l": "editor::SplitSelectionIntoLines", "ctrl-shift-l": "editor::SplitSelectionIntoLines",
"ctrl-shift-a": "editor::SelectLargerSyntaxNode", "ctrl-shift-a": "editor::SelectLargerSyntaxNode",
"ctrl-shift-d": "editor::DuplicateSelection", "ctrl-shift-d": "editor::DuplicateLineDown",
"alt-f3": "editor::SelectAllMatches", // find_all_under "alt-f3": "editor::SelectAllMatches", // find_all_under
"f12": "editor::GoToDefinition", "f12": "editor::GoToDefinition",
"ctrl-f12": "editor::GoToDefinitionSplit", "ctrl-f12": "editor::GoToDefinitionSplit",

View File

@@ -15,10 +15,8 @@
"ctrl-b": "editor::MoveLeft", "ctrl-b": "editor::MoveLeft",
"ctrl-n": "editor::MoveDown", "ctrl-n": "editor::MoveDown",
"ctrl-p": "editor::MoveUp", "ctrl-p": "editor::MoveUp",
"home": ["editor::MoveToBeginningOfLine", { "stop_at_soft_wraps": false }], "ctrl-a": "editor::MoveToBeginningOfLine",
"end": ["editor::MoveToEndOfLine", { "stop_at_soft_wraps": false }], "ctrl-e": "editor::MoveToEndOfLine",
"ctrl-a": ["editor::MoveToBeginningOfLine", { "stop_at_soft_wraps": false }],
"ctrl-e": ["editor::MoveToEndOfLine", { "stop_at_soft_wraps": false }],
"alt-f": "editor::MoveToNextSubwordEnd", "alt-f": "editor::MoveToNextSubwordEnd",
"alt-b": "editor::MoveToPreviousSubwordStart", "alt-b": "editor::MoveToPreviousSubwordStart",
"ctrl-d": "editor::Delete", "ctrl-d": "editor::Delete",
@@ -55,14 +53,6 @@
"shift shift": "file_finder::Toggle" "shift shift": "file_finder::Toggle"
} }
}, },
{
"context": "BufferSearchBar > Editor",
"bindings": {
"ctrl-s": "search::SelectNextMatch",
"ctrl-r": "search::SelectPrevMatch",
"ctrl-g": "buffer_search::Dismiss"
}
},
{ {
"context": "Pane", "context": "Pane",
"bindings": { "bindings": {

View File

@@ -11,7 +11,7 @@
"ctrl->": "zed::IncreaseBufferFontSize", "ctrl->": "zed::IncreaseBufferFontSize",
"ctrl-<": "zed::DecreaseBufferFontSize", "ctrl-<": "zed::DecreaseBufferFontSize",
"ctrl-shift-j": "editor::JoinLines", "ctrl-shift-j": "editor::JoinLines",
"cmd-d": "editor::DuplicateSelection", "cmd-d": "editor::DuplicateLineDown",
"cmd-backspace": "editor::DeleteLine", "cmd-backspace": "editor::DeleteLine",
"cmd-pagedown": "editor::MovePageDown", "cmd-pagedown": "editor::MovePageDown",
"cmd-pageup": "editor::MovePageUp", "cmd-pageup": "editor::MovePageUp",

View File

@@ -18,7 +18,7 @@
"ctrl-shift-m": "editor::SelectLargerSyntaxNode", "ctrl-shift-m": "editor::SelectLargerSyntaxNode",
"cmd-shift-l": "editor::SplitSelectionIntoLines", "cmd-shift-l": "editor::SplitSelectionIntoLines",
"cmd-shift-a": "editor::SelectLargerSyntaxNode", "cmd-shift-a": "editor::SelectLargerSyntaxNode",
"cmd-shift-d": "editor::DuplicateSelection", "cmd-shift-d": "editor::DuplicateLineDown",
"ctrl-cmd-g": "editor::SelectAllMatches", // find_all_under "ctrl-cmd-g": "editor::SelectAllMatches", // find_all_under
"shift-f12": "editor::FindAllReferences", "shift-f12": "editor::FindAllReferences",
"alt-cmd-down": "editor::GoToDefinition", "alt-cmd-down": "editor::GoToDefinition",

View File

@@ -144,15 +144,15 @@
// 4. Highlight the full line (default): // 4. Highlight the full line (default):
// "all" // "all"
"current_line_highlight": "all", "current_line_highlight": "all",
// The debounce delay before querying highlights from the language
// server based on the current cursor location.
"lsp_highlight_debounce": 75,
// Whether to pop the completions menu while typing in an editor without // Whether to pop the completions menu while typing in an editor without
// explicitly requesting it. // explicitly requesting it.
"show_completions_on_input": true, "show_completions_on_input": true,
// Whether to display inline and alongside documentation for items in the // Whether to display inline and alongside documentation for items in the
// completions menu // completions menu
"show_completion_documentation": true, "show_completion_documentation": true,
// The debounce delay before re-querying the language server for completion
// documentation when not included in original completion list.
"completion_documentation_secondary_query_debounce": 300,
// Show method signatures in the editor, when inside parentheses. // Show method signatures in the editor, when inside parentheses.
"auto_signature_help": false, "auto_signature_help": false,
/// Whether to show the signature help after completion or a bracket pair inserted. /// Whether to show the signature help after completion or a bracket pair inserted.
@@ -474,14 +474,6 @@
// Default width of the chat panel. // Default width of the chat panel.
"default_width": 240 "default_width": 240
}, },
"git_panel": {
// Whether to show the git panel button in the status bar.
"button": true,
// Where to the git panel. Can be 'left' or 'right'.
"dock": "left",
// Default width of the git panel.
"default_width": 360
},
"message_editor": { "message_editor": {
// Whether to automatically replace emoji shortcodes with emoji characters. // Whether to automatically replace emoji shortcodes with emoji characters.
// For example: typing `:wave:` gets replaced with `👋`. // For example: typing `:wave:` gets replaced with `👋`.
@@ -572,11 +564,9 @@
// What to do after closing the current tab. // What to do after closing the current tab.
// //
// 1. Activate the tab that was open previously (default) // 1. Activate the tab that was open previously (default)
// "history" // "History"
// 2. Activate the right neighbour tab if present // 2. Activate the neighbour tab (prefers the right one, if present)
// "neighbour" // "Neighbour"
// 3. Activate the left neighbour tab if present
// "left_neighbour"
"activate_on_close": "history", "activate_on_close": "history",
/// Which files containing diagnostic errors/warnings to mark in the tabs. /// Which files containing diagnostic errors/warnings to mark in the tabs.
/// Diagnostics are only shown when file icons are also active. /// Diagnostics are only shown when file icons are also active.

View File

@@ -55,7 +55,7 @@ use language_model::{
LanguageModelProvider, LanguageModelProviderId, LanguageModelRegistry, Role, LanguageModelProvider, LanguageModelProviderId, LanguageModelRegistry, Role,
ZED_CLOUD_PROVIDER_ID, ZED_CLOUD_PROVIDER_ID,
}; };
use language_model_selector::{LanguageModelSelector, LanguageModelSelectorPopoverMenu}; use language_model_selector::{LanguageModelPickerDelegate, LanguageModelSelector};
use multi_buffer::MultiBufferRow; use multi_buffer::MultiBufferRow;
use picker::{Picker, PickerDelegate}; use picker::{Picker, PickerDelegate};
use project::lsp_store::LocalLspAdapterDelegate; use project::lsp_store::LocalLspAdapterDelegate;
@@ -143,7 +143,7 @@ pub struct AssistantPanel {
languages: Arc<LanguageRegistry>, languages: Arc<LanguageRegistry>,
fs: Arc<dyn Fs>, fs: Arc<dyn Fs>,
subscriptions: Vec<Subscription>, subscriptions: Vec<Subscription>,
model_selector_menu_handle: PopoverMenuHandle<LanguageModelSelector>, model_selector_menu_handle: PopoverMenuHandle<Picker<LanguageModelPickerDelegate>>,
model_summary_editor: View<Editor>, model_summary_editor: View<Editor>,
authenticate_provider_task: Option<(LanguageModelProviderId, Task<()>)>, authenticate_provider_task: Option<(LanguageModelProviderId, Task<()>)>,
configuration_subscription: Option<Subscription>, configuration_subscription: Option<Subscription>,
@@ -305,7 +305,7 @@ impl PickerDelegate for SavedContextPickerDelegate {
ListItem::new(ix) ListItem::new(ix)
.inset(true) .inset(true)
.spacing(ListItemSpacing::Sparse) .spacing(ListItemSpacing::Sparse)
.toggle_state(selected) .selected(selected)
.child(item), .child(item),
) )
} }
@@ -341,12 +341,11 @@ impl AssistantPanel {
) -> Self { ) -> Self {
let model_selector_menu_handle = PopoverMenuHandle::default(); let model_selector_menu_handle = PopoverMenuHandle::default();
let model_summary_editor = cx.new_view(Editor::single_line); let model_summary_editor = cx.new_view(Editor::single_line);
let context_editor_toolbar = cx.new_view(|cx| { let context_editor_toolbar = cx.new_view(|_| {
ContextEditorToolbarItem::new( ContextEditorToolbarItem::new(
workspace, workspace,
model_selector_menu_handle.clone(), model_selector_menu_handle.clone(),
model_summary_editor.clone(), model_summary_editor.clone(),
cx,
) )
}); });
@@ -442,7 +441,7 @@ impl AssistantPanel {
) )
} }
}) })
.toggle_state( .selected(
pane.active_item() pane.active_item()
.map_or(false, |item| item.downcast::<ContextHistory>().is_some()), .map_or(false, |item| item.downcast::<ContextHistory>().is_some()),
); );
@@ -4456,36 +4455,23 @@ impl FollowableItem for ContextEditor {
} }
pub struct ContextEditorToolbarItem { pub struct ContextEditorToolbarItem {
fs: Arc<dyn Fs>,
active_context_editor: Option<WeakView<ContextEditor>>, active_context_editor: Option<WeakView<ContextEditor>>,
model_summary_editor: View<Editor>, model_summary_editor: View<Editor>,
language_model_selector: View<LanguageModelSelector>, model_selector_menu_handle: PopoverMenuHandle<Picker<LanguageModelPickerDelegate>>,
language_model_selector_menu_handle: PopoverMenuHandle<LanguageModelSelector>,
} }
impl ContextEditorToolbarItem { impl ContextEditorToolbarItem {
pub fn new( pub fn new(
workspace: &Workspace, workspace: &Workspace,
model_selector_menu_handle: PopoverMenuHandle<LanguageModelSelector>, model_selector_menu_handle: PopoverMenuHandle<Picker<LanguageModelPickerDelegate>>,
model_summary_editor: View<Editor>, model_summary_editor: View<Editor>,
cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
Self { Self {
fs: workspace.app_state().fs.clone(),
active_context_editor: None, active_context_editor: None,
model_summary_editor, model_summary_editor,
language_model_selector: cx.new_view(|cx| { model_selector_menu_handle,
let fs = workspace.app_state().fs.clone();
LanguageModelSelector::new(
move |model, cx| {
update_settings_file::<AssistantSettings>(
fs.clone(),
cx,
move |settings, _| settings.set_model(model.clone()),
);
},
cx,
)
}),
language_model_selector_menu_handle: model_selector_menu_handle,
} }
} }
@@ -4574,8 +4560,17 @@ impl Render for ContextEditorToolbarItem {
// .map(|remaining_items| format!("Files to scan: {}", remaining_items)) // .map(|remaining_items| format!("Files to scan: {}", remaining_items))
// }) // })
.child( .child(
LanguageModelSelectorPopoverMenu::new( LanguageModelSelector::new(
self.language_model_selector.clone(), {
let fs = self.fs.clone();
move |model, cx| {
update_settings_file::<AssistantSettings>(
fs.clone(),
cx,
move |settings, _| settings.set_model(model.clone()),
);
}
},
ButtonLike::new("active-model") ButtonLike::new("active-model")
.style(ButtonStyle::Subtle) .style(ButtonStyle::Subtle)
.child( .child(
@@ -4621,7 +4616,7 @@ impl Render for ContextEditorToolbarItem {
Tooltip::for_action("Change Model", &ToggleModelSelector, cx) Tooltip::for_action("Change Model", &ToggleModelSelector, cx)
}), }),
) )
.with_handle(self.language_model_selector_menu_handle.clone()), .with_handle(self.model_selector_menu_handle.clone()),
) )
.children(self.render_remaining_tokens(cx)); .children(self.render_remaining_tokens(cx));
@@ -4956,7 +4951,7 @@ fn render_slash_command_output_toggle(
("slash-command-output-fold-indicator", row.0 as u64), ("slash-command-output-fold-indicator", row.0 as u64),
!is_folded, !is_folded,
) )
.toggle_state(is_folded) .selected(is_folded)
.on_click(move |_e, cx| fold(!is_folded, cx)) .on_click(move |_e, cx| fold(!is_folded, cx))
.into_any_element() .into_any_element()
} }
@@ -4971,7 +4966,7 @@ fn fold_toggle(
) -> AnyElement { ) -> AnyElement {
move |row, is_folded, fold, _cx| { move |row, is_folded, fold, _cx| {
Disclosure::new((name, row.0 as u64), !is_folded) Disclosure::new((name, row.0 as u64), !is_folded)
.toggle_state(is_folded) .selected(is_folded)
.on_click(move |_e, cx| fold(!is_folded, cx)) .on_click(move |_e, cx| fold(!is_folded, cx))
.into_any_element() .into_any_element()
} }
@@ -5013,7 +5008,7 @@ fn render_quote_selection_output_toggle(
_cx: &mut WindowContext, _cx: &mut WindowContext,
) -> AnyElement { ) -> AnyElement {
Disclosure::new(("quote-selection-indicator", row.0 as u64), !is_folded) Disclosure::new(("quote-selection-indicator", row.0 as u64), !is_folded)
.toggle_state(is_folded) .selected(is_folded)
.on_click(move |_e, cx| fold(!is_folded, cx)) .on_click(move |_e, cx| fold(!is_folded, cx))
.into_any_element() .into_any_element()
} }
@@ -5036,7 +5031,7 @@ fn render_pending_slash_command_gutter_decoration(
icon = icon.icon_color(Color::Muted); icon = icon.icon_color(Color::Muted);
} }
PendingSlashCommandStatus::Running { .. } => { PendingSlashCommandStatus::Running { .. } => {
icon = icon.toggle_state(true); icon = icon.selected(true);
} }
PendingSlashCommandStatus::Error(_) => icon = icon.icon_color(Color::Error), PendingSlashCommandStatus::Error(_) => icon = icon.icon_color(Color::Error),
} }
@@ -5118,11 +5113,9 @@ fn make_lsp_adapter_delegate(
return Ok(None::<Arc<dyn LspAdapterDelegate>>); return Ok(None::<Arc<dyn LspAdapterDelegate>>);
}; };
let http_client = project.client().http_client().clone(); let http_client = project.client().http_client().clone();
project.lsp_store().update(cx, |_, cx| { project.lsp_store().update(cx, |lsp_store, cx| {
Ok(Some(LocalLspAdapterDelegate::new( Ok(Some(LocalLspAdapterDelegate::new(
project.languages().clone(), lsp_store,
project.environment(),
cx.weak_model(),
&worktree, &worktree,
http_client, http_client,
project.fs().clone(), project.fs().clone(),

View File

@@ -17,7 +17,7 @@ use futures::{
channel::mpsc, channel::mpsc,
stream::{self, StreamExt}, stream::{self, StreamExt},
}; };
use gpui::{prelude::*, AppContext, Model, SharedString, Task, TestAppContext, WeakView}; use gpui::{AppContext, Model, SharedString, Task, TestAppContext, WeakView};
use language::{Buffer, BufferSnapshot, LanguageRegistry, LspAdapterDelegate}; use language::{Buffer, BufferSnapshot, LanguageRegistry, LspAdapterDelegate};
use language_model::{LanguageModelCacheConfiguration, LanguageModelRegistry, Role}; use language_model::{LanguageModelCacheConfiguration, LanguageModelRegistry, Role};
use parking_lot::Mutex; use parking_lot::Mutex;
@@ -35,7 +35,7 @@ use std::{
sync::{atomic::AtomicBool, Arc}, sync::{atomic::AtomicBool, Arc},
}; };
use text::{network::Network, OffsetRangeExt as _, ReplicaId, ToOffset}; use text::{network::Network, OffsetRangeExt as _, ReplicaId, ToOffset};
use ui::{IconName, WindowContext}; use ui::{Context as _, IconName, WindowContext};
use unindent::Unindent; use unindent::Unindent;
use util::{ use util::{
test::{generate_marked_text, marked_text_ranges}, test::{generate_marked_text, marked_text_ranges},

View File

@@ -33,7 +33,7 @@ use language_model::{
LanguageModel, LanguageModelRegistry, LanguageModelRequest, LanguageModelRequestMessage, LanguageModel, LanguageModelRegistry, LanguageModelRequest, LanguageModelRequestMessage,
LanguageModelTextStream, Role, LanguageModelTextStream, Role,
}; };
use language_model_selector::{LanguageModelSelector, LanguageModelSelectorPopoverMenu}; use language_model_selector::LanguageModelSelector;
use language_models::report_assistant_event; use language_models::report_assistant_event;
use multi_buffer::MultiBufferRow; use multi_buffer::MultiBufferRow;
use parking_lot::Mutex; use parking_lot::Mutex;
@@ -1358,8 +1358,8 @@ enum PromptEditorEvent {
struct PromptEditor { struct PromptEditor {
id: InlineAssistId, id: InlineAssistId,
fs: Arc<dyn Fs>,
editor: View<Editor>, editor: View<Editor>,
language_model_selector: View<LanguageModelSelector>,
edited_since_done: bool, edited_since_done: bool,
gutter_dimensions: Arc<Mutex<GutterDimensions>>, gutter_dimensions: Arc<Mutex<GutterDimensions>>,
prompt_history: VecDeque<String>, prompt_history: VecDeque<String>,
@@ -1500,27 +1500,43 @@ impl Render for PromptEditor {
.w(gutter_dimensions.full_width() + (gutter_dimensions.margin / 2.0)) .w(gutter_dimensions.full_width() + (gutter_dimensions.margin / 2.0))
.justify_center() .justify_center()
.gap_2() .gap_2()
.child(LanguageModelSelectorPopoverMenu::new( .child(
self.language_model_selector.clone(), LanguageModelSelector::new(
IconButton::new("context", IconName::SettingsAlt) {
.shape(IconButtonShape::Square) let fs = self.fs.clone();
.icon_size(IconSize::Small) move |model, cx| {
.icon_color(Color::Muted) update_settings_file::<AssistantSettings>(
.tooltip(move |cx| { fs.clone(),
Tooltip::with_meta( cx,
format!( move |settings, _| settings.set_model(model.clone()),
"Using {}", );
LanguageModelRegistry::read_global(cx) }
.active_model() },
.map(|model| model.name().0) IconButton::new("context", IconName::SettingsAlt)
.unwrap_or_else(|| "No model selected".into()), .shape(IconButtonShape::Square)
), .icon_size(IconSize::Small)
None, .icon_color(Color::Muted)
"Change Model", .tooltip(move |cx| {
cx, Tooltip::with_meta(
) format!(
}), "Using {}",
)) LanguageModelRegistry::read_global(cx)
.active_model()
.map(|model| model.name().0)
.unwrap_or_else(|| "No model selected".into()),
),
None,
"Change Model",
cx,
)
}),
)
.info_text(
"Inline edits use context\n\
from the currently selected\n\
assistant panel tab.",
),
)
.map(|el| { .map(|el| {
let CodegenStatus::Error(error) = self.codegen.read(cx).status(cx) else { let CodegenStatus::Error(error) = self.codegen.read(cx).status(cx) else {
return el; return el;
@@ -1534,7 +1550,7 @@ impl Render for PromptEditor {
v_flex() v_flex()
.child( .child(
IconButton::new("rate-limit-error", IconName::XCircle) IconButton::new("rate-limit-error", IconName::XCircle)
.toggle_state(self.show_rate_limit_notice) .selected(self.show_rate_limit_notice)
.shape(IconButtonShape::Square) .shape(IconButtonShape::Square)
.icon_size(IconSize::Small) .icon_size(IconSize::Small)
.on_click(cx.listener(Self::toggle_rate_limit_notice)), .on_click(cx.listener(Self::toggle_rate_limit_notice)),
@@ -1626,19 +1642,6 @@ impl PromptEditor {
let mut this = Self { let mut this = Self {
id, id,
editor: prompt_editor, editor: prompt_editor,
language_model_selector: cx.new_view(|cx| {
let fs = fs.clone();
LanguageModelSelector::new(
move |model, cx| {
update_settings_file::<AssistantSettings>(
fs.clone(),
cx,
move |settings, _| settings.set_model(model.clone()),
);
},
cx,
)
}),
edited_since_done: false, edited_since_done: false,
gutter_dimensions, gutter_dimensions,
prompt_history, prompt_history,
@@ -1647,6 +1650,7 @@ impl PromptEditor {
_codegen_subscription: cx.observe(&codegen, Self::handle_codegen_changed), _codegen_subscription: cx.observe(&codegen, Self::handle_codegen_changed),
editor_subscriptions: Vec::new(), editor_subscriptions: Vec::new(),
codegen, codegen,
fs,
pending_token_count: Task::ready(Ok(())), pending_token_count: Task::ready(Ok(())),
token_counts: None, token_counts: None,
_token_count_subscriptions: token_count_subscriptions, _token_count_subscriptions: token_count_subscriptions,
@@ -2133,15 +2137,15 @@ impl PromptEditor {
"dont-show-again", "dont-show-again",
Label::new("Don't show again"), Label::new("Don't show again"),
if dismissed_rate_limit_notice() { if dismissed_rate_limit_notice() {
ui::ToggleState::Selected ui::Selection::Selected
} else { } else {
ui::ToggleState::Unselected ui::Selection::Unselected
}, },
|selection, cx| { |selection, cx| {
let is_dismissed = match selection { let is_dismissed = match selection {
ui::ToggleState::Unselected => false, ui::Selection::Unselected => false,
ui::ToggleState::Indeterminate => return, ui::Selection::Indeterminate => return,
ui::ToggleState::Selected => true, ui::Selection::Selected => true,
}; };
set_rate_limit_notice_dismissed(is_dismissed, cx) set_rate_limit_notice_dismissed(is_dismissed, cx)

View File

@@ -11,8 +11,8 @@ use futures::{
use fuzzy::StringMatchCandidate; use fuzzy::StringMatchCandidate;
use gpui::{ use gpui::{
actions, point, size, transparent_black, Action, AppContext, BackgroundExecutor, Bounds, actions, point, size, transparent_black, Action, AppContext, BackgroundExecutor, Bounds,
EventEmitter, Global, PromptLevel, ReadGlobal, Subscription, Task, TextStyle, TitlebarOptions, EventEmitter, Global, HighlightStyle, PromptLevel, ReadGlobal, Subscription, Task, TextStyle,
UpdateGlobal, View, WindowBounds, WindowHandle, WindowOptions, TitlebarOptions, UpdateGlobal, View, WindowBounds, WindowHandle, WindowOptions,
}; };
use heed::{ use heed::{
types::{SerdeBincode, SerdeJson, Str}, types::{SerdeBincode, SerdeJson, Str},
@@ -232,13 +232,13 @@ impl PickerDelegate for PromptPickerDelegate {
let element = ListItem::new(ix) let element = ListItem::new(ix)
.inset(true) .inset(true)
.spacing(ListItemSpacing::Sparse) .spacing(ListItemSpacing::Sparse)
.toggle_state(selected) .selected(selected)
.child(h_flex().h_5().line_height(relative(1.)).child(Label::new( .child(h_flex().h_5().line_height(relative(1.)).child(Label::new(
prompt.title.clone().unwrap_or("Untitled".into()), prompt.title.clone().unwrap_or("Untitled".into()),
))) )))
.end_slot::<IconButton>(default.then(|| { .end_slot::<IconButton>(default.then(|| {
IconButton::new("toggle-default-prompt", IconName::SparkleFilled) IconButton::new("toggle-default-prompt", IconName::SparkleFilled)
.toggle_state(true) .selected(true)
.icon_color(Color::Accent) .icon_color(Color::Accent)
.shape(IconButtonShape::Square) .shape(IconButtonShape::Square)
.tooltip(move |cx| Tooltip::text("Remove from Default Prompt", cx)) .tooltip(move |cx| Tooltip::text("Remove from Default Prompt", cx))
@@ -274,7 +274,7 @@ impl PickerDelegate for PromptPickerDelegate {
}) })
.child( .child(
IconButton::new("toggle-default-prompt", IconName::Sparkle) IconButton::new("toggle-default-prompt", IconName::Sparkle)
.toggle_state(default) .selected(default)
.selected_icon(IconName::SparkleFilled) .selected_icon(IconName::SparkleFilled)
.icon_color(if default { Color::Accent } else { Color::Muted }) .icon_color(if default { Color::Accent } else { Color::Muted })
.shape(IconButtonShape::Square) .shape(IconButtonShape::Square)
@@ -928,8 +928,10 @@ impl PromptLibrary {
status: cx.theme().status().clone(), status: cx.theme().status().clone(),
inlay_hints_style: inlay_hints_style:
editor::make_inlay_hints_style(cx), editor::make_inlay_hints_style(cx),
inline_completion_styles: suggestions_style: HighlightStyle {
editor::make_suggestion_styles(cx), color: Some(cx.theme().status().predictive),
..HighlightStyle::default()
},
..EditorStyle::default() ..EditorStyle::default()
}, },
)), )),
@@ -1053,7 +1055,7 @@ impl PromptLibrary {
IconName::Sparkle, IconName::Sparkle,
) )
.style(ButtonStyle::Transparent) .style(ButtonStyle::Transparent)
.toggle_state(prompt_metadata.default) .selected(prompt_metadata.default)
.selected_icon(IconName::SparkleFilled) .selected_icon(IconName::SparkleFilled)
.icon_color(if prompt_metadata.default { .icon_color(if prompt_metadata.default {
Color::Accent Color::Accent

View File

@@ -176,7 +176,7 @@ impl PickerDelegate for SlashCommandDelegate {
ListItem::new(ix) ListItem::new(ix)
.inset(true) .inset(true)
.spacing(ListItemSpacing::Dense) .spacing(ListItemSpacing::Dense)
.toggle_state(selected) .selected(selected)
.tooltip({ .tooltip({
let description = info.description.clone(); let description = info.description.clone();
move |cx| cx.new_view(|_| Tooltip::new(description.clone())).into() move |cx| cx.new_view(|_| Tooltip::new(description.clone())).into()
@@ -229,7 +229,7 @@ impl PickerDelegate for SlashCommandDelegate {
ListItem::new(ix) ListItem::new(ix)
.inset(true) .inset(true)
.spacing(ListItemSpacing::Dense) .spacing(ListItemSpacing::Dense)
.toggle_state(selected) .selected(selected)
.child(renderer(cx)), .child(renderer(cx)),
), ),
} }

View File

@@ -20,7 +20,7 @@ use language::Buffer;
use language_model::{ use language_model::{
LanguageModelRegistry, LanguageModelRequest, LanguageModelRequestMessage, Role, LanguageModelRegistry, LanguageModelRequest, LanguageModelRequestMessage, Role,
}; };
use language_model_selector::{LanguageModelSelector, LanguageModelSelectorPopoverMenu}; use language_model_selector::LanguageModelSelector;
use language_models::report_assistant_event; use language_models::report_assistant_event;
use settings::{update_settings_file, Settings}; use settings::{update_settings_file, Settings};
use std::{ use std::{
@@ -476,9 +476,9 @@ enum PromptEditorEvent {
struct PromptEditor { struct PromptEditor {
id: TerminalInlineAssistId, id: TerminalInlineAssistId,
fs: Arc<dyn Fs>,
height_in_lines: u8, height_in_lines: u8,
editor: View<Editor>, editor: View<Editor>,
language_model_selector: View<LanguageModelSelector>,
edited_since_done: bool, edited_since_done: bool,
prompt_history: VecDeque<String>, prompt_history: VecDeque<String>,
prompt_history_ix: Option<usize>, prompt_history_ix: Option<usize>,
@@ -614,8 +614,17 @@ impl Render for PromptEditor {
.w_12() .w_12()
.justify_center() .justify_center()
.gap_2() .gap_2()
.child(LanguageModelSelectorPopoverMenu::new( .child(LanguageModelSelector::new(
self.language_model_selector.clone(), {
let fs = self.fs.clone();
move |model, cx| {
update_settings_file::<AssistantSettings>(
fs.clone(),
cx,
move |settings, _| settings.set_model(model.clone()),
);
}
},
IconButton::new("context", IconName::SettingsAlt) IconButton::new("context", IconName::SettingsAlt)
.shape(IconButtonShape::Square) .shape(IconButtonShape::Square)
.icon_size(IconSize::Small) .icon_size(IconSize::Small)
@@ -709,19 +718,6 @@ impl PromptEditor {
id, id,
height_in_lines: 1, height_in_lines: 1,
editor: prompt_editor, editor: prompt_editor,
language_model_selector: cx.new_view(|cx| {
let fs = fs.clone();
LanguageModelSelector::new(
move |model, cx| {
update_settings_file::<AssistantSettings>(
fs.clone(),
cx,
move |settings, _| settings.set_model(model.clone()),
);
},
cx,
)
}),
edited_since_done: false, edited_since_done: false,
prompt_history, prompt_history,
prompt_history_ix: None, prompt_history_ix: None,
@@ -729,6 +725,7 @@ impl PromptEditor {
_codegen_subscription: cx.observe(&codegen, Self::handle_codegen_changed), _codegen_subscription: cx.observe(&codegen, Self::handle_codegen_changed),
editor_subscriptions: Vec::new(), editor_subscriptions: Vec::new(),
codegen, codegen,
fs,
pending_token_count: Task::ready(Ok(())), pending_token_count: Task::ready(Ok(())),
token_count: None, token_count: None,
_token_count_subscriptions: token_count_subscriptions, _token_count_subscriptions: token_count_subscriptions,

View File

@@ -13,55 +13,30 @@ path = "src/assistant.rs"
doctest = false doctest = false
[dependencies] [dependencies]
anthropic = { workspace = true, features = ["schemars"] }
anyhow.workspace = true anyhow.workspace = true
assets.workspace = true
assistant_tool.workspace = true assistant_tool.workspace = true
async-watch.workspace = true
client.workspace = true
chrono.workspace = true chrono.workspace = true
client.workspace = true
collections.workspace = true collections.workspace = true
command_palette_hooks.workspace = true command_palette_hooks.workspace = true
context_server.workspace = true context_server.workspace = true
db.workspace = true
editor.workspace = true editor.workspace = true
feature_flags.workspace = true feature_flags.workspace = true
fs.workspace = true
futures.workspace = true futures.workspace = true
fuzzy.workspace = true
gpui.workspace = true gpui.workspace = true
handlebars.workspace = true
html_to_markdown.workspace = true
http_client.workspace = true
language.workspace = true language.workspace = true
language_model.workspace = true language_model.workspace = true
language_model_selector.workspace = true language_model_selector.workspace = true
language_models.workspace = true language_models.workspace = true
log.workspace = true log.workspace = true
lsp.workspace = true
markdown.workspace = true markdown.workspace = true
menu.workspace = true
multi_buffer.workspace = true
ollama = { workspace = true, features = ["schemars"] }
open_ai = { workspace = true, features = ["schemars"] }
ordered-float.workspace = true
paths.workspace = true
parking_lot.workspace = true
picker.workspace = true picker.workspace = true
project.workspace = true project.workspace = true
proto.workspace = true proto.workspace = true
rope.workspace = true
schemars.workspace = true
serde.workspace = true serde.workspace = true
serde_json.workspace = true serde_json.workspace = true
serde_json_lenient.workspace = true
settings.workspace = true settings.workspace = true
similar.workspace = true
smol.workspace = true smol.workspace = true
telemetry_events.workspace = true
terminal_view.workspace = true
text.workspace = true
terminal.workspace = true
theme.workspace = true theme.workspace = true
time.workspace = true time.workspace = true
time_format.workspace = true time_format.workspace = true
@@ -70,8 +45,3 @@ unindent.workspace = true
util.workspace = true util.workspace = true
uuid.workspace = true uuid.workspace = true
workspace.workspace = true workspace.workspace = true
zed_actions.workspace = true
[dev-dependencies]
rand.workspace = true
indoc.workspace = true

View File

@@ -15,7 +15,6 @@ use ui::prelude::*;
use workspace::Workspace; use workspace::Workspace;
use crate::thread::{MessageId, Thread, ThreadError, ThreadEvent}; use crate::thread::{MessageId, Thread, ThreadError, ThreadEvent};
use crate::ui::ContextPill;
pub struct ActiveThread { pub struct ActiveThread {
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
@@ -203,8 +202,6 @@ impl ActiveThread {
return Empty.into_any(); return Empty.into_any();
}; };
let context = self.thread.read(cx).context_for_message(message_id);
let (role_icon, role_name) = match message.role { let (role_icon, role_name) = match message.role {
Role::User => (IconName::Person, "You"), Role::User => (IconName::Person, "You"),
Role::Assistant => (IconName::ZedAssistant, "Assistant"), Role::Assistant => (IconName::ZedAssistant, "Assistant"),
@@ -232,16 +229,7 @@ impl ActiveThread {
.child(Label::new(role_name).size(LabelSize::Small)), .child(Label::new(role_name).size(LabelSize::Small)),
), ),
) )
.child(v_flex().p_1p5().text_ui(cx).child(markdown.clone())) .child(v_flex().p_1p5().text_ui(cx).child(markdown.clone())),
.when_some(context, |parent, context| {
parent.child(
h_flex().flex_wrap().gap_2().p_1p5().children(
context
.iter()
.map(|context| ContextPill::new(context.clone())),
),
)
}),
) )
.into_any() .into_any()
} }

View File

@@ -1,29 +1,14 @@
mod active_thread; mod active_thread;
mod assistant_panel; mod assistant_panel;
mod assistant_settings;
mod context;
mod context_picker; mod context_picker;
mod inline_assistant;
mod message_editor; mod message_editor;
mod prompts;
mod streaming_diff;
mod terminal_inline_assistant;
mod thread; mod thread;
mod thread_history; mod thread_history;
mod thread_store; mod thread_store;
mod ui;
use std::sync::Arc;
use assistant_settings::AssistantSettings;
use client::Client;
use command_palette_hooks::CommandPaletteFilter; use command_palette_hooks::CommandPaletteFilter;
use feature_flags::{Assistant2FeatureFlag, FeatureFlagAppExt}; use feature_flags::{Assistant2FeatureFlag, FeatureFlagAppExt};
use fs::Fs;
use gpui::{actions, AppContext}; use gpui::{actions, AppContext};
use prompts::PromptLoadingParams;
use settings::Settings as _;
use util::ResultExt;
pub use crate::assistant_panel::AssistantPanel; pub use crate::assistant_panel::AssistantPanel;
@@ -34,43 +19,15 @@ actions!(
NewThread, NewThread,
ToggleModelSelector, ToggleModelSelector,
OpenHistory, OpenHistory,
Chat, Chat
ToggleInlineAssist,
CycleNextInlineAssist,
CyclePreviousInlineAssist
] ]
); );
const NAMESPACE: &str = "assistant2"; const NAMESPACE: &str = "assistant2";
/// Initializes the `assistant2` crate. /// Initializes the `assistant2` crate.
pub fn init(fs: Arc<dyn Fs>, client: Arc<Client>, stdout_is_a_pty: bool, cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
AssistantSettings::register(cx);
assistant_panel::init(cx); assistant_panel::init(cx);
let prompt_builder = prompts::PromptBuilder::new(Some(PromptLoadingParams {
fs: fs.clone(),
repo_path: stdout_is_a_pty
.then(|| std::env::current_dir().log_err())
.flatten(),
cx,
}))
.log_err()
.map(Arc::new)
.unwrap_or_else(|| Arc::new(prompts::PromptBuilder::new(None).unwrap()));
inline_assistant::init(
fs.clone(),
prompt_builder.clone(),
client.telemetry().clone(),
cx,
);
terminal_inline_assistant::init(
fs.clone(),
prompt_builder.clone(),
client.telemetry().clone(),
cx,
);
feature_gate_assistant2_actions(cx); feature_gate_assistant2_actions(cx);
} }

View File

@@ -9,8 +9,10 @@ use gpui::{
WindowContext, WindowContext,
}; };
use language::LanguageRegistry; use language::LanguageRegistry;
use language_model::LanguageModelRegistry;
use language_model_selector::LanguageModelSelector;
use time::UtcOffset; use time::UtcOffset;
use ui::{prelude::*, Divider, IconButtonShape, KeyBinding, Tab, Tooltip}; use ui::{prelude::*, ButtonLike, Divider, IconButtonShape, KeyBinding, Tab, Tooltip};
use workspace::dock::{DockPosition, Panel, PanelEvent}; use workspace::dock::{DockPosition, Panel, PanelEvent};
use workspace::Workspace; use workspace::Workspace;
@@ -19,7 +21,7 @@ use crate::message_editor::MessageEditor;
use crate::thread::{ThreadError, ThreadId}; use crate::thread::{ThreadError, ThreadId};
use crate::thread_history::{PastThread, ThreadHistory}; use crate::thread_history::{PastThread, ThreadHistory};
use crate::thread_store::ThreadStore; use crate::thread_store::ThreadStore;
use crate::{NewThread, OpenHistory, ToggleFocus}; use crate::{NewThread, OpenHistory, ToggleFocus, ToggleModelSelector};
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
cx.observe_new_views( cx.observe_new_views(
@@ -88,13 +90,13 @@ impl AssistantPanel {
thread: cx.new_view(|cx| { thread: cx.new_view(|cx| {
ActiveThread::new( ActiveThread::new(
thread.clone(), thread.clone(),
workspace.clone(), workspace,
language_registry, language_registry,
tools.clone(), tools.clone(),
cx, cx,
) )
}), }),
message_editor: cx.new_view(|cx| MessageEditor::new(workspace, thread.clone(), cx)), message_editor: cx.new_view(|cx| MessageEditor::new(thread.clone(), cx)),
tools, tools,
local_timezone: UtcOffset::from_whole_seconds( local_timezone: UtcOffset::from_whole_seconds(
chrono::Local::now().offset().local_minus_utc(), chrono::Local::now().offset().local_minus_utc(),
@@ -123,8 +125,7 @@ impl AssistantPanel {
cx, cx,
) )
}); });
self.message_editor = self.message_editor = cx.new_view(|cx| MessageEditor::new(thread, cx));
cx.new_view(|cx| MessageEditor::new(self.workspace.clone(), thread, cx));
self.message_editor.focus_handle(cx).focus(cx); self.message_editor.focus_handle(cx).focus(cx);
} }
@@ -146,8 +147,7 @@ impl AssistantPanel {
cx, cx,
) )
}); });
self.message_editor = self.message_editor = cx.new_view(|cx| MessageEditor::new(thread, cx));
cx.new_view(|cx| MessageEditor::new(self.workspace.clone(), thread, cx));
self.message_editor.focus_handle(cx).focus(cx); self.message_editor.focus_handle(cx).focus(cx);
} }
@@ -225,6 +225,7 @@ impl AssistantPanel {
.child( .child(
h_flex() h_flex()
.gap(DynamicSpacing::Base08.rems(cx)) .gap(DynamicSpacing::Base08.rems(cx))
.child(self.render_language_model_selector(cx))
.child(Divider::vertical()) .child(Divider::vertical())
.child( .child(
IconButton::new("new-thread", IconName::Plus) IconButton::new("new-thread", IconName::Plus)
@@ -279,6 +280,57 @@ impl AssistantPanel {
) )
} }
fn render_language_model_selector(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let active_provider = LanguageModelRegistry::read_global(cx).active_provider();
let active_model = LanguageModelRegistry::read_global(cx).active_model();
LanguageModelSelector::new(
|model, _cx| {
println!("Selected {:?}", model.name());
},
ButtonLike::new("active-model")
.style(ButtonStyle::Subtle)
.child(
h_flex()
.w_full()
.gap_0p5()
.child(
div()
.overflow_x_hidden()
.flex_grow()
.whitespace_nowrap()
.child(match (active_provider, active_model) {
(Some(provider), Some(model)) => h_flex()
.gap_1()
.child(
Icon::new(
model.icon().unwrap_or_else(|| provider.icon()),
)
.color(Color::Muted)
.size(IconSize::XSmall),
)
.child(
Label::new(model.name().0)
.size(LabelSize::Small)
.color(Color::Muted),
)
.into_any_element(),
_ => Label::new("No model selected")
.size(LabelSize::Small)
.color(Color::Muted)
.into_any_element(),
}),
)
.child(
Icon::new(IconName::ChevronDown)
.color(Color::Muted)
.size(IconSize::XSmall),
),
)
.tooltip(move |cx| Tooltip::for_action("Change Model", &ToggleModelSelector, cx)),
)
}
fn render_active_thread_or_empty_state(&self, cx: &mut ViewContext<Self>) -> AnyElement { fn render_active_thread_or_empty_state(&self, cx: &mut ViewContext<Self>) -> AnyElement {
if self.thread.read(cx).is_empty() { if self.thread.read(cx).is_empty() {
return self.render_thread_empty_state(cx).into_any_element(); return self.render_thread_empty_state(cx).into_any_element();
@@ -306,6 +358,46 @@ impl AssistantPanel {
.mb_4(), .mb_4(),
), ),
) )
.child(v_flex())
.child(
h_flex()
.w_full()
.justify_center()
.child(Label::new("Context Examples:").size(LabelSize::Small)),
)
.child(
h_flex()
.gap_2()
.justify_center()
.child(
h_flex()
.gap_1()
.p_0p5()
.rounded_md()
.border_1()
.border_color(cx.theme().colors().border_variant)
.child(
Icon::new(IconName::Terminal)
.size(IconSize::Small)
.color(Color::Disabled),
)
.child(Label::new("Terminal").size(LabelSize::Small)),
)
.child(
h_flex()
.gap_1()
.p_0p5()
.rounded_md()
.border_1()
.border_color(cx.theme().colors().border_variant)
.child(
Icon::new(IconName::Folder)
.size(IconSize::Small)
.color(Color::Disabled),
)
.child(Label::new("/src/components").size(LabelSize::Small)),
),
)
.when(!recent_threads.is_empty(), |parent| { .when(!recent_threads.is_empty(), |parent| {
parent parent
.child( .child(

View File

@@ -1,485 +0,0 @@
use std::sync::Arc;
use ::open_ai::Model as OpenAiModel;
use anthropic::Model as AnthropicModel;
use gpui::Pixels;
use language_model::{CloudModel, LanguageModel};
use ollama::Model as OllamaModel;
use schemars::{schema::Schema, JsonSchema};
use serde::{Deserialize, Serialize};
use settings::{Settings, SettingsSources};
#[derive(Copy, Clone, Default, Debug, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum AssistantDockPosition {
Left,
#[default]
Right,
Bottom,
}
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
#[serde(tag = "name", rename_all = "snake_case")]
pub enum AssistantProviderContentV1 {
#[serde(rename = "zed.dev")]
ZedDotDev { default_model: Option<CloudModel> },
#[serde(rename = "openai")]
OpenAi {
default_model: Option<OpenAiModel>,
api_url: Option<String>,
available_models: Option<Vec<OpenAiModel>>,
},
#[serde(rename = "anthropic")]
Anthropic {
default_model: Option<AnthropicModel>,
api_url: Option<String>,
},
#[serde(rename = "ollama")]
Ollama {
default_model: Option<OllamaModel>,
api_url: Option<String>,
},
}
#[derive(Debug, Default)]
pub struct AssistantSettings {
pub enabled: bool,
pub button: bool,
pub dock: AssistantDockPosition,
pub default_width: Pixels,
pub default_height: Pixels,
pub default_model: LanguageModelSelection,
pub inline_alternatives: Vec<LanguageModelSelection>,
pub using_outdated_settings_version: bool,
pub enable_experimental_live_diffs: bool,
}
/// Assistant panel settings
#[derive(Clone, Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum AssistantSettingsContent {
Versioned(VersionedAssistantSettingsContent),
Legacy(LegacyAssistantSettingsContent),
}
impl JsonSchema for AssistantSettingsContent {
fn schema_name() -> String {
VersionedAssistantSettingsContent::schema_name()
}
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> Schema {
VersionedAssistantSettingsContent::json_schema(gen)
}
fn is_referenceable() -> bool {
VersionedAssistantSettingsContent::is_referenceable()
}
}
impl Default for AssistantSettingsContent {
fn default() -> Self {
Self::Versioned(VersionedAssistantSettingsContent::default())
}
}
impl AssistantSettingsContent {
pub fn is_version_outdated(&self) -> bool {
match self {
AssistantSettingsContent::Versioned(settings) => match settings {
VersionedAssistantSettingsContent::V1(_) => true,
VersionedAssistantSettingsContent::V2(_) => false,
},
AssistantSettingsContent::Legacy(_) => true,
}
}
fn upgrade(&self) -> AssistantSettingsContentV2 {
match self {
AssistantSettingsContent::Versioned(settings) => match settings {
VersionedAssistantSettingsContent::V1(settings) => AssistantSettingsContentV2 {
enabled: settings.enabled,
button: settings.button,
dock: settings.dock,
default_width: settings.default_width,
default_height: settings.default_width,
default_model: settings
.provider
.clone()
.and_then(|provider| match provider {
AssistantProviderContentV1::ZedDotDev { default_model } => {
default_model.map(|model| LanguageModelSelection {
provider: "zed.dev".to_string(),
model: model.id().to_string(),
})
}
AssistantProviderContentV1::OpenAi { default_model, .. } => {
default_model.map(|model| LanguageModelSelection {
provider: "openai".to_string(),
model: model.id().to_string(),
})
}
AssistantProviderContentV1::Anthropic { default_model, .. } => {
default_model.map(|model| LanguageModelSelection {
provider: "anthropic".to_string(),
model: model.id().to_string(),
})
}
AssistantProviderContentV1::Ollama { default_model, .. } => {
default_model.map(|model| LanguageModelSelection {
provider: "ollama".to_string(),
model: model.id().to_string(),
})
}
}),
inline_alternatives: None,
enable_experimental_live_diffs: None,
},
VersionedAssistantSettingsContent::V2(settings) => settings.clone(),
},
AssistantSettingsContent::Legacy(settings) => AssistantSettingsContentV2 {
enabled: None,
button: settings.button,
dock: settings.dock,
default_width: settings.default_width,
default_height: settings.default_height,
default_model: Some(LanguageModelSelection {
provider: "openai".to_string(),
model: settings
.default_open_ai_model
.clone()
.unwrap_or_default()
.id()
.to_string(),
}),
inline_alternatives: None,
enable_experimental_live_diffs: None,
},
}
}
pub fn set_model(&mut self, language_model: Arc<dyn LanguageModel>) {
let model = language_model.id().0.to_string();
let provider = language_model.provider_id().0.to_string();
match self {
AssistantSettingsContent::Versioned(settings) => match settings {
VersionedAssistantSettingsContent::V1(settings) => match provider.as_ref() {
"zed.dev" => {
log::warn!("attempted to set zed.dev model on outdated settings");
}
"anthropic" => {
let api_url = match &settings.provider {
Some(AssistantProviderContentV1::Anthropic { api_url, .. }) => {
api_url.clone()
}
_ => None,
};
settings.provider = Some(AssistantProviderContentV1::Anthropic {
default_model: AnthropicModel::from_id(&model).ok(),
api_url,
});
}
"ollama" => {
let api_url = match &settings.provider {
Some(AssistantProviderContentV1::Ollama { api_url, .. }) => {
api_url.clone()
}
_ => None,
};
settings.provider = Some(AssistantProviderContentV1::Ollama {
default_model: Some(ollama::Model::new(&model, None, None)),
api_url,
});
}
"openai" => {
let (api_url, available_models) = match &settings.provider {
Some(AssistantProviderContentV1::OpenAi {
api_url,
available_models,
..
}) => (api_url.clone(), available_models.clone()),
_ => (None, None),
};
settings.provider = Some(AssistantProviderContentV1::OpenAi {
default_model: OpenAiModel::from_id(&model).ok(),
api_url,
available_models,
});
}
_ => {}
},
VersionedAssistantSettingsContent::V2(settings) => {
settings.default_model = Some(LanguageModelSelection { provider, model });
}
},
AssistantSettingsContent::Legacy(settings) => {
if let Ok(model) = OpenAiModel::from_id(&language_model.id().0) {
settings.default_open_ai_model = Some(model);
}
}
}
}
}
#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug)]
#[serde(tag = "version")]
pub enum VersionedAssistantSettingsContent {
#[serde(rename = "1")]
V1(AssistantSettingsContentV1),
#[serde(rename = "2")]
V2(AssistantSettingsContentV2),
}
impl Default for VersionedAssistantSettingsContent {
fn default() -> Self {
Self::V2(AssistantSettingsContentV2 {
enabled: None,
button: None,
dock: None,
default_width: None,
default_height: None,
default_model: None,
inline_alternatives: None,
enable_experimental_live_diffs: None,
})
}
}
#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug)]
pub struct AssistantSettingsContentV2 {
/// Whether the Assistant is enabled.
///
/// Default: true
enabled: Option<bool>,
/// Whether to show the assistant panel button in the status bar.
///
/// Default: true
button: Option<bool>,
/// Where to dock the assistant.
///
/// Default: right
dock: Option<AssistantDockPosition>,
/// Default width in pixels when the assistant is docked to the left or right.
///
/// Default: 640
default_width: Option<f32>,
/// Default height in pixels when the assistant is docked to the bottom.
///
/// Default: 320
default_height: Option<f32>,
/// The default model to use when creating new chats.
default_model: Option<LanguageModelSelection>,
/// Additional models with which to generate alternatives when performing inline assists.
inline_alternatives: Option<Vec<LanguageModelSelection>>,
/// Enable experimental live diffs in the assistant panel.
///
/// Default: false
enable_experimental_live_diffs: Option<bool>,
}
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
pub struct LanguageModelSelection {
#[schemars(schema_with = "providers_schema")]
pub provider: String,
pub model: String,
}
fn providers_schema(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
schemars::schema::SchemaObject {
enum_values: Some(vec![
"anthropic".into(),
"google".into(),
"ollama".into(),
"openai".into(),
"zed.dev".into(),
"copilot_chat".into(),
]),
..Default::default()
}
.into()
}
impl Default for LanguageModelSelection {
fn default() -> Self {
Self {
provider: "openai".to_string(),
model: "gpt-4".to_string(),
}
}
}
#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug)]
pub struct AssistantSettingsContentV1 {
/// Whether the Assistant is enabled.
///
/// Default: true
enabled: Option<bool>,
/// Whether to show the assistant panel button in the status bar.
///
/// Default: true
button: Option<bool>,
/// Where to dock the assistant.
///
/// Default: right
dock: Option<AssistantDockPosition>,
/// Default width in pixels when the assistant is docked to the left or right.
///
/// Default: 640
default_width: Option<f32>,
/// Default height in pixels when the assistant is docked to the bottom.
///
/// Default: 320
default_height: Option<f32>,
/// The provider of the assistant service.
///
/// This can be "openai", "anthropic", "ollama", "zed.dev"
/// each with their respective default models and configurations.
provider: Option<AssistantProviderContentV1>,
}
#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug)]
pub struct LegacyAssistantSettingsContent {
/// Whether to show the assistant panel button in the status bar.
///
/// Default: true
pub button: Option<bool>,
/// Where to dock the assistant.
///
/// Default: right
pub dock: Option<AssistantDockPosition>,
/// Default width in pixels when the assistant is docked to the left or right.
///
/// Default: 640
pub default_width: Option<f32>,
/// Default height in pixels when the assistant is docked to the bottom.
///
/// Default: 320
pub default_height: Option<f32>,
/// The default OpenAI model to use when creating new chats.
///
/// Default: gpt-4-1106-preview
pub default_open_ai_model: Option<OpenAiModel>,
/// OpenAI API base URL to use when creating new chats.
///
/// Default: https://api.openai.com/v1
pub openai_api_url: Option<String>,
}
impl Settings for AssistantSettings {
const KEY: Option<&'static str> = Some("assistant");
const PRESERVED_KEYS: Option<&'static [&'static str]> = Some(&["version"]);
type FileContent = AssistantSettingsContent;
fn load(
sources: SettingsSources<Self::FileContent>,
_: &mut gpui::AppContext,
) -> anyhow::Result<Self> {
let mut settings = AssistantSettings::default();
for value in sources.defaults_and_customizations() {
if value.is_version_outdated() {
settings.using_outdated_settings_version = true;
}
let value = value.upgrade();
merge(&mut settings.enabled, value.enabled);
merge(&mut settings.button, value.button);
merge(&mut settings.dock, value.dock);
merge(
&mut settings.default_width,
value.default_width.map(Into::into),
);
merge(
&mut settings.default_height,
value.default_height.map(Into::into),
);
merge(&mut settings.default_model, value.default_model);
merge(&mut settings.inline_alternatives, value.inline_alternatives);
merge(
&mut settings.enable_experimental_live_diffs,
value.enable_experimental_live_diffs,
);
}
Ok(settings)
}
}
fn merge<T>(target: &mut T, value: Option<T>) {
if let Some(value) = value {
*target = value;
}
}
#[cfg(test)]
mod tests {
use fs::Fs;
use gpui::{ReadGlobal, TestAppContext};
use super::*;
#[gpui::test]
async fn test_deserialize_assistant_settings_with_version(cx: &mut TestAppContext) {
let fs = fs::FakeFs::new(cx.executor().clone());
fs.create_dir(paths::settings_file().parent().unwrap())
.await
.unwrap();
cx.update(|cx| {
let test_settings = settings::SettingsStore::test(cx);
cx.set_global(test_settings);
AssistantSettings::register(cx);
});
cx.update(|cx| {
assert!(!AssistantSettings::get_global(cx).using_outdated_settings_version);
assert_eq!(
AssistantSettings::get_global(cx).default_model,
LanguageModelSelection {
provider: "zed.dev".into(),
model: "claude-3-5-sonnet".into(),
}
);
});
cx.update(|cx| {
settings::SettingsStore::global(cx).update_settings_file::<AssistantSettings>(
fs.clone(),
|settings, _| {
*settings = AssistantSettingsContent::Versioned(
VersionedAssistantSettingsContent::V2(AssistantSettingsContentV2 {
default_model: Some(LanguageModelSelection {
provider: "test-provider".into(),
model: "gpt-99".into(),
}),
inline_alternatives: None,
enabled: None,
button: None,
dock: None,
default_width: None,
default_height: None,
enable_experimental_live_diffs: None,
}),
)
},
);
});
cx.run_until_parked();
let raw_settings_value = fs.load(paths::settings_file()).await.unwrap();
assert!(raw_settings_value.contains(r#""version": "2""#));
#[derive(Debug, Deserialize)]
struct AssistantSettingsTest {
assistant: AssistantSettingsContent,
}
let assistant_settings: AssistantSettingsTest =
serde_json_lenient::from_str(&raw_settings_value).unwrap();
assert!(!assistant_settings.assistant.is_version_outdated());
}
}

View File

@@ -1,27 +0,0 @@
use gpui::SharedString;
use serde::{Deserialize, Serialize};
use util::post_inc;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Serialize, Deserialize)]
pub struct ContextId(pub(crate) usize);
impl ContextId {
pub fn post_inc(&mut self) -> Self {
Self(post_inc(&mut self.0))
}
}
/// Some context attached to a message in a thread.
#[derive(Debug, Clone)]
pub struct Context {
pub id: ContextId,
pub name: SharedString,
pub kind: ContextKind,
pub text: SharedString,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ContextKind {
File,
FetchedUrl,
}

View File

@@ -1,101 +1,15 @@
mod fetch_context_picker;
mod file_context_picker;
use std::sync::Arc; use std::sync::Arc;
use gpui::{ use gpui::{DismissEvent, SharedString, Task, WeakView};
AppContext, DismissEvent, EventEmitter, FocusHandle, FocusableView, SharedString, Task, View, use picker::{Picker, PickerDelegate, PickerEditorPosition};
WeakView, use ui::{prelude::*, ListItem, ListItemSpacing, PopoverMenu, PopoverTrigger, Tooltip};
};
use picker::{Picker, PickerDelegate};
use ui::{prelude::*, ListItem, ListItemSpacing, Tooltip};
use util::ResultExt;
use workspace::Workspace;
use crate::context_picker::fetch_context_picker::FetchContextPicker;
use crate::context_picker::file_context_picker::FileContextPicker;
use crate::message_editor::MessageEditor; use crate::message_editor::MessageEditor;
#[derive(Debug, Clone)] #[derive(IntoElement)]
enum ContextPickerMode { pub(super) struct ContextPicker<T: PopoverTrigger> {
Default, message_editor: WeakView<MessageEditor>,
File(View<FileContextPicker>), trigger: T,
Fetch(View<FetchContextPicker>),
}
pub(super) struct ContextPicker {
mode: ContextPickerMode,
picker: View<Picker<ContextPickerDelegate>>,
}
impl ContextPicker {
pub fn new(
workspace: WeakView<Workspace>,
message_editor: WeakView<MessageEditor>,
cx: &mut ViewContext<Self>,
) -> Self {
let delegate = ContextPickerDelegate {
context_picker: cx.view().downgrade(),
workspace: workspace.clone(),
message_editor: message_editor.clone(),
entries: vec![
ContextPickerEntry {
name: "directory".into(),
description: "Insert any directory".into(),
icon: IconName::Folder,
},
ContextPickerEntry {
name: "file".into(),
description: "Insert any file".into(),
icon: IconName::File,
},
ContextPickerEntry {
name: "fetch".into(),
description: "Fetch content from URL".into(),
icon: IconName::Globe,
},
],
selected_ix: 0,
};
let picker = cx.new_view(|cx| {
Picker::nonsearchable_uniform_list(delegate, cx).max_height(Some(rems(20.).into()))
});
ContextPicker {
mode: ContextPickerMode::Default,
picker,
}
}
pub fn reset_mode(&mut self) {
self.mode = ContextPickerMode::Default;
}
}
impl EventEmitter<DismissEvent> for ContextPicker {}
impl FocusableView for ContextPicker {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
match &self.mode {
ContextPickerMode::Default => self.picker.focus_handle(cx),
ContextPickerMode::File(file_picker) => file_picker.focus_handle(cx),
ContextPickerMode::Fetch(fetch_picker) => fetch_picker.focus_handle(cx),
}
}
}
impl Render for ContextPicker {
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
v_flex()
.w(px(400.))
.min_w(px(400.))
.map(|parent| match &self.mode {
ContextPickerMode::Default => parent.child(self.picker.clone()),
ContextPickerMode::File(file_picker) => parent.child(file_picker.clone()),
ContextPickerMode::Fetch(fetch_picker) => parent.child(fetch_picker.clone()),
})
}
} }
#[derive(Clone)] #[derive(Clone)]
@@ -106,18 +20,26 @@ struct ContextPickerEntry {
} }
pub(crate) struct ContextPickerDelegate { pub(crate) struct ContextPickerDelegate {
context_picker: WeakView<ContextPicker>, all_entries: Vec<ContextPickerEntry>,
workspace: WeakView<Workspace>, filtered_entries: Vec<ContextPickerEntry>,
message_editor: WeakView<MessageEditor>, message_editor: WeakView<MessageEditor>,
entries: Vec<ContextPickerEntry>,
selected_ix: usize, selected_ix: usize,
} }
impl<T: PopoverTrigger> ContextPicker<T> {
pub(crate) fn new(message_editor: WeakView<MessageEditor>, trigger: T) -> Self {
ContextPicker {
message_editor,
trigger,
}
}
}
impl PickerDelegate for ContextPickerDelegate { impl PickerDelegate for ContextPickerDelegate {
type ListItem = ListItem; type ListItem = ListItem;
fn match_count(&self) -> usize { fn match_count(&self) -> usize {
self.entries.len() self.filtered_entries.len()
} }
fn selected_index(&self) -> usize { fn selected_index(&self) -> usize {
@@ -125,7 +47,7 @@ impl PickerDelegate for ContextPickerDelegate {
} }
fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Picker<Self>>) { fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Picker<Self>>) {
self.selected_ix = ix.min(self.entries.len().saturating_sub(1)); self.selected_ix = ix.min(self.filtered_entries.len().saturating_sub(1));
cx.notify(); cx.notify();
} }
@@ -133,51 +55,52 @@ impl PickerDelegate for ContextPickerDelegate {
"Select a context source…".into() "Select a context source…".into()
} }
fn update_matches(&mut self, _query: String, _cx: &mut ViewContext<Picker<Self>>) -> Task<()> { fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
Task::ready(()) let all_commands = self.all_entries.clone();
cx.spawn(|this, mut cx| async move {
let filtered_commands = cx
.background_executor()
.spawn(async move {
if query.is_empty() {
all_commands
} else {
all_commands
.into_iter()
.filter(|model_info| {
model_info
.name
.to_lowercase()
.contains(&query.to_lowercase())
})
.collect()
}
})
.await;
this.update(&mut cx, |this, cx| {
this.delegate.filtered_entries = filtered_commands;
this.delegate.set_selected_index(0, cx);
cx.notify();
})
.ok();
})
} }
fn confirm(&mut self, _secondary: bool, cx: &mut ViewContext<Picker<Self>>) { fn confirm(&mut self, _secondary: bool, cx: &mut ViewContext<Picker<Self>>) {
if let Some(entry) = self.entries.get(self.selected_ix) { if let Some(entry) = self.filtered_entries.get(self.selected_ix) {
self.context_picker self.message_editor
.update(cx, |this, cx| { .update(cx, |_message_editor, _cx| {
match entry.name.to_string().as_str() { println!("Insert context from {}", entry.name);
"file" => {
this.mode = ContextPickerMode::File(cx.new_view(|cx| {
FileContextPicker::new(
self.context_picker.clone(),
self.workspace.clone(),
self.message_editor.clone(),
cx,
)
}));
}
"fetch" => {
this.mode = ContextPickerMode::Fetch(cx.new_view(|cx| {
FetchContextPicker::new(
self.context_picker.clone(),
self.workspace.clone(),
self.message_editor.clone(),
cx,
)
}));
}
_ => {}
}
cx.focus_self();
}) })
.log_err(); .ok();
cx.emit(DismissEvent);
} }
} }
fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) { fn dismissed(&mut self, _cx: &mut ViewContext<Picker<Self>>) {}
self.context_picker
.update(cx, |this, cx| match this.mode { fn editor_position(&self) -> PickerEditorPosition {
ContextPickerMode::Default => cx.emit(DismissEvent), PickerEditorPosition::End
ContextPickerMode::File(_) | ContextPickerMode::Fetch(_) => {}
})
.log_err();
} }
fn render_match( fn render_match(
@@ -186,13 +109,13 @@ impl PickerDelegate for ContextPickerDelegate {
selected: bool, selected: bool,
_cx: &mut ViewContext<Picker<Self>>, _cx: &mut ViewContext<Picker<Self>>,
) -> Option<Self::ListItem> { ) -> Option<Self::ListItem> {
let entry = &self.entries[ix]; let entry = self.filtered_entries.get(ix)?;
Some( Some(
ListItem::new(ix) ListItem::new(ix)
.inset(true) .inset(true)
.spacing(ListItemSpacing::Dense) .spacing(ListItemSpacing::Dense)
.toggle_state(selected) .selected(selected)
.tooltip({ .tooltip({
let description = entry.description.clone(); let description = entry.description.clone();
move |cx| cx.new_view(|_cx| Tooltip::new(description.clone())).into() move |cx| cx.new_view(|_cx| Tooltip::new(description.clone())).into()
@@ -225,3 +148,50 @@ impl PickerDelegate for ContextPickerDelegate {
) )
} }
} }
impl<T: PopoverTrigger> RenderOnce for ContextPicker<T> {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
let entries = vec![
ContextPickerEntry {
name: "directory".into(),
description: "Insert any directory".into(),
icon: IconName::Folder,
},
ContextPickerEntry {
name: "file".into(),
description: "Insert any file".into(),
icon: IconName::File,
},
ContextPickerEntry {
name: "web".into(),
description: "Fetch content from URL".into(),
icon: IconName::Globe,
},
];
let delegate = ContextPickerDelegate {
all_entries: entries.clone(),
message_editor: self.message_editor.clone(),
filtered_entries: entries,
selected_ix: 0,
};
let picker =
cx.new_view(|cx| Picker::uniform_list(delegate, cx).max_height(Some(rems(20.).into())));
let handle = self
.message_editor
.update(cx, |this, _| this.context_picker_handle.clone())
.ok();
PopoverMenu::new("context-picker")
.menu(move |_cx| Some(picker.clone()))
.trigger(self.trigger)
.attach(gpui::AnchorCorner::TopLeft)
.anchor(gpui::AnchorCorner::BottomLeft)
.offset(gpui::Point {
x: px(0.0),
y: px(-16.0),
})
.when_some(handle, |this, handle| this.with_handle(handle))
}
}

View File

@@ -1,218 +0,0 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use anyhow::{bail, Context as _, Result};
use futures::AsyncReadExt as _;
use gpui::{AppContext, DismissEvent, FocusHandle, FocusableView, Task, View, WeakView};
use html_to_markdown::{convert_html_to_markdown, markdown, TagHandler};
use http_client::{AsyncBody, HttpClientWithUrl};
use picker::{Picker, PickerDelegate};
use ui::{prelude::*, ListItem, ListItemSpacing, ViewContext};
use workspace::Workspace;
use crate::context::ContextKind;
use crate::context_picker::ContextPicker;
use crate::message_editor::MessageEditor;
pub struct FetchContextPicker {
picker: View<Picker<FetchContextPickerDelegate>>,
}
impl FetchContextPicker {
pub fn new(
context_picker: WeakView<ContextPicker>,
workspace: WeakView<Workspace>,
message_editor: WeakView<MessageEditor>,
cx: &mut ViewContext<Self>,
) -> Self {
let delegate = FetchContextPickerDelegate::new(context_picker, workspace, message_editor);
let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx));
Self { picker }
}
}
impl FocusableView for FetchContextPicker {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
self.picker.focus_handle(cx)
}
}
impl Render for FetchContextPicker {
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
self.picker.clone()
}
}
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
enum ContentType {
Html,
Plaintext,
Json,
}
pub struct FetchContextPickerDelegate {
context_picker: WeakView<ContextPicker>,
workspace: WeakView<Workspace>,
message_editor: WeakView<MessageEditor>,
url: String,
}
impl FetchContextPickerDelegate {
pub fn new(
context_picker: WeakView<ContextPicker>,
workspace: WeakView<Workspace>,
message_editor: WeakView<MessageEditor>,
) -> Self {
FetchContextPickerDelegate {
context_picker,
workspace,
message_editor,
url: String::new(),
}
}
async fn build_message(http_client: Arc<HttpClientWithUrl>, url: &str) -> Result<String> {
let mut url = url.to_owned();
if !url.starts_with("https://") && !url.starts_with("http://") {
url = format!("https://{url}");
}
let mut response = http_client.get(&url, AsyncBody::default(), true).await?;
let mut body = Vec::new();
response
.body_mut()
.read_to_end(&mut body)
.await
.context("error reading response body")?;
if response.status().is_client_error() {
let text = String::from_utf8_lossy(body.as_slice());
bail!(
"status error {}, response: {text:?}",
response.status().as_u16()
);
}
let Some(content_type) = response.headers().get("content-type") else {
bail!("missing Content-Type header");
};
let content_type = content_type
.to_str()
.context("invalid Content-Type header")?;
let content_type = match content_type {
"text/html" => ContentType::Html,
"text/plain" => ContentType::Plaintext,
"application/json" => ContentType::Json,
_ => ContentType::Html,
};
match content_type {
ContentType::Html => {
let mut handlers: Vec<TagHandler> = vec![
Rc::new(RefCell::new(markdown::WebpageChromeRemover)),
Rc::new(RefCell::new(markdown::ParagraphHandler)),
Rc::new(RefCell::new(markdown::HeadingHandler)),
Rc::new(RefCell::new(markdown::ListHandler)),
Rc::new(RefCell::new(markdown::TableHandler::new())),
Rc::new(RefCell::new(markdown::StyledTextHandler)),
];
if url.contains("wikipedia.org") {
use html_to_markdown::structure::wikipedia;
handlers.push(Rc::new(RefCell::new(wikipedia::WikipediaChromeRemover)));
handlers.push(Rc::new(RefCell::new(wikipedia::WikipediaInfoboxHandler)));
handlers.push(Rc::new(
RefCell::new(wikipedia::WikipediaCodeHandler::new()),
));
} else {
handlers.push(Rc::new(RefCell::new(markdown::CodeHandler)));
}
convert_html_to_markdown(&body[..], &mut handlers)
}
ContentType::Plaintext => Ok(std::str::from_utf8(&body)?.to_owned()),
ContentType::Json => {
let json: serde_json::Value = serde_json::from_slice(&body)?;
Ok(format!(
"```json\n{}\n```",
serde_json::to_string_pretty(&json)?
))
}
}
}
}
impl PickerDelegate for FetchContextPickerDelegate {
type ListItem = ListItem;
fn match_count(&self) -> usize {
1
}
fn selected_index(&self) -> usize {
0
}
fn set_selected_index(&mut self, _ix: usize, _cx: &mut ViewContext<Picker<Self>>) {}
fn placeholder_text(&self, _cx: &mut ui::WindowContext) -> Arc<str> {
"Enter a URL…".into()
}
fn update_matches(&mut self, query: String, _cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
self.url = query;
Task::ready(())
}
fn confirm(&mut self, _secondary: bool, cx: &mut ViewContext<Picker<Self>>) {
let Some(workspace) = self.workspace.upgrade() else {
return;
};
let http_client = workspace.read(cx).client().http_client().clone();
let url = self.url.clone();
cx.spawn(|this, mut cx| async move {
let text = Self::build_message(http_client, &url).await?;
this.update(&mut cx, |this, cx| {
this.delegate
.message_editor
.update(cx, |message_editor, _cx| {
message_editor.insert_context(ContextKind::FetchedUrl, url, text);
})
})??;
anyhow::Ok(())
})
.detach_and_log_err(cx);
}
fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
self.context_picker
.update(cx, |this, cx| {
this.reset_mode();
cx.emit(DismissEvent);
})
.ok();
}
fn render_match(
&self,
ix: usize,
selected: bool,
_cx: &mut ViewContext<Picker<Self>>,
) -> Option<Self::ListItem> {
Some(
ListItem::new(ix)
.inset(true)
.spacing(ListItemSpacing::Sparse)
.toggle_state(selected)
.child(self.url.clone()),
)
}
}

View File

@@ -1,289 +0,0 @@
use std::fmt::Write as _;
use std::ops::RangeInclusive;
use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use fuzzy::PathMatch;
use gpui::{AppContext, DismissEvent, FocusHandle, FocusableView, Task, View, WeakView};
use picker::{Picker, PickerDelegate};
use project::{PathMatchCandidateSet, WorktreeId};
use ui::{prelude::*, ListItem, ListItemSpacing};
use util::ResultExt as _;
use workspace::Workspace;
use crate::context::ContextKind;
use crate::context_picker::ContextPicker;
use crate::message_editor::MessageEditor;
pub struct FileContextPicker {
picker: View<Picker<FileContextPickerDelegate>>,
}
impl FileContextPicker {
pub fn new(
context_picker: WeakView<ContextPicker>,
workspace: WeakView<Workspace>,
message_editor: WeakView<MessageEditor>,
cx: &mut ViewContext<Self>,
) -> Self {
let delegate = FileContextPickerDelegate::new(context_picker, workspace, message_editor);
let picker = cx.new_view(|cx| Picker::uniform_list(delegate, cx));
Self { picker }
}
}
impl FocusableView for FileContextPicker {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
self.picker.focus_handle(cx)
}
}
impl Render for FileContextPicker {
fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
self.picker.clone()
}
}
pub struct FileContextPickerDelegate {
context_picker: WeakView<ContextPicker>,
workspace: WeakView<Workspace>,
message_editor: WeakView<MessageEditor>,
matches: Vec<PathMatch>,
selected_index: usize,
}
impl FileContextPickerDelegate {
pub fn new(
context_picker: WeakView<ContextPicker>,
workspace: WeakView<Workspace>,
message_editor: WeakView<MessageEditor>,
) -> Self {
Self {
context_picker,
workspace,
message_editor,
matches: Vec::new(),
selected_index: 0,
}
}
fn search(
&mut self,
query: String,
cancellation_flag: Arc<AtomicBool>,
workspace: &View<Workspace>,
cx: &mut ViewContext<Picker<Self>>,
) -> Task<Vec<PathMatch>> {
if query.is_empty() {
let workspace = workspace.read(cx);
let project = workspace.project().read(cx);
let entries = workspace.recent_navigation_history(Some(10), cx);
let entries = entries
.into_iter()
.map(|entries| entries.0)
.chain(project.worktrees(cx).flat_map(|worktree| {
let worktree = worktree.read(cx);
let id = worktree.id();
worktree
.child_entries(Path::new(""))
.filter(|entry| entry.kind.is_file())
.map(move |entry| project::ProjectPath {
worktree_id: id,
path: entry.path.clone(),
})
}))
.collect::<Vec<_>>();
let path_prefix: Arc<str> = Arc::default();
Task::ready(
entries
.into_iter()
.filter_map(|entry| {
let worktree = project.worktree_for_id(entry.worktree_id, cx)?;
let mut full_path = PathBuf::from(worktree.read(cx).root_name());
full_path.push(&entry.path);
Some(PathMatch {
score: 0.,
positions: Vec::new(),
worktree_id: entry.worktree_id.to_usize(),
path: full_path.into(),
path_prefix: path_prefix.clone(),
distance_to_relative_ancestor: 0,
is_dir: false,
})
})
.collect(),
)
} else {
let worktrees = workspace.read(cx).visible_worktrees(cx).collect::<Vec<_>>();
let candidate_sets = worktrees
.into_iter()
.map(|worktree| {
let worktree = worktree.read(cx);
PathMatchCandidateSet {
snapshot: worktree.snapshot(),
include_ignored: worktree
.root_entry()
.map_or(false, |entry| entry.is_ignored),
include_root_name: true,
candidates: project::Candidates::Files,
}
})
.collect::<Vec<_>>();
let executor = cx.background_executor().clone();
cx.foreground_executor().spawn(async move {
fuzzy::match_path_sets(
candidate_sets.as_slice(),
query.as_str(),
None,
false,
100,
&cancellation_flag,
executor,
)
.await
})
}
}
}
impl PickerDelegate for FileContextPickerDelegate {
type ListItem = ListItem;
fn match_count(&self) -> usize {
self.matches.len()
}
fn selected_index(&self) -> usize {
self.selected_index
}
fn set_selected_index(&mut self, ix: usize, _cx: &mut ViewContext<Picker<Self>>) {
self.selected_index = ix;
}
fn placeholder_text(&self, _cx: &mut WindowContext) -> Arc<str> {
"Search files…".into()
}
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
let Some(workspace) = self.workspace.upgrade() else {
return Task::ready(());
};
let search_task = self.search(query, Arc::<AtomicBool>::default(), &workspace, cx);
cx.spawn(|this, mut cx| async move {
// TODO: This should be probably be run in the background.
let paths = search_task.await;
this.update(&mut cx, |this, _cx| {
this.delegate.matches = paths;
})
.log_err();
})
}
fn confirm(&mut self, _secondary: bool, cx: &mut ViewContext<Picker<Self>>) {
let mat = &self.matches[self.selected_index];
let workspace = self.workspace.clone();
let Some(project) = workspace
.upgrade()
.map(|workspace| workspace.read(cx).project().clone())
else {
return;
};
let path = mat.path.clone();
let worktree_id = WorktreeId::from_usize(mat.worktree_id);
cx.spawn(|this, mut cx| async move {
let Some(open_buffer_task) = project
.update(&mut cx, |project, cx| {
project.open_buffer((worktree_id, path.clone()), cx)
})
.ok()
else {
return anyhow::Ok(());
};
let buffer = open_buffer_task.await?;
this.update(&mut cx, |this, cx| {
this.delegate
.message_editor
.update(cx, |message_editor, cx| {
let mut text = String::new();
text.push_str(&codeblock_fence_for_path(Some(&path), None));
text.push_str(&buffer.read(cx).text());
if !text.ends_with('\n') {
text.push('\n');
}
text.push_str("```\n");
message_editor.insert_context(
ContextKind::File,
path.to_string_lossy().to_string(),
text,
);
})
})??;
anyhow::Ok(())
})
.detach_and_log_err(cx);
}
fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
self.context_picker
.update(cx, |this, cx| {
this.reset_mode();
cx.emit(DismissEvent);
})
.ok();
}
fn render_match(
&self,
ix: usize,
selected: bool,
_cx: &mut ViewContext<Picker<Self>>,
) -> Option<Self::ListItem> {
let mat = &self.matches[ix];
Some(
ListItem::new(ix)
.inset(true)
.spacing(ListItemSpacing::Sparse)
.toggle_state(selected)
.child(mat.path.to_string_lossy().to_string()),
)
}
}
fn codeblock_fence_for_path(path: Option<&Path>, row_range: Option<RangeInclusive<u32>>) -> String {
let mut text = String::new();
write!(text, "```").unwrap();
if let Some(path) = path {
if let Some(extension) = path.extension().and_then(|ext| ext.to_str()) {
write!(text, "{} ", extension).unwrap();
}
write!(text, "{}", path.display()).unwrap();
} else {
write!(text, "untitled").unwrap();
}
if let Some(row_range) = row_range {
write!(text, ":{}-{}", row_range.start() + 1, row_range.end() + 1).unwrap();
}
text.push('\n');
text
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,79 +1,40 @@
use std::rc::Rc;
use editor::{Editor, EditorElement, EditorStyle}; use editor::{Editor, EditorElement, EditorStyle};
use gpui::{AppContext, FocusableView, Model, TextStyle, View, WeakView}; use gpui::{AppContext, FocusableView, Model, TextStyle, View};
use language_model::{LanguageModelRegistry, LanguageModelRequestTool}; use language_model::{LanguageModelRegistry, LanguageModelRequestTool};
use language_model_selector::{LanguageModelSelector, LanguageModelSelectorPopoverMenu}; use picker::Picker;
use settings::Settings; use settings::Settings;
use theme::ThemeSettings; use theme::ThemeSettings;
use ui::{ use ui::{
prelude::*, ButtonLike, CheckboxWithLabel, ElevationIndex, IconButtonShape, KeyBinding, prelude::*, ButtonLike, CheckboxWithLabel, ElevationIndex, IconButtonShape, KeyBinding,
PopoverMenu, PopoverMenuHandle, Tooltip, PopoverMenuHandle,
}; };
use workspace::Workspace;
use crate::context::{Context, ContextId, ContextKind}; use crate::context_picker::{ContextPicker, ContextPickerDelegate};
use crate::context_picker::ContextPicker;
use crate::thread::{RequestKind, Thread}; use crate::thread::{RequestKind, Thread};
use crate::ui::ContextPill; use crate::Chat;
use crate::{Chat, ToggleModelSelector};
pub struct MessageEditor { pub struct MessageEditor {
thread: Model<Thread>, thread: Model<Thread>,
editor: View<Editor>, editor: View<Editor>,
context: Vec<Context>, pub(crate) context_picker_handle: PopoverMenuHandle<Picker<ContextPickerDelegate>>,
next_context_id: ContextId,
context_picker: View<ContextPicker>,
pub(crate) context_picker_handle: PopoverMenuHandle<ContextPicker>,
language_model_selector: View<LanguageModelSelector>,
use_tools: bool, use_tools: bool,
} }
impl MessageEditor { impl MessageEditor {
pub fn new( pub fn new(thread: Model<Thread>, cx: &mut ViewContext<Self>) -> Self {
workspace: WeakView<Workspace>,
thread: Model<Thread>,
cx: &mut ViewContext<Self>,
) -> Self {
let weak_self = cx.view().downgrade();
Self { Self {
thread, thread,
editor: cx.new_view(|cx| { editor: cx.new_view(|cx| {
let mut editor = Editor::auto_height(80, cx); let mut editor = Editor::auto_height(80, cx);
editor.set_placeholder_text("Ask anything or type @ to add context", cx); editor.set_placeholder_text("Ask anything", cx);
editor editor
}), }),
context: Vec::new(),
next_context_id: ContextId(0),
context_picker: cx.new_view(|cx| ContextPicker::new(workspace.clone(), weak_self, cx)),
context_picker_handle: PopoverMenuHandle::default(), context_picker_handle: PopoverMenuHandle::default(),
language_model_selector: cx.new_view(|cx| {
LanguageModelSelector::new(
|model, _cx| {
println!("Selected {:?}", model.name());
},
cx,
)
}),
use_tools: false, use_tools: false,
} }
} }
pub fn insert_context(
&mut self,
kind: ContextKind,
name: impl Into<SharedString>,
text: impl Into<SharedString>,
) {
self.context.push(Context {
id: self.next_context_id.post_inc(),
name: name.into(),
kind,
text: text.into(),
});
}
fn chat(&mut self, _: &Chat, cx: &mut ViewContext<Self>) { fn chat(&mut self, _: &Chat, cx: &mut ViewContext<Self>) {
self.send_to_model(RequestKind::Chat, cx); self.send_to_model(RequestKind::Chat, cx);
} }
@@ -100,10 +61,9 @@ impl MessageEditor {
editor.clear(cx); editor.clear(cx);
text text
}); });
let context = self.context.drain(..).collect::<Vec<_>>();
self.thread.update(cx, |thread, cx| { self.thread.update(cx, |thread, cx| {
thread.insert_user_message(user_message, context, cx); thread.insert_user_message(user_message, cx);
let mut request = thread.to_completion_request(request_kind, cx); let mut request = thread.to_completion_request(request_kind, cx);
if self.use_tools { if self.use_tools {
@@ -124,55 +84,6 @@ impl MessageEditor {
None None
} }
fn render_language_model_selector(&self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let active_provider = LanguageModelRegistry::read_global(cx).active_provider();
let active_model = LanguageModelRegistry::read_global(cx).active_model();
LanguageModelSelectorPopoverMenu::new(
self.language_model_selector.clone(),
ButtonLike::new("active-model")
.style(ButtonStyle::Subtle)
.child(
h_flex()
.w_full()
.gap_0p5()
.child(
div()
.overflow_x_hidden()
.flex_grow()
.whitespace_nowrap()
.child(match (active_provider, active_model) {
(Some(provider), Some(model)) => h_flex()
.gap_1()
.child(
Icon::new(
model.icon().unwrap_or_else(|| provider.icon()),
)
.color(Color::Muted)
.size(IconSize::XSmall),
)
.child(
Label::new(model.name().0)
.size(LabelSize::Small)
.color(Color::Muted),
)
.into_any_element(),
_ => Label::new("No model selected")
.size(LabelSize::Small)
.color(Color::Muted)
.into_any_element(),
}),
)
.child(
Icon::new(IconName::ChevronDown)
.color(Color::Muted)
.size(IconSize::XSmall),
),
)
.tooltip(move |cx| Tooltip::for_action("Change Model", &ToggleModelSelector, cx)),
)
}
} }
impl FocusableView for MessageEditor { impl FocusableView for MessageEditor {
@@ -186,7 +97,6 @@ impl Render for MessageEditor {
let font_size = TextSize::Default.rems(cx); let font_size = TextSize::Default.rems(cx);
let line_height = font_size.to_pixels(cx.rem_size()) * 1.3; let line_height = font_size.to_pixels(cx.rem_size()) * 1.3;
let focus_handle = self.editor.focus_handle(cx); let focus_handle = self.editor.focus_handle(cx);
let context_picker = self.context_picker.clone();
v_flex() v_flex()
.key_context("MessageEditor") .key_context("MessageEditor")
@@ -196,46 +106,12 @@ impl Render for MessageEditor {
.p_2() .p_2()
.bg(cx.theme().colors().editor_background) .bg(cx.theme().colors().editor_background)
.child( .child(
h_flex() h_flex().gap_2().child(ContextPicker::new(
.flex_wrap() cx.view().downgrade(),
.gap_2() IconButton::new("add-context", IconName::Plus)
.child( .shape(IconButtonShape::Square)
PopoverMenu::new("context-picker") .icon_size(IconSize::Small),
.menu(move |_cx| Some(context_picker.clone())) )),
.trigger(
IconButton::new("add-context", IconName::Plus)
.shape(IconButtonShape::Square)
.icon_size(IconSize::Small),
)
.attach(gpui::AnchorCorner::TopLeft)
.anchor(gpui::AnchorCorner::BottomLeft)
.offset(gpui::Point {
x: px(0.0),
y: px(-16.0),
})
.with_handle(self.context_picker_handle.clone()),
)
.children(self.context.iter().map(|context| {
ContextPill::new(context.clone()).on_remove({
let context = context.clone();
Rc::new(cx.listener(move |this, _event, cx| {
this.context.retain(|other| other.id != context.id);
cx.notify();
}))
})
}))
.when(!self.context.is_empty(), |parent| {
parent.child(
IconButton::new("remove-all-context", IconName::Eraser)
.shape(IconButtonShape::Square)
.icon_size(IconSize::Small)
.tooltip(move |cx| Tooltip::text("Remove All Context", cx))
.on_click(cx.listener(|this, _event, cx| {
this.context.clear();
cx.notify();
})),
)
}),
) )
.child({ .child({
let settings = ThemeSettings::get_global(cx); let settings = ThemeSettings::get_global(cx);
@@ -268,20 +144,21 @@ impl Render for MessageEditor {
self.use_tools.into(), self.use_tools.into(),
cx.listener(|this, selection, _cx| { cx.listener(|this, selection, _cx| {
this.use_tools = match selection { this.use_tools = match selection {
ToggleState::Selected => true, Selection::Selected => true,
ToggleState::Unselected | ToggleState::Indeterminate => false, Selection::Unselected | Selection::Indeterminate => false,
}; };
}), }),
))) )))
.child( .child(
h_flex() h_flex()
.gap_2() .gap_2()
.child(self.render_language_model_selector(cx)) .child(Button::new("codebase", "Codebase").style(ButtonStyle::Filled))
.child(Label::new("or"))
.child( .child(
ButtonLike::new("chat") ButtonLike::new("chat")
.style(ButtonStyle::Filled) .style(ButtonStyle::Filled)
.layer(ElevationIndex::ModalSurface) .layer(ElevationIndex::ModalSurface)
.child(Label::new("Submit")) .child(Label::new("Chat"))
.children( .children(
KeyBinding::for_action_in(&Chat, &focus_handle, cx) KeyBinding::for_action_in(&Chat, &focus_handle, cx)
.map(|binding| binding.into_any_element()), .map(|binding| binding.into_any_element()),

View File

@@ -1,312 +0,0 @@
use anyhow::Result;
use assets::Assets;
use fs::Fs;
use futures::StreamExt;
use gpui::AssetSource;
use handlebars::{Handlebars, RenderError};
use language::{BufferSnapshot, LanguageName, Point};
use parking_lot::Mutex;
use serde::Serialize;
use std::{ops::Range, path::PathBuf, sync::Arc, time::Duration};
use text::LineEnding;
use util::ResultExt;
#[derive(Serialize)]
pub struct ContentPromptDiagnosticContext {
pub line_number: usize,
pub error_message: String,
pub code_content: String,
}
#[derive(Serialize)]
pub struct ContentPromptContext {
pub content_type: String,
pub language_name: Option<String>,
pub is_insert: bool,
pub is_truncated: bool,
pub document_content: String,
pub user_prompt: String,
pub rewrite_section: Option<String>,
pub diagnostic_errors: Vec<ContentPromptDiagnosticContext>,
}
#[derive(Serialize)]
pub struct TerminalAssistantPromptContext {
pub os: String,
pub arch: String,
pub shell: Option<String>,
pub working_directory: Option<String>,
pub latest_output: Vec<String>,
pub user_prompt: String,
}
#[derive(Serialize)]
pub struct ProjectSlashCommandPromptContext {
pub context_buffer: String,
}
pub struct PromptLoadingParams<'a> {
pub fs: Arc<dyn Fs>,
pub repo_path: Option<PathBuf>,
pub cx: &'a gpui::AppContext,
}
pub struct PromptBuilder {
handlebars: Arc<Mutex<Handlebars<'static>>>,
}
impl PromptBuilder {
pub fn new(loading_params: Option<PromptLoadingParams>) -> Result<Self> {
let mut handlebars = Handlebars::new();
Self::register_built_in_templates(&mut handlebars)?;
let handlebars = Arc::new(Mutex::new(handlebars));
if let Some(params) = loading_params {
Self::watch_fs_for_template_overrides(params, handlebars.clone());
}
Ok(Self { handlebars })
}
/// Watches the filesystem for changes to prompt template overrides.
///
/// This function sets up a file watcher on the prompt templates directory. It performs
/// an initial scan of the directory and registers any existing template overrides.
/// Then it continuously monitors for changes, reloading templates as they are
/// modified or added.
///
/// If the templates directory doesn't exist initially, it waits for it to be created.
/// If the directory is removed, it restores the built-in templates and waits for the
/// directory to be recreated.
///
/// # Arguments
///
/// * `params` - A `PromptLoadingParams` struct containing the filesystem, repository path,
/// and application context.
/// * `handlebars` - An `Arc<Mutex<Handlebars>>` for registering and updating templates.
fn watch_fs_for_template_overrides(
params: PromptLoadingParams,
handlebars: Arc<Mutex<Handlebars<'static>>>,
) {
let templates_dir = paths::prompt_overrides_dir(params.repo_path.as_deref());
params.cx.background_executor()
.spawn(async move {
let Some(parent_dir) = templates_dir.parent() else {
return;
};
let mut found_dir_once = false;
loop {
// Check if the templates directory exists and handle its status
// If it exists, log its presence and check if it's a symlink
// If it doesn't exist:
// - Log that we're using built-in prompts
// - Check if it's a broken symlink and log if so
// - Set up a watcher to detect when it's created
// After the first check, set the `found_dir_once` flag
// This allows us to avoid logging when looping back around after deleting the prompt overrides directory.
let dir_status = params.fs.is_dir(&templates_dir).await;
let symlink_status = params.fs.read_link(&templates_dir).await.ok();
if dir_status {
let mut log_message = format!("Prompt template overrides directory found at {}", templates_dir.display());
if let Some(target) = symlink_status {
log_message.push_str(" -> ");
log_message.push_str(&target.display().to_string());
}
log::info!("{}.", log_message);
} else {
if !found_dir_once {
log::info!("No prompt template overrides directory found at {}. Using built-in prompts.", templates_dir.display());
if let Some(target) = symlink_status {
log::info!("Symlink found pointing to {}, but target is invalid.", target.display());
}
}
if params.fs.is_dir(parent_dir).await {
let (mut changes, _watcher) = params.fs.watch(parent_dir, Duration::from_secs(1)).await;
while let Some(changed_paths) = changes.next().await {
if changed_paths.iter().any(|p| &p.path == &templates_dir) {
let mut log_message = format!("Prompt template overrides directory detected at {}", templates_dir.display());
if let Ok(target) = params.fs.read_link(&templates_dir).await {
log_message.push_str(" -> ");
log_message.push_str(&target.display().to_string());
}
log::info!("{}.", log_message);
break;
}
}
} else {
return;
}
}
found_dir_once = true;
// Initial scan of the prompt overrides directory
if let Ok(mut entries) = params.fs.read_dir(&templates_dir).await {
while let Some(Ok(file_path)) = entries.next().await {
if file_path.to_string_lossy().ends_with(".hbs") {
if let Ok(content) = params.fs.load(&file_path).await {
let file_name = file_path.file_stem().unwrap().to_string_lossy();
log::debug!("Registering prompt template override: {}", file_name);
handlebars.lock().register_template_string(&file_name, content).log_err();
}
}
}
}
// Watch both the parent directory and the template overrides directory:
// - Monitor the parent directory to detect if the template overrides directory is deleted.
// - Monitor the template overrides directory to re-register templates when they change.
// Combine both watch streams into a single stream.
let (parent_changes, parent_watcher) = params.fs.watch(parent_dir, Duration::from_secs(1)).await;
let (changes, watcher) = params.fs.watch(&templates_dir, Duration::from_secs(1)).await;
let mut combined_changes = futures::stream::select(changes, parent_changes);
while let Some(changed_paths) = combined_changes.next().await {
if changed_paths.iter().any(|p| &p.path == &templates_dir) {
if !params.fs.is_dir(&templates_dir).await {
log::info!("Prompt template overrides directory removed. Restoring built-in prompt templates.");
Self::register_built_in_templates(&mut handlebars.lock()).log_err();
break;
}
}
for event in changed_paths {
if event.path.starts_with(&templates_dir) && event.path.extension().map_or(false, |ext| ext == "hbs") {
log::info!("Reloading prompt template override: {}", event.path.display());
if let Some(content) = params.fs.load(&event.path).await.log_err() {
let file_name = event.path.file_stem().unwrap().to_string_lossy();
handlebars.lock().register_template_string(&file_name, content).log_err();
}
}
}
}
drop(watcher);
drop(parent_watcher);
}
})
.detach();
}
fn register_built_in_templates(handlebars: &mut Handlebars) -> Result<()> {
for path in Assets.list("prompts")? {
if let Some(id) = path.split('/').last().and_then(|s| s.strip_suffix(".hbs")) {
if let Some(prompt) = Assets.load(path.as_ref()).log_err().flatten() {
log::debug!("Registering built-in prompt template: {}", id);
let prompt = String::from_utf8_lossy(prompt.as_ref());
handlebars.register_template_string(id, LineEnding::normalize_cow(prompt))?
}
}
}
Ok(())
}
pub fn generate_inline_transformation_prompt(
&self,
user_prompt: String,
language_name: Option<&LanguageName>,
buffer: BufferSnapshot,
range: Range<usize>,
) -> Result<String, RenderError> {
let content_type = match language_name.as_ref().map(|l| l.0.as_ref()) {
None | Some("Markdown" | "Plain Text") => "text",
Some(_) => "code",
};
const MAX_CTX: usize = 50000;
let is_insert = range.is_empty();
let mut is_truncated = false;
let before_range = 0..range.start;
let truncated_before = if before_range.len() > MAX_CTX {
is_truncated = true;
let start = buffer.clip_offset(range.start - MAX_CTX, text::Bias::Right);
start..range.start
} else {
before_range
};
let after_range = range.end..buffer.len();
let truncated_after = if after_range.len() > MAX_CTX {
is_truncated = true;
let end = buffer.clip_offset(range.end + MAX_CTX, text::Bias::Left);
range.end..end
} else {
after_range
};
let mut document_content = String::new();
for chunk in buffer.text_for_range(truncated_before) {
document_content.push_str(chunk);
}
if is_insert {
document_content.push_str("<insert_here></insert_here>");
} else {
document_content.push_str("<rewrite_this>\n");
for chunk in buffer.text_for_range(range.clone()) {
document_content.push_str(chunk);
}
document_content.push_str("\n</rewrite_this>");
}
for chunk in buffer.text_for_range(truncated_after) {
document_content.push_str(chunk);
}
let rewrite_section = if !is_insert {
let mut section = String::new();
for chunk in buffer.text_for_range(range.clone()) {
section.push_str(chunk);
}
Some(section)
} else {
None
};
let diagnostics = buffer.diagnostics_in_range::<_, Point>(range, false);
let diagnostic_errors: Vec<ContentPromptDiagnosticContext> = diagnostics
.map(|entry| {
let start = entry.range.start;
ContentPromptDiagnosticContext {
line_number: (start.row + 1) as usize,
error_message: entry.diagnostic.message.clone(),
code_content: buffer.text_for_range(entry.range.clone()).collect(),
}
})
.collect();
let context = ContentPromptContext {
content_type: content_type.to_string(),
language_name: language_name.map(|s| s.to_string()),
is_insert,
is_truncated,
document_content,
user_prompt,
rewrite_section,
diagnostic_errors,
};
self.handlebars.lock().render("content_prompt", &context)
}
pub fn generate_terminal_assistant_prompt(
&self,
user_prompt: &str,
shell: Option<&str>,
working_directory: Option<&str>,
latest_output: &[String],
) -> Result<String, RenderError> {
let context = TerminalAssistantPromptContext {
os: std::env::consts::OS.to_string(),
arch: std::env::consts::ARCH.to_string(),
shell: shell.map(|s| s.to_string()),
working_directory: working_directory.map(|s| s.to_string()),
latest_output: latest_output.to_vec(),
user_prompt: user_prompt.to_string(),
};
self.handlebars
.lock()
.render("terminal_assistant_prompt", &context)
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -17,8 +17,6 @@ use serde::{Deserialize, Serialize};
use util::{post_inc, TryFutureExt as _}; use util::{post_inc, TryFutureExt as _};
use uuid::Uuid; use uuid::Uuid;
use crate::context::{Context, ContextKind};
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum RequestKind { pub enum RequestKind {
Chat, Chat,
@@ -64,7 +62,6 @@ pub struct Thread {
pending_summary: Task<Option<()>>, pending_summary: Task<Option<()>>,
messages: Vec<Message>, messages: Vec<Message>,
next_message_id: MessageId, next_message_id: MessageId,
context_by_message: HashMap<MessageId, Vec<Context>>,
completion_count: usize, completion_count: usize,
pending_completions: Vec<PendingCompletion>, pending_completions: Vec<PendingCompletion>,
tools: Arc<ToolWorkingSet>, tools: Arc<ToolWorkingSet>,
@@ -82,7 +79,6 @@ impl Thread {
pending_summary: Task::ready(None), pending_summary: Task::ready(None),
messages: Vec::new(), messages: Vec::new(),
next_message_id: MessageId(0), next_message_id: MessageId(0),
context_by_message: HashMap::default(),
completion_count: 0, completion_count: 0,
pending_completions: Vec::new(), pending_completions: Vec::new(),
tools, tools,
@@ -129,22 +125,12 @@ impl Thread {
&self.tools &self.tools
} }
pub fn context_for_message(&self, id: MessageId) -> Option<&Vec<Context>> {
self.context_by_message.get(&id)
}
pub fn pending_tool_uses(&self) -> Vec<&PendingToolUse> { pub fn pending_tool_uses(&self) -> Vec<&PendingToolUse> {
self.pending_tool_uses_by_id.values().collect() self.pending_tool_uses_by_id.values().collect()
} }
pub fn insert_user_message( pub fn insert_user_message(&mut self, text: impl Into<String>, cx: &mut ModelContext<Self>) {
&mut self, self.insert_message(Role::User, text, cx)
text: impl Into<String>,
context: Vec<Context>,
cx: &mut ModelContext<Self>,
) {
let message_id = self.insert_message(Role::User, text, cx);
self.context_by_message.insert(message_id, context);
} }
pub fn insert_message( pub fn insert_message(
@@ -152,7 +138,7 @@ impl Thread {
role: Role, role: Role,
text: impl Into<String>, text: impl Into<String>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> MessageId { ) {
let id = self.next_message_id.post_inc(); let id = self.next_message_id.post_inc();
self.messages.push(Message { self.messages.push(Message {
id, id,
@@ -161,7 +147,6 @@ impl Thread {
}); });
self.touch_updated_at(); self.touch_updated_at();
cx.emit(ThreadEvent::MessageAdded(id)); cx.emit(ThreadEvent::MessageAdded(id));
id
} }
pub fn to_completion_request( pub fn to_completion_request(
@@ -191,41 +176,6 @@ impl Thread {
} }
} }
if let Some(context) = self.context_for_message(message.id) {
let mut file_context = String::new();
let mut fetch_context = String::new();
for context in context.iter() {
match context.kind {
ContextKind::File => {
file_context.push_str(&context.text);
file_context.push('\n');
}
ContextKind::FetchedUrl => {
fetch_context.push_str(&context.name);
fetch_context.push('\n');
fetch_context.push_str(&context.text);
fetch_context.push('\n');
}
}
}
let mut context_text = String::new();
if !file_context.is_empty() {
context_text.push_str("The following files are available:\n");
context_text.push_str(&file_context);
}
if !fetch_context.is_empty() {
context_text.push_str("The following fetched results are available\n");
context_text.push_str(&fetch_context);
}
request_message
.content
.push(MessageContent::Text(context_text))
}
if !message.text.is_empty() { if !message.text.is_empty() {
request_message request_message
.content .content

View File

@@ -159,9 +159,9 @@ impl ThreadStore {
self.threads.push(cx.new_model(|cx| { self.threads.push(cx.new_model(|cx| {
let mut thread = Thread::new(self.tools.clone(), cx); let mut thread = Thread::new(self.tools.clone(), cx);
thread.set_summary("Introduction to quantum computing", cx); thread.set_summary("Introduction to quantum computing", cx);
thread.insert_user_message("Hello! Can you help me understand quantum computing?", Vec::new(), cx); thread.insert_user_message("Hello! Can you help me understand quantum computing?", cx);
thread.insert_message(Role::Assistant, "Of course! I'd be happy to help you understand quantum computing. Quantum computing is a fascinating field that uses the principles of quantum mechanics to process information. Unlike classical computers that use bits (0s and 1s), quantum computers use quantum bits or 'qubits'. These qubits can exist in multiple states simultaneously, a property called superposition. This allows quantum computers to perform certain calculations much faster than classical computers. What specific aspect of quantum computing would you like to know more about?", cx); thread.insert_message(Role::Assistant, "Of course! I'd be happy to help you understand quantum computing. Quantum computing is a fascinating field that uses the principles of quantum mechanics to process information. Unlike classical computers that use bits (0s and 1s), quantum computers use quantum bits or 'qubits'. These qubits can exist in multiple states simultaneously, a property called superposition. This allows quantum computers to perform certain calculations much faster than classical computers. What specific aspect of quantum computing would you like to know more about?", cx);
thread.insert_user_message("That's interesting! Can you explain how quantum entanglement is used in quantum computing?", Vec::new(), cx); thread.insert_user_message("That's interesting! Can you explain how quantum entanglement is used in quantum computing?", cx);
thread.insert_message(Role::Assistant, "Certainly! Quantum entanglement is a key principle used in quantum computing. When two qubits become entangled, the state of one qubit is directly related to the state of the other, regardless of the distance between them. This property is used in quantum computing to create complex quantum states and to perform operations on multiple qubits simultaneously. Entanglement allows quantum computers to process information in ways that classical computers cannot, potentially solving certain problems much more efficiently. For example, it's crucial in quantum error correction and in algorithms like quantum teleportation, which is important for quantum communication.", cx); thread.insert_message(Role::Assistant, "Certainly! Quantum entanglement is a key principle used in quantum computing. When two qubits become entangled, the state of one qubit is directly related to the state of the other, regardless of the distance between them. This property is used in quantum computing to create complex quantum states and to perform operations on multiple qubits simultaneously. Entanglement allows quantum computers to process information in ways that classical computers cannot, potentially solving certain problems much more efficiently. For example, it's crucial in quantum error correction and in algorithms like quantum teleportation, which is important for quantum communication.", cx);
thread thread
})); }));
@@ -169,7 +169,7 @@ impl ThreadStore {
self.threads.push(cx.new_model(|cx| { self.threads.push(cx.new_model(|cx| {
let mut thread = Thread::new(self.tools.clone(), cx); let mut thread = Thread::new(self.tools.clone(), cx);
thread.set_summary("Rust web development and async programming", cx); thread.set_summary("Rust web development and async programming", cx);
thread.insert_user_message("Can you show me an example of Rust code for a simple web server?", Vec::new(), cx); thread.insert_user_message("Can you show me an example of Rust code for a simple web server?", cx);
thread.insert_message(Role::Assistant, "Certainly! Here's an example of a simple web server in Rust using the `actix-web` framework: thread.insert_message(Role::Assistant, "Certainly! Here's an example of a simple web server in Rust using the `actix-web` framework:
```rust ```rust
@@ -206,7 +206,7 @@ impl ThreadStore {
``` ```
Then you can run the server with `cargo run` and access it at `http://localhost:8080`.".unindent(), cx); Then you can run the server with `cargo run` and access it at `http://localhost:8080`.".unindent(), cx);
thread.insert_user_message("That's great! Can you explain more about async functions in Rust?", Vec::new(), cx); thread.insert_user_message("That's great! Can you explain more about async functions in Rust?", cx);
thread.insert_message(Role::Assistant, "Certainly! Async functions are a key feature in Rust for writing efficient, non-blocking code, especially for I/O-bound operations. Here's an overview: thread.insert_message(Role::Assistant, "Certainly! Async functions are a key feature in Rust for writing efficient, non-blocking code, especially for I/O-bound operations. Here's an overview:
1. **Syntax**: Async functions are declared using the `async` keyword: 1. **Syntax**: Async functions are declared using the `async` keyword:

View File

@@ -1,3 +0,0 @@
mod context_pill;
pub use context_pill::*;

View File

@@ -1,49 +0,0 @@
use std::rc::Rc;
use gpui::ClickEvent;
use ui::{prelude::*, IconButtonShape};
use crate::context::Context;
#[derive(IntoElement)]
pub struct ContextPill {
context: Context,
on_remove: Option<Rc<dyn Fn(&ClickEvent, &mut WindowContext)>>,
}
impl ContextPill {
pub fn new(context: Context) -> Self {
Self {
context,
on_remove: None,
}
}
pub fn on_remove(mut self, on_remove: Rc<dyn Fn(&ClickEvent, &mut WindowContext)>) -> Self {
self.on_remove = Some(on_remove);
self
}
}
impl RenderOnce for ContextPill {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
h_flex()
.gap_1()
.px_1()
.border_1()
.border_color(cx.theme().colors().border)
.rounded_md()
.child(Label::new(self.context.name.clone()).size(LabelSize::Small))
.when_some(self.on_remove, |parent, on_remove| {
parent.child(
IconButton::new("remove", IconName::Close)
.shape(IconButtonShape::Square)
.icon_size(IconSize::XSmall)
.on_click({
let on_remove = on_remove.clone();
move |event, cx| on_remove(event, cx)
}),
)
})
}
}

View File

@@ -18,7 +18,6 @@ test-support = [
"collections/test-support", "collections/test-support",
"gpui/test-support", "gpui/test-support",
"livekit_client/test-support", "livekit_client/test-support",
"livekit_client_macos/test-support",
"project/test-support", "project/test-support",
"util/test-support" "util/test-support"
] ]

View File

@@ -20,7 +20,7 @@ pub struct CallSettingsContent {
/// Whether your current project should be shared when joining an empty channel. /// Whether your current project should be shared when joining an empty channel.
/// ///
/// Default: false /// Default: true
pub share_on_join: Option<bool>, pub share_on_join: Option<bool>,
} }

View File

@@ -1288,12 +1288,6 @@ impl Room {
}) })
} }
pub fn muted_by_user(&self) -> bool {
self.live_kit
.as_ref()
.map_or(false, |live_kit| live_kit.muted_by_user)
}
pub fn is_speaking(&self) -> bool { pub fn is_speaking(&self) -> bool {
self.live_kit self.live_kit
.as_ref() .as_ref()

View File

@@ -1307,12 +1307,6 @@ impl Room {
}) })
} }
pub fn muted_by_user(&self) -> bool {
self.live_kit
.as_ref()
.map_or(false, |live_kit| live_kit.muted_by_user)
}
pub fn is_speaking(&self) -> bool { pub fn is_speaking(&self) -> bool {
self.live_kit self.live_kit
.as_ref() .as_ref()

View File

@@ -19,6 +19,11 @@ LLM_DATABASE_URL = "postgres://postgres@localhost/zed_llm"
LLM_DATABASE_MAX_CONNECTIONS = 5 LLM_DATABASE_MAX_CONNECTIONS = 5
LLM_API_SECRET = "llm-secret" LLM_API_SECRET = "llm-secret"
# CLICKHOUSE_URL = ""
# CLICKHOUSE_USER = "default"
# CLICKHOUSE_PASSWORD = ""
# CLICKHOUSE_DATABASE = "default"
# SLACK_PANICS_WEBHOOK = "" # SLACK_PANICS_WEBHOOK = ""
# RUST_LOG=info # RUST_LOG=info

View File

@@ -29,6 +29,7 @@ axum = { version = "0.6", features = ["json", "headers", "ws"] }
axum-extra = { version = "0.4", features = ["erased-json"] } axum-extra = { version = "0.4", features = ["erased-json"] }
base64.workspace = true base64.workspace = true
chrono.workspace = true chrono.workspace = true
clickhouse.workspace = true
clock.workspace = true clock.workspace = true
collections.workspace = true collections.workspace = true
dashmap.workspace = true dashmap.workspace = true

View File

@@ -214,6 +214,26 @@ spec:
secretKeyRef: secretKeyRef:
name: blob-store name: blob-store
key: bucket key: bucket
- name: CLICKHOUSE_URL
valueFrom:
secretKeyRef:
name: clickhouse
key: url
- name: CLICKHOUSE_USER
valueFrom:
secretKeyRef:
name: clickhouse
key: user
- name: CLICKHOUSE_PASSWORD
valueFrom:
secretKeyRef:
name: clickhouse
key: password
- name: CLICKHOUSE_DATABASE
valueFrom:
secretKeyRef:
name: clickhouse
key: database
- name: SLACK_PANICS_WEBHOOK - name: SLACK_PANICS_WEBHOOK
valueFrom: valueFrom:
secretKeyRef: secretKeyRef:

View File

@@ -102,9 +102,6 @@ async fn update_billing_preferences(
.await? .await?
.ok_or_else(|| anyhow!("user not found"))?; .ok_or_else(|| anyhow!("user not found"))?;
let max_monthly_llm_usage_spending_in_cents =
body.max_monthly_llm_usage_spending_in_cents.max(0);
let billing_preferences = let billing_preferences =
if let Some(_billing_preferences) = app.db.get_billing_preferences(user.id).await? { if let Some(_billing_preferences) = app.db.get_billing_preferences(user.id).await? {
app.db app.db
@@ -112,7 +109,7 @@ async fn update_billing_preferences(
user.id, user.id,
&UpdateBillingPreferencesParams { &UpdateBillingPreferencesParams {
max_monthly_llm_usage_spending_in_cents: ActiveValue::set( max_monthly_llm_usage_spending_in_cents: ActiveValue::set(
max_monthly_llm_usage_spending_in_cents, body.max_monthly_llm_usage_spending_in_cents,
), ),
}, },
) )
@@ -122,7 +119,8 @@ async fn update_billing_preferences(
.create_billing_preferences( .create_billing_preferences(
user.id, user.id,
&crate::db::CreateBillingPreferencesParams { &crate::db::CreateBillingPreferencesParams {
max_monthly_llm_usage_spending_in_cents, max_monthly_llm_usage_spending_in_cents: body
.max_monthly_llm_usage_spending_in_cents,
}, },
) )
.await? .await?
@@ -130,7 +128,7 @@ async fn update_billing_preferences(
SnowflakeRow::new( SnowflakeRow::new(
"Spend Limit Updated", "Spend Limit Updated",
user.metrics_id, Some(user.metrics_id),
user.admin, user.admin,
None, None,
json!({ json!({

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,28 @@
use serde::Serialize;
/// Writes the given rows to the specified Clickhouse table.
pub async fn write_to_table<T: clickhouse::Row + Serialize + std::fmt::Debug>(
table: &str,
rows: &[T],
clickhouse_client: &clickhouse::Client,
) -> anyhow::Result<()> {
if rows.is_empty() {
return Ok(());
}
let mut insert = clickhouse_client.insert(table)?;
for event in rows {
insert.write(event).await?;
}
insert.end().await?;
let event_count = rows.len();
log::info!(
"wrote {event_count} {event_specifier} to '{table}'",
event_specifier = if event_count == 1 { "event" } else { "events" }
);
Ok(())
}

View File

@@ -1,6 +1,7 @@
pub mod api; pub mod api;
pub mod auth; pub mod auth;
mod cents; mod cents;
pub mod clickhouse;
pub mod db; pub mod db;
pub mod env; pub mod env;
pub mod executor; pub mod executor;
@@ -150,6 +151,10 @@ pub struct Config {
pub seed_path: Option<PathBuf>, pub seed_path: Option<PathBuf>,
pub database_max_connections: u32, pub database_max_connections: u32,
pub api_token: String, pub api_token: String,
pub clickhouse_url: Option<String>,
pub clickhouse_user: Option<String>,
pub clickhouse_password: Option<String>,
pub clickhouse_database: Option<String>,
pub invite_link_prefix: String, pub invite_link_prefix: String,
pub livekit_server: Option<String>, pub livekit_server: Option<String>,
pub livekit_key: Option<String>, pub livekit_key: Option<String>,
@@ -231,6 +236,10 @@ impl Config {
prediction_api_url: None, prediction_api_url: None,
prediction_api_key: None, prediction_api_key: None,
prediction_model: None, prediction_model: None,
clickhouse_url: None,
clickhouse_user: None,
clickhouse_password: None,
clickhouse_database: None,
zed_client_checksum_seed: None, zed_client_checksum_seed: None,
slack_panics_webhook: None, slack_panics_webhook: None,
auto_join_channel_id: None, auto_join_channel_id: None,
@@ -280,6 +289,7 @@ pub struct AppState {
pub stripe_billing: Option<Arc<StripeBilling>>, pub stripe_billing: Option<Arc<StripeBilling>>,
pub rate_limiter: Arc<RateLimiter>, pub rate_limiter: Arc<RateLimiter>,
pub executor: Executor, pub executor: Executor,
pub clickhouse_client: Option<::clickhouse::Client>,
pub kinesis_client: Option<::aws_sdk_kinesis::Client>, pub kinesis_client: Option<::aws_sdk_kinesis::Client>,
pub config: Config, pub config: Config,
} }
@@ -333,6 +343,10 @@ impl AppState {
stripe_client, stripe_client,
rate_limiter: Arc::new(RateLimiter::new(db)), rate_limiter: Arc::new(RateLimiter::new(db)),
executor, executor,
clickhouse_client: config
.clickhouse_url
.as_ref()
.and_then(|_| build_clickhouse_client(&config).log_err()),
kinesis_client: if config.kinesis_access_key.is_some() { kinesis_client: if config.kinesis_access_key.is_some() {
build_kinesis_client(&config).await.log_err() build_kinesis_client(&config).await.log_err()
} else { } else {
@@ -415,3 +429,31 @@ async fn build_kinesis_client(config: &Config) -> anyhow::Result<aws_sdk_kinesis
Ok(aws_sdk_kinesis::Client::new(&kinesis_config)) Ok(aws_sdk_kinesis::Client::new(&kinesis_config))
} }
fn build_clickhouse_client(config: &Config) -> anyhow::Result<::clickhouse::Client> {
Ok(::clickhouse::Client::default()
.with_url(
config
.clickhouse_url
.as_ref()
.ok_or_else(|| anyhow!("missing clickhouse_url"))?,
)
.with_user(
config
.clickhouse_user
.as_ref()
.ok_or_else(|| anyhow!("missing clickhouse_user"))?,
)
.with_password(
config
.clickhouse_password
.as_ref()
.ok_or_else(|| anyhow!("missing clickhouse_password"))?,
)
.with_database(
config
.clickhouse_database
.as_ref()
.ok_or_else(|| anyhow!("missing clickhouse_database"))?,
))
}

View File

@@ -1,11 +1,14 @@
mod authorization; mod authorization;
pub mod db; pub mod db;
mod telemetry;
mod token; mod token;
use crate::api::events::SnowflakeRow; use crate::api::events::SnowflakeRow;
use crate::api::CloudflareIpCountryHeader; use crate::api::CloudflareIpCountryHeader;
use crate::build_kinesis_client; use crate::build_kinesis_client;
use crate::{db::UserId, executor::Executor, Cents, Config, Error, Result}; use crate::{
build_clickhouse_client, db::UserId, executor::Executor, Cents, Config, Error, Result,
};
use anyhow::{anyhow, Context as _}; use anyhow::{anyhow, Context as _};
use authorization::authorize_access_to_language_model; use authorization::authorize_access_to_language_model;
use axum::routing::get; use axum::routing::get;
@@ -37,6 +40,7 @@ use std::{
task::{Context, Poll}, task::{Context, Poll},
}; };
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use telemetry::{report_llm_rate_limit, report_llm_usage, LlmRateLimitEventRow, LlmUsageEventRow};
use tokio::sync::RwLock; use tokio::sync::RwLock;
use util::ResultExt; use util::ResultExt;
@@ -48,6 +52,7 @@ pub struct LlmState {
pub db: Arc<LlmDatabase>, pub db: Arc<LlmDatabase>,
pub http_client: ReqwestClient, pub http_client: ReqwestClient,
pub kinesis_client: Option<aws_sdk_kinesis::Client>, pub kinesis_client: Option<aws_sdk_kinesis::Client>,
pub clickhouse_client: Option<clickhouse::Client>,
active_user_count_by_model: active_user_count_by_model:
RwLock<HashMap<(LanguageModelProvider, String), (DateTime<Utc>, ActiveUserCount)>>, RwLock<HashMap<(LanguageModelProvider, String), (DateTime<Utc>, ActiveUserCount)>>,
} }
@@ -84,6 +89,10 @@ impl LlmState {
} else { } else {
None None
}, },
clickhouse_client: config
.clickhouse_url
.as_ref()
.and_then(|_| build_clickhouse_client(&config).log_err()),
active_user_count_by_model: RwLock::new(HashMap::default()), active_user_count_by_model: RwLock::new(HashMap::default()),
config, config,
}; };
@@ -621,6 +630,34 @@ async fn check_usage_limit(
.await .await
.log_err(); .log_err();
if let Some(client) = state.clickhouse_client.as_ref() {
report_llm_rate_limit(
client,
LlmRateLimitEventRow {
time: Utc::now().timestamp_millis(),
user_id: claims.user_id as i32,
is_staff: claims.is_staff,
plan: match claims.plan {
Plan::Free => "free".to_string(),
Plan::ZedPro => "zed_pro".to_string(),
},
model: model.name.clone(),
provider: provider.to_string(),
usage_measure: resource.to_string(),
requests_this_minute: usage.requests_this_minute as u64,
tokens_this_minute: usage.tokens_this_minute as u64,
tokens_this_day: usage.tokens_this_day as u64,
users_in_recent_minutes: users_in_recent_minutes as u64,
users_in_recent_days: users_in_recent_days as u64,
max_requests_per_minute: per_user_max_requests_per_minute as u64,
max_tokens_per_minute: per_user_max_tokens_per_minute as u64,
max_tokens_per_day: per_user_max_tokens_per_day as u64,
},
)
.await
.log_err();
}
return Err(Error::http( return Err(Error::http(
StatusCode::TOO_MANY_REQUESTS, StatusCode::TOO_MANY_REQUESTS,
format!("Rate limit exceeded. Maximum {} reached.", resource), format!("Rate limit exceeded. Maximum {} reached.", resource),
@@ -707,8 +744,6 @@ impl<S> Drop for TokenCountingStream<S> {
); );
let properties = json!({ let properties = json!({
"has_llm_subscription": claims.has_llm_subscription,
"max_monthly_spend_in_cents": claims.max_monthly_spend_in_cents,
"plan": match claims.plan { "plan": match claims.plan {
Plan::Free => "free".to_string(), Plan::Free => "free".to_string(),
Plan::ZedPro => "zed_pro".to_string(), Plan::ZedPro => "zed_pro".to_string(),
@@ -728,6 +763,44 @@ impl<S> Drop for TokenCountingStream<S> {
.write(&state.kinesis_client, &state.config.kinesis_stream) .write(&state.kinesis_client, &state.config.kinesis_stream)
.await .await
.log_err(); .log_err();
if let Some(clickhouse_client) = state.clickhouse_client.as_ref() {
report_llm_usage(
clickhouse_client,
LlmUsageEventRow {
time: Utc::now().timestamp_millis(),
user_id: claims.user_id as i32,
is_staff: claims.is_staff,
plan: match claims.plan {
Plan::Free => "free".to_string(),
Plan::ZedPro => "zed_pro".to_string(),
},
model,
provider: provider.to_string(),
input_token_count: tokens.input as u64,
cache_creation_input_token_count: tokens.input_cache_creation as u64,
cache_read_input_token_count: tokens.input_cache_read as u64,
output_token_count: tokens.output as u64,
requests_this_minute: usage.requests_this_minute as u64,
tokens_this_minute: usage.tokens_this_minute as u64,
tokens_this_day: usage.tokens_this_day as u64,
input_tokens_this_month: usage.tokens_this_month.input as u64,
cache_creation_input_tokens_this_month: usage
.tokens_this_month
.input_cache_creation
as u64,
cache_read_input_tokens_this_month: usage
.tokens_this_month
.input_cache_read
as u64,
output_tokens_this_month: usage.tokens_this_month.output as u64,
spending_this_month: usage.spending_this_month.0 as u64,
lifetime_spending: usage.lifetime_spending.0 as u64,
},
)
.await
.log_err();
}
} }
}) })
} }

View File

@@ -0,0 +1,65 @@
use anyhow::{Context, Result};
use serde::Serialize;
use crate::clickhouse::write_to_table;
#[derive(Serialize, Debug, clickhouse::Row)]
pub struct LlmUsageEventRow {
pub time: i64,
pub user_id: i32,
pub is_staff: bool,
pub plan: String,
pub model: String,
pub provider: String,
pub input_token_count: u64,
pub cache_creation_input_token_count: u64,
pub cache_read_input_token_count: u64,
pub output_token_count: u64,
pub requests_this_minute: u64,
pub tokens_this_minute: u64,
pub tokens_this_day: u64,
pub input_tokens_this_month: u64,
pub cache_creation_input_tokens_this_month: u64,
pub cache_read_input_tokens_this_month: u64,
pub output_tokens_this_month: u64,
pub spending_this_month: u64,
pub lifetime_spending: u64,
}
#[derive(Serialize, Debug, clickhouse::Row)]
pub struct LlmRateLimitEventRow {
pub time: i64,
pub user_id: i32,
pub is_staff: bool,
pub plan: String,
pub model: String,
pub provider: String,
pub usage_measure: String,
pub requests_this_minute: u64,
pub tokens_this_minute: u64,
pub tokens_this_day: u64,
pub users_in_recent_minutes: u64,
pub users_in_recent_days: u64,
pub max_requests_per_minute: u64,
pub max_tokens_per_minute: u64,
pub max_tokens_per_day: u64,
}
pub async fn report_llm_usage(client: &clickhouse::Client, row: LlmUsageEventRow) -> Result<()> {
const LLM_USAGE_EVENTS_TABLE: &str = "llm_usage_events";
write_to_table(LLM_USAGE_EVENTS_TABLE, &[row], client)
.await
.with_context(|| format!("failed to upload to table '{LLM_USAGE_EVENTS_TABLE}'"))?;
Ok(())
}
pub async fn report_llm_rate_limit(
client: &clickhouse::Client,
row: LlmRateLimitEventRow,
) -> Result<()> {
const LLM_RATE_LIMIT_EVENTS_TABLE: &str = "llm_rate_limit_events";
write_to_table(LLM_RATE_LIMIT_EVENTS_TABLE, &[row], client)
.await
.with_context(|| format!("failed to upload to table '{LLM_RATE_LIMIT_EVENTS_TABLE}'"))?;
Ok(())
}

View File

@@ -17,8 +17,10 @@ pub struct LlmTokenClaims {
pub exp: u64, pub exp: u64,
pub jti: String, pub jti: String,
pub user_id: u64, pub user_id: u64,
#[serde(default)]
pub system_id: Option<String>, pub system_id: Option<String>,
pub metrics_id: Uuid, #[serde(default)]
pub metrics_id: Option<Uuid>,
pub github_user_login: String, pub github_user_login: String,
pub is_staff: bool, pub is_staff: bool,
pub has_llm_closed_beta_feature_flag: bool, pub has_llm_closed_beta_feature_flag: bool,
@@ -54,7 +56,7 @@ impl LlmTokenClaims {
jti: uuid::Uuid::new_v4().to_string(), jti: uuid::Uuid::new_v4().to_string(),
user_id: user.id.to_proto(), user_id: user.id.to_proto(),
system_id, system_id,
metrics_id: user.metrics_id, metrics_id: Some(user.metrics_id),
github_user_login: user.github_login.clone(), github_user_login: user.github_login.clone(),
is_staff, is_staff,
has_llm_closed_beta_feature_flag, has_llm_closed_beta_feature_flag,

View File

@@ -310,9 +310,6 @@ impl Server {
.add_request_handler(forward_read_only_project_request::<proto::OpenBufferByPath>) .add_request_handler(forward_read_only_project_request::<proto::OpenBufferByPath>)
.add_request_handler(forward_read_only_project_request::<proto::GitBranches>) .add_request_handler(forward_read_only_project_request::<proto::GitBranches>)
.add_request_handler(forward_read_only_project_request::<proto::GetStagedText>) .add_request_handler(forward_read_only_project_request::<proto::GetStagedText>)
.add_request_handler(
forward_mutating_project_request::<proto::RegisterBufferWithLanguageServers>,
)
.add_request_handler(forward_mutating_project_request::<proto::UpdateGitBranch>) .add_request_handler(forward_mutating_project_request::<proto::UpdateGitBranch>)
.add_request_handler(forward_mutating_project_request::<proto::GetCompletions>) .add_request_handler(forward_mutating_project_request::<proto::GetCompletions>)
.add_request_handler( .add_request_handler(

View File

@@ -994,12 +994,10 @@ async fn test_language_server_statuses(cx_a: &mut TestAppContext, cx_b: &mut Tes
}), }),
) )
.await; .await;
let (project_a, _) = client_a.build_local_project("/dir", cx_a).await; let (project_a, worktree_id) = client_a.build_local_project("/dir", cx_a).await;
let _buffer_a = project_a let _buffer_a = project_a
.update(cx_a, |p, cx| { .update(cx_a, |p, cx| p.open_buffer((worktree_id, "main.rs"), cx))
p.open_local_buffer_with_lsp("/dir/main.rs", cx)
})
.await .await
.unwrap(); .unwrap();
@@ -1589,6 +1587,7 @@ async fn test_mutual_editor_inlay_hint_cache_update(
}) })
.await .await
.unwrap(); .unwrap();
let fake_language_server = fake_language_servers.next().await.unwrap();
let editor_a = workspace_a let editor_a = workspace_a
.update(cx_a, |workspace, cx| { .update(cx_a, |workspace, cx| {
workspace.open_path((worktree_id, "main.rs"), None, true, cx) workspace.open_path((worktree_id, "main.rs"), None, true, cx)
@@ -1598,8 +1597,6 @@ async fn test_mutual_editor_inlay_hint_cache_update(
.downcast::<Editor>() .downcast::<Editor>()
.unwrap(); .unwrap();
let fake_language_server = fake_language_servers.next().await.unwrap();
// Set up the language server to return an additional inlay hint on each request. // Set up the language server to return an additional inlay hint on each request.
let edits_made = Arc::new(AtomicUsize::new(0)); let edits_made = Arc::new(AtomicUsize::new(0));
let closure_edits_made = Arc::clone(&edits_made); let closure_edits_made = Arc::clone(&edits_made);

View File

@@ -3891,7 +3891,13 @@ async fn test_collaborating_with_diagnostics(
// Cause the language server to start. // Cause the language server to start.
let _buffer = project_a let _buffer = project_a
.update(cx_a, |project, cx| { .update(cx_a, |project, cx| {
project.open_local_buffer_with_lsp("/a/other.rs", cx) project.open_buffer(
ProjectPath {
worktree_id,
path: Path::new("other.rs").into(),
},
cx,
)
}) })
.await .await
.unwrap(); .unwrap();
@@ -4170,9 +4176,7 @@ async fn test_collaborating_with_lsp_progress_updates_and_diagnostics_ordering(
// Join the project as client B and open all three files. // Join the project as client B and open all three files.
let project_b = client_b.join_remote_project(project_id, cx_b).await; let project_b = client_b.join_remote_project(project_id, cx_b).await;
let guest_buffers = futures::future::try_join_all(file_names.iter().map(|file_name| { let guest_buffers = futures::future::try_join_all(file_names.iter().map(|file_name| {
project_b.update(cx_b, |p, cx| { project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, file_name), cx))
p.open_buffer_with_lsp((worktree_id, file_name), cx)
})
})) }))
.await .await
.unwrap(); .unwrap();
@@ -4226,7 +4230,7 @@ async fn test_collaborating_with_lsp_progress_updates_and_diagnostics_ordering(
cx.subscribe(&project_b, move |_, _, event, cx| { cx.subscribe(&project_b, move |_, _, event, cx| {
if let project::Event::DiskBasedDiagnosticsFinished { .. } = event { if let project::Event::DiskBasedDiagnosticsFinished { .. } = event {
disk_based_diagnostics_finished.store(true, SeqCst); disk_based_diagnostics_finished.store(true, SeqCst);
for (buffer, _) in &guest_buffers { for buffer in &guest_buffers {
assert_eq!( assert_eq!(
buffer buffer
.read(cx) .read(cx)
@@ -4347,6 +4351,7 @@ async fn test_formatting_buffer(
cx_a: &mut TestAppContext, cx_a: &mut TestAppContext,
cx_b: &mut TestAppContext, cx_b: &mut TestAppContext,
) { ) {
executor.allow_parking();
let mut server = TestServer::start(executor.clone()).await; let mut server = TestServer::start(executor.clone()).await;
let client_a = server.create_client(cx_a, "user_a").await; let client_a = server.create_client(cx_a, "user_a").await;
let client_b = server.create_client(cx_b, "user_b").await; let client_b = server.create_client(cx_b, "user_b").await;
@@ -4374,16 +4379,10 @@ async fn test_formatting_buffer(
.await .await
.unwrap(); .unwrap();
let project_b = client_b.join_remote_project(project_id, cx_b).await; let project_b = client_b.join_remote_project(project_id, cx_b).await;
let lsp_store_b = project_b.update(cx_b, |p, _| p.lsp_store());
let buffer_b = project_b let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx));
.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx)) let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
.await
.unwrap();
let _handle = lsp_store_b.update(cx_b, |lsp_store, cx| {
lsp_store.register_buffer_with_language_servers(&buffer_b, cx)
});
let fake_language_server = fake_language_servers.next().await.unwrap(); let fake_language_server = fake_language_servers.next().await.unwrap();
fake_language_server.handle_request::<lsp::request::Formatting, _, _>(|_, _| async move { fake_language_server.handle_request::<lsp::request::Formatting, _, _>(|_, _| async move {
Ok(Some(vec![ Ok(Some(vec![
@@ -4432,8 +4431,6 @@ async fn test_formatting_buffer(
}); });
}); });
}); });
executor.allow_parking();
project_b project_b
.update(cx_b, |project, cx| { .update(cx_b, |project, cx| {
project.format( project.format(
@@ -4506,12 +4503,8 @@ async fn test_prettier_formatting_buffer(
.await .await
.unwrap(); .unwrap();
let project_b = client_b.join_remote_project(project_id, cx_b).await; let project_b = client_b.join_remote_project(project_id, cx_b).await;
let (buffer_b, _) = project_b let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.ts"), cx));
.update(cx_b, |p, cx| { let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
p.open_buffer_with_lsp((worktree_id, "a.ts"), cx)
})
.await
.unwrap();
cx_a.update(|cx| { cx_a.update(|cx| {
SettingsStore::update_global(cx, |store, cx| { SettingsStore::update_global(cx, |store, cx| {
@@ -4627,12 +4620,8 @@ async fn test_definition(
let project_b = client_b.join_remote_project(project_id, cx_b).await; let project_b = client_b.join_remote_project(project_id, cx_b).await;
// Open the file on client B. // Open the file on client B.
let (buffer_b, _handle) = project_b let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx));
.update(cx_b, |p, cx| { let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
p.open_buffer_with_lsp((worktree_id, "a.rs"), cx)
})
.await
.unwrap();
// Request the definition of a symbol as the guest. // Request the definition of a symbol as the guest.
let fake_language_server = fake_language_servers.next().await.unwrap(); let fake_language_server = fake_language_servers.next().await.unwrap();
@@ -4776,12 +4765,8 @@ async fn test_references(
let project_b = client_b.join_remote_project(project_id, cx_b).await; let project_b = client_b.join_remote_project(project_id, cx_b).await;
// Open the file on client B. // Open the file on client B.
let (buffer_b, _handle) = project_b let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "one.rs"), cx));
.update(cx_b, |p, cx| { let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
p.open_buffer_with_lsp((worktree_id, "one.rs"), cx)
})
.await
.unwrap();
// Request references to a symbol as the guest. // Request references to a symbol as the guest.
let fake_language_server = fake_language_servers.next().await.unwrap(); let fake_language_server = fake_language_servers.next().await.unwrap();
@@ -5027,12 +5012,8 @@ async fn test_document_highlights(
let project_b = client_b.join_remote_project(project_id, cx_b).await; let project_b = client_b.join_remote_project(project_id, cx_b).await;
// Open the file on client B. // Open the file on client B.
let (buffer_b, _handle) = project_b let open_b = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "main.rs"), cx));
.update(cx_b, |p, cx| { let buffer_b = cx_b.executor().spawn(open_b).await.unwrap();
p.open_buffer_with_lsp((worktree_id, "main.rs"), cx)
})
.await
.unwrap();
// Request document highlights as the guest. // Request document highlights as the guest.
let fake_language_server = fake_language_servers.next().await.unwrap(); let fake_language_server = fake_language_servers.next().await.unwrap();
@@ -5149,12 +5130,8 @@ async fn test_lsp_hover(
let project_b = client_b.join_remote_project(project_id, cx_b).await; let project_b = client_b.join_remote_project(project_id, cx_b).await;
// Open the file as the guest // Open the file as the guest
let (buffer_b, _handle) = project_b let open_buffer = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "main.rs"), cx));
.update(cx_b, |p, cx| { let buffer_b = cx_b.executor().spawn(open_buffer).await.unwrap();
p.open_buffer_with_lsp((worktree_id, "main.rs"), cx)
})
.await
.unwrap();
let mut servers_with_hover_requests = HashMap::default(); let mut servers_with_hover_requests = HashMap::default();
for i in 0..language_server_names.len() { for i in 0..language_server_names.len() {
@@ -5329,12 +5306,9 @@ async fn test_project_symbols(
let project_b = client_b.join_remote_project(project_id, cx_b).await; let project_b = client_b.join_remote_project(project_id, cx_b).await;
// Cause the language server to start. // Cause the language server to start.
let _buffer = project_b let open_buffer_task =
.update(cx_b, |p, cx| { project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "one.rs"), cx));
p.open_buffer_with_lsp((worktree_id, "one.rs"), cx) let _buffer = cx_b.executor().spawn(open_buffer_task).await.unwrap();
})
.await
.unwrap();
let fake_language_server = fake_language_servers.next().await.unwrap(); let fake_language_server = fake_language_servers.next().await.unwrap();
fake_language_server.handle_request::<lsp::WorkspaceSymbolRequest, _, _>(|_, _| async move { fake_language_server.handle_request::<lsp::WorkspaceSymbolRequest, _, _>(|_, _| async move {
@@ -5426,12 +5400,8 @@ async fn test_open_buffer_while_getting_definition_pointing_to_it(
.unwrap(); .unwrap();
let project_b = client_b.join_remote_project(project_id, cx_b).await; let project_b = client_b.join_remote_project(project_id, cx_b).await;
let (buffer_b1, _lsp) = project_b let open_buffer_task = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx));
.update(cx_b, |p, cx| { let buffer_b1 = cx_b.executor().spawn(open_buffer_task).await.unwrap();
p.open_buffer_with_lsp((worktree_id, "a.rs"), cx)
})
.await
.unwrap();
let fake_language_server = fake_language_servers.next().await.unwrap(); let fake_language_server = fake_language_servers.next().await.unwrap();
fake_language_server.handle_request::<lsp::request::GotoDefinition, _, _>(|_, _| async move { fake_language_server.handle_request::<lsp::request::GotoDefinition, _, _>(|_, _| async move {
@@ -5447,22 +5417,13 @@ async fn test_open_buffer_while_getting_definition_pointing_to_it(
let buffer_b2; let buffer_b2;
if rng.gen() { if rng.gen() {
definitions = project_b.update(cx_b, |p, cx| p.definition(&buffer_b1, 23, cx)); definitions = project_b.update(cx_b, |p, cx| p.definition(&buffer_b1, 23, cx));
(buffer_b2, _) = project_b buffer_b2 = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "b.rs"), cx));
.update(cx_b, |p, cx| {
p.open_buffer_with_lsp((worktree_id, "b.rs"), cx)
})
.await
.unwrap();
} else { } else {
(buffer_b2, _) = project_b buffer_b2 = project_b.update(cx_b, |p, cx| p.open_buffer((worktree_id, "b.rs"), cx));
.update(cx_b, |p, cx| {
p.open_buffer_with_lsp((worktree_id, "b.rs"), cx)
})
.await
.unwrap();
definitions = project_b.update(cx_b, |p, cx| p.definition(&buffer_b1, 23, cx)); definitions = project_b.update(cx_b, |p, cx| p.definition(&buffer_b1, 23, cx));
} }
let buffer_b2 = buffer_b2.await.unwrap();
let definitions = definitions.await.unwrap(); let definitions = definitions.await.unwrap();
assert_eq!(definitions.len(), 1); assert_eq!(definitions.len(), 1);
assert_eq!(definitions[0].target.buffer, buffer_b2); assert_eq!(definitions[0].target.buffer, buffer_b2);

View File

@@ -426,10 +426,8 @@ async fn test_ssh_collaboration_formatting_with_prettier(
executor.run_until_parked(); executor.run_until_parked();
// Opens the buffer and formats it // Opens the buffer and formats it
let (buffer_b, _handle) = project_b let buffer_b = project_b
.update(cx_b, |p, cx| { .update(cx_b, |p, cx| p.open_buffer((worktree_id, "a.ts"), cx))
p.open_buffer_with_lsp((worktree_id, "a.ts"), cx)
})
.await .await
.expect("user B opens buffer for formatting"); .expect("user B opens buffer for formatting");

View File

@@ -518,6 +518,7 @@ impl TestServer {
stripe_billing: None, stripe_billing: None,
rate_limiter: Arc::new(RateLimiter::new(test_db.db().clone())), rate_limiter: Arc::new(RateLimiter::new(test_db.db().clone())),
executor, executor,
clickhouse_client: None,
kinesis_client: None, kinesis_client: None,
config: Config { config: Config {
http_port: 0, http_port: 0,
@@ -548,6 +549,10 @@ impl TestServer {
prediction_api_url: None, prediction_api_url: None,
prediction_api_key: None, prediction_api_key: None,
prediction_model: None, prediction_model: None,
clickhouse_url: None,
clickhouse_user: None,
clickhouse_password: None,
clickhouse_database: None,
zed_client_checksum_seed: None, zed_client_checksum_seed: None,
slack_panics_webhook: None, slack_panics_webhook: None,
auto_join_channel_id: None, auto_join_channel_id: None,

View File

@@ -841,7 +841,7 @@ impl CollabPanel {
ListItem::new(SharedString::from(user.github_login.clone())) ListItem::new(SharedString::from(user.github_login.clone()))
.start_slot(Avatar::new(user.avatar_uri.clone())) .start_slot(Avatar::new(user.avatar_uri.clone()))
.child(Label::new(user.github_login.clone())) .child(Label::new(user.github_login.clone()))
.toggle_state(is_selected) .selected(is_selected)
.end_slot(if is_pending { .end_slot(if is_pending {
Label::new("Calling").color(Color::Muted).into_any_element() Label::new("Calling").color(Color::Muted).into_any_element()
} else if is_current_user { } else if is_current_user {
@@ -894,7 +894,7 @@ impl CollabPanel {
.into(); .into();
ListItem::new(project_id as usize) ListItem::new(project_id as usize)
.toggle_state(is_selected) .selected(is_selected)
.on_click(cx.listener(move |this, _, cx| { .on_click(cx.listener(move |this, _, cx| {
this.workspace this.workspace
.update(cx, |workspace, cx| { .update(cx, |workspace, cx| {
@@ -924,7 +924,7 @@ impl CollabPanel {
let id = peer_id.map_or(usize::MAX, |id| id.as_u64() as usize); let id = peer_id.map_or(usize::MAX, |id| id.as_u64() as usize);
ListItem::new(("screen", id)) ListItem::new(("screen", id))
.toggle_state(is_selected) .selected(is_selected)
.start_slot( .start_slot(
h_flex() h_flex()
.gap_1() .gap_1()
@@ -964,7 +964,7 @@ impl CollabPanel {
let channel_store = self.channel_store.read(cx); let channel_store = self.channel_store.read(cx);
let has_channel_buffer_changed = channel_store.has_channel_buffer_changed(channel_id); let has_channel_buffer_changed = channel_store.has_channel_buffer_changed(channel_id);
ListItem::new("channel-notes") ListItem::new("channel-notes")
.toggle_state(is_selected) .selected(is_selected)
.on_click(cx.listener(move |this, _, cx| { .on_click(cx.listener(move |this, _, cx| {
this.open_channel_notes(channel_id, cx); this.open_channel_notes(channel_id, cx);
})) }))
@@ -996,7 +996,7 @@ impl CollabPanel {
let channel_store = self.channel_store.read(cx); let channel_store = self.channel_store.read(cx);
let has_messages_notification = channel_store.has_new_messages(channel_id); let has_messages_notification = channel_store.has_new_messages(channel_id);
ListItem::new("channel-chat") ListItem::new("channel-chat")
.toggle_state(is_selected) .selected(is_selected)
.on_click(cx.listener(move |this, _, cx| { .on_click(cx.listener(move |this, _, cx| {
this.join_channel_chat(channel_id, cx); this.join_channel_chat(channel_id, cx);
})) }))
@@ -2253,7 +2253,7 @@ impl CollabPanel {
}) })
.inset(true) .inset(true)
.end_slot::<AnyElement>(button) .end_slot::<AnyElement>(button)
.toggle_state(is_selected), .selected(is_selected),
) )
} }
@@ -2270,7 +2270,7 @@ impl CollabPanel {
let item = ListItem::new(github_login.clone()) let item = ListItem::new(github_login.clone())
.indent_level(1) .indent_level(1)
.indent_step_size(px(20.)) .indent_step_size(px(20.))
.toggle_state(is_selected) .selected(is_selected)
.child( .child(
h_flex() h_flex()
.w_full() .w_full()
@@ -2381,7 +2381,7 @@ impl CollabPanel {
ListItem::new(github_login.clone()) ListItem::new(github_login.clone())
.indent_level(1) .indent_level(1)
.indent_step_size(px(20.)) .indent_step_size(px(20.))
.toggle_state(is_selected) .selected(is_selected)
.child( .child(
h_flex() h_flex()
.w_full() .w_full()
@@ -2425,7 +2425,7 @@ impl CollabPanel {
]; ];
ListItem::new(("channel-invite", channel.id.0 as usize)) ListItem::new(("channel-invite", channel.id.0 as usize))
.toggle_state(is_selected) .selected(is_selected)
.child( .child(
h_flex() h_flex()
.w_full() .w_full()
@@ -2448,7 +2448,7 @@ impl CollabPanel {
ListItem::new("contact-placeholder") ListItem::new("contact-placeholder")
.child(Icon::new(IconName::Plus)) .child(Icon::new(IconName::Plus))
.child(Label::new("Add a Contact")) .child(Label::new("Add a Contact"))
.toggle_state(is_selected) .selected(is_selected)
.on_click(cx.listener(|this, _, cx| this.toggle_contact_finder(cx))) .on_click(cx.listener(|this, _, cx| this.toggle_contact_finder(cx)))
} }
@@ -2547,7 +2547,7 @@ impl CollabPanel {
// Add one level of depth for the disclosure arrow. // Add one level of depth for the disclosure arrow.
.indent_level(depth + 1) .indent_level(depth + 1)
.indent_step_size(px(20.)) .indent_step_size(px(20.))
.toggle_state(is_selected || is_active) .selected(is_selected || is_active)
.toggle(disclosed) .toggle(disclosed)
.on_toggle( .on_toggle(
cx.listener(move |this, _, cx| { cx.listener(move |this, _, cx| {

View File

@@ -89,15 +89,15 @@ impl ChannelModal {
cx.notify() cx.notify()
} }
fn set_channel_visibility(&mut self, selection: &ToggleState, cx: &mut ViewContext<Self>) { fn set_channel_visibility(&mut self, selection: &Selection, cx: &mut ViewContext<Self>) {
self.channel_store.update(cx, |channel_store, cx| { self.channel_store.update(cx, |channel_store, cx| {
channel_store channel_store
.set_channel_visibility( .set_channel_visibility(
self.channel_id, self.channel_id,
match selection { match selection {
ToggleState::Unselected => ChannelVisibility::Members, Selection::Unselected => ChannelVisibility::Members,
ToggleState::Selected => ChannelVisibility::Public, Selection::Selected => ChannelVisibility::Public,
ToggleState::Indeterminate => return, Selection::Indeterminate => return,
}, },
cx, cx,
) )
@@ -159,9 +159,9 @@ impl Render for ChannelModal {
"is-public", "is-public",
Label::new("Public").size(LabelSize::Small), Label::new("Public").size(LabelSize::Small),
if visibility == ChannelVisibility::Public { if visibility == ChannelVisibility::Public {
ui::ToggleState::Selected ui::Selection::Selected
} else { } else {
ui::ToggleState::Unselected ui::Selection::Unselected
}, },
cx.listener(Self::set_channel_visibility), cx.listener(Self::set_channel_visibility),
)) ))
@@ -386,7 +386,7 @@ impl PickerDelegate for ChannelModalDelegate {
ListItem::new(ix) ListItem::new(ix)
.inset(true) .inset(true)
.spacing(ListItemSpacing::Sparse) .spacing(ListItemSpacing::Sparse)
.toggle_state(selected) .selected(selected)
.start_slot(Avatar::new(user.avatar_uri.clone())) .start_slot(Avatar::new(user.avatar_uri.clone()))
.child(Label::new(user.github_login.clone())) .child(Label::new(user.github_login.clone()))
.end_slot(h_flex().gap_2().map(|slot| { .end_slot(h_flex().gap_2().map(|slot| {

View File

@@ -151,7 +151,7 @@ impl PickerDelegate for ContactFinderDelegate {
ListItem::new(ix) ListItem::new(ix)
.inset(true) .inset(true)
.spacing(ListItemSpacing::Sparse) .spacing(ListItemSpacing::Sparse)
.toggle_state(selected) .selected(selected)
.start_slot(Avatar::new(user.avatar_uri.clone())) .start_slot(Avatar::new(user.avatar_uri.clone()))
.child(Label::new(user.github_login.clone())) .child(Label::new(user.github_login.clone()))
.end_slot::<Icon>(icon_path.map(Icon::from_path)), .end_slot::<Icon>(icon_path.map(Icon::from_path)),

View File

@@ -397,7 +397,7 @@ impl PickerDelegate for CommandPaletteDelegate {
ListItem::new(ix) ListItem::new(ix)
.inset(true) .inset(true)
.spacing(ListItemSpacing::Sparse) .spacing(ListItemSpacing::Sparse)
.toggle_state(selected) .selected(selected)
.child( .child(
h_flex() h_flex()
.w_full() .w_full()

View File

@@ -6,12 +6,13 @@ use anyhow::{anyhow, Result};
use chrono::DateTime; use chrono::DateTime;
use fs::Fs; use fs::Fs;
use futures::{io::BufReader, stream::BoxStream, AsyncBufReadExt, AsyncReadExt, StreamExt}; use futures::{io::BufReader, stream::BoxStream, AsyncBufReadExt, AsyncReadExt, StreamExt};
use gpui::{prelude::*, AppContext, AsyncAppContext, Global}; use gpui::{AppContext, AsyncAppContext, Global};
use http_client::{AsyncBody, HttpClient, Method, Request as HttpRequest}; use http_client::{AsyncBody, HttpClient, Method, Request as HttpRequest};
use paths::home_dir; use paths::home_dir;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use settings::watch_config_file; use settings::watch_config_file;
use strum::EnumIter; use strum::EnumIter;
use ui::Context;
pub const COPILOT_CHAT_COMPLETION_URL: &str = "https://api.githubcopilot.com/chat/completions"; pub const COPILOT_CHAT_COMPLETION_URL: &str = "https://api.githubcopilot.com/chat/completions";
pub const COPILOT_CHAT_AUTH_URL: &str = "https://api.github.com/copilot_internal/v2/token"; pub const COPILOT_CHAT_AUTH_URL: &str = "https://api.github.com/copilot_internal/v2/token";

View File

@@ -296,6 +296,7 @@ mod tests {
editor.set_inline_completion_provider(Some(copilot_provider), cx) editor.set_inline_completion_provider(Some(copilot_provider), cx)
}); });
// When inserting, ensure autocompletion is favored over Copilot suggestions.
cx.set_state(indoc! {" cx.set_state(indoc! {"
oneˇ oneˇ
two two
@@ -322,9 +323,8 @@ mod tests {
); );
executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT);
cx.update_editor(|editor, cx| { cx.update_editor(|editor, cx| {
// We want to show both: the inline completion and the completion menu
assert!(editor.context_menu_visible()); assert!(editor.context_menu_visible());
assert!(editor.has_active_inline_completion()); assert!(!editor.has_active_inline_completion());
// Confirming a completion inserts it and hides the context menu, without showing // Confirming a completion inserts it and hides the context menu, without showing
// the copilot suggestion afterwards. // the copilot suggestion afterwards.
@@ -338,7 +338,40 @@ mod tests {
assert_eq!(editor.display_text(cx), "one.completion_a\ntwo\nthree\n"); assert_eq!(editor.display_text(cx), "one.completion_a\ntwo\nthree\n");
}); });
// Reset editor and test that accepting completions works // Ensure Copilot suggestions are shown right away if no autocompletion is available.
cx.set_state(indoc! {"
oneˇ
two
three
"});
cx.simulate_keystroke(".");
drop(handle_completion_request(
&mut cx,
indoc! {"
one.|<>
two
three
"},
vec![],
));
handle_copilot_completion_request(
&copilot_lsp,
vec![crate::request::Completion {
text: "one.copilot1".into(),
range: lsp::Range::new(lsp::Position::new(0, 0), lsp::Position::new(0, 4)),
..Default::default()
}],
vec![],
);
executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT);
cx.update_editor(|editor, cx| {
assert!(!editor.context_menu_visible());
assert!(editor.has_active_inline_completion());
assert_eq!(editor.display_text(cx), "one.copilot1\ntwo\nthree\n");
assert_eq!(editor.text(cx), "one.\ntwo\nthree\n");
});
// Reset editor, and ensure autocompletion is still favored over Copilot suggestions.
cx.set_state(indoc! {" cx.set_state(indoc! {"
oneˇ oneˇ
two two
@@ -366,12 +399,17 @@ mod tests {
executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT);
cx.update_editor(|editor, cx| { cx.update_editor(|editor, cx| {
assert!(editor.context_menu_visible()); assert!(editor.context_menu_visible());
assert!(!editor.has_active_inline_completion());
// When hiding the context menu, the Copilot suggestion becomes visible.
editor.cancel(&Default::default(), cx);
assert!(!editor.context_menu_visible());
assert!(editor.has_active_inline_completion()); assert!(editor.has_active_inline_completion());
assert_eq!(editor.display_text(cx), "one.copilot1\ntwo\nthree\n"); assert_eq!(editor.display_text(cx), "one.copilot1\ntwo\nthree\n");
assert_eq!(editor.text(cx), "one.\ntwo\nthree\n"); assert_eq!(editor.text(cx), "one.\ntwo\nthree\n");
}); });
// Ensure existing inline completion is interpolated when inserting again. // Ensure existing completion is interpolated when inserting again.
cx.simulate_keystroke("c"); cx.simulate_keystroke("c");
executor.run_until_parked(); executor.run_until_parked();
cx.update_editor(|editor, cx| { cx.update_editor(|editor, cx| {
@@ -842,7 +880,7 @@ mod tests {
cx.update_editor(|editor, cx| editor.next_inline_completion(&Default::default(), cx)); cx.update_editor(|editor, cx| editor.next_inline_completion(&Default::default(), cx));
executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT);
cx.update_editor(|editor, cx| { cx.update_editor(|editor, cx| {
assert!(!editor.context_menu_visible()); assert!(!editor.context_menu_visible(), "Even there are some completions available, those are not triggered when active copilot suggestion is present");
assert!(editor.has_active_inline_completion()); assert!(editor.has_active_inline_completion());
assert_eq!(editor.display_text(cx), "one\ntwo.foo()\nthree\n"); assert_eq!(editor.display_text(cx), "one\ntwo.foo()\nthree\n");
assert_eq!(editor.text(cx), "one\ntw\nthree\n"); assert_eq!(editor.text(cx), "one\ntw\nthree\n");
@@ -896,9 +934,15 @@ mod tests {
); );
executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT); executor.advance_clock(COPILOT_DEBOUNCE_TIMEOUT);
cx.update_editor(|editor, cx| { cx.update_editor(|editor, cx| {
assert!(editor.context_menu_visible()); assert!(
assert!(editor.has_active_inline_completion(),); editor.context_menu_visible(),
assert_eq!(editor.display_text(cx), "one\ntwo.foo()\nthree\n"); "On completion trigger input, the completions should be fetched and visible"
);
assert!(
!editor.has_active_inline_completion(),
"On completion trigger input, copilot suggestion should be dismissed"
);
assert_eq!(editor.display_text(cx), "one\ntwo.\nthree\n");
assert_eq!(editor.text(cx), "one\ntwo.\nthree\n"); assert_eq!(editor.text(cx), "one\ntwo.\nthree\n");
}); });
} }

View File

@@ -138,16 +138,27 @@ impl ProjectDiagnosticsEditor {
language_server_id, language_server_id,
path, path,
} => { } => {
this.paths_to_update let max_severity = this.max_severity();
.insert((path.clone(), Some(*language_server_id))); let has_diagnostics_to_display = project.read(cx).lsp_store().read(cx).diagnostics_for_buffer(path)
this.summary = project.read(cx).diagnostic_summary(false, cx); .into_iter().flatten()
cx.emit(EditorEvent::TitleChanged); .filter(|(server_id, _)| language_server_id == server_id)
.flat_map(|(_, diagnostics)| diagnostics)
.any(|diagnostic| diagnostic.diagnostic.severity <= max_severity);
if this.editor.focus_handle(cx).contains_focused(cx) || this.focus_handle.contains_focused(cx) { if has_diagnostics_to_display {
log::debug!("diagnostics updated for server {language_server_id}, path {path:?}. recording change"); this.paths_to_update
.insert((path.clone(), Some(*language_server_id)));
this.summary = project.read(cx).diagnostic_summary(false, cx);
cx.emit(EditorEvent::TitleChanged);
if this.editor.focus_handle(cx).contains_focused(cx) || this.focus_handle.contains_focused(cx) {
log::debug!("diagnostics updated for server {language_server_id}, path {path:?}. recording change");
} else {
log::debug!("diagnostics updated for server {language_server_id}, path {path:?}. updating excerpts");
this.update_stale_excerpts(cx);
}
} else { } else {
log::debug!("diagnostics updated for server {language_server_id}, path {path:?}. updating excerpts"); log::debug!("diagnostics updated for server {language_server_id}, path {path:?}. no diagnostics to display");
this.update_stale_excerpts(cx);
} }
} }
_ => {} _ => {}
@@ -352,16 +363,12 @@ impl ProjectDiagnosticsEditor {
ExcerptId::min() ExcerptId::min()
}; };
let max_severity = self.max_severity();
let path_state = &mut self.path_states[path_ix]; let path_state = &mut self.path_states[path_ix];
let mut new_group_ixs = Vec::new(); let mut new_group_ixs = Vec::new();
let mut blocks_to_add = Vec::new(); let mut blocks_to_add = Vec::new();
let mut blocks_to_remove = HashSet::default(); let mut blocks_to_remove = HashSet::default();
let mut first_excerpt_id = None; let mut first_excerpt_id = None;
let max_severity = if self.include_warnings {
DiagnosticSeverity::WARNING
} else {
DiagnosticSeverity::ERROR
};
let excerpts_snapshot = self.excerpts.update(cx, |excerpts, cx| { let excerpts_snapshot = self.excerpts.update(cx, |excerpts, cx| {
let mut old_groups = mem::take(&mut path_state.diagnostic_groups) let mut old_groups = mem::take(&mut path_state.diagnostic_groups)
.into_iter() .into_iter()
@@ -650,6 +657,14 @@ impl ProjectDiagnosticsEditor {
prev_path = Some(path); prev_path = Some(path);
} }
} }
fn max_severity(&self) -> DiagnosticSeverity {
if self.include_warnings {
DiagnosticSeverity::WARNING
} else {
DiagnosticSeverity::ERROR
}
}
} }
impl FocusableView for ProjectDiagnosticsEditor { impl FocusableView for ProjectDiagnosticsEditor {

View File

@@ -809,7 +809,7 @@ async fn test_random_diagnostics(cx: &mut TestAppContext, mut rng: StdRng) {
updated_language_servers.insert(server_id); updated_language_servers.insert(server_id);
lsp_store.update(cx, |lsp_store, cx| { project.update(cx, |project, cx| {
log::info!("updating diagnostics. language server {server_id} path {path:?}"); log::info!("updating diagnostics. language server {server_id} path {path:?}");
randomly_update_diagnostics_for_path( randomly_update_diagnostics_for_path(
&fs, &fs,
@@ -818,12 +818,10 @@ async fn test_random_diagnostics(cx: &mut TestAppContext, mut rng: StdRng) {
&mut next_group_id, &mut next_group_id,
&mut rng, &mut rng,
); );
lsp_store project
.update_diagnostic_entries(server_id, path, None, diagnostics.clone(), cx) .update_diagnostic_entries(server_id, path, None, diagnostics.clone(), cx)
.unwrap() .unwrap()
}); });
cx.executor()
.advance_clock(DIAGNOSTICS_UPDATE_DEBOUNCE + Duration::from_millis(10));
cx.run_until_parked(); cx.run_until_parked();
} }
@@ -844,25 +842,10 @@ async fn test_random_diagnostics(cx: &mut TestAppContext, mut rng: StdRng) {
cx, cx,
) )
}); });
cx.executor()
.advance_clock(DIAGNOSTICS_UPDATE_DEBOUNCE + Duration::from_millis(10));
cx.run_until_parked(); cx.run_until_parked();
let mutated_excerpts = get_diagnostics_excerpts(&mutated_view, cx); let mutated_excerpts = get_diagnostics_excerpts(&mutated_view, cx);
let reference_excerpts = get_diagnostics_excerpts(&reference_view, cx); let reference_excerpts = get_diagnostics_excerpts(&reference_view, cx);
for ((path, language_server_id), diagnostics) in current_diagnostics {
for diagnostic in diagnostics {
let found_excerpt = reference_excerpts.iter().any(|info| {
let row_range = info.range.context.start.row..info.range.context.end.row;
info.path == path.strip_prefix("/test").unwrap()
&& info.language_server == language_server_id
&& row_range.contains(&diagnostic.range.start.0.row)
});
assert!(found_excerpt, "diagnostic not found in reference view");
}
}
assert_eq!(mutated_excerpts, reference_excerpts); assert_eq!(mutated_excerpts, reference_excerpts);
} }

View File

@@ -251,7 +251,6 @@ gpui::actions!(
DisplayCursorNames, DisplayCursorNames,
DuplicateLineDown, DuplicateLineDown,
DuplicateLineUp, DuplicateLineUp,
DuplicateSelection,
ExpandAllHunkDiffs, ExpandAllHunkDiffs,
ExpandMacroRecursively, ExpandMacroRecursively,
FindAllReferences, FindAllReferences,

View File

@@ -1,895 +0,0 @@
use std::{cell::Cell, cmp::Reverse, ops::Range, sync::Arc};
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{
div, px, uniform_list, AnyElement, BackgroundExecutor, Div, FontWeight, ListSizingBehavior,
Model, MouseButton, Pixels, ScrollStrategy, SharedString, StrikethroughStyle, StyledText,
UniformListScrollHandle, ViewContext, WeakView,
};
use language::Buffer;
use language::{CodeLabel, Documentation};
use lsp::LanguageServerId;
use multi_buffer::{Anchor, ExcerptId};
use ordered_float::OrderedFloat;
use parking_lot::RwLock;
use project::{CodeAction, Completion, TaskSourceKind};
use task::ResolvedTask;
use ui::{
h_flex, ActiveTheme as _, Color, FluentBuilder as _, InteractiveElement as _, IntoElement,
Label, LabelCommon as _, LabelSize, ListItem, ParentElement as _, Popover,
StatefulInteractiveElement as _, Styled, StyledExt as _, Toggleable as _,
};
use util::ResultExt as _;
use workspace::Workspace;
use crate::{
actions::{ConfirmCodeAction, ConfirmCompletion},
display_map::DisplayPoint,
render_parsed_markdown, split_words, styled_runs_for_code_label, CodeActionProvider,
CompletionId, CompletionProvider, DisplayRow, Editor, EditorStyle, ResolvedTasks,
};
pub enum CodeContextMenu {
Completions(CompletionsMenu),
CodeActions(CodeActionsMenu),
}
impl CodeContextMenu {
pub fn select_first(
&mut self,
provider: Option<&dyn CompletionProvider>,
cx: &mut ViewContext<Editor>,
) -> bool {
if self.visible() {
match self {
CodeContextMenu::Completions(menu) => menu.select_first(provider, cx),
CodeContextMenu::CodeActions(menu) => menu.select_first(cx),
}
true
} else {
false
}
}
pub fn select_prev(
&mut self,
provider: Option<&dyn CompletionProvider>,
cx: &mut ViewContext<Editor>,
) -> bool {
if self.visible() {
match self {
CodeContextMenu::Completions(menu) => menu.select_prev(provider, cx),
CodeContextMenu::CodeActions(menu) => menu.select_prev(cx),
}
true
} else {
false
}
}
pub fn select_next(
&mut self,
provider: Option<&dyn CompletionProvider>,
cx: &mut ViewContext<Editor>,
) -> bool {
if self.visible() {
match self {
CodeContextMenu::Completions(menu) => menu.select_next(provider, cx),
CodeContextMenu::CodeActions(menu) => menu.select_next(cx),
}
true
} else {
false
}
}
pub fn select_last(
&mut self,
provider: Option<&dyn CompletionProvider>,
cx: &mut ViewContext<Editor>,
) -> bool {
if self.visible() {
match self {
CodeContextMenu::Completions(menu) => menu.select_last(provider, cx),
CodeContextMenu::CodeActions(menu) => menu.select_last(cx),
}
true
} else {
false
}
}
pub fn visible(&self) -> bool {
match self {
CodeContextMenu::Completions(menu) => menu.visible(),
CodeContextMenu::CodeActions(menu) => menu.visible(),
}
}
pub fn render(
&self,
cursor_position: DisplayPoint,
style: &EditorStyle,
max_height: Pixels,
workspace: Option<WeakView<Workspace>>,
cx: &mut ViewContext<Editor>,
) -> (ContextMenuOrigin, AnyElement) {
match self {
CodeContextMenu::Completions(menu) => (
ContextMenuOrigin::EditorPoint(cursor_position),
menu.render(style, max_height, workspace, cx),
),
CodeContextMenu::CodeActions(menu) => {
menu.render(cursor_position, style, max_height, cx)
}
}
}
}
pub enum ContextMenuOrigin {
EditorPoint(DisplayPoint),
GutterIndicator(DisplayRow),
}
#[derive(Clone, Debug)]
pub struct CompletionsMenu {
pub id: CompletionId,
sort_completions: bool,
pub initial_position: Anchor,
pub buffer: Model<Buffer>,
pub completions: Arc<RwLock<Box<[Completion]>>>,
match_candidates: Arc<[StringMatchCandidate]>,
pub matches: Arc<[StringMatch]>,
pub selected_item: usize,
scroll_handle: UniformListScrollHandle,
resolve_completions: bool,
pub aside_was_displayed: Cell<bool>,
show_completion_documentation: bool,
}
impl CompletionsMenu {
pub fn new(
id: CompletionId,
sort_completions: bool,
show_completion_documentation: bool,
initial_position: Anchor,
buffer: Model<Buffer>,
completions: Box<[Completion]>,
aside_was_displayed: bool,
) -> Self {
let match_candidates = completions
.iter()
.enumerate()
.map(|(id, completion)| {
StringMatchCandidate::new(
id,
completion.label.text[completion.label.filter_range.clone()].into(),
)
})
.collect();
Self {
id,
sort_completions,
initial_position,
buffer,
show_completion_documentation,
completions: Arc::new(RwLock::new(completions)),
match_candidates,
matches: Vec::new().into(),
selected_item: 0,
scroll_handle: UniformListScrollHandle::new(),
resolve_completions: true,
aside_was_displayed: Cell::new(aside_was_displayed),
}
}
pub fn new_snippet_choices(
id: CompletionId,
sort_completions: bool,
choices: &Vec<String>,
selection: Range<Anchor>,
buffer: Model<Buffer>,
) -> Self {
let completions = choices
.iter()
.map(|choice| Completion {
old_range: selection.start.text_anchor..selection.end.text_anchor,
new_text: choice.to_string(),
label: CodeLabel {
text: choice.to_string(),
runs: Default::default(),
filter_range: Default::default(),
},
server_id: LanguageServerId(usize::MAX),
documentation: None,
lsp_completion: Default::default(),
confirm: None,
})
.collect();
let match_candidates = choices
.iter()
.enumerate()
.map(|(id, completion)| StringMatchCandidate::new(id, completion.to_string()))
.collect();
let matches = choices
.iter()
.enumerate()
.map(|(id, completion)| StringMatch {
candidate_id: id,
score: 1.,
positions: vec![],
string: completion.clone(),
})
.collect();
Self {
id,
sort_completions,
initial_position: selection.start,
buffer,
completions: Arc::new(RwLock::new(completions)),
match_candidates,
matches,
selected_item: 0,
scroll_handle: UniformListScrollHandle::new(),
resolve_completions: false,
aside_was_displayed: Cell::new(false),
show_completion_documentation: false,
}
}
fn select_first(
&mut self,
provider: Option<&dyn CompletionProvider>,
cx: &mut ViewContext<Editor>,
) {
self.selected_item = 0;
self.scroll_handle
.scroll_to_item(self.selected_item, ScrollStrategy::Top);
self.resolve_selected_completion(provider, cx);
cx.notify();
}
fn select_prev(
&mut self,
provider: Option<&dyn CompletionProvider>,
cx: &mut ViewContext<Editor>,
) {
if self.selected_item > 0 {
self.selected_item -= 1;
} else {
self.selected_item = self.matches.len() - 1;
}
self.scroll_handle
.scroll_to_item(self.selected_item, ScrollStrategy::Top);
self.resolve_selected_completion(provider, cx);
cx.notify();
}
fn select_next(
&mut self,
provider: Option<&dyn CompletionProvider>,
cx: &mut ViewContext<Editor>,
) {
if self.selected_item + 1 < self.matches.len() {
self.selected_item += 1;
} else {
self.selected_item = 0;
}
self.scroll_handle
.scroll_to_item(self.selected_item, ScrollStrategy::Top);
self.resolve_selected_completion(provider, cx);
cx.notify();
}
fn select_last(
&mut self,
provider: Option<&dyn CompletionProvider>,
cx: &mut ViewContext<Editor>,
) {
self.selected_item = self.matches.len() - 1;
self.scroll_handle
.scroll_to_item(self.selected_item, ScrollStrategy::Top);
self.resolve_selected_completion(provider, cx);
cx.notify();
}
pub fn resolve_selected_completion(
&mut self,
provider: Option<&dyn CompletionProvider>,
cx: &mut ViewContext<Editor>,
) {
if !self.resolve_completions {
return;
}
let Some(provider) = provider else {
return;
};
let completion_index = self.matches[self.selected_item].candidate_id;
let resolve_task = provider.resolve_completions(
self.buffer.clone(),
vec![completion_index],
self.completions.clone(),
cx,
);
cx.spawn(move |editor, mut cx| async move {
if let Some(true) = resolve_task.await.log_err() {
editor.update(&mut cx, |_, cx| cx.notify()).ok();
}
})
.detach();
}
fn visible(&self) -> bool {
!self.matches.is_empty()
}
fn render(
&self,
style: &EditorStyle,
max_height: Pixels,
workspace: Option<WeakView<Workspace>>,
cx: &mut ViewContext<Editor>,
) -> AnyElement {
let show_completion_documentation = self.show_completion_documentation;
let widest_completion_ix = self
.matches
.iter()
.enumerate()
.max_by_key(|(_, mat)| {
let completions = self.completions.read();
let completion = &completions[mat.candidate_id];
let documentation = &completion.documentation;
let mut len = completion.label.text.chars().count();
if let Some(Documentation::SingleLine(text)) = documentation {
if show_completion_documentation {
len += text.chars().count();
}
}
len
})
.map(|(ix, _)| ix);
let completions = self.completions.clone();
let matches = self.matches.clone();
let selected_item = self.selected_item;
let style = style.clone();
let multiline_docs = if show_completion_documentation {
let mat = &self.matches[selected_item];
match &self.completions.read()[mat.candidate_id].documentation {
Some(Documentation::MultiLinePlainText(text)) => {
Some(div().child(SharedString::from(text.clone())))
}
Some(Documentation::MultiLineMarkdown(parsed)) if !parsed.text.is_empty() => {
Some(div().child(render_parsed_markdown(
"completions_markdown",
parsed,
&style,
workspace,
cx,
)))
}
Some(Documentation::Undocumented) if self.aside_was_displayed.get() => {
Some(div().child("No documentation"))
}
_ => None,
}
} else {
None
};
let aside_contents = if let Some(multiline_docs) = multiline_docs {
Some(multiline_docs)
} else if self.aside_was_displayed.get() {
Some(div().child("Fetching documentation..."))
} else {
None
};
self.aside_was_displayed.set(aside_contents.is_some());
let aside_contents = aside_contents.map(|div| {
div.id("multiline_docs")
.max_h(max_height)
.flex_1()
.px_1p5()
.py_1()
.min_w(px(260.))
.max_w(px(640.))
.w(px(500.))
.overflow_y_scroll()
.occlude()
});
let list = uniform_list(
cx.view().clone(),
"completions",
matches.len(),
move |_editor, range, cx| {
let start_ix = range.start;
let completions_guard = completions.read();
matches[range]
.iter()
.enumerate()
.map(|(ix, mat)| {
let item_ix = start_ix + ix;
let candidate_id = mat.candidate_id;
let completion = &completions_guard[candidate_id];
let documentation = if show_completion_documentation {
&completion.documentation
} else {
&None
};
let highlights = gpui::combine_highlights(
mat.ranges().map(|range| (range, FontWeight::BOLD.into())),
styled_runs_for_code_label(&completion.label, &style.syntax).map(
|(range, mut highlight)| {
// Ignore font weight for syntax highlighting, as we'll use it
// for fuzzy matches.
highlight.font_weight = None;
if completion.lsp_completion.deprecated.unwrap_or(false) {
highlight.strikethrough = Some(StrikethroughStyle {
thickness: 1.0.into(),
..Default::default()
});
highlight.color = Some(cx.theme().colors().text_muted);
}
(range, highlight)
},
),
);
let completion_label = StyledText::new(completion.label.text.clone())
.with_highlights(&style.text, highlights);
let documentation_label =
if let Some(Documentation::SingleLine(text)) = documentation {
if text.trim().is_empty() {
None
} else {
Some(
Label::new(text.clone())
.ml_4()
.size(LabelSize::Small)
.color(Color::Muted),
)
}
} else {
None
};
let color_swatch = completion
.color()
.map(|color| div().size_4().bg(color).rounded_sm());
div().min_w(px(220.)).max_w(px(540.)).child(
ListItem::new(mat.candidate_id)
.inset(true)
.toggle_state(item_ix == selected_item)
.on_click(cx.listener(move |editor, _event, cx| {
cx.stop_propagation();
if let Some(task) = editor.confirm_completion(
&ConfirmCompletion {
item_ix: Some(item_ix),
},
cx,
) {
task.detach_and_log_err(cx)
}
}))
.start_slot::<Div>(color_swatch)
.child(h_flex().overflow_hidden().child(completion_label))
.end_slot::<Label>(documentation_label),
)
})
.collect()
},
)
.occlude()
.max_h(max_height)
.track_scroll(self.scroll_handle.clone())
.with_width_from_item(widest_completion_ix)
.with_sizing_behavior(ListSizingBehavior::Infer);
Popover::new()
.child(list)
.when_some(aside_contents, |popover, aside_contents| {
popover.aside(aside_contents)
})
.into_any_element()
}
pub async fn filter(&mut self, query: Option<&str>, executor: BackgroundExecutor) {
let mut matches = if let Some(query) = query {
fuzzy::match_strings(
&self.match_candidates,
query,
query.chars().any(|c| c.is_uppercase()),
100,
&Default::default(),
executor,
)
.await
} else {
self.match_candidates
.iter()
.enumerate()
.map(|(candidate_id, candidate)| StringMatch {
candidate_id,
score: Default::default(),
positions: Default::default(),
string: candidate.string.clone(),
})
.collect()
};
// Remove all candidates where the query's start does not match the start of any word in the candidate
if let Some(query) = query {
if let Some(query_start) = query.chars().next() {
matches.retain(|string_match| {
split_words(&string_match.string).any(|word| {
// Check that the first codepoint of the word as lowercase matches the first
// codepoint of the query as lowercase
word.chars()
.flat_map(|codepoint| codepoint.to_lowercase())
.zip(query_start.to_lowercase())
.all(|(word_cp, query_cp)| word_cp == query_cp)
})
});
}
}
let completions = self.completions.read();
if self.sort_completions {
matches.sort_unstable_by_key(|mat| {
// We do want to strike a balance here between what the language server tells us
// to sort by (the sort_text) and what are "obvious" good matches (i.e. when you type
// `Creat` and there is a local variable called `CreateComponent`).
// So what we do is: we bucket all matches into two buckets
// - Strong matches
// - Weak matches
// Strong matches are the ones with a high fuzzy-matcher score (the "obvious" matches)
// and the Weak matches are the rest.
//
// For the strong matches, we sort by our fuzzy-finder score first and for the weak
// matches, we prefer language-server sort_text first.
//
// The thinking behind that: we want to show strong matches first in order of relevance(fuzzy score).
// Rest of the matches(weak) can be sorted as language-server expects.
#[derive(PartialEq, Eq, PartialOrd, Ord)]
enum MatchScore<'a> {
Strong {
score: Reverse<OrderedFloat<f64>>,
sort_text: Option<&'a str>,
sort_key: (usize, &'a str),
},
Weak {
sort_text: Option<&'a str>,
score: Reverse<OrderedFloat<f64>>,
sort_key: (usize, &'a str),
},
}
let completion = &completions[mat.candidate_id];
let sort_key = completion.sort_key();
let sort_text = completion.lsp_completion.sort_text.as_deref();
let score = Reverse(OrderedFloat(mat.score));
if mat.score >= 0.2 {
MatchScore::Strong {
score,
sort_text,
sort_key,
}
} else {
MatchScore::Weak {
sort_text,
score,
sort_key,
}
}
});
}
for mat in &mut matches {
let completion = &completions[mat.candidate_id];
mat.string.clone_from(&completion.label.text);
for position in &mut mat.positions {
*position += completion.label.filter_range.start;
}
}
drop(completions);
self.matches = matches.into();
self.selected_item = 0;
}
}
#[derive(Clone)]
pub struct AvailableCodeAction {
pub excerpt_id: ExcerptId,
pub action: CodeAction,
pub provider: Arc<dyn CodeActionProvider>,
}
#[derive(Clone)]
pub struct CodeActionContents {
pub tasks: Option<Arc<ResolvedTasks>>,
pub actions: Option<Arc<[AvailableCodeAction]>>,
}
impl CodeActionContents {
fn len(&self) -> usize {
match (&self.tasks, &self.actions) {
(Some(tasks), Some(actions)) => actions.len() + tasks.templates.len(),
(Some(tasks), None) => tasks.templates.len(),
(None, Some(actions)) => actions.len(),
(None, None) => 0,
}
}
fn is_empty(&self) -> bool {
match (&self.tasks, &self.actions) {
(Some(tasks), Some(actions)) => actions.is_empty() && tasks.templates.is_empty(),
(Some(tasks), None) => tasks.templates.is_empty(),
(None, Some(actions)) => actions.is_empty(),
(None, None) => true,
}
}
fn iter(&self) -> impl Iterator<Item = CodeActionsItem> + '_ {
self.tasks
.iter()
.flat_map(|tasks| {
tasks
.templates
.iter()
.map(|(kind, task)| CodeActionsItem::Task(kind.clone(), task.clone()))
})
.chain(self.actions.iter().flat_map(|actions| {
actions.iter().map(|available| CodeActionsItem::CodeAction {
excerpt_id: available.excerpt_id,
action: available.action.clone(),
provider: available.provider.clone(),
})
}))
}
pub fn get(&self, index: usize) -> Option<CodeActionsItem> {
match (&self.tasks, &self.actions) {
(Some(tasks), Some(actions)) => {
if index < tasks.templates.len() {
tasks
.templates
.get(index)
.cloned()
.map(|(kind, task)| CodeActionsItem::Task(kind, task))
} else {
actions.get(index - tasks.templates.len()).map(|available| {
CodeActionsItem::CodeAction {
excerpt_id: available.excerpt_id,
action: available.action.clone(),
provider: available.provider.clone(),
}
})
}
}
(Some(tasks), None) => tasks
.templates
.get(index)
.cloned()
.map(|(kind, task)| CodeActionsItem::Task(kind, task)),
(None, Some(actions)) => {
actions
.get(index)
.map(|available| CodeActionsItem::CodeAction {
excerpt_id: available.excerpt_id,
action: available.action.clone(),
provider: available.provider.clone(),
})
}
(None, None) => None,
}
}
}
#[allow(clippy::large_enum_variant)]
#[derive(Clone)]
pub enum CodeActionsItem {
Task(TaskSourceKind, ResolvedTask),
CodeAction {
excerpt_id: ExcerptId,
action: CodeAction,
provider: Arc<dyn CodeActionProvider>,
},
}
impl CodeActionsItem {
fn as_task(&self) -> Option<&ResolvedTask> {
let Self::Task(_, task) = self else {
return None;
};
Some(task)
}
fn as_code_action(&self) -> Option<&CodeAction> {
let Self::CodeAction { action, .. } = self else {
return None;
};
Some(action)
}
pub fn label(&self) -> String {
match self {
Self::CodeAction { action, .. } => action.lsp_action.title.clone(),
Self::Task(_, task) => task.resolved_label.clone(),
}
}
}
pub struct CodeActionsMenu {
pub actions: CodeActionContents,
pub buffer: Model<Buffer>,
pub selected_item: usize,
pub scroll_handle: UniformListScrollHandle,
pub deployed_from_indicator: Option<DisplayRow>,
}
impl CodeActionsMenu {
fn select_first(&mut self, cx: &mut ViewContext<Editor>) {
self.selected_item = 0;
self.scroll_handle
.scroll_to_item(self.selected_item, ScrollStrategy::Top);
cx.notify()
}
fn select_prev(&mut self, cx: &mut ViewContext<Editor>) {
if self.selected_item > 0 {
self.selected_item -= 1;
} else {
self.selected_item = self.actions.len() - 1;
}
self.scroll_handle
.scroll_to_item(self.selected_item, ScrollStrategy::Top);
cx.notify();
}
fn select_next(&mut self, cx: &mut ViewContext<Editor>) {
if self.selected_item + 1 < self.actions.len() {
self.selected_item += 1;
} else {
self.selected_item = 0;
}
self.scroll_handle
.scroll_to_item(self.selected_item, ScrollStrategy::Top);
cx.notify();
}
fn select_last(&mut self, cx: &mut ViewContext<Editor>) {
self.selected_item = self.actions.len() - 1;
self.scroll_handle
.scroll_to_item(self.selected_item, ScrollStrategy::Top);
cx.notify()
}
fn visible(&self) -> bool {
!self.actions.is_empty()
}
fn render(
&self,
cursor_position: DisplayPoint,
_style: &EditorStyle,
max_height: Pixels,
cx: &mut ViewContext<Editor>,
) -> (ContextMenuOrigin, AnyElement) {
let actions = self.actions.clone();
let selected_item = self.selected_item;
let element = uniform_list(
cx.view().clone(),
"code_actions_menu",
self.actions.len(),
move |_this, range, cx| {
actions
.iter()
.skip(range.start)
.take(range.end - range.start)
.enumerate()
.map(|(ix, action)| {
let item_ix = range.start + ix;
let selected = selected_item == item_ix;
let colors = cx.theme().colors();
div()
.px_1()
.rounded_md()
.text_color(colors.text)
.when(selected, |style| {
style
.bg(colors.element_active)
.text_color(colors.text_accent)
})
.hover(|style| {
style
.bg(colors.element_hover)
.text_color(colors.text_accent)
})
.whitespace_nowrap()
.when_some(action.as_code_action(), |this, action| {
this.on_mouse_down(
MouseButton::Left,
cx.listener(move |editor, _, cx| {
cx.stop_propagation();
if let Some(task) = editor.confirm_code_action(
&ConfirmCodeAction {
item_ix: Some(item_ix),
},
cx,
) {
task.detach_and_log_err(cx)
}
}),
)
// TASK: It would be good to make lsp_action.title a SharedString to avoid allocating here.
.child(SharedString::from(
action.lsp_action.title.replace("\n", ""),
))
})
.when_some(action.as_task(), |this, task| {
this.on_mouse_down(
MouseButton::Left,
cx.listener(move |editor, _, cx| {
cx.stop_propagation();
if let Some(task) = editor.confirm_code_action(
&ConfirmCodeAction {
item_ix: Some(item_ix),
},
cx,
) {
task.detach_and_log_err(cx)
}
}),
)
.child(SharedString::from(task.resolved_label.replace("\n", "")))
})
})
.collect()
},
)
.elevation_1(cx)
.p_1()
.max_h(max_height)
.occlude()
.track_scroll(self.scroll_handle.clone())
.with_width_from_item(
self.actions
.iter()
.enumerate()
.max_by_key(|(_, action)| match action {
CodeActionsItem::Task(_, task) => task.resolved_label.chars().count(),
CodeActionsItem::CodeAction { action, .. } => {
action.lsp_action.title.chars().count()
}
})
.map(|(ix, _)| ix),
)
.with_sizing_behavior(ListSizingBehavior::Infer)
.into_any_element();
let cursor_position = if let Some(row) = self.deployed_from_indicator {
ContextMenuOrigin::GutterIndicator(row)
} else {
ContextMenuOrigin::EditorPoint(cursor_position)
};
(cursor_position, element)
}
}

View File

@@ -0,0 +1,46 @@
use std::time::Duration;
use futures::{channel::oneshot, FutureExt};
use gpui::{Task, ViewContext};
use crate::Editor;
#[derive(Debug)]
pub struct DebouncedDelay {
task: Option<Task<()>>,
cancel_channel: Option<oneshot::Sender<()>>,
}
impl DebouncedDelay {
pub fn new() -> DebouncedDelay {
DebouncedDelay {
task: None,
cancel_channel: None,
}
}
pub fn fire_new<F>(&mut self, delay: Duration, cx: &mut ViewContext<Editor>, func: F)
where
F: 'static + Send + FnOnce(&mut Editor, &mut ViewContext<Editor>) -> Task<()>,
{
if let Some(channel) = self.cancel_channel.take() {
_ = channel.send(());
}
let (sender, mut receiver) = oneshot::channel::<()>();
self.cancel_channel = Some(sender);
drop(self.task.take());
self.task = Some(cx.spawn(move |model, mut cx| async move {
let mut timer = cx.background_executor().timer(delay).fuse();
futures::select_biased! {
_ = receiver => return,
_ = timer => {}
}
if let Ok(task) = model.update(&mut cx, |project, cx| (func)(project, cx)) {
task.await;
}
}));
}
}

View File

@@ -535,16 +535,10 @@ pub(crate) struct Highlights<'a> {
pub styles: HighlightStyles, pub styles: HighlightStyles,
} }
#[derive(Clone, Copy, Debug)]
pub struct InlineCompletionStyles {
pub insertion: HighlightStyle,
pub whitespace: HighlightStyle,
}
#[derive(Default, Debug, Clone, Copy)] #[derive(Default, Debug, Clone, Copy)]
pub struct HighlightStyles { pub struct HighlightStyles {
pub inlay_hint: Option<HighlightStyle>, pub inlay_hint: Option<HighlightStyle>,
pub inline_completion: Option<InlineCompletionStyles>, pub suggestion: Option<HighlightStyle>,
} }
#[derive(Clone)] #[derive(Clone)]
@@ -865,7 +859,7 @@ impl DisplaySnapshot {
language_aware, language_aware,
HighlightStyles { HighlightStyles {
inlay_hint: Some(editor_style.inlay_hints_style), inlay_hint: Some(editor_style.inlay_hints_style),
inline_completion: Some(editor_style.inline_completion_styles), suggestion: Some(editor_style.suggestions_style),
}, },
) )
.flat_map(|chunk| { .flat_map(|chunk| {

View File

@@ -62,9 +62,9 @@ impl Inlay {
} }
} }
pub fn inline_completion<T: Into<Rope>>(id: usize, position: Anchor, text: T) -> Self { pub fn suggestion<T: Into<Rope>>(id: usize, position: Anchor, text: T) -> Self {
Self { Self {
id: InlayId::InlineCompletion(id), id: InlayId::Suggestion(id),
position, position,
text: text.into(), text: text.into(),
} }
@@ -346,15 +346,7 @@ impl<'a> Iterator for InlayChunks<'a> {
} }
let mut highlight_style = match inlay.id { let mut highlight_style = match inlay.id {
InlayId::InlineCompletion(_) => { InlayId::Suggestion(_) => self.highlight_styles.suggestion,
self.highlight_styles.inline_completion.map(|s| {
if inlay.text.chars().all(|c| c.is_whitespace()) {
s.whitespace
} else {
s.insertion
}
})
}
InlayId::Hint(_) => self.highlight_styles.inlay_hint, InlayId::Hint(_) => self.highlight_styles.inlay_hint,
}; };
let next_inlay_highlight_endpoint; let next_inlay_highlight_endpoint;
@@ -701,7 +693,7 @@ impl InlayMap {
let inlay_id = if i % 2 == 0 { let inlay_id = if i % 2 == 0 {
InlayId::Hint(post_inc(next_inlay_id)) InlayId::Hint(post_inc(next_inlay_id))
} else { } else {
InlayId::InlineCompletion(post_inc(next_inlay_id)) InlayId::Suggestion(post_inc(next_inlay_id))
}; };
log::info!( log::info!(
"creating inlay {:?} at buffer offset {} with bias {:?} and text {:?}", "creating inlay {:?} at buffer offset {} with bias {:?} and text {:?}",
@@ -1397,7 +1389,7 @@ mod tests {
text: "|123|".into(), text: "|123|".into(),
}, },
Inlay { Inlay {
id: InlayId::InlineCompletion(post_inc(&mut next_inlay_id)), id: InlayId::Suggestion(post_inc(&mut next_inlay_id)),
position: buffer.read(cx).snapshot(cx).anchor_after(3), position: buffer.read(cx).snapshot(cx).anchor_after(3),
text: "|456|".into(), text: "|456|".into(),
}, },
@@ -1613,7 +1605,7 @@ mod tests {
text: "|456|".into(), text: "|456|".into(),
}, },
Inlay { Inlay {
id: InlayId::InlineCompletion(post_inc(&mut next_inlay_id)), id: InlayId::Suggestion(post_inc(&mut next_inlay_id)),
position: buffer.read(cx).snapshot(cx).anchor_before(7), position: buffer.read(cx).snapshot(cx).anchor_before(7),
text: "\n|567|\n".into(), text: "\n|567|\n".into(),
}, },

File diff suppressed because it is too large Load Diff

View File

@@ -9,8 +9,10 @@ pub struct EditorSettings {
pub cursor_blink: bool, pub cursor_blink: bool,
pub cursor_shape: Option<CursorShape>, pub cursor_shape: Option<CursorShape>,
pub current_line_highlight: CurrentLineHighlight, pub current_line_highlight: CurrentLineHighlight,
pub lsp_highlight_debounce: u64,
pub hover_popover_enabled: bool, pub hover_popover_enabled: bool,
pub show_completions_on_input: bool,
pub show_completion_documentation: bool,
pub completion_documentation_secondary_query_debounce: u64,
pub toolbar: Toolbar, pub toolbar: Toolbar,
pub scrollbar: Scrollbar, pub scrollbar: Scrollbar,
pub gutter: Gutter, pub gutter: Gutter,
@@ -186,17 +188,27 @@ pub struct EditorSettingsContent {
/// ///
/// Default: all /// Default: all
pub current_line_highlight: Option<CurrentLineHighlight>, pub current_line_highlight: Option<CurrentLineHighlight>,
/// The debounce delay before querying highlights from the language
/// server based on the current cursor location.
///
/// Default: 75
pub lsp_highlight_debounce: Option<u64>,
/// Whether to show the informational hover box when moving the mouse /// Whether to show the informational hover box when moving the mouse
/// over symbols in the editor. /// over symbols in the editor.
/// ///
/// Default: true /// Default: true
pub hover_popover_enabled: Option<bool>, pub hover_popover_enabled: Option<bool>,
/// Whether to pop the completions menu while typing in an editor without
/// explicitly requesting it.
///
/// Default: true
pub show_completions_on_input: Option<bool>,
/// Whether to display inline and alongside documentation for items in the
/// completions menu.
///
/// Default: true
pub show_completion_documentation: Option<bool>,
/// The debounce delay before re-querying the language server for completion
/// documentation when not included in original completion list.
///
/// Default: 300 ms
pub completion_documentation_secondary_query_debounce: Option<u64>,
/// Toolbar related settings /// Toolbar related settings
pub toolbar: Option<ToolbarContent>, pub toolbar: Option<ToolbarContent>,
/// Scrollbar related settings /// Scrollbar related settings

View File

@@ -265,8 +265,8 @@ impl RenderOnce for BufferFontLigaturesControl {
|selection, cx| { |selection, cx| {
Self::write( Self::write(
match selection { match selection {
ToggleState::Selected => true, Selection::Selected => true,
ToggleState::Unselected | ToggleState::Indeterminate => false, Selection::Unselected | Selection::Indeterminate => false,
}, },
cx, cx,
); );
@@ -318,8 +318,8 @@ impl RenderOnce for InlineGitBlameControl {
|selection, cx| { |selection, cx| {
Self::write( Self::write(
match selection { match selection {
ToggleState::Selected => true, Selection::Selected => true,
ToggleState::Unselected | ToggleState::Indeterminate => false, Selection::Unselected | Selection::Indeterminate => false,
}, },
cx, cx,
); );
@@ -371,8 +371,8 @@ impl RenderOnce for LineNumbersControl {
|selection, cx| { |selection, cx| {
Self::write( Self::write(
match selection { match selection {
ToggleState::Selected => true, Selection::Selected => true,
ToggleState::Unselected | ToggleState::Indeterminate => false, Selection::Unselected | Selection::Indeterminate => false,
}, },
cx, cx,
); );

View File

@@ -9,8 +9,8 @@ use crate::{
}; };
use futures::StreamExt; use futures::StreamExt;
use gpui::{ use gpui::{
div, BackgroundExecutor, SemanticVersion, TestAppContext, UpdateGlobal, VisualTestContext, div, SemanticVersion, TestAppContext, UpdateGlobal, VisualTestContext, WindowBounds,
WindowBounds, WindowOptions, WindowOptions,
}; };
use indoc::indoc; use indoc::indoc;
use language::{ use language::{
@@ -31,9 +31,10 @@ use project::{
project_settings::{LspSettings, ProjectSettings}, project_settings::{LspSettings, ProjectSettings},
}; };
use serde_json::{self, json}; use serde_json::{self, json};
use std::sync::atomic::{self, AtomicBool, AtomicUsize}; use std::sync::atomic::AtomicUsize;
use std::sync::atomic::{self, AtomicBool};
use std::{cell::RefCell, future::Future, rc::Rc, time::Instant}; use std::{cell::RefCell, future::Future, rc::Rc, time::Instant};
use test::{build_editor_with_project, editor_lsp_test_context::rust_lang}; use test::editor_lsp_test_context::rust_lang;
use unindent::Unindent; use unindent::Unindent;
use util::{ use util::{
assert_set_eq, assert_set_eq,
@@ -3891,28 +3892,6 @@ fn test_duplicate_line(cx: &mut TestAppContext) {
] ]
); );
}); });
let view = cx.add_window(|cx| {
let buffer = MultiBuffer::build_simple("abc\ndef\nghi\n", cx);
build_editor(buffer, cx)
});
_ = view.update(cx, |view, cx| {
view.change_selections(None, cx, |s| {
s.select_display_ranges([
DisplayPoint::new(DisplayRow(0), 1)..DisplayPoint::new(DisplayRow(1), 1),
DisplayPoint::new(DisplayRow(1), 2)..DisplayPoint::new(DisplayRow(2), 1),
])
});
view.duplicate_selection(&DuplicateSelection, cx);
assert_eq!(view.display_text(cx), "abc\ndbc\ndef\ngf\nghi\n");
assert_eq!(
view.selections.display_ranges(cx),
vec![
DisplayPoint::new(DisplayRow(0), 1)..DisplayPoint::new(DisplayRow(1), 1),
DisplayPoint::new(DisplayRow(2), 2)..DisplayPoint::new(DisplayRow(3), 1),
]
);
});
} }
#[gpui::test] #[gpui::test]
@@ -6857,15 +6836,14 @@ async fn test_document_format_during_save(cx: &mut gpui::TestAppContext) {
.await .await
.unwrap(); .unwrap();
let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
let (editor, cx) =
cx.add_window_view(|cx| build_editor_with_project(project.clone(), buffer, cx));
editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx));
assert!(cx.read(|cx| editor.is_dirty(cx)));
cx.executor().start_waiting(); cx.executor().start_waiting();
let fake_server = fake_servers.next().await.unwrap(); let fake_server = fake_servers.next().await.unwrap();
let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
let (editor, cx) = cx.add_window_view(|cx| build_editor(buffer, cx));
editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx));
assert!(cx.read(|cx| editor.is_dirty(cx)));
let save = editor let save = editor
.update(cx, |editor, cx| editor.save(true, project.clone(), cx)) .update(cx, |editor, cx| editor.save(true, project.clone(), cx))
.unwrap(); .unwrap();
@@ -7139,7 +7117,6 @@ async fn test_multibuffer_format_during_save(cx: &mut gpui::TestAppContext) {
assert!(!buffer.is_dirty()); assert!(!buffer.is_dirty());
assert_eq!(buffer.text(), sample_text_3,) assert_eq!(buffer.text(), sample_text_3,)
}); });
cx.executor().run_until_parked();
cx.executor().start_waiting(); cx.executor().start_waiting();
let save = multi_buffer_editor let save = multi_buffer_editor
@@ -7211,15 +7188,14 @@ async fn test_range_format_during_save(cx: &mut gpui::TestAppContext) {
.await .await
.unwrap(); .unwrap();
let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
let (editor, cx) =
cx.add_window_view(|cx| build_editor_with_project(project.clone(), buffer, cx));
editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx));
assert!(cx.read(|cx| editor.is_dirty(cx)));
cx.executor().start_waiting(); cx.executor().start_waiting();
let fake_server = fake_servers.next().await.unwrap(); let fake_server = fake_servers.next().await.unwrap();
let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
let (editor, cx) = cx.add_window_view(|cx| build_editor(buffer, cx));
editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx));
assert!(cx.read(|cx| editor.is_dirty(cx)));
let save = editor let save = editor
.update(cx, |editor, cx| editor.save(true, project.clone(), cx)) .update(cx, |editor, cx| editor.save(true, project.clone(), cx))
.unwrap(); .unwrap();
@@ -7363,14 +7339,13 @@ async fn test_document_format_manual_trigger(cx: &mut gpui::TestAppContext) {
.await .await
.unwrap(); .unwrap();
let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
let (editor, cx) =
cx.add_window_view(|cx| build_editor_with_project(project.clone(), buffer, cx));
editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx));
cx.executor().start_waiting(); cx.executor().start_waiting();
let fake_server = fake_servers.next().await.unwrap(); let fake_server = fake_servers.next().await.unwrap();
let buffer = cx.new_model(|cx| MultiBuffer::singleton(buffer, cx));
let (editor, cx) = cx.add_window_view(|cx| build_editor(buffer, cx));
editor.update(cx, |editor, cx| editor.set_text("one\ntwo\nthree\n", cx));
let format = editor let format = editor
.update(cx, |editor, cx| { .update(cx, |editor, cx| {
editor.perform_format( editor.perform_format(
@@ -8401,8 +8376,12 @@ async fn test_completion(cx: &mut gpui::TestAppContext) {
handle_resolve_completion_request(&mut cx, None).await; handle_resolve_completion_request(&mut cx, None).await;
apply_additional_edits.await.unwrap(); apply_additional_edits.await.unwrap();
update_test_language_settings(&mut cx, |settings| { cx.update(|cx| {
settings.defaults.show_completions_on_input = Some(false); cx.update_global::<SettingsStore, _>(|settings, cx| {
settings.update_user_settings::<EditorSettings>(cx, |settings| {
settings.show_completions_on_input = Some(false);
});
})
}); });
cx.set_state("editorˇ"); cx.set_state("editorˇ");
cx.simulate_keystroke("."); cx.simulate_keystroke(".");
@@ -8468,7 +8447,7 @@ async fn test_completion_page_up_down_keys(cx: &mut gpui::TestAppContext) {
cx.executor().run_until_parked(); cx.executor().run_until_parked();
cx.update_editor(|editor, _| { cx.update_editor(|editor, _| {
if let Some(CodeContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() { if let Some(ContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() {
assert_eq!( assert_eq!(
menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(), menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(),
&["first", "last"] &["first", "last"]
@@ -8480,7 +8459,7 @@ async fn test_completion_page_up_down_keys(cx: &mut gpui::TestAppContext) {
cx.update_editor(|editor, cx| { cx.update_editor(|editor, cx| {
editor.move_page_down(&MovePageDown::default(), cx); editor.move_page_down(&MovePageDown::default(), cx);
if let Some(CodeContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() { if let Some(ContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() {
assert!( assert!(
menu.selected_item == 1, menu.selected_item == 1,
"expected PageDown to select the last item from the context menu" "expected PageDown to select the last item from the context menu"
@@ -8492,7 +8471,7 @@ async fn test_completion_page_up_down_keys(cx: &mut gpui::TestAppContext) {
cx.update_editor(|editor, cx| { cx.update_editor(|editor, cx| {
editor.move_page_up(&MovePageUp::default(), cx); editor.move_page_up(&MovePageUp::default(), cx);
if let Some(CodeContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() { if let Some(ContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() {
assert!( assert!(
menu.selected_item == 0, menu.selected_item == 0,
"expected PageUp to select the first item from the context menu" "expected PageUp to select the first item from the context menu"
@@ -8560,7 +8539,7 @@ async fn test_completion_sort(cx: &mut gpui::TestAppContext) {
cx.executor().run_until_parked(); cx.executor().run_until_parked();
cx.update_editor(|editor, _| { cx.update_editor(|editor, _| {
if let Some(CodeContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() { if let Some(ContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() {
assert_eq!( assert_eq!(
menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(), menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(),
&["r", "ret", "Range", "return"] &["r", "ret", "Range", "return"]
@@ -9948,8 +9927,7 @@ async fn go_to_prev_overlapping_diagnostic(
init_test(cx, |_| {}); init_test(cx, |_| {});
let mut cx = EditorTestContext::new(cx).await; let mut cx = EditorTestContext::new(cx).await;
let lsp_store = let project = cx.update_editor(|editor, _| editor.project.clone().unwrap());
cx.update_editor(|editor, cx| editor.project.as_ref().unwrap().read(cx).lsp_store());
cx.set_state(indoc! {" cx.set_state(indoc! {"
ˇfn func(abc def: i32) -> u32 { ˇfn func(abc def: i32) -> u32 {
@@ -9957,8 +9935,8 @@ async fn go_to_prev_overlapping_diagnostic(
"}); "});
cx.update(|cx| { cx.update(|cx| {
lsp_store.update(cx, |lsp_store, cx| { project.update(cx, |project, cx| {
lsp_store project
.update_diagnostics( .update_diagnostics(
LanguageServerId(0), LanguageServerId(0),
lsp::PublishDiagnosticsParams { lsp::PublishDiagnosticsParams {
@@ -10047,12 +10025,11 @@ async fn test_diagnostics_with_links(cx: &mut TestAppContext) {
fn func(abˇc def: i32) -> u32 { fn func(abˇc def: i32) -> u32 {
} }
"}); "});
let lsp_store = let project = cx.update_editor(|editor, _| editor.project.clone().unwrap());
cx.update_editor(|editor, cx| editor.project.as_ref().unwrap().read(cx).lsp_store());
cx.update(|cx| { cx.update(|cx| {
lsp_store.update(cx, |lsp_store, cx| { project.update(cx, |project, cx| {
lsp_store.update_diagnostics( project.update_diagnostics(
LanguageServerId(0), LanguageServerId(0),
lsp::PublishDiagnosticsParams { lsp::PublishDiagnosticsParams {
uri: lsp::Url::from_file_path("/root/file").unwrap(), uri: lsp::Url::from_file_path("/root/file").unwrap(),
@@ -10357,6 +10334,9 @@ async fn test_on_type_formatting_not_triggered(cx: &mut gpui::TestAppContext) {
}) })
.await .await
.unwrap(); .unwrap();
cx.executor().run_until_parked();
cx.executor().start_waiting();
let fake_server = fake_servers.next().await.unwrap();
let editor_handle = workspace let editor_handle = workspace
.update(cx, |workspace, cx| { .update(cx, |workspace, cx| {
workspace.open_path((worktree_id, "main.rs"), None, true, cx) workspace.open_path((worktree_id, "main.rs"), None, true, cx)
@@ -10367,9 +10347,6 @@ async fn test_on_type_formatting_not_triggered(cx: &mut gpui::TestAppContext) {
.downcast::<Editor>() .downcast::<Editor>()
.unwrap(); .unwrap();
cx.executor().start_waiting();
let fake_server = fake_servers.next().await.unwrap();
fake_server.handle_request::<lsp::request::OnTypeFormatting, _, _>(|params, _| async move { fake_server.handle_request::<lsp::request::OnTypeFormatting, _, _>(|params, _| async move {
assert_eq!( assert_eq!(
params.text_document_position.text_document.uri, params.text_document_position.text_document.uri,
@@ -10459,7 +10436,7 @@ async fn test_language_server_restart_due_to_settings_change(cx: &mut gpui::Test
let _window = cx.add_window(|cx| Workspace::test_new(project.clone(), cx)); let _window = cx.add_window(|cx| Workspace::test_new(project.clone(), cx));
let _buffer = project let _buffer = project
.update(cx, |project, cx| { .update(cx, |project, cx| {
project.open_local_buffer_with_lsp("/a/main.rs", cx) project.open_local_buffer("/a/main.rs", cx)
}) })
.await .await
.unwrap(); .unwrap();
@@ -10693,12 +10670,12 @@ async fn test_completions_resolve_updates_labels(cx: &mut gpui::TestAppContext)
.as_ref() .as_ref()
.expect("Should have the context menu deployed"); .expect("Should have the context menu deployed");
match context_menu { match context_menu {
CodeContextMenu::Completions(completions_menu) => { ContextMenu::Completions(completions_menu) => {
let completions = completions_menu.completions.read(); let completions = completions_menu.completions.read();
assert_eq!(completions.len(), 1, "Should have one completion"); assert_eq!(completions.len(), 1, "Should have one completion");
assert_eq!(completions.get(0).unwrap().label.text, "unresolved"); assert_eq!(completions.get(0).unwrap().label.text, "unresolved");
} }
CodeContextMenu::CodeActions(_) => panic!("Should show the completions menu"), ContextMenu::CodeActions(_) => panic!("Should show the completions menu"),
} }
}); });
@@ -10724,7 +10701,7 @@ async fn test_completions_resolve_updates_labels(cx: &mut gpui::TestAppContext)
.as_ref() .as_ref()
.expect("Should have the context menu deployed"); .expect("Should have the context menu deployed");
match context_menu { match context_menu {
CodeContextMenu::Completions(completions_menu) => { ContextMenu::Completions(completions_menu) => {
let completions = completions_menu.completions.read(); let completions = completions_menu.completions.read();
assert_eq!(completions.len(), 1, "Should have one completion"); assert_eq!(completions.len(), 1, "Should have one completion");
assert_eq!( assert_eq!(
@@ -10733,7 +10710,7 @@ async fn test_completions_resolve_updates_labels(cx: &mut gpui::TestAppContext)
"Should update the completion label after resolving" "Should update the completion label after resolving"
); );
} }
CodeContextMenu::CodeActions(_) => panic!("Should show the completions menu"), ContextMenu::CodeActions(_) => panic!("Should show the completions menu"),
} }
}); });
} }
@@ -10912,7 +10889,7 @@ async fn test_completions_default_resolve_data_handling(cx: &mut gpui::TestAppCo
cx.update_editor(|editor, _| { cx.update_editor(|editor, _| {
let menu = editor.context_menu.read(); let menu = editor.context_menu.read();
match menu.as_ref().expect("should have the completions menu") { match menu.as_ref().expect("should have the completions menu") {
CodeContextMenu::Completions(completions_menu) => { ContextMenu::Completions(completions_menu) => {
assert_eq!( assert_eq!(
completions_menu completions_menu
.matches .matches
@@ -10922,7 +10899,7 @@ async fn test_completions_default_resolve_data_handling(cx: &mut gpui::TestAppCo
vec!["Some(2)", "vec![2]"] vec!["Some(2)", "vec![2]"]
); );
} }
CodeContextMenu::CodeActions(_) => panic!("Expected to have the completions menu"), ContextMenu::CodeActions(_) => panic!("Expected to have the completions menu"),
} }
}); });
assert_eq!( assert_eq!(
@@ -11015,7 +10992,7 @@ async fn test_completions_in_languages_with_extra_word_characters(cx: &mut gpui:
cx.simulate_keystroke("-"); cx.simulate_keystroke("-");
cx.executor().run_until_parked(); cx.executor().run_until_parked();
cx.update_editor(|editor, _| { cx.update_editor(|editor, _| {
if let Some(CodeContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() { if let Some(ContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() {
assert_eq!( assert_eq!(
menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(), menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(),
&["bg-red", "bg-blue", "bg-yellow"] &["bg-red", "bg-blue", "bg-yellow"]
@@ -11028,7 +11005,7 @@ async fn test_completions_in_languages_with_extra_word_characters(cx: &mut gpui:
cx.simulate_keystroke("l"); cx.simulate_keystroke("l");
cx.executor().run_until_parked(); cx.executor().run_until_parked();
cx.update_editor(|editor, _| { cx.update_editor(|editor, _| {
if let Some(CodeContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() { if let Some(ContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() {
assert_eq!( assert_eq!(
menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(), menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(),
&["bg-blue", "bg-yellow"] &["bg-blue", "bg-yellow"]
@@ -11044,7 +11021,7 @@ async fn test_completions_in_languages_with_extra_word_characters(cx: &mut gpui:
cx.simulate_keystroke("l"); cx.simulate_keystroke("l");
cx.executor().run_until_parked(); cx.executor().run_until_parked();
cx.update_editor(|editor, _| { cx.update_editor(|editor, _| {
if let Some(CodeContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() { if let Some(ContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() {
assert_eq!( assert_eq!(
menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(), menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(),
&["bg-yellow"] &["bg-yellow"]

View File

@@ -1,6 +1,5 @@
use crate::{ use crate::{
blame_entry_tooltip::{blame_entry_relative_timestamp, BlameEntryTooltip}, blame_entry_tooltip::{blame_entry_relative_timestamp, BlameEntryTooltip},
code_context_menus::CodeActionsMenu,
display_map::{ display_map::{
Block, BlockContext, BlockStyle, DisplaySnapshot, HighlightedChunk, ToDisplayPoint, Block, BlockContext, BlockStyle, DisplaySnapshot, HighlightedChunk, ToDisplayPoint,
}, },
@@ -17,8 +16,8 @@ use crate::{
items::BufferSearchHighlights, items::BufferSearchHighlights,
mouse_context_menu::{self, MenuPosition, MouseContextMenu}, mouse_context_menu::{self, MenuPosition, MouseContextMenu},
scroll::scroll_amount::ScrollAmount, scroll::scroll_amount::ScrollAmount,
BlockId, ChunkReplacement, CursorShape, CustomBlockId, DisplayPoint, DisplayRow, BlockId, ChunkReplacement, CodeActionsMenu, CursorShape, CustomBlockId, DisplayPoint,
DocumentHighlightRead, DocumentHighlightWrite, Editor, EditorMode, EditorSettings, DisplayRow, DocumentHighlightRead, DocumentHighlightWrite, Editor, EditorMode, EditorSettings,
EditorSnapshot, EditorStyle, ExpandExcerpts, FocusedBlock, GutterDimensions, HalfPageDown, EditorSnapshot, EditorStyle, ExpandExcerpts, FocusedBlock, GutterDimensions, HalfPageDown,
HalfPageUp, HandleInput, HoveredCursor, HoveredHunk, InlineCompletion, JumpData, LineDown, HalfPageUp, HandleInput, HoveredCursor, HoveredHunk, InlineCompletion, JumpData, LineDown,
LineUp, OpenExcerpts, PageDown, PageUp, Point, RowExt, RowRangeExt, SelectPhase, Selection, LineUp, OpenExcerpts, PageDown, PageUp, Point, RowExt, RowRangeExt, SelectPhase, Selection,
@@ -50,7 +49,7 @@ use language::{
use lsp::DiagnosticSeverity; use lsp::DiagnosticSeverity;
use multi_buffer::{ use multi_buffer::{
Anchor, AnchorRangeExt, ExcerptId, ExpandExcerptDirection, MultiBufferPoint, MultiBufferRow, Anchor, AnchorRangeExt, ExcerptId, ExpandExcerptDirection, MultiBufferPoint, MultiBufferRow,
MultiBufferSnapshot, ToOffset, MultiBufferSnapshot,
}; };
use project::{ use project::{
project_settings::{GitGutterSetting, ProjectSettings}, project_settings::{GitGutterSetting, ProjectSettings},
@@ -218,7 +217,6 @@ impl EditorElement {
register_action(view, cx, Editor::cut_to_end_of_line); register_action(view, cx, Editor::cut_to_end_of_line);
register_action(view, cx, Editor::duplicate_line_up); register_action(view, cx, Editor::duplicate_line_up);
register_action(view, cx, Editor::duplicate_line_down); register_action(view, cx, Editor::duplicate_line_down);
register_action(view, cx, Editor::duplicate_selection);
register_action(view, cx, Editor::move_line_up); register_action(view, cx, Editor::move_line_up);
register_action(view, cx, Editor::move_line_down); register_action(view, cx, Editor::move_line_down);
register_action(view, cx, Editor::transpose); register_action(view, cx, Editor::transpose);
@@ -1682,7 +1680,7 @@ impl EditorElement {
) -> Vec<AnyElement> { ) -> Vec<AnyElement> {
self.editor.update(cx, |editor, cx| { self.editor.update(cx, |editor, cx| {
let active_task_indicator_row = let active_task_indicator_row =
if let Some(crate::CodeContextMenu::CodeActions(CodeActionsMenu { if let Some(crate::ContextMenu::CodeActions(CodeActionsMenu {
deployed_from_indicator, deployed_from_indicator,
actions, actions,
.. ..
@@ -1697,23 +1695,16 @@ impl EditorElement {
None None
}; };
let offset_range_start = snapshot
.display_point_to_anchor(DisplayPoint::new(range.start, 0), Bias::Left)
.to_offset(&snapshot.buffer_snapshot);
let offset_range_end = snapshot
.display_point_to_anchor(DisplayPoint::new(range.end, 0), Bias::Right)
.to_offset(&snapshot.buffer_snapshot);
editor editor
.tasks .tasks
.iter() .iter()
.filter_map(|(_, tasks)| { .filter_map(|(_, tasks)| {
if tasks.offset.0 < offset_range_start || tasks.offset.0 >= offset_range_end {
return None;
}
let multibuffer_point = tasks.offset.0.to_point(&snapshot.buffer_snapshot); let multibuffer_point = tasks.offset.0.to_point(&snapshot.buffer_snapshot);
let multibuffer_row = MultiBufferRow(multibuffer_point.row); let multibuffer_row = MultiBufferRow(multibuffer_point.row);
let display_row = multibuffer_point.to_display_point(snapshot).row();
if range.start > display_row || range.end < display_row {
return None;
}
if snapshot.is_line_folded(multibuffer_row) { if snapshot.is_line_folded(multibuffer_row) {
// Skip folded indicators, unless it's the starting line of a fold. // Skip folded indicators, unless it's the starting line of a fold.
if multibuffer_row if multibuffer_row
@@ -1726,7 +1717,6 @@ impl EditorElement {
return None; return None;
} }
} }
let display_row = multibuffer_point.to_display_point(snapshot).row();
let button = editor.render_run_indicator( let button = editor.render_run_indicator(
&self.style, &self.style,
Some(display_row) == active_task_indicator_row, Some(display_row) == active_task_indicator_row,
@@ -1765,7 +1755,7 @@ impl EditorElement {
let mut button = None; let mut button = None;
let row = newest_selection_head.row(); let row = newest_selection_head.row();
self.editor.update(cx, |editor, cx| { self.editor.update(cx, |editor, cx| {
if let Some(crate::CodeContextMenu::CodeActions(CodeActionsMenu { if let Some(crate::ContextMenu::CodeActions(CodeActionsMenu {
deployed_from_indicator, deployed_from_indicator,
.. ..
})) = editor.context_menu.read().as_ref() })) = editor.context_menu.read().as_ref()
@@ -2755,34 +2745,22 @@ impl EditorElement {
match &active_inline_completion.completion { match &active_inline_completion.completion {
InlineCompletion::Move(target_position) => { InlineCompletion::Move(target_position) => {
let tab_kbd = h_flex() let container_element = div()
.px_0p5() .bg(cx.theme().colors().editor_background)
.font(theme::ThemeSettings::get_global(cx).buffer_font.clone())
.text_size(TextSize::XSmall.rems(cx))
.text_color(cx.theme().colors().text.opacity(0.8))
.child("tab");
let icon_container = div().mt(px(2.5)); // For optical alignment
let container_element = h_flex()
.items_center()
.py_0p5()
.px_1()
.gap_1()
.bg(cx.theme().colors().editor_subheader_background)
.border_1() .border_1()
.border_color(cx.theme().colors().text_accent.opacity(0.2)) .border_color(cx.theme().colors().border)
.rounded_md() .rounded_md()
.shadow_sm(); .px_1();
let target_display_point = target_position.to_display_point(editor_snapshot); let target_display_point = target_position.to_display_point(editor_snapshot);
if target_display_point.row().as_f32() < scroll_top { if target_display_point.row().as_f32() < scroll_top {
let mut element = container_element let mut element = container_element
.child(tab_kbd)
.child(Label::new("Jump to Edit").size(LabelSize::Small))
.child( .child(
icon_container h_flex()
.child(Icon::new(IconName::ArrowUp).size(IconSize::Small)), .gap_1()
.child(Icon::new(IconName::Tab))
.child(Label::new("Jump to Edit"))
.child(Icon::new(IconName::ArrowUp)),
) )
.into_any(); .into_any();
let size = element.layout_as_root(AvailableSpace::min_size(), cx); let size = element.layout_as_root(AvailableSpace::min_size(), cx);
@@ -2791,11 +2769,12 @@ impl EditorElement {
Some(element) Some(element)
} else if (target_display_point.row().as_f32() + 1.) > scroll_bottom { } else if (target_display_point.row().as_f32() + 1.) > scroll_bottom {
let mut element = container_element let mut element = container_element
.child(tab_kbd)
.child(Label::new("Jump to Edit").size(LabelSize::Small))
.child( .child(
icon_container h_flex()
.child(Icon::new(IconName::ArrowDown).size(IconSize::Small)), .gap_1()
.child(Icon::new(IconName::Tab))
.child(Label::new("Jump to Edit"))
.child(Icon::new(IconName::ArrowDown)),
) )
.into_any(); .into_any();
let size = element.layout_as_root(AvailableSpace::min_size(), cx); let size = element.layout_as_root(AvailableSpace::min_size(), cx);
@@ -2807,8 +2786,12 @@ impl EditorElement {
Some(element) Some(element)
} else { } else {
let mut element = container_element let mut element = container_element
.child(tab_kbd) .child(
.child(Label::new("Jump to Edit").size(LabelSize::Small)) h_flex()
.gap_1()
.child(Icon::new(IconName::Tab))
.child(Label::new("Jump to Edit")),
)
.into_any(); .into_any();
let target_line_end = DisplayPoint::new( let target_line_end = DisplayPoint::new(
@@ -2850,7 +2833,8 @@ impl EditorElement {
return None; return None;
} }
let (text, highlights) = inline_completion_popover_text(editor_snapshot, edits, cx); let (text, highlights) =
inline_completion_popover_text(edit_start, editor_snapshot, edits, cx);
let longest_row = let longest_row =
editor_snapshot.longest_row_in_range(edit_start.row()..edit_end.row() + 1); editor_snapshot.longest_row_in_range(edit_start.row()..edit_end.row() + 1);
@@ -4315,17 +4299,11 @@ impl EditorElement {
} }
fn inline_completion_popover_text( fn inline_completion_popover_text(
edit_start: DisplayPoint,
editor_snapshot: &EditorSnapshot, editor_snapshot: &EditorSnapshot,
edits: &Vec<(Range<Anchor>, String)>, edits: &Vec<(Range<Anchor>, String)>,
cx: &WindowContext, cx: &WindowContext,
) -> (String, Vec<(Range<usize>, HighlightStyle)>) { ) -> (String, Vec<(Range<usize>, HighlightStyle)>) {
let edit_start = edits
.first()
.unwrap()
.0
.start
.to_display_point(editor_snapshot);
let mut text = String::new(); let mut text = String::new();
let mut offset = DisplayPoint::new(edit_start.row(), 0).to_offset(editor_snapshot, Bias::Left); let mut offset = DisplayPoint::new(edit_start.row(), 0).to_offset(editor_snapshot, Bias::Left);
let mut highlights = Vec::new(); let mut highlights = Vec::new();
@@ -4350,22 +4328,6 @@ fn inline_completion_popover_text(
}, },
)); ));
} }
let edit_end = edits
.last()
.unwrap()
.0
.end
.to_display_point(editor_snapshot);
let end_of_line = DisplayPoint::new(edit_end.row(), editor_snapshot.line_len(edit_end.row()))
.to_offset(editor_snapshot, Bias::Right);
text.extend(
editor_snapshot
.buffer_snapshot
.chunks(offset..end_of_line, false)
.map(|chunk| chunk.text),
);
(text, highlights) (text, highlights)
} }
@@ -6669,6 +6631,7 @@ mod tests {
use language::language_settings; use language::language_settings;
use log::info; use log::info;
use std::num::NonZeroU32; use std::num::NonZeroU32;
use ui::Context;
use util::test::sample_text; use util::test::sample_text;
#[gpui::test] #[gpui::test]
@@ -7133,11 +7096,13 @@ mod tests {
let snapshot = editor.snapshot(cx); let snapshot = editor.snapshot(cx);
let edit_range = snapshot.buffer_snapshot.anchor_after(Point::new(0, 6)) let edit_range = snapshot.buffer_snapshot.anchor_after(Point::new(0, 6))
..snapshot.buffer_snapshot.anchor_before(Point::new(0, 6)); ..snapshot.buffer_snapshot.anchor_before(Point::new(0, 6));
let edit_start = DisplayPoint::new(DisplayRow(0), 6);
let edits = vec![(edit_range, " beautiful".to_string())]; let edits = vec![(edit_range, " beautiful".to_string())];
let (text, highlights) = inline_completion_popover_text(&snapshot, &edits, cx); let (text, highlights) =
inline_completion_popover_text(edit_start, &snapshot, &edits, cx);
assert_eq!(text, "Hello, beautiful world!"); assert_eq!(text, "Hello, beautiful");
assert_eq!(highlights.len(), 1); assert_eq!(highlights.len(), 1);
assert_eq!(highlights[0].0, 6..16); assert_eq!(highlights[0].0, 6..16);
assert_eq!( assert_eq!(
@@ -7159,15 +7124,17 @@ mod tests {
window window
.update(cx, |editor, cx| { .update(cx, |editor, cx| {
let snapshot = editor.snapshot(cx); let snapshot = editor.snapshot(cx);
let edit_start = DisplayPoint::new(DisplayRow(0), 0);
let edits = vec![( let edits = vec![(
snapshot.buffer_snapshot.anchor_after(Point::new(0, 0)) snapshot.buffer_snapshot.anchor_after(Point::new(0, 0))
..snapshot.buffer_snapshot.anchor_before(Point::new(0, 4)), ..snapshot.buffer_snapshot.anchor_before(Point::new(0, 4)),
"That".to_string(), "That".to_string(),
)]; )];
let (text, highlights) = inline_completion_popover_text(&snapshot, &edits, cx); let (text, highlights) =
inline_completion_popover_text(edit_start, &snapshot, &edits, cx);
assert_eq!(text, "That is a test."); assert_eq!(text, "That");
assert_eq!(highlights.len(), 1); assert_eq!(highlights.len(), 1);
assert_eq!(highlights[0].0, 0..4); assert_eq!(highlights[0].0, 0..4);
assert_eq!( assert_eq!(
@@ -7189,6 +7156,7 @@ mod tests {
window window
.update(cx, |editor, cx| { .update(cx, |editor, cx| {
let snapshot = editor.snapshot(cx); let snapshot = editor.snapshot(cx);
let edit_start = DisplayPoint::new(DisplayRow(0), 0);
let edits = vec![ let edits = vec![
( (
snapshot.buffer_snapshot.anchor_after(Point::new(0, 0)) snapshot.buffer_snapshot.anchor_after(Point::new(0, 0))
@@ -7197,14 +7165,15 @@ mod tests {
), ),
( (
snapshot.buffer_snapshot.anchor_after(Point::new(0, 12)) snapshot.buffer_snapshot.anchor_after(Point::new(0, 12))
..snapshot.buffer_snapshot.anchor_before(Point::new(0, 12)), ..snapshot.buffer_snapshot.anchor_before(Point::new(0, 13)),
" and universe".into(), " and universe".into(),
), ),
]; ];
let (text, highlights) = inline_completion_popover_text(&snapshot, &edits, cx); let (text, highlights) =
inline_completion_popover_text(edit_start, &snapshot, &edits, cx);
assert_eq!(text, "Greetings, world and universe!"); assert_eq!(text, "Greetings, world and universe");
assert_eq!(highlights.len(), 2); assert_eq!(highlights.len(), 2);
assert_eq!(highlights[0].0, 0..9); assert_eq!(highlights[0].0, 0..9);
assert_eq!(highlights[1].0, 16..29); assert_eq!(highlights[1].0, 16..29);
@@ -7234,6 +7203,7 @@ mod tests {
window window
.update(cx, |editor, cx| { .update(cx, |editor, cx| {
let snapshot = editor.snapshot(cx); let snapshot = editor.snapshot(cx);
let edit_start = DisplayPoint::new(DisplayRow(1), 0);
let edits = vec![ let edits = vec![
( (
snapshot.buffer_snapshot.anchor_before(Point::new(1, 7)) snapshot.buffer_snapshot.anchor_before(Point::new(1, 7))
@@ -7252,9 +7222,10 @@ mod tests {
), ),
]; ];
let (text, highlights) = inline_completion_popover_text(&snapshot, &edits, cx); let (text, highlights) =
inline_completion_popover_text(edit_start, &snapshot, &edits, cx);
assert_eq!(text, "Second modified\nNew third line\nFourth updated line"); assert_eq!(text, "Second modified\nNew third line\nFourth updated");
assert_eq!(highlights.len(), 3); assert_eq!(highlights.len(), 3);
assert_eq!(highlights[0].0, 7..15); // "modified" assert_eq!(highlights[0].0, 7..15); // "modified"
assert_eq!(highlights[1].0, 16..30); // "New third line" assert_eq!(highlights[1].0, 16..30); // "New third line"

View File

@@ -18,11 +18,20 @@ use gpui::{
InteractiveElement, Model, Render, Subscription, Task, View, WeakView, InteractiveElement, Model, Render, Subscription, Task, View, WeakView,
}; };
use language::{Buffer, BufferRow}; use language::{Buffer, BufferRow};
use multi_buffer::{ExcerptId, ExcerptRange, ExpandExcerptDirection, MultiBuffer}; use multi_buffer::{
Anchor, ExcerptId, ExcerptRange, ExpandExcerptDirection, MultiBuffer, MultiBufferPoint,
};
use project::{Project, ProjectEntryId, ProjectPath, WorktreeId}; use project::{Project, ProjectEntryId, ProjectPath, WorktreeId};
use text::{OffsetRangeExt, ToPoint}; use rand::{
distributions::{DistString, Standard},
prelude::*,
};
use text::{Edit, OffsetRangeExt, ToPoint};
use theme::ActiveTheme; use theme::ActiveTheme;
use ui::prelude::*; use ui::{
div, h_flex, Color, Context, FluentBuilder, Icon, IconName, IntoElement, Label, LabelCommon,
ParentElement, SharedString, Styled, ViewContext, VisualContext, WindowContext,
};
use util::{paths::compare_paths, ResultExt}; use util::{paths::compare_paths, ResultExt};
use workspace::{ use workspace::{
item::{BreadcrumbText, Item, ItemEvent, ItemHandle, TabContentParams}, item::{BreadcrumbText, Item, ItemEvent, ItemHandle, TabContentParams},
@@ -240,7 +249,7 @@ impl ProjectDiffEditor {
.map_err(|_| anyhow!("Unexpected non-buffer")) .map_err(|_| anyhow!("Unexpected non-buffer"))
}) })
.with_context(|| { .with_context(|| {
format!("loading {:?} for git diff", entry_path.path) format!("loading {} for git diff", entry_path.path.display())
}) })
.log_err() .log_err()
else { else {
@@ -310,11 +319,11 @@ impl ProjectDiffEditor {
project_diff_editor project_diff_editor
.update(&mut cx, |project_diff_editor, cx| { .update(&mut cx, |project_diff_editor, cx| {
project_diff_editor.update_excerpts(id, new_changes, new_entry_order, cx); project_diff_editor.update_excerpts(id, new_changes, new_entry_order, cx);
project_diff_editor.editor.update(cx, |editor, cx| { for change_set in change_sets {
for change_set in change_sets { project_diff_editor.editor.update(cx, |editor, cx| {
editor.diff_map.add_change_set(change_set, cx) editor.diff_map.add_change_set(change_set, cx)
} });
}); }
}) })
.ok(); .ok();
}), }),
@@ -328,6 +337,7 @@ impl ProjectDiffEditor {
new_entry_order: Vec<(ProjectPath, ProjectEntryId)>, new_entry_order: Vec<(ProjectPath, ProjectEntryId)>,
cx: &mut ViewContext<ProjectDiffEditor>, cx: &mut ViewContext<ProjectDiffEditor>,
) { ) {
println!("update_excerpts.................");
if let Some(current_order) = self.entry_order.get(&worktree_id) { if let Some(current_order) = self.entry_order.get(&worktree_id) {
let current_entries = self.buffer_changes.entry(worktree_id).or_default(); let current_entries = self.buffer_changes.entry(worktree_id).or_default();
let mut new_order_entries = new_entry_order.iter().fuse().peekable(); let mut new_order_entries = new_entry_order.iter().fuse().peekable();
@@ -1096,8 +1106,11 @@ impl Render for ProjectDiffEditor {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use anyhow::anyhow;
use futures::{prelude::*, stream::FuturesUnordered};
use gpui::{SemanticVersion, TestAppContext, VisualTestContext}; use gpui::{SemanticVersion, TestAppContext, VisualTestContext};
use project::buffer_store::BufferChangeSet; use project::buffer_store::BufferChangeSet;
use rand::distributions::Alphanumeric;
use serde_json::json; use serde_json::json;
use settings::SettingsStore; use settings::SettingsStore;
use std::{ use std::{
@@ -1105,127 +1118,275 @@ mod tests {
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use crate::hunks_for_ranges;
use super::*; use super::*;
// TODO finish #[gpui::test(iterations = 100)]
// #[gpui::test] async fn random_edits(cx: &mut TestAppContext, mut rng: StdRng) {
// async fn randomized_tests(cx: &mut TestAppContext) { // TODO switch to RandomCharIter from util or the random_edits thing
// // Create a new project (how?? temp fs?), fn line(rng: &mut StdRng) -> String {
// let fs = FakeFs::new(cx.executor()); let len = rng.gen_range(0..20);
// let project = Project::test(fs, [], cx).await; let mut s = Alphanumeric.sample_string(rng, len);
s.push('\n');
s
}
// // create random files with random content fn original_file(rng: &mut StdRng) -> String {
let line_count = rng.gen_range(0..10);
(0..line_count).map(|_| line(rng)).collect()
}
// // Commit it into git somehow (technically can do with "real" fs in a temp dir) fn edit_file(rng: &mut StdRng, old: &str) -> Vec<(Range<usize>, Range<usize>, String)> {
// // let mut old_lines = old.lines().collect::<Vec<_>>().into_iter();
// // Apply randomized changes to the project: select a random file, random change and apply to buffers let mut edits = Vec::new();
// } let u = rng.gen_range(0..=old_lines.len());
let mut old_offset = old_lines
.by_ref()
.take(u)
.map(|line| line.len() + 1)
.sum::<usize>();
let mut new_offset = old_offset;
while old_lines.len() > 0 {
let d = rng.gen_range(0..=old_lines.len());
let advance = old_lines
.by_ref()
.take(d)
.map(|line| line.len() + 1)
.sum::<usize>();
let d_range = old_offset..old_offset + advance;
old_offset += advance;
let a_min = if d == 0 { 1 } else { 0 };
let a = rng.gen_range(a_min..=5);
let piece = (0..a).map(|_| line(rng)).collect::<String>();
let a_range = new_offset..new_offset + piece.len();
new_offset += piece.len();
edits.push((d_range, a_range, piece));
if old_lines.len() > 0 {
let u = rng.gen_range(1..=old_lines.len());
let advance = old_lines
.by_ref()
.take(u)
.map(|line| line.len() + 1)
.sum::<usize>();
old_offset += advance;
new_offset += advance;
}
}
edits
}
#[gpui::test(iterations = 30)]
async fn simple_edit_test(cx: &mut TestAppContext) {
cx.executor().allow_parking(); cx.executor().allow_parking();
init_test(cx); init_test(cx);
let rng = &mut rng;
let originals = HashMap::from_iter([
("file0", original_file(rng)),
// ("file1", original_file(rng)),
// ("file2", original_file(rng)),
]);
let fs = fs::FakeFs::new(cx.executor().clone()); let fs = fs::FakeFs::new(cx.executor().clone());
fs.insert_tree( let mut files = json!(originals);
"/root", files
json!({ .as_object_mut()
".git": {}, .unwrap()
"file_a": "This is file_a", .insert(".git".to_owned(), json!({}));
"file_b": "This is file_b", fs.insert_tree("/project", files).await;
}), let project = Project::test(fs.clone(), [Path::new("/project")], cx).await;
)
.await;
let project = Project::test(fs.clone(), [Path::new("/root")], cx).await;
let workspace = cx.add_window(|cx| Workspace::test_new(project.clone(), cx)); let workspace = cx.add_window(|cx| Workspace::test_new(project.clone(), cx));
let cx = &mut VisualTestContext::from_window(*workspace.deref(), cx); let cx = &mut VisualTestContext::from_window(*workspace.deref(), cx);
let file_a_editor = workspace let (file_editors, project_diff_editor) = workspace
.update(cx, |workspace, cx| { .update(cx, |workspace, cx| {
let file_a_editor = let file_editors = originals
workspace.open_abs_path(PathBuf::from("/root/file_a"), true, cx); .keys()
.map(|name| {
workspace.open_abs_path(
PathBuf::from(format!("/project/{}", name)),
true,
cx,
)
})
.collect::<Vec<_>>();
ProjectDiffEditor::deploy(workspace, &Deploy, cx); ProjectDiffEditor::deploy(workspace, &Deploy, cx);
file_a_editor let project_diff_editor = workspace
})
.unwrap()
.await
.expect("did not open an item at all")
.downcast::<Editor>()
.expect("did not open an editor for file_a");
let project_diff_editor = workspace
.update(cx, |workspace, cx| {
workspace
.active_pane() .active_pane()
.read(cx) .read(cx)
.items() .items()
.find_map(|item| item.downcast::<ProjectDiffEditor>()) .find_map(|item| item.downcast::<ProjectDiffEditor>())
.expect("Didn't open project diff editor");
(file_editors, project_diff_editor)
}) })
.unwrap() .unwrap();
.expect("did not find a ProjectDiffEditor"); let file_editors = file_editors
.into_iter()
.collect::<FuturesUnordered<_>>()
.map(|result| result?.downcast::<Editor>().ok_or(anyhow!("downcast")))
.try_collect::<Vec<_>>()
.await
.expect("Didn't open file editors");
project_diff_editor.update(cx, |project_diff_editor, cx| { project_diff_editor.update(cx, |project_diff_editor, cx| {
assert!( assert!(
project_diff_editor.editor.read(cx).text(cx).is_empty(), project_diff_editor.editor.read(cx).text(cx).is_empty(),
"Should have no changes after opening the diff on no git changes" "Should have no diff before files are edited"
); );
}); });
let old_text = file_a_editor.update(cx, |editor, cx| editor.text(cx)); let mut all_edits = Vec::new();
let change = "an edit after git add"; for editor in &file_editors {
file_a_editor let (mut old_text, mut edits) = (String::new(), Vec::new());
.update(cx, |file_a_editor, cx| { editor
file_a_editor.insert(change, cx); .update(cx, |editor, cx| {
file_a_editor.save(false, project.clone(), cx) old_text = dbg!(editor.text(cx));
}) edits = dbg!(edit_file(rng, &old_text));
.await editor.edit(
.expect("failed to save a file"); edits
file_a_editor.update(cx, |file_a_editor, cx| { .clone()
let change_set = cx.new_model(|cx| { .into_iter()
BufferChangeSet::new_with_base_text( .map(|(old, _new, content)| (old, content)),
old_text.clone(), cx,
file_a_editor
.buffer()
.read(cx)
.as_singleton()
.unwrap()
.read(cx)
.text_snapshot(),
cx,
)
});
file_a_editor
.diff_map
.add_change_set(change_set.clone(), cx);
project.update(cx, |project, cx| {
project.buffer_store().update(cx, |buffer_store, cx| {
buffer_store.set_change_set(
file_a_editor
.buffer()
.read(cx)
.as_singleton()
.unwrap()
.read(cx)
.remote_id(),
change_set,
); );
}); editor.save(false, project.clone(), cx)
})
.await
.expect("Failed to save edits");
let buffer_id = editor.update(cx, |editor, cx| {
let buffer = editor.buffer().read(cx).as_singleton().unwrap().read(cx);
let snapshot = buffer.text_snapshot();
let id = buffer.remote_id();
let change_set =
cx.new_model(|cx| BufferChangeSet::new_with_base_text(old_text, snapshot, cx));
editor
.diff_map
.add_change_set_with_project(project.clone(), change_set, cx);
id
}); });
}); all_edits.extend(edits.into_iter().map(|(old, new, _)| (buffer_id, old, new)));
}
fs.set_status_for_repo_via_git_operation( fs.set_status_for_repo_via_git_operation(
Path::new("/root/.git"), Path::new("/project/.git"),
&[(Path::new("file_a"), GitFileStatus::Modified)], &originals
.keys()
.map(|name| (Path::new(name), GitFileStatus::Modified))
.collect::<Vec<_>>(),
); );
cx.executor() cx.executor()
.advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100)); .advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(100));
cx.run_until_parked(); cx.run_until_parked();
project_diff_editor.update(cx, |project_diff_editor, cx| { project_diff_editor.update(cx, |project_diff_editor, cx| {
assert_eq!( let mut hunks: Vec<_> = project_diff_editor.editor.update(cx, |editor, cx| {
// TODO assert it better: extract added text (based on the background changes) and deleted text (based on the deleted blocks added) let snapshot = editor.snapshot(cx);
project_diff_editor.editor.read(cx).text(cx), hunks_for_ranges(
format!("{change}{old_text}"), [MultiBufferPoint::zero()..snapshot.buffer_snapshot.max_point()].into_iter(),
"Should have a new change shown in the beginning, and the old text shown as deleted text afterwards" &snapshot,
); )
.into_iter()
.map(|hunk| {
let point = MultiBufferPoint::new(hunk.row_range.start.0, 0);
let buffer_snapshot = snapshot
.buffer_snapshot
.excerpt_containing(point..point)
.unwrap()
.buffer();
(
hunk.buffer_id,
hunk.diff_base_byte_range,
hunk.buffer_range.to_offset(buffer_snapshot),
)
})
.collect()
});
hunks.sort_by_key(|(buffer_id, old, _)| (*buffer_id, old.start));
all_edits.sort_by_key(|(buffer_id, old, _)| (*buffer_id, old.start));
pretty_assertions::assert_eq!(hunks, all_edits);
});
}
#[gpui::test]
async fn repro(cx: &mut TestAppContext) {
let old_text = "r4zU3hQFgVh74o\n";
let edit = (0..15, "");
let new_text = "";
cx.executor().allow_parking();
init_test(cx);
let fs = fs::FakeFs::new(cx.executor().clone());
fs.insert_tree("/project", json!({".git": {}, "file": old_text}))
.await;
let project = Project::test(fs.clone(), [Path::new("/project")], cx).await;
let workspace = cx.add_window(|cx| Workspace::test_new(project.clone(), cx));
let cx = &mut VisualTestContext::from_window(*workspace.deref(), cx);
let (editor, project_diff_editor) = workspace
.update(cx, |workspace, cx| {
let editor = workspace.open_abs_path("/project/file".into(), true, cx);
ProjectDiffEditor::deploy(workspace, &Deploy, cx);
let project_diff_editor = workspace
.active_pane()
.read(cx)
.items()
.find_map(|item| item.downcast::<ProjectDiffEditor>())
.expect("Didn't open project diff editor");
(editor, project_diff_editor)
})
.unwrap();
let editor = editor
.await
.and_then(|item| item.downcast::<Editor>().ok_or(anyhow!("downcast")))
.unwrap();
editor
.update(cx, |editor, cx| {
editor.edit([edit.clone()], cx);
editor.save(false, project.clone(), cx)
})
.await
.expect("failed to save a file");
editor.update(cx, |editor, cx| {
let change_set = cx.new_model(|cx| {
let snapshot = editor
.buffer()
.read(cx)
.as_singleton()
.unwrap()
.read(cx)
.text_snapshot();
assert_eq!(snapshot.text(), new_text);
BufferChangeSet::new_with_base_text(old_text.to_owned(), snapshot, cx)
});
editor
.diff_map
.add_change_set_with_project(project.clone(), change_set, cx);
});
fs.set_status_for_repo_via_git_operation(
Path::new("/project/.git"),
&[(Path::new("file"), GitFileStatus::Modified)],
);
cx.executor()
.advance_clock(UPDATE_DEBOUNCE + Duration::from_millis(1000));
cx.run_until_parked();
project_diff_editor.update(cx, |project_diff_editor, cx| {
let mut hunks: Vec<_> = project_diff_editor.editor.update(cx, |editor, cx| {
let snapshot = editor.snapshot(cx);
hunks_for_ranges(
[MultiBufferPoint::zero()..snapshot.buffer_snapshot.max_point()].into_iter(),
&snapshot,
)
.into_iter()
.map(|hunk| {
let point = MultiBufferPoint::new(hunk.row_range.start.0, 0);
let buffer_snapshot = snapshot
.buffer_snapshot
.excerpt_containing(point..point)
.unwrap()
.buffer();
hunk.diff_base_byte_range
})
.collect()
});
pretty_assertions::assert_eq!(hunks, [edit.0]);
}); });
} }

View File

@@ -694,65 +694,6 @@ pub(crate) fn find_url(
None None
} }
pub(crate) fn find_url_from_range(
buffer: &Model<language::Buffer>,
range: Range<text::Anchor>,
mut cx: AsyncWindowContext,
) -> Option<String> {
const LIMIT: usize = 2048;
let Ok(snapshot) = buffer.update(&mut cx, |buffer, _| buffer.snapshot()) else {
return None;
};
let start_offset = range.start.to_offset(&snapshot);
let end_offset = range.end.to_offset(&snapshot);
let mut token_start = start_offset.min(end_offset);
let mut token_end = start_offset.max(end_offset);
let range_len = token_end - token_start;
if range_len >= LIMIT {
return None;
}
// Skip leading whitespace
for ch in snapshot.chars_at(token_start).take(range_len) {
if !ch.is_whitespace() {
break;
}
token_start += ch.len_utf8();
}
// Skip trailing whitespace
for ch in snapshot.reversed_chars_at(token_end).take(range_len) {
if !ch.is_whitespace() {
break;
}
token_end -= ch.len_utf8();
}
if token_start >= token_end {
return None;
}
let text = snapshot
.text_for_range(token_start..token_end)
.collect::<String>();
let mut finder = LinkFinder::new();
finder.kinds(&[LinkKind::Url]);
if let Some(link) = finder.links(&text).next() {
if link.start() == 0 && link.end() == text.len() {
return Some(link.as_str().to_string());
}
}
None
}
pub(crate) async fn find_file( pub(crate) async fn find_file(
buffer: &Model<language::Buffer>, buffer: &Model<language::Buffer>,
project: Option<Model<Project>>, project: Option<Model<Project>>,

View File

@@ -359,7 +359,6 @@ fn show_hover(
let mut base_text_style = cx.text_style(); let mut base_text_style = cx.text_style();
base_text_style.refine(&TextStyleRefinement { base_text_style.refine(&TextStyleRefinement {
font_family: Some(settings.ui_font.family.clone()), font_family: Some(settings.ui_font.family.clone()),
font_fallbacks: settings.ui_font.fallbacks.clone(),
font_size: Some(settings.ui_font_size.into()), font_size: Some(settings.ui_font_size.into()),
color: Some(cx.theme().colors().editor_foreground), color: Some(cx.theme().colors().editor_foreground),
background_color: Some(gpui::transparent_black()), background_color: Some(gpui::transparent_black()),
@@ -548,14 +547,11 @@ async fn parse_blocks(
.new_view(|cx| { .new_view(|cx| {
let settings = ThemeSettings::get_global(cx); let settings = ThemeSettings::get_global(cx);
let ui_font_family = settings.ui_font.family.clone(); let ui_font_family = settings.ui_font.family.clone();
let ui_font_fallbacks = settings.ui_font.fallbacks.clone();
let buffer_font_family = settings.buffer_font.family.clone(); let buffer_font_family = settings.buffer_font.family.clone();
let buffer_font_fallbacks = settings.buffer_font.fallbacks.clone();
let mut base_text_style = cx.text_style(); let mut base_text_style = cx.text_style();
base_text_style.refine(&TextStyleRefinement { base_text_style.refine(&TextStyleRefinement {
font_family: Some(ui_font_family.clone()), font_family: Some(ui_font_family.clone()),
font_fallbacks: ui_font_fallbacks,
color: Some(cx.theme().colors().editor_foreground), color: Some(cx.theme().colors().editor_foreground),
..Default::default() ..Default::default()
}); });
@@ -566,7 +562,6 @@ async fn parse_blocks(
inline_code: TextStyleRefinement { inline_code: TextStyleRefinement {
background_color: Some(cx.theme().colors().background), background_color: Some(cx.theme().colors().background),
font_family: Some(buffer_font_family), font_family: Some(buffer_font_family),
font_fallbacks: buffer_font_fallbacks,
..Default::default() ..Default::default()
}, },
rule_color: cx.theme().colors().border, rule_color: cx.theme().colors().border,

View File

@@ -9,7 +9,7 @@ use multi_buffer::{
Anchor, AnchorRangeExt, ExcerptRange, MultiBuffer, MultiBufferDiffHunk, MultiBufferRow, Anchor, AnchorRangeExt, ExcerptRange, MultiBuffer, MultiBufferDiffHunk, MultiBufferRow,
MultiBufferSnapshot, ToOffset, ToPoint, MultiBufferSnapshot, ToOffset, ToPoint,
}; };
use project::buffer_store::BufferChangeSet; use project::{buffer_store::BufferChangeSet, Project};
use std::{ops::Range, sync::Arc}; use std::{ops::Range, sync::Arc};
use sum_tree::TreeMap; use sum_tree::TreeMap;
use text::OffsetRangeExt; use text::OffsetRangeExt;
@@ -18,7 +18,7 @@ use ui::{
ParentElement, PopoverMenu, Styled, Tooltip, ViewContext, VisualContext, ParentElement, PopoverMenu, Styled, Tooltip, ViewContext, VisualContext,
}; };
use util::RangeExt; use util::RangeExt;
use workspace::Item; use workspace::{Item, ItemHandle};
use crate::{ use crate::{
editor_settings::CurrentLineHighlight, hunk_status, hunks_for_selections, ApplyAllDiffHunks, editor_settings::CurrentLineHighlight, hunk_status, hunks_for_selections, ApplyAllDiffHunks,
@@ -80,6 +80,40 @@ impl DiffMap {
self.snapshot.clone() self.snapshot.clone()
} }
#[cfg(any(test, feature = "test-support"))]
pub fn add_change_set_with_project(
&mut self,
project: Model<Project>,
change_set: Model<BufferChangeSet>,
cx: &mut ViewContext<Editor>,
) {
let buffer_id = change_set.read(cx).buffer_id;
self.snapshot
.0
.insert(buffer_id, change_set.read(cx).diff_to_buffer.clone());
Editor::sync_expanded_diff_hunks(self, buffer_id, cx);
self.diff_bases.insert(
buffer_id,
DiffBaseState {
last_version: None,
_subscription: cx.observe(&change_set, move |editor, change_set, cx| {
editor
.diff_map
.snapshot
.0
.insert(buffer_id, change_set.read(cx).diff_to_buffer.clone());
Editor::sync_expanded_diff_hunks(&mut editor.diff_map, buffer_id, cx);
}),
change_set: change_set.clone(),
},
);
project.update(cx, |project, cx| {
project.buffer_store().update(cx, |buffer_store, cx| {
buffer_store.set_change_set(buffer_id, change_set);
});
});
}
pub fn add_change_set( pub fn add_change_set(
&mut self, &mut self,
change_set: Model<BufferChangeSet>, change_set: Model<BufferChangeSet>,
@@ -89,6 +123,7 @@ impl DiffMap {
self.snapshot self.snapshot
.0 .0
.insert(buffer_id, change_set.read(cx).diff_to_buffer.clone()); .insert(buffer_id, change_set.read(cx).diff_to_buffer.clone());
Editor::sync_expanded_diff_hunks(self, buffer_id, cx);
self.diff_bases.insert( self.diff_bases.insert(
buffer_id, buffer_id,
DiffBaseState { DiffBaseState {
@@ -104,7 +139,6 @@ impl DiffMap {
change_set, change_set,
}, },
); );
Editor::sync_expanded_diff_hunks(self, buffer_id, cx);
} }
pub fn hunks(&self, include_folded: bool) -> impl Iterator<Item = &ExpandedHunk> { pub fn hunks(&self, include_folded: bool) -> impl Iterator<Item = &ExpandedHunk> {
@@ -137,10 +171,15 @@ impl DiffMapSnapshot {
.filter_map(move |excerpt| { .filter_map(move |excerpt| {
let buffer = excerpt.buffer(); let buffer = excerpt.buffer();
let buffer_id = buffer.remote_id(); let buffer_id = buffer.remote_id();
let diff = self.0.get(&buffer_id)?; let Some(diff) = self.0.get(&buffer_id) else {
eprintln!("boom");
dbg!(&self.0);
return None;
};
let buffer_range = excerpt.map_range_to_buffer(range.clone()); let buffer_range = excerpt.map_range_to_buffer(range.clone());
let buffer_range = let buffer_range =
buffer.anchor_before(buffer_range.start)..buffer.anchor_after(buffer_range.end); buffer.anchor_before(buffer_range.start)..buffer.anchor_after(buffer_range.end);
dbg!("some hunks");
Some( Some(
diff.hunks_intersecting_range(buffer_range, excerpt.buffer()) diff.hunks_intersecting_range(buffer_range, excerpt.buffer())
.map(move |hunk| { .map(move |hunk| {
@@ -726,7 +765,7 @@ impl Editor {
.shape(IconButtonShape::Square) .shape(IconButtonShape::Square)
.icon_size(IconSize::Small) .icon_size(IconSize::Small)
.style(ButtonStyle::Subtle) .style(ButtonStyle::Subtle)
.toggle_state( .selected(
hunk_controls_menu_handle hunk_controls_menu_handle
.is_deployed(), .is_deployed(),
) )

File diff suppressed because it is too large Load Diff

View File

@@ -1,9 +1,10 @@
use gpui::{prelude::*, Model}; use gpui::Model;
use indoc::indoc; use indoc::indoc;
use inline_completion::InlineCompletionProvider; use inline_completion::InlineCompletionProvider;
use multi_buffer::{Anchor, MultiBufferSnapshot, ToPoint}; use multi_buffer::{Anchor, MultiBufferSnapshot, ToPoint};
use std::ops::Range; use std::ops::Range;
use text::{Point, ToOffset}; use text::{Point, ToOffset};
use ui::Context;
use crate::{ use crate::{
editor_tests::init_test, test::editor_test_context::EditorTestContext, InlineCompletion, editor_tests::init_test, test::editor_test_context::EditorTestContext, InlineCompletion,

View File

@@ -37,7 +37,7 @@ where
.find_map(|(trigger_anchor, language, buffer)| { .find_map(|(trigger_anchor, language, buffer)| {
project project
.read(cx) .read(cx)
.language_servers_for_local_buffer(buffer.read(cx), cx) .language_servers_for_buffer(buffer.read(cx), cx)
.find_map(|(adapter, server)| { .find_map(|(adapter, server)| {
if adapter.name.0.as_ref() == language_server_name { if adapter.name.0.as_ref() == language_server_name {
Some(( Some((

View File

@@ -841,12 +841,12 @@ mod tests {
.flat_map(|offset| { .flat_map(|offset| {
[ [
Inlay { Inlay {
id: InlayId::InlineCompletion(post_inc(&mut id)), id: InlayId::Suggestion(post_inc(&mut id)),
position: buffer_snapshot.anchor_at(offset, Bias::Left), position: buffer_snapshot.anchor_at(offset, Bias::Left),
text: "test".into(), text: "test".into(),
}, },
Inlay { Inlay {
id: InlayId::InlineCompletion(post_inc(&mut id)), id: InlayId::Suggestion(post_inc(&mut id)),
position: buffer_snapshot.anchor_at(offset, Bias::Right), position: buffer_snapshot.anchor_at(offset, Bias::Right),
text: "test".into(), text: "test".into(),
}, },

View File

@@ -6,8 +6,8 @@ use collections::BTreeMap;
use futures::Future; use futures::Future;
use git::diff::DiffHunkStatus; use git::diff::DiffHunkStatus;
use gpui::{ use gpui::{
prelude::*, AnyWindowHandle, AppContext, Keystroke, ModelContext, Pixels, Point, View, AnyWindowHandle, AppContext, Keystroke, ModelContext, Pixels, Point, View, ViewContext,
ViewContext, VisualTestContext, WindowHandle, VisualTestContext, WindowHandle,
}; };
use itertools::Itertools; use itertools::Itertools;
use language::{Buffer, BufferSnapshot, LanguageRegistry}; use language::{Buffer, BufferSnapshot, LanguageRegistry};
@@ -23,6 +23,8 @@ use std::{
Arc, Arc,
}, },
}; };
use ui::Context;
use util::{ use util::{
assert_set_eq, assert_set_eq,
test::{generate_marked_text, marked_text_ranges}, test::{generate_marked_text, marked_text_ranges},

View File

@@ -24,8 +24,6 @@ interface github {
} }
/// Returns the latest release for the given GitHub repository. /// Returns the latest release for the given GitHub repository.
///
/// Takes repo as a string in the form "<owner-name>/<repo-name>", for example: "zed-industries/zed".
latest-github-release: func(repo: string, options: github-release-options) -> result<github-release, string>; latest-github-release: func(repo: string, options: github-release-options) -> result<github-release, string>;
/// Returns the GitHub release with the specified tag name for the given GitHub repository. /// Returns the GitHub release with the specified tag name for the given GitHub repository.

Some files were not shown because too many files have changed in this diff Show More