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

Made toggle_comments language dependent #463

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 5 additions & 5 deletions helix-core/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Change> = 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()));

Expand Down Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions helix-core/src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ where
highlight_config: OnceCell::new(),
//
roots: vec![],
comment_token: None,
auto_format: false,
language_server: None,
indent: Some(IndentationConfiguration {
Expand Down
1 change: 1 addition & 0 deletions helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct LanguageConfiguration {
pub scope: String, // source.rust
pub file_types: Vec<String>, // filename ends_with? <Gemfile, rb, etc>
pub roots: Vec<String>, // these indicate project roots <.git, Cargo.toml>
pub comment_token: Option<String>,

#[serde(default)]
pub auto_format: bool,
Expand Down
6 changes: 5 additions & 1 deletion helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ injection-regex = "rust"
file-types = ["rs"]
roots = []
auto-format = true
comment_token = "//"

language-server = { command = "rust-analyzer" }
indent = { tab-width = 4, unit = " " }
Expand All @@ -15,6 +16,7 @@ scope = "source.toml"
injection-regex = "toml"
file-types = ["toml"]
roots = []
comment_token = "#"

indent = { tab-width = 2, unit = " " }

Expand Down Expand Up @@ -42,6 +44,7 @@ scope = "source.c"
injection-regex = "c"
file-types = ["c"] # TODO: ["h"]
roots = []
comment_token = "//"

language-server = { command = "clangd" }
indent = { tab-width = 2, unit = " " }
Expand All @@ -52,6 +55,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 = " " }
Expand All @@ -63,6 +67,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?
Expand All @@ -74,6 +79,7 @@ scope = "source.js"
injection-regex = "^(js|javascript)$"
file-types = ["js"]
roots = []
comment_token = "//"
# TODO: highlights-jsx, highlights-params

indent = { tab-width = 2, unit = " " }
Expand Down Expand Up @@ -113,6 +119,7 @@ scope = "source.python"
injection-regex = "python"
file-types = ["py"]
roots = []
comment_token = "#"

language-server = { command = "pyls" }
# TODO: pyls needs utf-8 offsets
Expand All @@ -133,6 +140,7 @@ scope = "source.ruby"
injection-regex = "ruby"
file-types = ["rb"]
roots = []
comment_token = "#"

language-server = { command = "solargraph", args = ["stdio"] }
indent = { tab-width = 2, unit = " " }
Expand All @@ -143,6 +151,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 = " " }
Expand All @@ -162,6 +171,7 @@ scope = "source.tex"
injection-regex = "tex"
file-types = ["tex"]
roots = []
comment_token = "%"

indent = { tab-width = 4, unit = "\t" }

Expand All @@ -171,6 +181,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 = " " }

Expand All @@ -180,5 +191,6 @@ indent = { tab-width = 2, unit = " " }
# injection-regex = "haskell"
# file-types = ["hs"]
# roots = []
# comment_token = "--"
#
# indent = { tab-width = 2, unit = " " }