Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] main from zed-industries:main #4

Merged
merged 5 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions .github/ISSUE_TEMPLATE/0_feature_request.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# yaml-language-server: $schema=https://json.schemastore.org/github-issue-config.json
blank_issues_enabled: false
contact_links:
- name: Feature Request
url: https://github.com/zed-industries/zed/discussions/new/choose
about: To request a feature, open a new Discussion in one of the appropriate Discussion categories
- name: Zed Discussion Forum
url: https://github.com/zed-industries/zed/discussions
about: A community discussion forum
Expand Down
1 change: 1 addition & 0 deletions assets/keymaps/vim.json
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@
"(": "vim::Parentheses",
")": "vim::Parentheses",
"b": "vim::Parentheses",
// "b": "vim::AnyBrackets",
"[": "vim::SquareBrackets",
"]": "vim::SquareBrackets",
"r": "vim::SquareBrackets",
Expand Down
2 changes: 1 addition & 1 deletion crates/editor/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,4 @@ gpui::actions!(
action_as!(go_to_line, ToggleGoToLine as Toggle);

action_with_deprecated_aliases!(editor, OpenSelectedFilename, ["editor::OpenFile"]);
action_with_deprecated_aliases!(editor, ToggleSelectedDiffHunks, ["editor::ToggleDiffHunk"]);
action_with_deprecated_aliases!(editor, ToggleSelectedDiffHunks, ["editor::ToggleHunkDiff"]);
2 changes: 1 addition & 1 deletion crates/feedback/src/feedback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const fn zed_repo_url() -> &'static str {
}

fn request_feature_url() -> String {
"https://github.com/zed-industries/zed/issues/new?template=0_feature_request.yml".to_string()
"https://github.com/zed-industries/zed/discussions/new/choose".to_string()
}

