use super::*;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct IndentStyle {
pub width: u32,
pub use_spaces: bool,
}
pub fn detect_indentation(lines: &[String]) -> Option<IndentStyle> {
let mut tab_lines = 0usize;
let mut space_lines = 0usize;
let mut steps: [usize; 9] = [0; 9];
let mut prev_depth = 0usize;
let mut min_run = usize::MAX;
for line in lines.iter().take(10_000) {
if line.starts_with('\t') {
if line.chars().any(|c| !c.is_whitespace()) {
tab_lines += 1;
}
continue;
}
let depth = line.chars().take_while(|&c| c == ' ').count();
if depth == 0 {
if !line.is_empty() {
prev_depth = 0;
}
continue;
}
if line.chars().nth(depth).is_none() {
continue;
}
space_lines += 1;
min_run = min_run.min(depth);
let step = depth.abs_diff(prev_depth);
if (2..=8).contains(&step) {
steps[step] += 1;
}
prev_depth = depth;
}
if tab_lines == 0 && space_lines == 0 {
return None;
}
if tab_lines >= space_lines {
return Some(IndentStyle {
width: 4,
use_spaces: false,
});
}
let width = steps
.iter()
.enumerate()
.skip(2)
.max_by(|a, b| a.1.cmp(b.1).then(b.0.cmp(&a.0)))
.filter(|&(_, &n)| n > 0)
.map(|(w, _)| w)
.or(((2..=8).contains(&min_run)).then_some(min_run))?;
Some(IndentStyle {
width: width as u32,
use_spaces: true,
})
}
impl IndentStyle {
pub fn unit(self) -> String {
if self.use_spaces {
" ".repeat(self.width as usize)
} else {
"\t".to_string()
}
}
pub fn label(self) -> String {
let kind = if self.use_spaces {
"Spaces"
} else {
"Tab Size"
};
format!("{kind}: {}", self.width)
}
}
impl Editor {
pub fn indent_style(&self) -> IndentStyle {
if let Some(pinned) = self.indent_override {
return pinned;
}
let base = self.detected_indent.unwrap_or(IndentStyle {
width: indent_unit_for(self.lang).chars().count() as u32,
use_spaces: true,
});
IndentStyle {
width: self.editorconfig.indent_width.unwrap_or(base.width),
use_spaces: self.editorconfig.use_spaces.unwrap_or(base.use_spaces),
}
}
pub fn set_indent_style(&mut self, style: IndentStyle) {
self.indent_override = Some(style);
self.hscroll_content_cols = None;
}
pub(super) fn indent_unit(&self) -> String {
self.indent_style().unit()
}
pub fn indent_preference(&self) -> (u32, bool) {
let s = self.indent_style();
(s.width, s.use_spaces)
}
pub fn language_label(&self) -> &'static str {
if self.is_plain_log() {
return "Log";
}
if self.lang == Some(LangKind::Css) {
let ext = self
.path
.as_deref()
.and_then(|p| p.extension())
.and_then(|e| e.to_str())
.map(str::to_ascii_lowercase);
match ext.as_deref() {
Some("scss") => return "SCSS",
Some("less") => return "Less",
Some("sass") => return "Sass",
_ => {}
}
}
language_label(self.lang)
}
pub fn language(&self) -> Option<LangKind> {
self.lang
}
pub fn set_language(&mut self, lang: Option<LangKind>) {
self.lang = lang;
self.language_by_hand = false;
self.recompute_highlights();
}
pub fn pick_language(&mut self, lang: Option<LangKind>) {
self.set_language(lang);
self.language_by_hand = true;
}
pub fn language_picked_by_hand(&self) -> bool {
self.language_by_hand
}
pub fn scope_id(&self) -> &'static str {
let ext_is = |want: &str| {
self.path
.as_deref()
.and_then(|p| p.extension())
.and_then(|e| e.to_str())
.is_some_and(|e| e.eq_ignore_ascii_case(want))
};
match self.lang {
Some(LangKind::JavaScript) if ext_is("jsx") => "javascriptreact",
Some(LangKind::Json) if ext_is("jsonc") => "jsonc",
_ => language_scope_id(self.lang),
}
}
}
pub const MAX_RULERS: usize = 16;
pub fn normalize_rulers(rulers: &[u16]) -> Vec<u16> {
let mut out = rulers.to_vec();
out.sort_unstable();
out.dedup();
out.truncate(MAX_RULERS);
out
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WhitespaceMode {
None,
#[default]
Selection,
All,
}
impl WhitespaceMode {
pub fn pref_id(self) -> &'static str {
match self {
Self::None => "none",
Self::Selection => "selection",
Self::All => "all",
}
}
pub fn from_pref(id: &str) -> Self {
match id {
"none" => Self::None,
"all" => Self::All,
_ => Self::Selection,
}
}
pub fn label(self) -> &'static str {
match self {
Self::None => "none",
Self::Selection => "selection",
Self::All => "all",
}
}
pub fn next(self) -> Self {
match self {
Self::Selection => Self::All,
Self::All => Self::None,
Self::None => Self::Selection,
}
}
}
pub(super) fn indent_unit_for(lang: Option<LangKind>) -> &'static str {
match lang {
Some(LangKind::Yaml) => " ",
_ => " ",
}
}
pub fn language_scope_id(lang: Option<LangKind>) -> &'static str {
match lang {
None => "plaintext",
Some(LangKind::Rust) => "rust",
Some(LangKind::Python) => "python",
Some(LangKind::JavaScript) => "javascript",
Some(LangKind::TypeScript) => "typescript",
Some(LangKind::Tsx) => "typescriptreact",
Some(LangKind::Json) => "json",
Some(LangKind::Xml) => "xml",
Some(LangKind::Toml) => "toml",
Some(LangKind::Yaml) => "yaml",
Some(LangKind::Markdown) => "markdown",
Some(LangKind::Go) => "go",
Some(LangKind::Html) => "html",
Some(LangKind::Css) => "css",
Some(LangKind::Bash) => "shellscript",
Some(LangKind::C) => "c",
Some(LangKind::Cpp) => "cpp",
Some(LangKind::Lua) => "lua",
Some(LangKind::Ql) => "ql",
Some(LangKind::Dbscheme) => "dbscheme",
Some(LangKind::Java) => "java",
Some(LangKind::Ruby) => "ruby",
Some(LangKind::CSharp) => "csharp",
Some(LangKind::Sql) => "sql",
Some(LangKind::Make) => "makefile",
Some(LangKind::Dockerfile) => "dockerfile",
Some(LangKind::Vue) => "vue",
Some(LangKind::Svelte) => "svelte",
}
}
pub const SELECTABLE_LANGUAGES: &[LangKind] = &[
LangKind::Rust,
LangKind::Python,
LangKind::JavaScript,
LangKind::TypeScript,
LangKind::Tsx,
LangKind::Json,
LangKind::Toml,
LangKind::Yaml,
LangKind::Markdown,
LangKind::Go,
LangKind::Html,
LangKind::Css,
LangKind::Bash,
LangKind::C,
LangKind::Cpp,
LangKind::Lua,
LangKind::Ql,
LangKind::Dbscheme,
LangKind::Java,
LangKind::Ruby,
LangKind::CSharp,
LangKind::Sql,
LangKind::Make,
LangKind::Dockerfile,
LangKind::Vue,
LangKind::Svelte,
];