Allow extending special environments from options

See #860.
This commit is contained in:
Patrick Förster 2023-03-12 09:08:09 +01:00
parent 3f428331b4
commit 8f1ae1d9cc
10 changed files with 141 additions and 141 deletions

View file

@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- Add experimental settings to allow extending the list of special environments:
- `texlab.experimental.mathEnvironments`
- `texlab.experimental.enumEnvironments`
- `texlab.experimental.verbatimEnvironments`
### Changed
- Do not show caption or section names in label inlay hints ([#858](https://github.com/latex-lsp/texlab/issues/858))

10
Cargo.lock generated
View file

@ -1453,15 +1453,6 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_millis"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e2dc780ca5ee2c369d1d01d100270203c4ff923d2a4264812d723766434d00"
dependencies = [
"serde",
]
[[package]]
name = "serde_regex"
version = "1.1.0"
@ -1634,7 +1625,6 @@ dependencies = [
"salsa-2022",
"serde",
"serde_json",
"serde_millis",
"serde_regex",
"serde_repr",
"smol_str",

View file

@ -60,7 +60,6 @@ rowan = "0.15.10"
rustc-hash = "1.1.0"
serde = "1.0.152"
serde_json = "1.0.93"
serde_millis = "0.1.1"
serde_regex = "1.1.0"
serde_repr = "0.1.10"
smol_str = { version = "0.1.24", features = ["serde"] }

View file

@ -1054,53 +1054,5 @@
"topaths",
"trees",
"turtle"
],
"mathEnvironments": [
"align",
"align*",
"alignat",
"alignat*",
"aligned",
"aligned*",
"alignedat",
"alignedat*",
"array",
"array*",
"Bmatrix",
"Bmatrix*",
"bmatrix",
"bmatrix*",
"cases",
"cases*",
"CD",
"CD*",
"eqnarray",
"eqnarray*",
"equation",
"equation*",
"subequations",
"subequations*",
"gather",
"gather*",
"gathered",
"gathered*",
"matrix",
"matrix*",
"multline",
"multline*",
"pmatrix",
"pmatrix*",
"smallmatrix",
"smallmatrix*",
"split",
"split*",
"subarray",
"subarray*",
"Vmatrix",
"Vmatrix*",
"vmatrix",
"vmatrix*"
],
"enumEnvironments": ["enumerate", "itemize", "description"],
"verbatimEnvironments": ["pycode", "minted", "asy", "lstlisting", "verbatim"]
]
}

View file

@ -1,6 +1,7 @@
use std::time::Duration;
use regex::Regex;
use rustc_hash::FxHashSet;
#[derive(Debug)]
pub struct Config {
@ -10,6 +11,7 @@ pub struct Config {
pub formatting: FormattingConfig,
pub synctex: Option<SynctexConfig>,
pub symbols: SymbolConfig,
pub syntax: SyntaxConfig,
}
#[derive(Debug)]
@ -69,6 +71,13 @@ pub struct SymbolConfig {
pub ignored_patterns: Vec<Regex>,
}
#[derive(Debug)]
pub struct SyntaxConfig {
pub math_environments: FxHashSet<String>,
pub enum_environments: FxHashSet<String>,
pub verbatim_environments: FxHashSet<String>,
}
impl Default for Config {
fn default() -> Self {
Self {
@ -78,6 +87,7 @@ impl Default for Config {
formatting: FormattingConfig::default(),
synctex: None,
symbols: SymbolConfig::default(),
syntax: SyntaxConfig::default(),
}
}
}
@ -146,3 +156,75 @@ impl Default for SymbolConfig {
}
}
}
impl Default for SyntaxConfig {
fn default() -> Self {
let math_environments = [
"align",
"align*",
"alignat",
"alignat*",
"aligned",
"aligned*",
"alignedat",
"alignedat*",
"array",
"array*",
"Bmatrix",
"Bmatrix*",
"bmatrix",
"bmatrix*",
"cases",
"cases*",
"CD",
"CD*",
"eqnarray",
"eqnarray*",
"equation",
"equation*",
"IEEEeqnarray",
"IEEEeqnarray*",
"subequations",
"subequations*",
"gather",
"gather*",
"gathered",
"gathered*",
"matrix",
"matrix*",
"multline",
"multline*",
"pmatrix",
"pmatrix*",
"smallmatrix",
"smallmatrix*",
"split",
"split*",
"subarray",
"subarray*",
"Vmatrix",
"Vmatrix*",
"vmatrix",
"vmatrix*",
]
.into_iter()
.map(String::from)
.collect();
let enum_environments = ["enumerate", "itemize", "description"]
.into_iter()
.map(String::from)
.collect();
let verbatim_environments = ["pycode", "minted", "asy", "lstlisting", "verbatim"]
.into_iter()
.map(String::from)
.collect();
Self {
math_environments,
enum_environments,
verbatim_environments,
}
}
}

