Skip to main content

oo_ide/schema/format/
mod.rs

1//! Format-specific cursor context detectors.
2//!
3//! Each sub-module is responsible for parsing a specific file format and
4//! converting a (buffer, cursor) pair into a [`CursorContext`].
5//!
6//! Add new sub-modules here when adding YAML/JSON/etc. support.
7
8pub mod toml;
9pub mod yaml;
10pub mod json;
11
12use std::path::Path;
13
14use crate::editor::position::Position;
15use crate::schema::context::CursorContext;
16
17/// Detect the cursor context for the file at `path`.
18///
19/// Returns `None` if the format is unsupported or context detection fails,
20/// in which case the caller should fall back to the legacy heuristic.
21pub fn detect_cursor_context(
22    lines: &[String],
23    trigger: &Position,
24    path: Option<&Path>,
25) -> Option<CursorContext> {
26    let ext = path?.extension()?.to_str()?;
27    match ext {
28        "toml" => toml::detect_context(lines, trigger),
29        "yaml" | "yml" => yaml::detect_context(lines, trigger),
30        "json" => json::detect_context(lines, trigger),
31        _ => None,
32    }
33}