@@ -42,7 +42,7 @@ use helix_core::{
42
42
} ;
43
43
use helix_view:: {
44
44
document:: { FormatterError , Mode , SCRATCH_BUFFER_NAME } ,
45
- editor:: Action ,
45
+ editor:: { Action , Config } ,
46
46
info:: Info ,
47
47
input:: KeyEvent ,
48
48
keyboard:: KeyCode ,
@@ -1668,6 +1668,34 @@ fn switch_to_lowercase(cx: &mut Context) {
1668
1668
} ) ;
1669
1669
}
1670
1670
1671
+ /// Checks, if the line from line `line_num` is commented with one of the comment tokens in the language config and
1672
+ /// returns the token which comments out the line.
1673
+ fn has_line_comment ( config : & Config , doc : & Document , line_num : usize ) -> Option < String > {
1674
+ if config. continue_comments {
1675
+ let line_tokens = {
1676
+ let mut line_tokens: Vec < String > = doc
1677
+ . language_config ( )
1678
+ . and_then ( |lc| lc. comment_tokens . as_ref ( ) ) ?
1679
+ . clone ( ) ;
1680
+ line_tokens. sort_by ( |a, b| a. len ( ) . partial_cmp ( & b. len ( ) ) . unwrap ( ) . reverse ( ) ) ;
1681
+ line_tokens
1682
+ } ;
1683
+
1684
+ let text = doc. text ( ) ;
1685
+ let line = text. line ( line_num) ;
1686
+
1687
+ if let Some ( pos) = line. first_non_whitespace_char ( ) {
1688
+ let text_line = line. slice ( pos..) ;
1689
+ return line_tokens
1690
+ . iter ( )
1691
+ . find ( |& token| text_line. starts_with ( token. as_str ( ) ) )
1692
+ . map ( |token| token. clone ( ) ) ;
1693
+ }
1694
+ }
1695
+
1696
+ None
1697
+ }
1698
+
1671
1699
pub fn scroll ( cx : & mut Context , offset : usize , direction : Direction , sync_cursor : bool ) {
1672
1700
use Direction :: * ;
1673
1701
let config = cx. editor . config ( ) ;
@@ -3417,8 +3445,9 @@ fn open(cx: &mut Context, open: Open) {
3417
3445
text. push_str ( doc. line_ending . as_str ( ) ) ;
3418
3446
text. push_str ( & indent) ;
3419
3447
3420
- if config. continue_comments {
3421
- handle_comment_continue ( doc, & mut text, cursor_line) ;
3448
+ if let Some ( comment_token) = has_line_comment ( & config, & doc, line_num) {
3449
+ text. push_str ( & comment_token) ;
3450
+ text. push ( ' ' ) ;
3422
3451
}
3423
3452
3424
3453
let text = text. repeat ( count) ;
@@ -3445,18 +3474,6 @@ fn open(cx: &mut Context, open: Open) {
3445
3474
goto_line_end_newline ( cx) ;
3446
3475
}
3447
3476
3448
- // Currently only continues single-line comments
3449
- // TODO: Handle block comments as well
3450
- fn handle_comment_continue ( doc : & Document , text : & mut String , cursor_line : usize ) {
3451
- let line = doc. text ( ) . line ( cursor_line) ;
3452
-
3453
- if let Some ( lang_config) = doc. language_config ( ) {
3454
- let comment_tokens = & lang_config. comment_tokens ;
3455
-
3456
- comment:: handle_comment_continue ( & line, text, comment_tokens) ;
3457
- }
3458
- }
3459
-
3460
3477
// o inserts a new line after each line with a selection
3461
3478
fn open_below ( cx : & mut Context ) {
3462
3479
open ( cx, Open :: Below )
@@ -3918,8 +3935,9 @@ pub mod insert {
3918
3935
new_text. push_str ( doc. line_ending . as_str ( ) ) ;
3919
3936
new_text. push_str ( & indent) ;
3920
3937
3921
- if config. continue_comments {
3922
- handle_comment_continue ( doc, & mut new_text, current_line) ;
3938
+ if let Some ( comment_token) = has_line_comment ( & config, & doc, current_line) {
3939
+ new_text. push_str ( & comment_token) ;
3940
+ new_text. push ( ' ' ) ;
3923
3941
}
3924
3942
3925
3943
new_text. chars ( ) . count ( )
0 commit comments