fn file_bug_report_url(specs: &SystemSpecs) -> String {
Expand Down
9 changes: 5 additions & 4 deletions crates/settings/src/settings_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use fs::Fs;
use futures::{channel::mpsc, StreamExt};
use gpui::{App, BackgroundExecutor, ReadGlobal, UpdateGlobal};
use std::{path::PathBuf, sync::Arc, time::Duration};
use util::ResultExt;

pub const EMPTY_THEME_NAME: &str = "empty-theme";

Expand Down Expand Up @@ -73,9 +72,11 @@ pub fn handle_settings_file_changes(
.block(user_settings_file_rx.next())
.unwrap();
SettingsStore::update_global(cx, |store, cx| {
store
.set_user_settings(&user_settings_content, cx)
.log_err();
let result = store.set_user_settings(&user_settings_content, cx);
if let Err(err) = &result {
log::error!("Failed to load user settings: {err}");
}
settings_changed(result.err(), cx);
});
cx.spawn(move |cx| async move {
while let Some(user_settings_content) = user_settings_file_rx.next().await {
Expand Down
239 changes: 229 additions & 10 deletions crates/vim/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum Object {
AnyQuotes,
DoubleQuotes,
VerticalBars,
AnyBrackets,
Parentheses,
SquareBrackets,
CurlyBrackets,
Expand Down Expand Up @@ -74,6 +75,7 @@ actions!(
DoubleQuotes,
VerticalBars,
Parentheses,
AnyBrackets,
SquareBrackets,
CurlyBrackets,
AngleBrackets,
Expand Down Expand Up @@ -115,6 +117,9 @@ pub fn register(editor: &mut Editor, cx: &mut Context<Vim>) {
Vim::action(editor, cx, |vim, _: &AnyQuotes, window, cx| {
vim.object(Object::AnyQuotes, window, cx)
});
Vim::action(editor, cx, |vim, _: &AnyBrackets, window, cx| {
vim.object(Object::AnyBrackets, window, cx)
});
Vim::action(editor, cx, |vim, _: &DoubleQuotes, window, cx| {
vim.object(Object::DoubleQuotes, window, cx)
});
Expand Down Expand Up @@ -186,6 +191,7 @@ impl Object {
| Object::DoubleQuotes => false,
Object::Sentence
| Object::Paragraph
| Object::AnyBrackets
| Object::Parentheses
| Object::Tag
| Object::AngleBrackets
Expand All @@ -212,6 +218,7 @@ impl Object {
| Object::AnyQuotes
| Object::DoubleQuotes
| Object::VerticalBars
| Object::AnyBrackets
| Object::Parentheses
| Object::SquareBrackets
| Object::Tag
Expand Down Expand Up @@ -239,6 +246,7 @@ impl Object {
}
}
Object::Parentheses
| Object::AnyBrackets
| Object::SquareBrackets
| Object::CurlyBrackets
| Object::AngleBrackets
Expand Down Expand Up @@ -306,16 +314,7 @@ impl Object {
quote,
)
})
.min_by_key(|range| {
// Calculate proximity of ranges to the cursor
let start_distance = (relative_offset
- range.start.to_offset(map, Bias::Left) as isize)
.abs();
let end_distance = (relative_offset
- range.end.to_offset(map, Bias::Right) as isize)
.abs();
start_distance + end_distance
})
.min_by_key(|range| calculate_range_distance(range, relative_offset, map))
}
Object::DoubleQuotes => {
surrounding_markers(map, relative_to, around, self.is_multiline(), '"', '"')
Expand All @@ -331,6 +330,24 @@ impl Object {
let range = selection.range();
surrounding_html_tag(map, head, range, around)
}
Object::AnyBrackets => {
let bracket_pairs = [('(', ')'), ('[', ']'), ('{', '}'), ('<', '>')];
let relative_offset = relative_to.to_offset(map, Bias::Left) as isize;

bracket_pairs
.iter()
.flat_map(|&(open_bracket, close_bracket)| {
surrounding_markers(
map,
relative_to,
around,
self.is_multiline(),
open_bracket,
close_bracket,
)
})
.min_by_key(|range| calculate_range_distance(range, relative_offset, map))
}
Object::SquareBrackets => {
surrounding_markers(map, relative_to, around, self.is_multiline(), '[', ']')
}
Expand Down Expand Up @@ -584,6 +601,37 @@ fn around_word(
}
}

/// Calculate distance between a range and a cursor position
///
/// Returns a score where:
/// - Lower values indicate better matches
/// - Range containing cursor gets priority (returns range length)
/// - For non-containing ranges, uses minimum distance to boundaries as primary factor
/// - Range length is used as secondary factor for tiebreaking
fn calculate_range_distance(
range: &Range<DisplayPoint>,
cursor_offset: isize,
map: &DisplaySnapshot,
) -> isize {
let start_offset = range.start.to_offset(map, Bias::Left) as isize;
let end_offset = range.end.to_offset(map, Bias::Right) as isize;
let range_length = end_offset - start_offset;

// If cursor is inside the range, return range length
if cursor_offset >= start_offset && cursor_offset <= end_offset {
return range_length;
}

// Calculate minimum distance to range boundaries
let start_distance = (cursor_offset - start_offset).abs();
let end_distance = (cursor_offset - end_offset).abs();
let min_distance = start_distance.min(end_distance);

// Use min_distance as primary factor, range_length as secondary
// Multiply by large number to ensure distance is primary factor
min_distance * 10000 + range_length
}

fn around_subword(
map: &DisplaySnapshot,
relative_to: DisplayPoint,
Expand Down Expand Up @@ -1302,9 +1350,11 @@ fn surrounding_markers(

#[cfg(test)]
mod test {
use gpui::KeyBinding;
use indoc::indoc;

use crate::{
object::AnyBrackets,
state::Mode,
test::{NeovimBackedTestContext, VimTestContext},
};
Expand Down Expand Up @@ -1914,6 +1964,30 @@ mod test {

const TEST_CASES: &[(&str, &str, &str, Mode)] = &[
// Single quotes
(
"c i q",
"Thisˇ is a 'quote' example.",
"This is a 'ˇ' example.",
Mode::Insert,
),
(
"c a q",
"Thisˇ is a 'quote' example.",
"This is a ˇexample.",
Mode::Insert,
),
(
"c i q",
"This is a \"simple 'qˇuote'\" example.",
"This is a \"simple 'ˇ'\" example.",
Mode::Insert,
),
(
"c a q",
"This is a \"simple 'qˇuote'\" example.",
"This is a \"simpleˇ\" example.",
Mode::Insert,
),
(
"c i q",
"This is a 'qˇuote' example.",
Expand Down Expand Up @@ -2022,6 +2096,151 @@ mod test {
}
}

#[gpui::test]
async fn test_anybrackets_object(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new(cx, true).await;
cx.update(|_, cx| {
cx.bind_keys([KeyBinding::new(
"b",
AnyBrackets,
Some("vim_operator == a || vim_operator == i || vim_operator == cs"),
)]);
});

const TEST_CASES: &[(&str, &str, &str, Mode)] = &[
// Bracket (Parentheses)
(
"c i b",
"Thisˇ is a (simple [quote]) example.",
"This is a (ˇ) example.",
Mode::Insert,
),
(
"c i b",
"This is a [simple (qˇuote)] example.",
"This is a [simple (ˇ)] example.",
Mode::Insert,
),
(
"c a b",
"This is a [simple (qˇuote)] example.",
"This is a [simple ˇ] example.",
Mode::Insert,
),
(
"c a b",
"Thisˇ is a (simple [quote]) example.",
"This is a ˇ example.",
Mode::Insert,
),
(
"c i b",
"This is a (qˇuote) example.",
"This is a (ˇ) example.",
Mode::Insert,
),
(
"c a b",
"This is a (qˇuote) example.",
"This is a ˇ example.",
Mode::Insert,
),
(
"d i b",
"This is a (qˇuote) example.",
"This is a (ˇ) example.",
Mode::Normal,
),
(
"d a b",
"This is a (qˇuote) example.",
"This is a ˇ example.",
Mode::Normal,
),
// Square brackets
(
"c i b",
"This is a [qˇuote] example.",
"This is a [ˇ] example.",
Mode::Insert,
),
(
"c a b",
"This is a [qˇuote] example.",
"This is a ˇ example.",
Mode::Insert,
),
(
"d i b",
"This is a [qˇuote] example.",
"This is a [ˇ] example.",
Mode::Normal,
),
(
"d a b",
"This is a [qˇuote] example.",
"This is a ˇ example.",
Mode::Normal,
),
// Curly brackets
(
"c i b",
"This is a {qˇuote} example.",
"This is a {ˇ} example.",
Mode::Insert,
),
(
"c a b",
"This is a {qˇuote} example.",
"This is a ˇ example.",
Mode::Insert,
),
(
"d i b",
"This is a {qˇuote} example.",
"This is a {ˇ} example.",
Mode::Normal,
),
(
"d a b",
"This is a {qˇuote} example.",
"This is a ˇ example.",
Mode::Normal,
),
];

for (keystrokes, initial_state, expected_state, expected_mode) in TEST_CASES {
cx.set_state(initial_state, Mode::Normal);

cx.simulate_keystrokes(keystrokes);

cx.assert_state(expected_state, *expected_mode);
}

const INVALID_CASES: &[(&str, &str, Mode)] = &[
("c i b", "this is a (qˇuote example.", Mode::Normal), // Missing closing bracket
("c a b", "this is a (qˇuote example.", Mode::Normal), // Missing closing bracket
("d i b", "this is a (qˇuote example.", Mode::Normal), // Missing closing bracket
("d a b", "this is a (qˇuote example.", Mode::Normal), // Missing closing bracket
("c i b", "this is a [qˇuote example.", Mode::Normal), // Missing closing square bracket
("c a b", "this is a [qˇuote example.", Mode::Normal), // Missing closing square bracket
("d i b", "this is a [qˇuote example.", Mode::Normal), // Missing closing square bracket
("d a b", "this is a [qˇuote example.", Mode::Normal), // Missing closing square bracket
("c i b", "this is a {qˇuote example.", Mode::Normal), // Missing closing curly bracket
("c a b", "this is a {qˇuote example.", Mode::Normal), // Missing closing curly bracket
("d i b", "this is a {qˇuote example.", Mode::Normal), // Missing closing curly bracket
("d a b", "this is a {qˇuote example.", Mode::Normal), // Missing closing curly bracket
];

for (keystrokes, initial_state, mode) in INVALID_CASES {
cx.set_state(initial_state, Mode::Normal);

cx.simulate_keystrokes(keystrokes);

cx.assert_state(initial_state, *mode);
}
}

#[gpui::test]
async fn test_tags(cx: &mut gpui::TestAppContext) {
let mut cx = VimTestContext::new_html(cx).await;
Expand Down
Loading