Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions crates/mcpls-core/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ impl WorkspaceConfig {
}
}

/// Extract a file extension from a glob-like file pattern.
///
/// Supports common patterns such as `**/*.rs` and `*.h`.
/// Returns `None` for patterns without a simple trailing extension.
fn extract_extension_from_pattern(pattern: &str) -> Option<String> {
let (_, ext) = pattern.rsplit_once('.')?;
if ext.is_empty() {
return None;
}

// Keep this conservative: only accept plain extension-like tokens.
if ext
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
{
Some(ext.to_string())
} else {
None
}
Comment on lines +119 to +137
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add direct unit tests for extract_extension_from_pattern

Currently this function is only tested indirectly through test_build_effective_extension_map_overrides_with_file_patterns and test_build_effective_extension_map_ignores_complex_patterns_without_extension.

Please add dedicated unit tests covering edge cases:

  • Empty string pattern: ""None
  • Pattern without dot: "**/*"None
  • Dotfile pattern: ".gitignore"None
  • Multi-dot extensions: "foo.tar.gz"Some("gz")

This ensures the conservative parsing logic is well-validated.

Optional: Consider returning &str instead of String since rsplit_once already returns slices of the input.

}

fn default_position_encodings() -> Vec<String> {
vec!["utf-8".to_string(), "utf-16".to_string()]
}
Expand Down Expand Up @@ -258,6 +279,25 @@ fn default_language_extensions() -> Vec<LanguageExtensionMapping> {
}

impl ServerConfig {
/// Build the effective extension map used for language detection.
///
/// Starts with workspace mappings and overlays mappings inferred from
/// configured LSP server `file_patterns`.
#[must_use]
pub fn build_effective_extension_map(&self) -> HashMap<String, String> {
let mut map = self.workspace.build_extension_map();

for server in &self.lsp_servers {
for pattern in &server.file_patterns {
if let Some(ext) = extract_extension_from_pattern(pattern) {
map.insert(ext, server.language_id.clone());
}
}
}

map
}

/// Load configuration from the default path.
///
/// Default paths checked in order:
Expand Down Expand Up @@ -656,6 +696,48 @@ mod tests {
assert_eq!(map.get("unknown"), None);
}

#[test]
fn test_build_effective_extension_map_overrides_with_file_patterns() {
let config = ServerConfig {
workspace: WorkspaceConfig::default(),
lsp_servers: vec![LspServerConfig {
language_id: "cpp".to_string(),
command: "clangd".to_string(),
args: vec![],
env: HashMap::new(),
file_patterns: vec!["**/*.c".to_string(), "**/*.h".to_string()],
initialization_options: None,
timeout_seconds: 30,
heuristics: None,
}],
};

let map = config.build_effective_extension_map();
assert_eq!(map.get("c"), Some(&"cpp".to_string()));
assert_eq!(map.get("h"), Some(&"cpp".to_string()));
}

#[test]
fn test_build_effective_extension_map_ignores_complex_patterns_without_extension() {
let config = ServerConfig {
workspace: WorkspaceConfig::default(),
lsp_servers: vec![LspServerConfig {
language_id: "cpp".to_string(),
command: "clangd".to_string(),
args: vec![],
env: HashMap::new(),
file_patterns: vec!["**/*".to_string(), "**/*.{h,hpp}".to_string()],
initialization_options: None,
timeout_seconds: 30,
heuristics: None,
}],
};

let map = config.build_effective_extension_map();
// Default C/C++ mappings remain unchanged when patterns cannot be parsed.
assert_eq!(map.get("h"), Some(&"c".to_string()));
}

#[test]
fn test_get_language_for_extension() {
let workspace = WorkspaceConfig {
Expand Down
2 changes: 1 addition & 1 deletion crates/mcpls-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub async fn serve(config: ServerConfig) -> Result<(), Error> {
info!("Starting MCPLS server...");

let workspace_roots = resolve_workspace_roots(&config.workspace.roots);
let extension_map = config.workspace.build_extension_map();
let extension_map = config.build_effective_extension_map();
let max_depth = Some(config.workspace.heuristics_max_depth);

let mut translator = Translator::new().with_extensions(extension_map);
Expand Down
Loading