Allow overriding editor settings on a per-language basis

This commit is contained in:
Antonio Scandurra
2021-11-29 09:39:13 +01:00
parent e0998dbfda
commit 93de2bcfed
9 changed files with 88 additions and 30 deletions
+6 -2
View File
@@ -1,6 +1,6 @@
use super::{
DisplayPoint, DisplayRow, Editor, EditorMode, EditorSettings, EditorStyle, Input, Scroll,
Select, SelectPhase, Snapshot, MAX_LINE_LEN,
Select, SelectPhase, Snapshot, SoftWrap, MAX_LINE_LEN,
};
use clock::ReplicaId;
use gpui::{
@@ -703,7 +703,11 @@ impl Element for EditorElement {
let em_width = style.text.em_width(cx.font_cache);
let em_advance = style.text.em_advance(cx.font_cache);
let overscroll = vec2f(em_width, 0.);
let wrap_width = text_width - text_offset.x() - overscroll.x() - em_width;
let wrap_width = match self.settings.soft_wrap {
SoftWrap::None => None,
SoftWrap::EditorWidth => Some(text_width - text_offset.x() - overscroll.x() - em_width),
SoftWrap::Column(column) => Some(column as f32 * em_advance),
};
let snapshot = self.update_view(cx.app, |view, cx| {
if view.set_wrap_width(wrap_width, cx) {
view.snapshot(cx)
+13 -1
View File
@@ -12,7 +12,8 @@ use project::{ProjectPath, Worktree};
use std::fmt::Write;
use std::path::Path;
use workspace::{
EntryOpener, ItemHandle, ItemView, ItemViewHandle, Settings, StatusItemView, WeakItemHandle,
settings, EntryOpener, ItemHandle, ItemView, ItemViewHandle, Settings, StatusItemView,
WeakItemHandle,
};
pub struct BufferOpener;
@@ -47,6 +48,7 @@ impl ItemHandle for BufferItemHandle {
settings: watch::Receiver<Settings>,
cx: &mut MutableAppContext,
) -> Box<dyn ItemViewHandle> {
let buffer = self.0.downgrade();
Box::new(cx.add_view(window_id, |cx| {
Editor::for_buffer(
self.0.clone(),
@@ -71,8 +73,18 @@ impl ItemHandle for BufferItemHandle {
font_properties,
underline: None,
};
let language = buffer.upgrade(cx).and_then(|buf| buf.read(cx).language());
let soft_wrap = match settings.soft_wrap(language) {
settings::SoftWrap::None => crate::SoftWrap::None,
settings::SoftWrap::EditorWidth => crate::SoftWrap::EditorWidth,
settings::SoftWrap::PreferredLineLength => crate::SoftWrap::Column(
settings.preferred_line_length(language).saturating_sub(1),
),
};
EditorSettings {
tab_size: settings.tab_size,
soft_wrap,
style: theme,
}
},
+12 -3
View File
@@ -333,9 +333,17 @@ pub enum EditorMode {
#[derive(Clone)]
pub struct EditorSettings {
pub tab_size: usize,
pub soft_wrap: SoftWrap,
pub style: EditorStyle,
}
#[derive(Clone)]
pub enum SoftWrap {
None,
EditorWidth,
Column(u32),
}
pub struct Editor {
handle: WeakViewHandle<Self>,
buffer: ModelHandle<Buffer>,
@@ -3379,9 +3387,9 @@ impl Editor {
.text()
}
pub fn set_wrap_width(&self, width: f32, cx: &mut MutableAppContext) -> bool {
pub fn set_wrap_width(&self, width: Option<f32>, cx: &mut MutableAppContext) -> bool {
self.display_map
.update(cx, |map, cx| map.set_wrap_width(Some(width), cx))
.update(cx, |map, cx| map.set_wrap_width(width, cx))
}
pub fn set_highlighted_row(&mut self, row: Option<u32>) {
@@ -3539,6 +3547,7 @@ impl EditorSettings {
pub fn test(cx: &AppContext) -> Self {
Self {
tab_size: 4,
soft_wrap: SoftWrap::None,
style: {
let font_cache: &gpui::FontCache = cx.font_cache();
let font_family_name = Arc::from("Monaco");
@@ -4409,7 +4418,7 @@ mod tests {
let (_, view) = cx.add_window(Default::default(), |cx| build_editor(buffer, settings, cx));
view.update(cx, |view, cx| {
view.set_wrap_width(140., cx);
view.set_wrap_width(Some(140.), cx);
assert_eq!(
view.display_text(cx),
"use one::{\n two::three::\n four::five\n};"