@@ -28,14 +28,14 @@ use simplicityhl::{
2828} ;
2929
3030use crate :: completion:: { self , CompletionProvider } ;
31+ use crate :: function:: Functions ;
3132use crate :: utils:: {
3233 find_related_call, get_call_span, get_comments_from_lines, position_to_span, span_to_positions,
3334} ;
3435
3536#[ derive( Debug ) ]
3637struct Document {
37- functions : Vec < parse:: Function > ,
38- functions_docs : HashMap < String , String > ,
38+ functions : Functions ,
3939 text : Rope ,
4040}
4141
@@ -243,18 +243,8 @@ impl Backend {
243243 return None ;
244244 }
245245
246- let mut completions = CompletionProvider :: get_function_completions (
247- & doc. functions
248- . iter ( )
249- . map ( |func| {
250- let function_doc = doc
251- . functions_docs
252- . get ( & func. name ( ) . to_string ( ) )
253- . map_or ( String :: new ( ) , String :: clone) ;
254- ( func. to_owned ( ) , function_doc)
255- } )
256- . collect :: < Vec < _ > > ( ) ,
257- ) ;
246+ let mut completions =
247+ CompletionProvider :: get_function_completions ( & doc. functions . functions_and_docs ( ) ) ;
258248 completions. extend_from_slice ( self . completion_provider . builtins ( ) ) ;
259249 completions. extend_from_slice ( self . completion_provider . modules ( ) ) ;
260250
@@ -269,8 +259,8 @@ impl Backend {
269259 let token_pos = params. text_document_position_params . position ;
270260 let token_span = position_to_span ( token_pos) . ok ( ) ?;
271261
272- let call = find_related_call ( & document. functions , token_span) . ok ( ) ?;
273- let call_span = get_call_span ( call) . ok ( ) ?;
262+ let call = find_related_call ( & document. functions . functions ( ) , token_span) . ok ( ) ?;
263+ let call_span = get_call_span ( & call) . ok ( ) ?;
274264 let ( start, end) = span_to_positions ( & call_span) . ok ( ) ?;
275265
276266 let description = match call. name ( ) {
@@ -289,8 +279,7 @@ impl Backend {
289279 )
290280 }
291281 parse:: CallName :: Custom ( func) => {
292- let function = document. functions . iter ( ) . find ( |f| f. name ( ) == func) ?;
293- let function_doc = document. functions_docs . get ( & func. to_string ( ) ) ?;
282+ let ( function, function_doc) = document. functions . get ( func. as_inner ( ) ) ?;
294283
295284 let template = completion:: function_to_template ( function, function_doc) ;
296285 format ! (
@@ -335,14 +324,11 @@ impl Backend {
335324 let token_position = params. text_document_position_params . position ;
336325 let token_span = position_to_span ( token_position) . ok ( ) ?;
337326
338- let call = find_related_call ( & document. functions , token_span) . ok ( ) ?;
327+ let call = find_related_call ( & document. functions . functions ( ) , token_span) . ok ( ) ?;
339328
340329 match call. name ( ) {
341330 simplicityhl:: parse:: CallName :: Custom ( func) => {
342- let function = document
343- . functions
344- . iter ( )
345- . find ( |function| function. name ( ) == func) ?;
331+ let function = document. functions . get_func ( func. as_inner ( ) ) ?;
346332
347333 let ( start, end) = span_to_positions ( function. as_ref ( ) ) . ok ( ) ?;
348334 Some ( GotoDefinitionResponse :: from ( Location :: new (
@@ -358,8 +344,7 @@ impl Backend {
358344/// Create [`Document`] using parsed program and code.
359345fn create_document ( program : & simplicityhl:: parse:: Program , text : & str ) -> Document {
360346 let mut document = Document {
361- functions : vec ! [ ] ,
362- functions_docs : HashMap :: new ( ) ,
347+ functions : Functions :: new ( ) ,
363348 text : Rope :: from_str ( text) ,
364349 } ;
365350
@@ -376,9 +361,9 @@ fn create_document(program: &simplicityhl::parse::Program, text: &str) -> Docume
376361 . for_each ( |func| {
377362 let start_line = u32:: try_from ( func. as_ref ( ) . start . line . get ( ) ) . unwrap_or_default ( ) - 1 ;
378363
379- document. functions . push ( func. to_owned ( ) ) ;
380- document. functions_docs . insert (
364+ document. functions . insert (
381365 func. name ( ) . to_string ( ) ,
366+ func. to_owned ( ) ,
382367 get_comments_from_lines ( start_line, & document. text ) ,
383368 ) ;
384369 } ) ;
@@ -423,7 +408,6 @@ mod tests {
423408 assert ! ( err. is_none( ) , "Expected no parsing error" ) ;
424409 let doc = doc. expect ( "Expected Some(Document)" ) ;
425410 assert_eq ! ( doc. functions. len( ) , 2 ) ;
426- assert ! ( doc. functions_docs. contains_key( "add" ) ) ;
427411 }
428412
429413 #[ test]
0 commit comments