@ -1,5 +1,5 @@
use crate ::compositor ::{ Component , Context , Event , EventResult } ;
use crate ::compositor ::{ Component , Context , Event , EventResult } ;
use helix_view ::{ apply_transaction , editor ::CompleteAction };
use helix_view ::{ apply_transaction , editor ::CompleteAction , ViewId };
use tui ::buffer ::Buffer as Surface ;
use tui ::buffer ::Buffer as Surface ;
use tui ::text ::Spans ;
use tui ::text ::Spans ;
@ -107,6 +107,7 @@ impl Completion {
let menu = Menu ::new ( items , ( ) , move | editor : & mut Editor , item , event | {
let menu = Menu ::new ( items , ( ) , move | editor : & mut Editor , item , event | {
fn item_to_transaction (
fn item_to_transaction (
doc : & Document ,
doc : & Document ,
view_id : ViewId ,
item : & CompletionItem ,
item : & CompletionItem ,
offset_encoding : helix_lsp ::OffsetEncoding ,
offset_encoding : helix_lsp ::OffsetEncoding ,
start_offset : usize ,
start_offset : usize ,
@ -121,9 +122,10 @@ impl Completion {
}
}
} ;
} ;
util ::generate_transaction_from_ edits (
util ::generate_transaction_from_ completion_ edit(
doc . text ( ) ,
doc . text ( ) ,
vec! [ edit ] ,
doc . selection ( view_id ) ,
edit ,
offset_encoding , // TODO: should probably transcode in Client
offset_encoding , // TODO: should probably transcode in Client
)
)
} else {
} else {
@ -132,10 +134,23 @@ impl Completion {
// in these cases we need to check for a common prefix and remove it
// in these cases we need to check for a common prefix and remove it
let prefix = Cow ::from ( doc . text ( ) . slice ( start_offset .. trigger_offset ) ) ;
let prefix = Cow ::from ( doc . text ( ) . slice ( start_offset .. trigger_offset ) ) ;
let text = text . trim_start_matches ::< & str > ( & prefix ) ;
let text = text . trim_start_matches ::< & str > ( & prefix ) ;
Transaction ::change (
doc . text ( ) ,
// TODO: this needs to be true for the numbers to work out correctly
vec! [ ( trigger_offset , trigger_offset , Some ( text . into ( ) ) ) ] . into_iter ( ) ,
// in the closure below. It's passed in to a callback as this same
)
// formula, but can the value change between the LSP request and
// response? If it does, can we recover?
debug_assert! (
doc . selection ( view_id )
. primary ( )
. cursor ( doc . text ( ) . slice ( .. ) )
= = trigger_offset
) ;
Transaction ::change_by_selection ( doc . text ( ) , doc . selection ( view_id ) , | range | {
let cursor = range . cursor ( doc . text ( ) . slice ( .. ) ) ;
( cursor , cursor , Some ( text . into ( ) ) )
} )
} ;
} ;
transaction
transaction
@ -164,6 +179,7 @@ impl Completion {
let transaction = item_to_transaction (
let transaction = item_to_transaction (
doc ,
doc ,
view . id ,
item ,
item ,
offset_encoding ,
offset_encoding ,
start_offset ,
start_offset ,
@ -185,6 +201,7 @@ impl Completion {
let transaction = item_to_transaction (
let transaction = item_to_transaction (
doc ,
doc ,
view . id ,
item ,
item ,
offset_encoding ,
offset_encoding ,
start_offset ,
start_offset ,