use std::collections::BTreeSet;
use sephera_compression::SupportedLanguage;
use sephera_core::{
path_utils as paths,
plugins::{ImportPlugin, ResolveContext, ResolverPlugin},
};
#[derive(Debug, Clone, Copy)]
pub struct CCppPlugin {
pub language: SupportedLanguage,
}
impl ImportPlugin for CCppPlugin {
fn extract_from_node(
&self,
source: &[u8],
node: &tree_sitter::Node<'_>,
) -> Option<Vec<sephera_core::types::ImportStatement>> {
crate::extract::extract_from_node(source, node)
}
}
impl ResolverPlugin for CCppPlugin {
fn resolve(
&self,
import_path: &str,
context: ResolveContext<'_>,
) -> Option<String> {
if import_path.starts_with('<') {
return None;
}
let parent = paths::parent(context.source_file);
let relative = paths::resolve_relative(&parent, import_path);
if context.contains(&relative) {
return Some(relative);
}
if context.contains(import_path) {
return Some(import_path.to_owned());
}
include_path_match(import_path, &context)
}
}
fn include_path_match(
import_path: &str,
context: &ResolveContext<'_>,
) -> Option<String> {
let wanted = import_path.strip_prefix("./").unwrap_or(import_path);
if wanted.starts_with("..") || wanted.starts_with('/') {
return None;
}
let suffix = format!("/{wanted}");
let known: &BTreeSet<String> = context.known_files;
let mut found: Option<&str> = None;
for candidate in known {
if !candidate.ends_with(&suffix) {
continue;
}
if found.is_some() {
return None;
}
found = Some(candidate.as_str());
}
found.map(ToOwned::to_owned)
}
#[cfg(test)]
mod tests {
use super::*;
fn resolve(
import_path: &str,
source_file: &str,
files: &[&str],
) -> Option<String> {
let known: BTreeSet<String> =
files.iter().map(|f| (*f).to_owned()).collect();
let context = sephera_core::plugins::test_context(source_file, &known);
CCppPlugin {
language: SupportedLanguage::C,
}
.resolve(import_path, context)
}
#[test]
fn resolves_header_beside_the_importer() {
let files = ["src/main.c", "src/util.h"];
assert_eq!(
resolve("util.h", "src/main.c", &files),
Some("src/util.h".to_owned())
);
}
#[test]
fn resolves_a_header_found_on_the_include_path() {
let files = ["src/main.c", "include/shared.h", "src/local.h"];
assert_eq!(
resolve("shared.h", "src/main.c", &files),
Some("include/shared.h".to_owned())
);
}
#[test]
fn resolves_a_nested_include_path() {
let files = ["src/main.c", "third_party/lua/lua.h"];
assert_eq!(
resolve("lua/lua.h", "src/main.c", &files),
Some("third_party/lua/lua.h".to_owned())
);
}
#[test]
fn refuses_to_guess_when_two_headers_share_a_tail() {
let files = ["src/main.c", "a/util.h", "b/util.h"];
assert_eq!(resolve("util.h", "src/main.c", &files), None);
}
#[test]
fn a_tail_match_respects_path_segments() {
let files = ["src/main.c", "include/preshared.h"];
assert_eq!(resolve("shared.h", "src/main.c", &files), None);
}
#[test]
fn a_system_header_is_never_matched_locally() {
let files = ["src/main.c", "include/stdio.h"];
assert_eq!(resolve("<stdio.h>", "src/main.c", &files), None);
}
#[test]
fn resolves_header_from_an_include_path() {
let files = ["src/main.c", "include/util.h"];
assert_eq!(
resolve("util.h", "src/main.c", &files),
Some("include/util.h".to_owned())
);
}
#[test]
fn resolves_header_when_quoted_path_is_relative_to_the_importer() {
let files = ["src/main.c", "include/util.h"];
assert_eq!(
resolve("../include/util.h", "src/main.c", &files),
Some("include/util.h".to_owned())
);
}
#[test]
fn missing_header_is_not_resolved() {
let files = ["src/main.c"];
assert_eq!(resolve("nothing.h", "src/main.c", &files), None);
}
#[test]
fn system_include_is_not_resolved() {
let files = ["src/main.c"];
assert_eq!(resolve("<stdio.h>", "src/main.c", &files), None);
}
#[test]
fn resolves_nested_relative_header() {
let files = ["src/deep/main.c", "src/util.h"];
assert_eq!(
resolve("../util.h", "src/deep/main.c", &files),
Some("src/util.h".to_owned())
);
}
}