@@ -247,7 +247,8 @@ impl MappableCommand {
247
247
extend_search_prev, "Add previous search match to selection" ,
248
248
search_selection, "Use current selection as search pattern" ,
249
249
global_search, "Global search in workspace folder" ,
250
- extend_line, "Select current line, if already selected, extend to next line" ,
250
+ extend_line, "Select current line, if already selected, extend to another line based on the anchor" ,
251
+ extend_line_below, "Select current line, if already selected, extend to next line" ,
251
252
extend_line_above, "Select current line, if already selected, extend to previous line" ,
252
253
extend_to_line_bounds, "Extend selection to line bounds" ,
253
254
shrink_to_line_bounds, "Shrink selection to line bounds" ,
@@ -1945,6 +1946,15 @@ enum Extend {
1945
1946
}
1946
1947
1947
1948
fn extend_line ( cx : & mut Context ) {
1949
+ let ( view, doc) = current_ref ! ( cx. editor) ;
1950
+ let extend = match doc. selection ( view. id ) . primary ( ) . direction ( ) {
1951
+ Direction :: Forward => Extend :: Below ,
1952
+ Direction :: Backward => Extend :: Above ,
1953
+ } ;
1954
+ extend_line_impl ( cx, extend) ;
1955
+ }
1956
+
1957
+ fn extend_line_below ( cx : & mut Context ) {
1948
1958
extend_line_impl ( cx, Extend :: Below ) ;
1949
1959
}
1950
1960
@@ -1960,20 +1970,32 @@ fn extend_line_impl(cx: &mut Context, extend: Extend) {
1960
1970
let selection = doc. selection ( view. id ) . clone ( ) . transform ( |range| {
1961
1971
let ( start_line, end_line) = range. line_range ( text. slice ( ..) ) ;
1962
1972
1963
- let start = text. line_to_char ( start_line) ;
1964
- let end = text. line_to_char ( ( end_line + count) . min ( text. len_lines ( ) ) ) ;
1973
+ let start = text. line_to_char ( match extend {
1974
+ Extend :: Above => start_line. saturating_sub ( count) ,
1975
+ Extend :: Below => start_line,
1976
+ } ) ;
1977
+ let end = text. line_to_char (
1978
+ match extend {
1979
+ Extend :: Above => end_line + 1 , // the start of next line
1980
+ Extend :: Below => ( end_line + count) ,
1981
+ }
1982
+ . min ( text. len_lines ( ) ) ,
1983
+ ) ;
1965
1984
1966
1985
// extend to previous/next line if current line is selected
1967
1986
let ( anchor, head) = if range. from ( ) == start && range. to ( ) == end {
1968
1987
match extend {
1969
- Extend :: Above => ( end, text. line_to_char ( start_line. saturating_sub ( 1 ) ) ) ,
1988
+ Extend :: Above => ( end, text. line_to_char ( start_line. saturating_sub ( count + 1 ) ) ) ,
1970
1989
Extend :: Below => (
1971
1990
start,
1972
1991
text. line_to_char ( ( end_line + count + 1 ) . min ( text. len_lines ( ) ) ) ,
1973
1992
) ,
1974
1993
}
1975
1994
} else {
1976
- ( start, end)
1995
+ match extend {
1996
+ Extend :: Above => ( end, start) ,
1997
+ Extend :: Below => ( start, end) ,
1998
+ }
1977
1999
} ;
1978
2000
1979
2001
Range :: new ( anchor, head)
0 commit comments