on master help sign in
Made toggle_comments language dependent (#463)
cd65a48Cor Peters
helix-core/src/comment.rs+5−5
@@ -38,18 +38,18 @@ fn find_line_comment(
38}
38}
39
39
40#[must_use]
40#[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 {
42 let text = doc.slice(..);
42 let text = doc.slice(..);
43 let mut changes: Vec<Change> = Vec::new();
43 let mut changes: Vec<Change> = Vec::new();
44
44
45− let token = "//";
45+ let token = token.unwrap_or("//");
46 let comment = Tendril::from(format!("{} ", token));
46 let comment = Tendril::from(format!("{} ", token));
47
47
48 for selection in selection {
48 for selection in selection {
49 let start = text.char_to_line(selection.from());
49 let start = text.char_to_line(selection.from());
50 let end = text.char_to_line(selection.to());
50 let end = text.char_to_line(selection.to());
51 let lines = start..end + 1;
51 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());
53
53
54 changes.reserve((end - start).saturating_sub(skipped.len()));
54 changes.reserve((end - start).saturating_sub(skipped.len()));
55
55
@@ -95,14 +95,14 @@ mod test {
95 assert_eq!(res, (false, vec![1], 2));
95 assert_eq!(res, (false, vec![1], 2));
96
96
97 // comment
97 // comment
98− let transaction = toggle_line_comments(&state.doc, &state.selection);
98+ let transaction = toggle_line_comments(&state.doc, &state.selection, None);
99 transaction.apply(&mut state.doc);
99 transaction.apply(&mut state.doc);
100 state.selection = state.selection.clone().map(transaction.changes());
100 state.selection = state.selection.clone().map(transaction.changes());
101
101
102 assert_eq!(state.doc, " // 1\n\n // 2\n // 3");
102 assert_eq!(state.doc, " // 1\n\n // 2\n // 3");
103
103
104 // uncomment
104 // uncomment
105− let transaction = toggle_line_comments(&state.doc, &state.selection);
105+ let transaction = toggle_line_comments(&state.doc, &state.selection, None);
106 transaction.apply(&mut state.doc);
106 transaction.apply(&mut state.doc);
107 state.selection = state.selection.clone().map(transaction.changes());
107 state.selection = state.selection.clone().map(transaction.changes());
108 assert_eq!(state.doc, " 1\n\n 2\n 3");
108 assert_eq!(state.doc, " 1\n\n 2\n 3");
helix-core/src/indent.rs+1
@@ -265,6 +265,7 @@ where
265 config: None,
265 config: None,
266 //
266 //
267 roots: vec![],
267 roots: vec![],
268+ comment_token: None,
268 auto_format: false,
269 auto_format: false,
269 language_server: None,
270 language_server: None,
270 indent: Some(IndentationConfiguration {
271 indent: Some(IndentationConfiguration {
helix-core/src/syntax.rs+1
@@ -35,6 +35,7 @@ pub struct LanguageConfiguration {
35 pub scope: String, // source.rust
35 pub scope: String, // source.rust
36 pub file_types: Vec<String>, // filename ends_with? <Gemfile, rb, etc>
36 pub file_types: Vec<String>, // filename ends_with? <Gemfile, rb, etc>
37 pub roots: Vec<String>, // these indicate project roots <.git, Cargo.toml>
37 pub roots: Vec<String>, // these indicate project roots <.git, Cargo.toml>
38+ pub comment_token: Option<String>,
38 pub config: Option<String>,
39 pub config: Option<String>,
39
40
40 #[serde(default)]
41 #[serde(default)]
helix-term/src/commands.rs+5−1
@@ -3399,7 +3399,11 @@ fn hover(cx: &mut Context) {
3399// comments
3399// comments
3400fn toggle_comments(cx: &mut Context) {
3400fn toggle_comments(cx: &mut Context) {
3401 let (view, doc) = current!(cx.editor);
3401 let (view, doc) = current!(cx.editor);
3402+ let token = doc
3403+ .language_config()
3404+ .and_then(|lc| lc.comment_token.as_ref())
3405+ .map(|tc| tc.as_ref());
3402− let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id));
3406+ let transaction = comment::toggle_line_comments(doc.text(), doc.selection(view.id), token);
3403
3407
3404 doc.apply(&transaction, view.id);
3408 doc.apply(&transaction, view.id);
3405 doc.append_changes_to_history(view.id);
3409 doc.append_changes_to_history(view.id);
languages.toml+12
@@ -5,6 +5,7 @@ injection-regex = "rust"
5file-types = ["rs"]
5file-types = ["rs"]
6roots = []
6roots = []
7auto-format = true
7auto-format = true
8+comment_token = "//"
8config = """
9config = """
9{
10{
10 "cargo": {
11 "cargo": {
@@ -25,6 +26,7 @@ scope = "source.toml"
25injection-regex = "toml"
26injection-regex = "toml"
26file-types = ["toml"]
27file-types = ["toml"]
27roots = []
28roots = []
29+comment_token = "#"
28
30
29indent = { tab-width = 2, unit = " " }
31indent = { tab-width = 2, unit = " " }
30
32
@@ -52,6 +54,7 @@ scope = "source.c"
52injection-regex = "c"
54injection-regex = "c"
53file-types = ["c"] # TODO: ["h"]
55file-types = ["c"] # TODO: ["h"]
54roots = []
56roots = []
57+comment_token = "//"
55
58
56language-server = { command = "clangd" }
59language-server = { command = "clangd" }
57indent = { tab-width = 2, unit = " " }
60indent = { tab-width = 2, unit = " " }
@@ -62,6 +65,7 @@ scope = "source.cpp"
62injection-regex = "cpp"
65injection-regex = "cpp"
63file-types = ["cc", "cpp", "hpp", "h"]
66file-types = ["cc", "cpp", "hpp", "h"]
64roots = []
67roots = []
68+comment_token = "//"
65
69
66language-server = { command = "clangd" }
70language-server = { command = "clangd" }
67indent = { tab-width = 2, unit = " " }
71indent = { tab-width = 2, unit = " " }
@@ -73,6 +77,7 @@ injection-regex = "go"
73file-types = ["go"]
77file-types = ["go"]
74roots = ["Gopkg.toml", "go.mod"]
78roots = ["Gopkg.toml", "go.mod"]
75auto-format = true
79auto-format = true
80+comment_token = "//"
76
81
77language-server = { command = "gopls" }
82language-server = { command = "gopls" }
78# TODO: gopls needs utf-8 offsets?
83# TODO: gopls needs utf-8 offsets?
@@ -84,6 +89,7 @@ scope = "source.js"
84injection-regex = "^(js|javascript)$"
89injection-regex = "^(js|javascript)$"
85file-types = ["js"]
90file-types = ["js"]
86roots = []
91roots = []
92+comment_token = "//"
87# TODO: highlights-jsx, highlights-params
93# TODO: highlights-jsx, highlights-params
88
94
89indent = { tab-width = 2, unit = " " }
95indent = { tab-width = 2, unit = " " }
@@ -123,6 +129,7 @@ scope = "source.python"
123injection-regex = "python"
129injection-regex = "python"
124file-types = ["py"]
130file-types = ["py"]
125roots = []
131roots = []
132+comment_token = "#"
126
133
127language-server = { command = "pyls" }
134language-server = { command = "pyls" }
128# TODO: pyls needs utf-8 offsets
135# TODO: pyls needs utf-8 offsets
@@ -143,6 +150,7 @@ scope = "source.ruby"
143injection-regex = "ruby"
150injection-regex = "ruby"
144file-types = ["rb"]
151file-types = ["rb"]
145roots = []
152roots = []
153+comment_token = "#"
146
154
147language-server = { command = "solargraph", args = ["stdio"] }
155language-server = { command = "solargraph", args = ["stdio"] }
148indent = { tab-width = 2, unit = " " }
156indent = { tab-width = 2, unit = " " }
@@ -153,6 +161,7 @@ scope = "source.bash"
153injection-regex = "bash"
161injection-regex = "bash"
154file-types = ["sh", "bash"]
162file-types = ["sh", "bash"]
155roots = []
163roots = []
164+comment_token = "#"
156
165
157language-server = { command = "bash-language-server", args = ["start"] }
166language-server = { command = "bash-language-server", args = ["start"] }
158indent = { tab-width = 2, unit = " " }
167indent = { tab-width = 2, unit = " " }
@@ -172,6 +181,7 @@ scope = "source.tex"
172injection-regex = "tex"
181injection-regex = "tex"
173file-types = ["tex"]
182file-types = ["tex"]
174roots = []
183roots = []
184+comment_token = "%"
175
185
176indent = { tab-width = 4, unit = "\t" }
186indent = { tab-width = 4, unit = "\t" }
177
187
@@ -181,6 +191,7 @@ scope = "source.julia"
181injection-regex = "julia"
191injection-regex = "julia"
182file-types = ["jl"]
192file-types = ["jl"]
183roots = []
193roots = []
194+comment_token = "#"
184language-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);" ] }
195language-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);" ] }
185indent = { tab-width = 2, unit = " " }
196indent = { tab-width = 2, unit = " " }
186
197
@@ -190,5 +201,6 @@ indent = { tab-width = 2, unit = " " }
190# injection-regex = "haskell"
201# injection-regex = "haskell"
191# file-types = ["hs"]
202# file-types = ["hs"]
192# roots = []
203# roots = []
204+# comment_token = "--"
193#
205#
194# indent = { tab-width = 2, unit = " " }
206# indent = { tab-width = 2, unit = " " }