Skip to content

Commit c19eb31

Browse files
sinelawclaude
andcommitted
fix: toggle comment now works for YAML files
When loading a file, use config-based language detection (detect_language) as a fallback when tree-sitter doesn't recognize the file extension. This makes self.active_state().language reliable for all languages defined in the config, including YAML/YML which has comment_prefix configured but no tree-sitter support. The fix is in EditorState::from_file_with_languages, so all code that uses the language field benefits from this fix. Fixes #774 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 032b0af commit c19eb31

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

crates/fresh-editor/src/app/render.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2972,7 +2972,6 @@ impl Editor {
29722972
pub(super) fn toggle_comment(&mut self) {
29732973
// Determine comment prefix from language config
29742974
// If no language detected or no comment prefix configured, do nothing
2975-
// Determine comment prefix from language config
29762975
let language = &self.active_state().language;
29772976
let comment_prefix = self
29782977
.config

crates/fresh-editor/src/state.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,10 @@ impl EditorState {
309309
reference_highlighter.set_language(lang);
310310
lang.to_string()
311311
} else {
312-
"text".to_string()
312+
// Fall back to config-based detection for languages without tree-sitter support
313+
// (e.g., YAML, TOML, etc.)
314+
crate::services::lsp::manager::detect_language(path, languages)
315+
.unwrap_or_else(|| "text".to_string())
313316
};
314317

315318
// Initialize marker list with buffer size

crates/fresh-editor/tests/e2e/toggle_comment.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,58 @@ fn test_toggle_comment_single_line_no_newline() {
307307
);
308308
}
309309

310+
/// Test that Toggle Comment uses # for YAML files (issue #774)
311+
///
312+
/// Bug: Ctrl+/ doesn't work in yml and yaml files
313+
/// Root cause: Language::from_path doesn't recognize .yml/.yaml extensions,
314+
/// so the language defaults to "text" which has no comment_prefix configured.
315+
#[test]
316+
fn test_toggle_comment_yaml_prefix() {
317+
let temp_dir = TempDir::new().unwrap();
318+
let file_path = temp_dir.path().join("test.yaml");
319+
std::fs::write(&file_path, "key: value\nnested:\n child: 123").unwrap();
320+
321+
let config = Config::default();
322+
let mut harness =
323+
EditorTestHarness::create(80, 24, HarnessOptions::new().with_config(config)).unwrap();
324+
harness.open_file(&file_path).unwrap();
325+
harness.render().unwrap();
326+
327+
// Toggle comment on first line
328+
run_command(&mut harness, "Toggle Comment");
329+
330+
let content = harness.get_buffer_content().unwrap();
331+
assert!(
332+
content.starts_with("# key: value"),
333+
"YAML files should use # for comments. Got: {:?}",
334+
content
335+
);
336+
}
337+
338+
/// Test that Toggle Comment uses # for YML files (issue #774)
339+
#[test]
340+
fn test_toggle_comment_yml_prefix() {
341+
let temp_dir = TempDir::new().unwrap();
342+
let file_path = temp_dir.path().join("config.yml");
343+
std::fs::write(&file_path, "server:\n port: 8080").unwrap();
344+
345+
let config = Config::default();
346+
let mut harness =
347+
EditorTestHarness::create(80, 24, HarnessOptions::new().with_config(config)).unwrap();
348+
harness.open_file(&file_path).unwrap();
349+
harness.render().unwrap();
350+
351+
// Toggle comment on first line
352+
run_command(&mut harness, "Toggle Comment");
353+
354+
let content = harness.get_buffer_content().unwrap();
355+
assert!(
356+
content.starts_with("# server:"),
357+
"YML files should use # for comments. Got: {:?}",
358+
content
359+
);
360+
}
361+
310362
/// Test toggle comment on file with selection at exact buffer end
311363
#[test]
312364
fn test_toggle_comment_selection_at_buffer_end() {

0 commit comments

Comments
 (0)