View file

@ -1,12 +1,7 @@
use lsp_types::DiagnosticSeverity;
use rowan::{ast::AstNode, NodeOrToken, TextRange};
use crate::{
db::document::Document,
syntax::latex,
util::{lang_data::LANGUAGE_DATA, line_index_ext::LineIndexExt},
Db,
};
use crate::{db::document::Document, syntax::latex, util::line_index_ext::LineIndexExt, Db};
use super::{Diagnostic, DiagnosticCode, TexCode};
@ -33,7 +28,8 @@ pub fn collect(db: &dyn Db, document: Document) -> Vec<Diagnostic> {
.and_then(|begin| begin.name())
.and_then(|name| name.key())
.map_or(false, |name| {
LANGUAGE_DATA
db.config()
.syntax
.verbatim_environments
.contains(&name.to_string())
})

View file

@ -9,7 +9,6 @@ use crate::{
syntax::latex::{self, HasBrack, HasCurly},
util::{
label::{find_caption_by_parent, LabeledFloatKind},
lang_data::LANGUAGE_DATA,
line_index_ext::LineIndexExt,
},
Db,
@ -41,17 +40,9 @@ fn visit(db: &dyn Db, document: Document, node: latex::SyntaxNode) -> Vec<Intern
.and_then(|name| name.key())
.map(|name| name.to_string())
.and_then(|name| {
if LANGUAGE_DATA
.math_environments
.iter()
.any(|env| env == &name)
{
if db.config().syntax.math_environments.contains(&name) {
visit_equation_environment(db, document, node.clone())
} else if LANGUAGE_DATA
.enum_environments
.iter()
.any(|env| env == &name)
{
} else if db.config().syntax.enum_environments.contains(&name) {
visit_enumeration(db, document, node.clone(), &name)
} else if let Ok(float_kind) = LabeledFloatKind::from_str(&name) {
visit_float(db, document, node.clone(), float_kind)
@ -132,6 +123,7 @@ fn visit_enum_item(
document: Document,
node: latex::SyntaxNode,
) -> Option<InternalSymbol> {
let enum_envs = &db.config().syntax.enum_environments;
let enum_item = latex::EnumItem::cast(node.clone())?;
if !enum_item
.syntax()
@ -140,12 +132,7 @@ fn visit_enum_item(
.filter_map(|environment| environment.begin())
.filter_map(|begin| begin.name())
.filter_map(|name| name.key())
.any(|name| {
LANGUAGE_DATA
.enum_environments
.iter()
.any(|e| e == &name.to_string())
})
.any(|name| enum_envs.contains(&name.to_string()))
{
return None;
}

View file

@ -15,21 +15,13 @@ pub struct Options {
pub latex_formatter: LatexFormatter,
pub formatter_line_length: Option<i32>,
pub diagnostics: DiagnosticsOptions,
pub diagnostics_delay: DiagnosticsDelay,
pub diagnostics_delay: Option<u64>,
pub build: BuildOptions,
pub chktex: ChktexOptions,
pub symbols: SymbolOptions,
pub latexindent: LatexindentOptions,
pub forward_search: ForwardSearchOptions,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
pub struct DiagnosticsDelay(#[serde(with = "serde_millis")] pub Duration);
impl Default for DiagnosticsDelay {
fn default() -> Self {
Self(Duration::from_millis(300))
}
pub experimental: ExperimentalOptions,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)]
@ -72,37 +64,12 @@ pub struct LatexindentOptions {
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct BuildOptions {
pub executable: BuildExecutable,
pub args: BuildArgs,
pub executable: Option<String>,
pub args: Option<Vec<String>>,
pub on_save: bool,
pub forward_search_after: bool,
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(transparent)]
pub struct BuildExecutable(pub String);
impl Default for BuildExecutable {
fn default() -> Self {
Self("latexmk".to_string())
}
}
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
#[serde(transparent)]
pub struct BuildArgs(pub Vec<String>);
impl Default for BuildArgs {
fn default() -> Self {
Self(vec![
"-pdf".to_string(),
"-interaction=nonstopmode".to_string(),
"-synctex=1".to_string(),
"%f".to_string(),
])
}
}
#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
@ -138,6 +105,15 @@ pub struct SymbolOptions {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegexPattern(#[serde(with = "serde_regex")] pub Regex);
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct ExperimentalOptions {
pub math_environments: Vec<String>,
pub enum_environments: Vec<String>,
pub verbatim_environments: Vec<String>,
}
#[derive(Debug, PartialEq, Eq, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
@ -150,8 +126,8 @@ impl From<Options> for Config {
let mut config = Config::default();
config.root_dir = value.root_directory;
config.build.program = value.build.executable.0;
config.build.args = value.build.args.0;
config.build.program = value.build.executable.unwrap_or(config.build.program);
config.build.args = value.build.args.unwrap_or(config.build.args);
config.build.on_save = value.build.on_save;
config.build.forward_search_after = value.build.forward_search_after;
config.build.output_dir = value.aux_directory.unwrap_or_else(|| String::from("."));
@ -170,7 +146,10 @@ impl From<Options> for Config {
.map(|pattern| pattern.0)
.collect();
config.diagnostics.delay = value.diagnostics_delay.0;
config.diagnostics.delay = value
.diagnostics_delay
.map_or(config.diagnostics.delay, Duration::from_millis);
config.diagnostics.chktex.on_open = value.chktex.on_open_and_save;
config.diagnostics.chktex.on_save = value.chktex.on_open_and_save;
config.diagnostics.chktex.on_edit = value.chktex.on_edit;
@ -215,6 +194,21 @@ impl From<Options> for Config {
.map(|pattern| pattern.0)
.collect();
config
.syntax
.math_environments
.extend(value.experimental.math_environments);
config
.syntax
.enum_environments
.extend(value.experimental.enum_environments);
config
.syntax
.verbatim_environments
.extend(value.experimental.verbatim_environments);
config
}
}

View file

@ -10,8 +10,6 @@ use crate::{
use self::LabeledObject::*;
use super::lang_data::LANGUAGE_DATA;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum LabeledFloatKind {
Figure,
@ -135,7 +133,7 @@ pub fn render(db: &dyn Db, document: Document, label_def: label::Name) -> Option
render_label_float(parent.clone(), label_num)
.or_else(|| render_label_section(parent.clone(), label_num))
.or_else(|| render_label_enum_item(db, parent.clone(), label_num))
.or_else(|| render_label_equation(parent.clone(), label_num))
.or_else(|| render_label_equation(db, parent.clone(), label_num))
.or_else(|| render_label_theorem(db, document, parent, label_num))
})
}
@ -214,20 +212,20 @@ fn render_label_enum_item(
})
}
fn render_label_equation(parent: latex::SyntaxNode, number: Option<Word>) -> Option<RenderedLabel> {
let environment = latex::Environment::cast(parent)?;
let environment_name = environment.begin()?.name()?.key()?.to_string();
fn render_label_equation(
db: &dyn Db,
parent: latex::SyntaxNode,
number: Option<Word>,
) -> Option<RenderedLabel> {
let env = latex::Environment::cast(parent)?;
let env_name = env.begin()?.name()?.key()?.to_string();
if !LANGUAGE_DATA
.math_environments
.iter()
.any(|name| name == &environment_name)
{
if !db.config().syntax.math_environments.contains(&env_name) {
return None;
}
Some(RenderedLabel {
range: latex::small_range(&environment),
range: latex::small_range(&env),
number,
object: LabeledObject::Equation,
})

View file

@ -1,5 +1,3 @@
use std::collections::BTreeSet;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
@ -38,9 +36,6 @@ pub struct LanguageData {
pub fields: Vec<BibtexFieldDoc>,
pub pgf_libraries: Vec<String>,
pub tikz_libraries: Vec<String>,
pub math_environments: Vec<String>,
pub enum_environments: Vec<String>,
pub verbatim_environments: BTreeSet<String>,
}
impl LanguageData {