From cd65a48635d49ccc2aed55a315c3a33875e13f88 Mon Sep 17 00:00:00 2001 From: Cor Peters Date: Sun, 18 Jul 2021 18:33:38 +0200 Subject: [PATCH] Made toggle_comments language dependent (#463) * Made toggle_comments language dependent * Fixed Test Cases * Added clippy suggestion * Small Fixes * Clippy Suggestion Co-authored-by: Cor --- helix-core/src/comment.rs | 10 +++++----- helix-core/src/indent.rs | 1 + helix-core/src/syntax.rs | 1 + helix-term/src/commands.rs | 6 +++++- languages.toml | 12 ++++++++++++ 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/helix-core/src/comment.rs b/helix-core/src/comment.rs index 07f685d83..5d564055d 100644 --- a/helix-core/src/comment.rs +++ b/helix-core/src/comment.rs @@ -38,18 +38,18 @@ fn find_line_comment( } #[must_use] -pub fn toggle_line_comments(doc: &Rope, selection: &Selection) -> Transaction { +pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&str>) -> Transaction { let text = doc.slice(..); let mut changes: Vec = Vec::new(); - let token = "//"; + let token = token.unwrap_or("//"); let comment = Tendril::from(format!("{} ", token)); for selection in selection { let start = text.char_to_line(selection.from()); let end = text.char_to_line(selection.to()); let lines = start..end + 1; - let (commented, skipped, min) = find_line_comment(token, text, lines.clone()); + let (commented, skipped, min) = find_line_comment(&token, text, lines.clone()); changes.reserve((end - start).saturating_sub(skipped.len())); @@ -95,14 +95,14 @@ mod test { assert_eq!(res, (false, vec![1], 2)); // comment - let transaction = toggle_line_comments(&state.doc, &state.selection); + let transaction = toggle_line_comments(&state.doc, &state.selection, None); transaction.apply(&mut state.doc); state.selection = state.selection.clone().map(transaction.changes()); assert_eq!(state.doc, " // 1\n\n // 2\n // 3"); // uncomment - let transaction = toggle_line_comments(&state.doc, &state.selection); + let transaction = toggle_line_comments(&state.doc, &state.selection, None); transaction.apply(&mut state.doc); state.selection = state.selection.clone().map(transaction.changes()); assert_eq!(state.doc, " 1\n\n 2\n 3"); diff --git a/helix-core/src/indent.rs b/helix-core/src/indent.rs index 292ade4b5..0ca05fb3c 100644 --- a/helix-core/src/indent.rs +++ b/helix-core/src/indent.rs @@ -265,6 +265,7 @@ where config: None, // roots: vec![], + comment_token: None, auto_format: false, language_server: None, indent: Some(IndentationConfiguration { diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 621cd0469..9acf3d879 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -35,6 +35,7 @@ pub struct LanguageConfiguration { pub scope: String, // source.rust pub file_types: Vec, // filename ends_with? pub roots: Vec, // these indicate project roots <.git, Cargo.toml> + pub comment_token: Option, pub config: Option, #[serde(default)] diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 7e769f4e1..5fc96cd92 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -3399,7 +3399,11 @@ fn hover(cx: &mut Context) { // comments fn toggle_comments(cx: &mut Context) { let (view, doc) = current!(cx.editor); - let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id)); + let token = doc + .language_config() + .and_then(|lc| lc.comment_token.as_ref()) + .map(|tc| tc.as_ref()); + let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id), token); doc.apply(&transaction, view.id); doc.append_changes_to_history(view.id); diff --git a/languages.toml b/languages.toml index ee495635f..05268ddd0 100644 --- a/languages.toml +++ b/languages.toml @@ -5,6 +5,7 @@ injection-regex = "rust" file-types = ["rs"] roots = [] auto-format = true +comment_token = "//" config = """ { "cargo": { @@ -25,6 +26,7 @@ scope = "source.toml" injection-regex = "toml" file-types = ["toml"] roots = [] +comment_token = "#" indent = { tab-width = 2, unit = " " } @@ -52,6 +54,7 @@ scope = "source.c" injection-regex = "c" file-types = ["c"] # TODO: ["h"] roots = [] +comment_token = "//" language-server = { command = "clangd" } indent = { tab-width = 2, unit = " " } @@ -62,6 +65,7 @@ scope = "source.cpp" injection-regex = "cpp" file-types = ["cc", "cpp", "hpp", "h"] roots = [] +comment_token = "//" language-server = { command = "clangd" } indent = { tab-width = 2, unit = " " } @@ -73,6 +77,7 @@ injection-regex = "go" file-types = ["go"] roots = ["Gopkg.toml", "go.mod"] auto-format = true +comment_token = "//" language-server = { command = "gopls" } # TODO: gopls needs utf-8 offsets? @@ -84,6 +89,7 @@ scope = "source.js" injection-regex = "^(js|javascript)$" file-types = ["js"] roots = [] +comment_token = "//" # TODO: highlights-jsx, highlights-params indent = { tab-width = 2, unit = " " } @@ -123,6 +129,7 @@ scope = "source.python" injection-regex = "python" file-types = ["py"] roots = [] +comment_token = "#" language-server = { command = "pyls" } # TODO: pyls needs utf-8 offsets @@ -143,6 +150,7 @@ scope = "source.ruby" injection-regex = "ruby" file-types = ["rb"] roots = [] +comment_token = "#" language-server = { command = "solargraph", args = ["stdio"] } indent = { tab-width = 2, unit = " " } @@ -153,6 +161,7 @@ scope = "source.bash" injection-regex = "bash" file-types = ["sh", "bash"] roots = [] +comment_token = "#" language-server = { command = "bash-language-server", args = ["start"] } indent = { tab-width = 2, unit = " " } @@ -172,6 +181,7 @@ scope = "source.tex" injection-regex = "tex" file-types = ["tex"] roots = [] +comment_token = "%" indent = { tab-width = 4, unit = "\t" } @@ -181,6 +191,7 @@ scope = "source.julia" injection-regex = "julia" file-types = ["jl"] roots = [] +comment_token = "#" language-server = { command = "julia", args = [ "--startup-file=no", "--history-file=no", "-e", "using LanguageServer;using Pkg;import StaticLint;import SymbolServer;env_path = dirname(Pkg.Types.Context().env.project_file);server = LanguageServer.LanguageServerInstance(stdin, stdout, env_path, \"\");server.runlinter = true;run(server);" ] } indent = { tab-width = 2, unit = " " } @@ -190,5 +201,6 @@ indent = { tab-width = 2, unit = " " } # injection-regex = "haskell" # file-types = ["hs"] # roots = [] +# comment_token = "--" # # indent = { tab-width = 2, unit = " " }