Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comment-end-token language config option #6202

Closed
Closed
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
1 change: 1 addition & 0 deletions book/src/languages.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ These configuration keys are available:
| `auto-format` | Whether to autoformat this language when saving |
| `diagnostic-severity` | Minimal severity of diagnostic for it to be displayed. (Allowed values: `Error`, `Warning`, `Info`, `Hint`) |
| `comment-token` | The token to use as a comment-token |
| `comment-end-token` | The token to use to end comments (in languages with no line comments) |
| `indent` | The indent to use. Has sub keys `unit` (the text inserted into the document when indenting; usually set to N spaces or `"\t"` for tabs) and `tab-width` (the number of spaces rendered for a tab) |
| `language-server` | The Language Server to run. See the Language Server configuration section below. |
| `config` | Language Server configuration |
Expand Down
39 changes: 34 additions & 5 deletions helix-core/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,19 @@ fn find_line_comment(
}

#[must_use]
pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&str>) -> Transaction {
pub fn toggle_line_comments(
doc: &Rope,
selection: &Selection,
token: Option<&str>,
end_token: Option<&str>,
) -> Transaction {
let text = doc.slice(..);

let token = token.unwrap_or("//");
let comment = Tendril::from(format!("{} ", token));

let end_comment = end_token.map(|t| Tendril::from(format!(" {}", t)));

let mut lines: Vec<usize> = Vec::with_capacity(selection.len());

let mut min_next_line = 0;
Expand All @@ -81,13 +88,20 @@ pub fn toggle_line_comments(doc: &Rope, selection: &Selection, token: Option<&st

for line in to_change {
let pos = text.line_to_char(line) + min;
let end_pos = pos + text.line(line).len_chars() - min - 1 /* skip \n*/;

if !commented {
// comment line
changes.push((pos, pos, Some(comment.clone())));
if let Some(end_comment) = &end_comment {
changes.push((end_pos, end_pos, Some(end_comment.clone())));
}
} else {
// uncomment line
changes.push((pos, pos + token.len() + margin, None));
if let Some(end_token) = &end_token {
changes.push((end_pos - end_token.len() - margin, end_pos, None));
}
}
}

Expand All @@ -112,14 +126,14 @@ mod test {
assert_eq!(res, (false, vec![0, 2], 2, 0));

// comment
let transaction = toggle_line_comments(&doc, &selection, None);
let transaction = toggle_line_comments(&doc, &selection, None, None);
transaction.apply(&mut doc);
selection = selection.map(transaction.changes());

assert_eq!(doc, " // 1\n\n // 2\n // 3");

// uncomment
let transaction = toggle_line_comments(&doc, &selection, None);
let transaction = toggle_line_comments(&doc, &selection, None, None);
transaction.apply(&mut doc);
selection = selection.map(transaction.changes());
assert_eq!(doc, " 1\n\n 2\n 3");
Expand All @@ -130,7 +144,7 @@ mod test {
// reset the selection.
selection = Selection::single(0, doc.len_chars() - 1);

let transaction = toggle_line_comments(&doc, &selection, None);
let transaction = toggle_line_comments(&doc, &selection, None, None);
transaction.apply(&mut doc);
selection = selection.map(transaction.changes());
assert_eq!(doc, " 1\n\n 2\n 3");
Expand All @@ -141,12 +155,27 @@ mod test {
// reset the selection.
selection = Selection::single(0, doc.len_chars() - 1);

let transaction = toggle_line_comments(&doc, &selection, None);
let transaction = toggle_line_comments(&doc, &selection, None, None);
transaction.apply(&mut doc);
selection = selection.map(transaction.changes());
assert_eq!(doc, "");
assert!(selection.len() == 1); // to ignore the selection unused warning

// TODO: account for uncommenting with uneven comment indentation

// comment and uncomment with end of comment tokens
doc = Rope::from("1 2\n 3\n");
selection = Selection::single(0, doc.len_chars() - 1);

let transaction = toggle_line_comments(&doc, &selection, Some("/*"), Some("*/"));
transaction.apply(&mut doc);
selection = selection.map(transaction.changes());
assert_eq!(doc, "/* 1 2 */\n/* 3 */\n");

let transaction = toggle_line_comments(&doc, &selection, Some("/*"), Some("*/"));
transaction.apply(&mut doc);
selection = selection.map(transaction.changes());
assert_eq!(doc, "1 2\n 3\n");
assert!(selection.len() == 1); // to ignore the selection unused warning
}
}
1 change: 1 addition & 0 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub struct LanguageConfiguration {
pub shebangs: Vec<String>, // interpreter(s) associated with language
pub roots: Vec<String>, // these indicate project roots <.git, Cargo.toml>
pub comment_token: Option<String>,
pub comment_end_token: Option<String>,
pub max_line_length: Option<usize>,

#[serde(default, skip_serializing, deserialize_with = "deserialize_lsp_config")]
Expand Down
9 changes: 8 additions & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4221,7 +4221,14 @@ fn toggle_comments(cx: &mut Context) {
.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);

let end_token = doc
.language_config()
.and_then(|lc| lc.comment_end_token.as_ref())
.map(|tc| tc.as_ref());

let transaction =
comment::toggle_line_comments(doc.text(), doc.selection(view.id), token, end_token);

doc.apply(&transaction, view.id);
exit_select_mode(cx);
Expand Down
7 changes: 5 additions & 2 deletions languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,8 @@ injection-regex = "ocaml"
file-types = ["ml"]
shebangs = []
roots = []
comment-token = "(**)"
comment-token = "(*"
comment-end-token = "*)"
language-server = { command = "ocamllsp" }
indent = { tab-width = 2, unit = " " }

Expand All @@ -721,7 +722,8 @@ scope = "source.ocaml.interface"
file-types = ["mli"]
shebangs = []
roots = []
comment-token = "(**)"
comment-token = "(*"
comment-end-token = "*)"
language-server = { command = "ocamllsp" }
indent = { tab-width = 2, unit = " " }

Expand Down Expand Up @@ -1870,6 +1872,7 @@ scope = "source.sml"
injection-regex = "sml"
file-types = ["sml"]
comment-token = "(*"
comment-end-token = "*)"
roots = []

[[grammar]]
Expand Down