@@ -812,88 +812,106 @@ pub fn handle_code_lens(
812812 params : lsp_types:: CodeLensParams ,
813813) -> Result < Option < Vec < CodeLens > > > {
814814 let _p = profile ( "handle_code_lens" ) ;
815- let file_id = from_proto:: file_id ( & world, & params. text_document . uri ) ?;
816- let line_index = world. analysis ( ) . file_line_index ( file_id) ?;
817-
818815 let mut lenses: Vec < CodeLens > = Default :: default ( ) ;
819816
817+ if world. config . lens . none ( ) {
818+ // early return before any db query!
819+ return Ok ( Some ( lenses) ) ;
820+ }
821+
822+ let file_id = from_proto:: file_id ( & world, & params. text_document . uri ) ?;
823+ let line_index = world. analysis ( ) . file_line_index ( file_id) ?;
820824 let cargo_spec = CargoTargetSpec :: for_file ( & world, file_id) ?;
821- // Gather runnables
822- for runnable in world. analysis ( ) . runnables ( file_id) ? {
823- let title = match & runnable. kind {
824- RunnableKind :: Test { .. } | RunnableKind :: TestMod { .. } => "▶\u{fe0e} Run Test" ,
825- RunnableKind :: DocTest { .. } => "▶\u{fe0e} Run Doctest" ,
826- RunnableKind :: Bench { .. } => "Run Bench" ,
827- RunnableKind :: Bin => {
828- // Do not suggest binary run on other target than binary
829- match & cargo_spec {
830- Some ( spec) => match spec. target_kind {
831- TargetKind :: Bin => "Run" ,
832- _ => continue ,
833- } ,
834- None => continue ,
825+
826+ if world. config . lens . runnable ( ) {
827+ // Gather runnables
828+ for runnable in world. analysis ( ) . runnables ( file_id) ? {
829+ let ( run_title, debugee ) = match & runnable. kind {
830+ RunnableKind :: Test { .. } | RunnableKind :: TestMod { .. } => ( "▶️\u{fe0e} Run Test" , true ) ,
831+ RunnableKind :: DocTest { .. } => {
832+ // cargo does not support -no-run for doctests
833+ ( "▶️\u{fe0e} Run Doctest" , false )
835834 }
835+ RunnableKind :: Bench { .. } => {
836+ // Nothing wrong with bench debugging
837+ ( "Run Bench" , true )
838+ } ,
839+ RunnableKind :: Bin => {
840+ // Do not suggest binary run on other target than binary
841+ match & cargo_spec {
842+ Some ( spec) => match spec. target_kind {
843+ TargetKind :: Bin => ( "Run" , true ) ,
844+ _ => continue ,
845+ } ,
846+ None => continue ,
847+ }
848+ }
849+ } ;
850+
851+ let mut r = to_lsp_runnable ( & world, file_id, runnable) ?;
852+ if world. config . lens . run {
853+ let lens = CodeLens {
854+ range : r. range ,
855+ command : Some ( Command {
856+ title : run_title. to_string ( ) ,
857+ command : "rust-analyzer.runSingle" . into ( ) ,
858+ arguments : Some ( vec ! [ to_value( & r) . unwrap( ) ] ) ,
859+ } ) ,
860+ data : None ,
861+ } ;
862+ lenses. push ( lens) ;
836863 }
837- }
838- . to_string ( ) ;
839- let mut r = to_lsp_runnable ( & world, file_id, runnable) ?;
840- let lens = CodeLens {
841- range : r. range ,
842- command : Some ( Command {
843- title,
844- command : "rust-analyzer.runSingle" . into ( ) ,
845- arguments : Some ( vec ! [ to_value( & r) . unwrap( ) ] ) ,
846- } ) ,
847- data : None ,
848- } ;
849- lenses. push ( lens) ;
850864
851- if r. args [ 0 ] == "run" {
852- r. args [ 0 ] = "build" . into ( ) ;
853- } else {
854- r. args . push ( "--no-run" . into ( ) ) ;
865+ if debugee && world. config . lens . debug {
866+ if r. args [ 0 ] == "run" {
867+ r. args [ 0 ] = "build" . into ( ) ;
868+ } else {
869+ r. args . push ( "--no-run" . into ( ) ) ;
870+ }
871+ let debug_lens = CodeLens {
872+ range : r. range ,
873+ command : Some ( Command {
874+ title : "Debug" . into ( ) ,
875+ command : "rust-analyzer.debugSingle" . into ( ) ,
876+ arguments : Some ( vec ! [ to_value( r) . unwrap( ) ] ) ,
877+ } ) ,
878+ data : None ,
879+ } ;
880+ lenses. push ( debug_lens) ;
881+ }
855882 }
856- let debug_lens = CodeLens {
857- range : r. range ,
858- command : Some ( Command {
859- title : "Debug" . into ( ) ,
860- command : "rust-analyzer.debugSingle" . into ( ) ,
861- arguments : Some ( vec ! [ to_value( r) . unwrap( ) ] ) ,
862- } ) ,
863- data : None ,
864- } ;
865- lenses. push ( debug_lens) ;
866883 }
867884
868- // Handle impls
869- lenses. extend (
870- world
871- . analysis ( )
872- . file_structure ( file_id) ?
873- . into_iter ( )
874- . filter ( |it| match it. kind {
875- SyntaxKind :: TRAIT_DEF | SyntaxKind :: STRUCT_DEF | SyntaxKind :: ENUM_DEF => true ,
876- _ => false ,
877- } )
878- . map ( |it| {
879- let range = to_proto:: range ( & line_index, it. node_range ) ;
880- let pos = range. start ;
881- let lens_params = lsp_types:: request:: GotoImplementationParams {
882- text_document_position_params : lsp_types:: TextDocumentPositionParams :: new (
883- params. text_document . clone ( ) ,
884- pos,
885- ) ,
886- work_done_progress_params : Default :: default ( ) ,
887- partial_result_params : Default :: default ( ) ,
888- } ;
889- CodeLens {
890- range,
891- command : None ,
892- data : Some ( to_value ( CodeLensResolveData :: Impls ( lens_params) ) . unwrap ( ) ) ,
893- }
894- } ) ,
895- ) ;
896-
885+ if world. config . lens . impementations {
886+ // Handle impls
887+ lenses. extend (
888+ world
889+ . analysis ( )
890+ . file_structure ( file_id) ?
891+ . into_iter ( )
892+ . filter ( |it| match it. kind {
893+ SyntaxKind :: TRAIT_DEF | SyntaxKind :: STRUCT_DEF | SyntaxKind :: ENUM_DEF => true ,
894+ _ => false ,
895+ } )
896+ . map ( |it| {
897+ let range = to_proto:: range ( & line_index, it. node_range ) ;
898+ let pos = range. start ;
899+ let lens_params = lsp_types:: request:: GotoImplementationParams {
900+ text_document_position_params : lsp_types:: TextDocumentPositionParams :: new (
901+ params. text_document . clone ( ) ,
902+ pos,
903+ ) ,
904+ work_done_progress_params : Default :: default ( ) ,
905+ partial_result_params : Default :: default ( ) ,
906+ } ;
907+ CodeLens {
908+ range,
909+ command : None ,
910+ data : Some ( to_value ( CodeLensResolveData :: Impls ( lens_params) ) . unwrap ( ) ) ,
911+ }
912+ } ) ,
913+ ) ;
914+ }
897915 Ok ( Some ( lenses) )
898916}
899917
0 commit comments