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

continue-comment: avoid continue comment for shebangs #12663

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion helix-core/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I> {
iter: I,
Expand Down
48 changes: 39 additions & 9 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<Regex> =
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_some())
.unwrap_or(false);

if is_not_shebang
&& comment_continuation == CommentContinuation::Enabled
&& config.continue_comments
Copy link
Contributor Author

@TornaxO7 TornaxO7 Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit unsure about this part of the code. Is it fine if I move that into a private function within this module?

{
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
Expand Down Expand Up @@ -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<Regex> =
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_some())
.unwrap_or(false);

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| {
Expand Down
Loading