on master help sign in
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 <prive@corpeters.nl>

commitcd65a48635d49ccc2aed55a315c3a33875e13f88 authorCor Peters <luctius@users.noreply.github.com> dateSun 18 Jul 2021, 18:33 (5 years ago) parent0aa4390
5 files changed+24−6
helix-core/src/comment.rs+5−5 helix-core/src/indent.rs+1 helix-core/src/syntax.rs+1 helix-term/src/commands.rs+5−1 languages.toml+12
helix-core/src/comment.rs+5−5
@@ -38,18 +38,18 @@ fn find_line_comment(
3838}
3939
4040#[must_use]
41−pub fn toggle_line_comments(doc: &Rope, selection: &Selection) -> Transaction {
41+pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&str>) -> Transaction {
4242 let text = doc.slice(..);
4343 let mut changes: Vec<Change> = Vec::new();
4444
45− let token = "//";
45+ let token = token.unwrap_or("//");
4646 let comment = Tendril::from(format!("{} ", token));
4747
4848 for selection in selection {
4949 let start = text.char_to_line(selection.from());
5050 let end = text.char_to_line(selection.to());
5151 let lines = start..end + 1;
52− let (commented, skipped, min) = find_line_comment(token, text, lines.clone());
52+ let (commented, skipped, min) = find_line_comment(&token, text, lines.clone());
5353
5454 changes.reserve((end - start).saturating_sub(skipped.len()));
5555
@@ -95,14 +95,14 @@ mod test {
9595 assert_eq!(res, (false, vec![1], 2));
9696
9797 // comment
98− let transaction = toggle_line_comments(&state.doc, &state.selection);
98+ let transaction = toggle_line_comments(&state.doc, &state.selection, None);
9999 transaction.apply(&mut state.doc);
100100 state.selection = state.selection.clone().map(transaction.changes());
101101
102102 assert_eq!(state.doc, " // 1\n\n // 2\n // 3");
103103
104104 // uncomment
105− let transaction = toggle_line_comments(&state.doc, &state.selection);
105+ let transaction = toggle_line_comments(&state.doc, &state.selection, None);
106106 transaction.apply(&mut state.doc);
107107 state.selection = state.selection.clone().map(transaction.changes());
108108 assert_eq!(state.doc, " 1\n\n 2\n 3");
helix-core/src/indent.rs+1
@@ -265,6 +265,7 @@ where
265265 config: None,
266266 //
267267 roots: vec![],
268+ comment_token: None,
268269 auto_format: false,
269270 language_server: None,
270271 indent: Some(IndentationConfiguration {
helix-core/src/syntax.rs+1
@@ -35,6 +35,7 @@ pub struct LanguageConfiguration {
3535 pub scope: String, // source.rust
3636 pub file_types: Vec<String>, // filename ends_with? <Gemfile, rb, etc>
3737 pub roots: Vec<String>, // these indicate project roots <.git, Cargo.toml>
38+ pub comment_token: Option<String>,
3839 pub config: Option<String>,
3940
4041 #[serde(default)]
helix-term/src/commands.rs+5−1
@@ -3399,7 +3399,11 @@ fn hover(cx: &mut Context) {
33993399// comments
34003400fn toggle_comments(cx: &mut Context) {
34013401 let (view, doc) = current!(cx.editor);
3402− let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id));
3402+ let token = doc
3403+ .language_config()
3404+ .and_then(|lc| lc.comment_token.as_ref())
3405+ .map(|tc| tc.as_ref());
3406+ let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id), token);
34033407
34043408 doc.apply(&transaction, view.id);
34053409 doc.append_changes_to_history(view.id);
languages.toml+12
@@ -5,6 +5,7 @@ injection-regex = "rust"
55file-types = ["rs"]
66roots = []
77auto-format = true
8+comment_token = "//"
89config = """
910{
1011 "cargo": {
@@ -25,6 +26,7 @@ scope = "source.toml"
2526injection-regex = "toml"
2627file-types = ["toml"]
2728roots = []
29+comment_token = "#"
2830
2931indent = { tab-width = 2, unit = " " }
3032
@@ -52,6 +54,7 @@ scope = "source.c"
5254injection-regex = "c"
5355file-types = ["c"] # TODO: ["h"]
5456roots = []
57+comment_token = "//"
5558
5659language-server = { command = "clangd" }
5760indent = { tab-width = 2, unit = " " }
@@ -62,6 +65,7 @@ scope = "source.cpp"
6265injection-regex = "cpp"
6366file-types = ["cc", "cpp", "hpp", "h"]
6467roots = []
68+comment_token = "//"
6569
6670language-server = { command = "clangd" }
6771indent = { tab-width = 2, unit = " " }
@@ -73,6 +77,7 @@ injection-regex = "go"
7377file-types = ["go"]
7478roots = ["Gopkg.toml", "go.mod"]
7579auto-format = true
80+comment_token = "//"
7681
7782language-server = { command = "gopls" }
7883# TODO: gopls needs utf-8 offsets?
@@ -84,6 +89,7 @@ scope = "source.js"
8489injection-regex = "^(js|javascript)$"
8590file-types = ["js"]
8691roots = []
92+comment_token = "//"
8793# TODO: highlights-jsx, highlights-params
8894
8995indent = { tab-width = 2, unit = " " }
@@ -123,6 +129,7 @@ scope = "source.python"
123129injection-regex = "python"
124130file-types = ["py"]
125131roots = []
132+comment_token = "#"
126133
127134language-server = { command = "pyls" }
128135# TODO: pyls needs utf-8 offsets
@@ -143,6 +150,7 @@ scope = "source.ruby"
143150injection-regex = "ruby"
144151file-types = ["rb"]
145152roots = []
153+comment_token = "#"
146154
147155language-server = { command = "solargraph", args = ["stdio"] }
148156indent = { tab-width = 2, unit = " " }
@@ -153,6 +161,7 @@ scope = "source.bash"
153161injection-regex = "bash"
154162file-types = ["sh", "bash"]
155163roots = []
164+comment_token = "#"
156165
157166language-server = { command = "bash-language-server", args = ["start"] }
158167indent = { tab-width = 2, unit = " " }
@@ -172,6 +181,7 @@ scope = "source.tex"
172181injection-regex = "tex"
173182file-types = ["tex"]
174183roots = []
184+comment_token = "%"
175185
176186indent = { tab-width = 4, unit = "\t" }
177187
@@ -181,6 +191,7 @@ scope = "source.julia"
181191injection-regex = "julia"
182192file-types = ["jl"]
183193roots = []
194+comment_token = "#"
184195language-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);" ] }
185196indent = { tab-width = 2, unit = " " }
186197
@@ -190,5 +201,6 @@ indent = { tab-width = 2, unit = " " }
190201# injection-regex = "haskell"
191202# file-types = ["hs"]
192203# roots = []
204+# comment_token = "--"
193205#
194206# indent = { tab-width = 2, unit = " " }