Start work on respecting project-specific settings

This commit is contained in:
Max Brunsfeld 2023-05-29 13:48:27 -07:00
parent e4530471de
commit 89446c7fd4
16 changed files with 326 additions and 83 deletions

View file

@ -216,6 +216,11 @@ pub trait File: Send + Sync {
/// of its worktree, then this method will return the name of the worktree itself.
fn file_name<'a>(&'a self, cx: &'a AppContext) -> &'a OsStr;
/// Returns the id of the worktree to which this file belongs.
///
/// This is needed for looking up project-specific settings.
fn worktree_id(&self) -> usize;
fn is_deleted(&self) -> bool;
fn as_any(&self) -> &dyn Any;
@ -1803,7 +1808,11 @@ impl BufferSnapshot {
pub fn language_indent_size_at<T: ToOffset>(&self, position: T, cx: &AppContext) -> IndentSize {
let language_name = self.language_at(position).map(|language| language.name());
let settings = language_settings(language_name.as_deref(), cx);
let settings = language_settings(
language_name.as_deref(),
self.file().map(|f| f.as_ref()),
cx,
);
if settings.hard_tabs {
IndentSize::tab()
} else {
@ -2128,7 +2137,11 @@ impl BufferSnapshot {
cx: &'a AppContext,
) -> &'a LanguageSettings {
let language = self.language_at(position);
language_settings(language.map(|l| l.name()).as_deref(), cx)
language_settings(
language.map(|l| l.name()).as_deref(),
self.file.as_ref().map(AsRef::as_ref),
cx,
)
}
pub fn language_scope_at<D: ToOffset>(&self, position: D) -> Option<LanguageScope> {

View file

@ -1,3 +1,4 @@
use crate::File;
use anyhow::Result;
use collections::HashMap;
use globset::GlobMatcher;
@ -13,8 +14,16 @@ pub fn init(cx: &mut AppContext) {
settings::register::<AllLanguageSettings>(cx);
}
pub fn language_settings<'a>(language: Option<&str>, cx: &'a AppContext) -> &'a LanguageSettings {
settings::get::<AllLanguageSettings>(cx).language(language)
pub fn language_settings<'a>(
language: Option<&str>,
file: Option<&dyn File>,
cx: &'a AppContext,
) -> &'a LanguageSettings {
settings::get_local::<AllLanguageSettings>(
file.map(|f| (f.worktree_id(), f.path().as_ref())),
cx,
)
.language(language)
}
pub fn all_language_settings<'a>(cx: &'a AppContext) -> &'a AllLanguageSettings {