diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 852a975277db..577535431c63 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -2555,7 +2555,7 @@ pub enum InjectionLanguageMarker<'a> { Shebang(RopeSlice<'a>), } -const SHEBANG: &str = r"#!\s*(?:\S*[/\\](?:env\s+(?:\-\S+\s+)*)?)?([^\s\.\d]+)"; +pub const SHEBANG: &str = r"#!\s*(?:\S*[/\\](?:env\s+(?:\-\S+\s+)*)?)?([^\s\.\d]+)"; pub struct Merge { iter: I, diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index e49d752b56ad..ec2d268d8111 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -31,7 +31,7 @@ use helix_core::{ regex::{self, Regex}, search::{self, CharMatcher}, selection, shellwords, surround, - syntax::{BlockCommentToken, LanguageServerFeature}, + syntax::{BlockCommentToken, LanguageServerFeature, SHEBANG}, text_annotations::{Overlay, TextAnnotations}, textobject, unicode::width::UnicodeWidthChar, @@ -3551,13 +3551,29 @@ fn open(cx: &mut Context, open: Open, comment_continuation: CommentContinuation) let mut ranges = SmallVec::with_capacity(selection.len()); - let continue_comment_tokens = - if comment_continuation == CommentContinuation::Enabled && config.continue_comments { + let continue_comment_tokens = { + static SHEBANG_REGEX: Lazy = + Lazy::new(|| Regex::new(&["^", SHEBANG].concat()).unwrap()); + + let curr_line_num = selection.primary().cursor_line(text); + let line = text.line(curr_line_num); + + let is_not_shebang = curr_line_num > 0 + || line + .as_str() + .map(|line| SHEBANG_REGEX.captures(line).is_none()) + .unwrap_or(true); + + if is_not_shebang + && comment_continuation == CommentContinuation::Enabled + && config.continue_comments + { doc.language_config() .and_then(|config| config.comment_tokens.as_ref()) } else { None - }; + } + }; let mut transaction = Transaction::change_by_selection(contents, selection, |range| { // the line number, where the cursor is currently @@ -4069,11 +4085,25 @@ pub mod insert { let mut global_offs = 0; let mut new_text = String::new(); - let continue_comment_tokens = if config.continue_comments { - doc.language_config() - .and_then(|config| config.comment_tokens.as_ref()) - } else { - None + let continue_comment_tokens = { + static SHEBANG_REGEX: Lazy = + Lazy::new(|| Regex::new(&["^", SHEBANG].concat()).unwrap()); + + let curr_line_num = selection.primary().cursor_line(text); + let line = text.line(curr_line_num); + + let is_not_shebang = curr_line_num > 0 + || line + .as_str() + .map(|line| SHEBANG_REGEX.captures(line).is_none()) + .unwrap_or(true); + + if is_not_shebang && config.continue_comments { + doc.language_config() + .and_then(|config| config.comment_tokens.as_ref()) + } else { + None + } }; let mut transaction = Transaction::change_by_selection(contents, selection, |range| {