mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-08 07:35:01 +03:00
Add wrap guides (#2767)
fixes https://github.com/zed-industries/community/issues/48 Release notes - Added wrap guides and two associated language settings: `"show_wrap_guides": bool` and `"wrap_guides": [..]`. The first controls whether wrap guides are shown when `"soft_wrap": "preferred_line_length"` is enabled and the second allows Zed to show additional wrap guides at whichever column index you prefer. Here's a screenshot of Zed with wrap guides at 60 and 90, and soft wrap active with a preferred_line_length of 80: <img width="956" alt="Screenshot 2023-07-20 at 4 42 11 PM" src="https://github.com/zed-industries/zed/assets/2280405/48f36be1-3bdc-48eb-bfca-e61fcfd6dbc2">
This commit is contained in:
commit
76188c9508
@ -50,6 +50,13 @@
|
|||||||
// 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 show wrap guides in the editor. Setting this to true will
|
||||||
|
// show a guide at the 'preferred_line_length' value if softwrap is set to
|
||||||
|
// 'preferred_line_length', and will show any additional guides as specified
|
||||||
|
// by the 'wrap_guides' setting.
|
||||||
|
"show_wrap_guides": true,
|
||||||
|
// Character counts at which to show wrap guides in the editor.
|
||||||
|
"wrap_guides": [],
|
||||||
// Whether to use additional LSP queries to format (and amend) the code after
|
// Whether to use additional LSP queries to format (and amend) the code after
|
||||||
// every "trigger" symbol input, defined by LSP server capabilities.
|
// every "trigger" symbol input, defined by LSP server capabilities.
|
||||||
"use_on_type_format": true,
|
"use_on_type_format": true,
|
||||||
@ -356,12 +363,6 @@
|
|||||||
// LSP Specific settings.
|
// LSP Specific settings.
|
||||||
"lsp": {
|
"lsp": {
|
||||||
// Specify the LSP name as a key here.
|
// Specify the LSP name as a key here.
|
||||||
// As of 8/10/22, supported LSPs are:
|
|
||||||
// pyright
|
|
||||||
// gopls
|
|
||||||
// rust-analyzer
|
|
||||||
// typescript-language-server
|
|
||||||
// vscode-json-languageserver
|
|
||||||
// "rust-analyzer": {
|
// "rust-analyzer": {
|
||||||
// //These initialization options are merged into Zed's defaults
|
// //These initialization options are merged into Zed's defaults
|
||||||
// "initialization_options": {
|
// "initialization_options": {
|
||||||
|
@ -7086,6 +7086,20 @@ impl Editor {
|
|||||||
.text()
|
.text()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn wrap_guides(&self, cx: &AppContext) -> SmallVec<[(usize, bool); 2]> {
|
||||||
|
let mut wrap_guides = smallvec::smallvec![];
|
||||||
|
|
||||||
|
let settings = self.buffer.read(cx).settings_at(0, cx);
|
||||||
|
if settings.show_wrap_guides {
|
||||||
|
if let SoftWrap::Column(soft_wrap) = self.soft_wrap_mode(cx) {
|
||||||
|
wrap_guides.push((soft_wrap as usize, true));
|
||||||
|
}
|
||||||
|
wrap_guides.extend(settings.wrap_guides.iter().map(|guide| (*guide, false)))
|
||||||
|
}
|
||||||
|
|
||||||
|
wrap_guides
|
||||||
|
}
|
||||||
|
|
||||||
pub fn soft_wrap_mode(&self, cx: &AppContext) -> SoftWrap {
|
pub fn soft_wrap_mode(&self, cx: &AppContext) -> SoftWrap {
|
||||||
let settings = self.buffer.read(cx).settings_at(0, cx);
|
let settings = self.buffer.read(cx).settings_at(0, cx);
|
||||||
let mode = self
|
let mode = self
|
||||||
|
@ -541,6 +541,24 @@ impl EditorElement {
|
|||||||
corner_radius: 0.,
|
corner_radius: 0.,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (wrap_position, active) in layout.wrap_guides.iter() {
|
||||||
|
let x = text_bounds.origin_x() + wrap_position + layout.position_map.em_width / 2.;
|
||||||
|
let color = if *active {
|
||||||
|
self.style.active_wrap_guide
|
||||||
|
} else {
|
||||||
|
self.style.wrap_guide
|
||||||
|
};
|
||||||
|
scene.push_quad(Quad {
|
||||||
|
bounds: RectF::new(
|
||||||
|
vec2f(x, text_bounds.origin_y()),
|
||||||
|
vec2f(1., text_bounds.height()),
|
||||||
|
),
|
||||||
|
background: Some(color),
|
||||||
|
border: Border::new(0., Color::transparent_black()),
|
||||||
|
corner_radius: 0.,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1320,16 +1338,15 @@ impl EditorElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn max_line_number_width(&self, snapshot: &EditorSnapshot, cx: &ViewContext<Editor>) -> f32 {
|
fn column_pixels(&self, column: usize, cx: &ViewContext<Editor>) -> f32 {
|
||||||
let digit_count = (snapshot.max_buffer_row() as f32 + 1.).log10().floor() as usize + 1;
|
|
||||||
let style = &self.style;
|
let style = &self.style;
|
||||||
|
|
||||||
cx.text_layout_cache()
|
cx.text_layout_cache()
|
||||||
.layout_str(
|
.layout_str(
|
||||||
"1".repeat(digit_count).as_str(),
|
" ".repeat(column).as_str(),
|
||||||
style.text.font_size,
|
style.text.font_size,
|
||||||
&[(
|
&[(
|
||||||
digit_count,
|
column,
|
||||||
RunStyle {
|
RunStyle {
|
||||||
font_id: style.text.font_id,
|
font_id: style.text.font_id,
|
||||||
color: Color::black(),
|
color: Color::black(),
|
||||||
@ -1340,6 +1357,11 @@ impl EditorElement {
|
|||||||
.width()
|
.width()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn max_line_number_width(&self, snapshot: &EditorSnapshot, cx: &ViewContext<Editor>) -> f32 {
|
||||||
|
let digit_count = (snapshot.max_buffer_row() as f32 + 1.).log10().floor() as usize + 1;
|
||||||
|
self.column_pixels(digit_count, cx)
|
||||||
|
}
|
||||||
|
|
||||||
//Folds contained in a hunk are ignored apart from shrinking visual size
|
//Folds contained in a hunk are ignored apart from shrinking visual size
|
||||||
//If a fold contains any hunks then that fold line is marked as modified
|
//If a fold contains any hunks then that fold line is marked as modified
|
||||||
fn layout_git_gutters(
|
fn layout_git_gutters(
|
||||||
@ -2025,6 +2047,12 @@ impl Element<Editor> for EditorElement {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let wrap_guides = editor
|
||||||
|
.wrap_guides(cx)
|
||||||
|
.iter()
|
||||||
|
.map(|(guide, active)| (self.column_pixels(*guide, cx), *active))
|
||||||
|
.collect();
|
||||||
|
|
||||||
let scroll_height = (snapshot.max_point().row() + 1) as f32 * line_height;
|
let scroll_height = (snapshot.max_point().row() + 1) as f32 * line_height;
|
||||||
if let EditorMode::AutoHeight { max_lines } = snapshot.mode {
|
if let EditorMode::AutoHeight { max_lines } = snapshot.mode {
|
||||||
size.set_y(
|
size.set_y(
|
||||||
@ -2385,6 +2413,7 @@ impl Element<Editor> for EditorElement {
|
|||||||
snapshot,
|
snapshot,
|
||||||
}),
|
}),
|
||||||
visible_display_row_range: start_row..end_row,
|
visible_display_row_range: start_row..end_row,
|
||||||
|
wrap_guides,
|
||||||
gutter_size,
|
gutter_size,
|
||||||
gutter_padding,
|
gutter_padding,
|
||||||
text_size,
|
text_size,
|
||||||
@ -2535,6 +2564,7 @@ pub struct LayoutState {
|
|||||||
gutter_margin: f32,
|
gutter_margin: f32,
|
||||||
text_size: Vector2F,
|
text_size: Vector2F,
|
||||||
mode: EditorMode,
|
mode: EditorMode,
|
||||||
|
wrap_guides: SmallVec<[(f32, bool); 2]>,
|
||||||
visible_display_row_range: Range<u32>,
|
visible_display_row_range: Range<u32>,
|
||||||
active_rows: BTreeMap<u32, bool>,
|
active_rows: BTreeMap<u32, bool>,
|
||||||
highlighted_rows: Option<Range<u32>>,
|
highlighted_rows: Option<Range<u32>>,
|
||||||
|
@ -44,6 +44,8 @@ pub struct LanguageSettings {
|
|||||||
pub hard_tabs: bool,
|
pub hard_tabs: bool,
|
||||||
pub soft_wrap: SoftWrap,
|
pub soft_wrap: SoftWrap,
|
||||||
pub preferred_line_length: u32,
|
pub preferred_line_length: u32,
|
||||||
|
pub show_wrap_guides: bool,
|
||||||
|
pub wrap_guides: Vec<usize>,
|
||||||
pub format_on_save: FormatOnSave,
|
pub format_on_save: FormatOnSave,
|
||||||
pub remove_trailing_whitespace_on_save: bool,
|
pub remove_trailing_whitespace_on_save: bool,
|
||||||
pub ensure_final_newline_on_save: bool,
|
pub ensure_final_newline_on_save: bool,
|
||||||
@ -84,6 +86,10 @@ pub struct LanguageSettingsContent {
|
|||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub preferred_line_length: Option<u32>,
|
pub preferred_line_length: Option<u32>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
pub show_wrap_guides: Option<bool>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub wrap_guides: Option<Vec<usize>>,
|
||||||
|
#[serde(default)]
|
||||||
pub format_on_save: Option<FormatOnSave>,
|
pub format_on_save: Option<FormatOnSave>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub remove_trailing_whitespace_on_save: Option<bool>,
|
pub remove_trailing_whitespace_on_save: Option<bool>,
|
||||||
@ -378,6 +384,9 @@ fn merge_settings(settings: &mut LanguageSettings, src: &LanguageSettingsContent
|
|||||||
merge(&mut settings.tab_size, src.tab_size);
|
merge(&mut settings.tab_size, src.tab_size);
|
||||||
merge(&mut settings.hard_tabs, src.hard_tabs);
|
merge(&mut settings.hard_tabs, src.hard_tabs);
|
||||||
merge(&mut settings.soft_wrap, src.soft_wrap);
|
merge(&mut settings.soft_wrap, src.soft_wrap);
|
||||||
|
merge(&mut settings.show_wrap_guides, src.show_wrap_guides);
|
||||||
|
merge(&mut settings.wrap_guides, src.wrap_guides.clone());
|
||||||
|
|
||||||
merge(
|
merge(
|
||||||
&mut settings.preferred_line_length,
|
&mut settings.preferred_line_length,
|
||||||
src.preferred_line_length,
|
src.preferred_line_length,
|
||||||
|
@ -691,6 +691,8 @@ pub struct Editor {
|
|||||||
pub document_highlight_read_background: Color,
|
pub document_highlight_read_background: Color,
|
||||||
pub document_highlight_write_background: Color,
|
pub document_highlight_write_background: Color,
|
||||||
pub diff: DiffStyle,
|
pub diff: DiffStyle,
|
||||||
|
pub wrap_guide: Color,
|
||||||
|
pub active_wrap_guide: Color,
|
||||||
pub line_number: Color,
|
pub line_number: Color,
|
||||||
pub line_number_active: Color,
|
pub line_number_active: Color,
|
||||||
pub guest_selections: Vec<SelectionStyle>,
|
pub guest_selections: Vec<SelectionStyle>,
|
||||||
|
@ -170,6 +170,8 @@ export default function editor(): any {
|
|||||||
line_number: with_opacity(foreground(layer), 0.35),
|
line_number: with_opacity(foreground(layer), 0.35),
|
||||||
line_number_active: foreground(layer),
|
line_number_active: foreground(layer),
|
||||||
rename_fade: 0.6,
|
rename_fade: 0.6,
|
||||||
|
wrap_guide: with_opacity(foreground(layer), 0.1),
|
||||||
|
active_wrap_guide: with_opacity(foreground(layer), 0.2),
|
||||||
unnecessary_code_fade: 0.5,
|
unnecessary_code_fade: 0.5,
|
||||||
selection: theme.players[0],
|
selection: theme.players[0],
|
||||||
whitespace: theme.ramps.neutral(0.5).hex(),
|
whitespace: theme.ramps.neutral(0.5).hex(),
|
||||||
|
Loading…
Reference in New Issue
Block a user