@@ -35,6 +35,8 @@ use harp::utils::r_assert_type;
3535use harp:: utils:: r_is_function;
3636use harp:: vector:: CharacterVector ;
3737use harp:: vector:: Vector ;
38+ use harp:: RSymbol ;
39+ use harp:: R_ENVS ;
3840use libr:: R_GlobalEnv ;
3941use libr:: Rf_ScalarLogical ;
4042use libr:: ENVSXP ;
@@ -53,17 +55,6 @@ use crate::variables::variable::try_dispatch_view;
5355use crate :: variables:: variable:: PositronVariable ;
5456use crate :: view:: view;
5557
56- /// Enumeration of treatments for the .Last.value variable
57- pub enum LastValue {
58- /// Always show the .Last.value variable in the Variables pane. This is used
59- /// by tests to show the value without changing the global option.
60- Always ,
61-
62- /// Use the value of the global option `positron.show_last_value` to
63- /// determine whether to show the .Last.value variable
64- UseOption ,
65- }
66-
6758/**
6859 * The R Variables handler provides the server side of Positron's Variables panel, and is
6960 * responsible for creating and updating the list of variables.
@@ -90,14 +81,6 @@ pub struct RVariables {
9081 current_bindings : RThreadSafe < Vec < Binding > > ,
9182 version : u64 ,
9283
93- /// Whether to always show the .Last.value in the Variables pane, regardless
94- /// of the value of positron.show_last_value
95- show_last_value : LastValue ,
96-
97- /// Whether we are currently showing the .Last.value variable in the Variables
98- /// pane.
99- showing_last_value : bool ,
100-
10184 /// Whether we need to send an initial `Refresh` event to the frontend,
10285 /// which is required for frontend initialization. Set on construction,
10386 /// cleared after the first `update()` call.
@@ -116,24 +99,6 @@ impl RVariables {
11699 comm : CommSocket ,
117100 comm_event_tx : Sender < CommEvent > ,
118101 iopub_tx : Sender < IOPubMessage > ,
119- ) {
120- // Start with default settings
121- Self :: start_with_config ( env, comm, comm_event_tx, iopub_tx, LastValue :: UseOption ) ;
122- }
123-
124- /**
125- * Creates a new RVariables instance with specific configuration.
126- *
127- * - `env`: An R environment to scan for variables, typically R_GlobalEnv
128- * - `comm`: A channel used to send messages to the frontend
129- * - `show_last_value`: Whether to include .Last.value in the variables list
130- */
131- pub fn start_with_config (
132- env : RObject ,
133- comm : CommSocket ,
134- comm_event_tx : Sender < CommEvent > ,
135- iopub_tx : Sender < IOPubMessage > ,
136- show_last_value : LastValue ,
137102 ) {
138103 // Validate that the RObject we were passed is actually an environment
139104 if let Err ( err) = r_assert_type ( env. sexp , & [ ENVSXP ] ) {
@@ -159,8 +124,6 @@ impl RVariables {
159124 env,
160125 current_bindings,
161126 version: 0 ,
162- show_last_value,
163- showing_last_value: false ,
164127 needs_initial_refresh: true ,
165128 } ;
166129 environment. execution_thread( ) ;
@@ -256,10 +219,6 @@ impl RVariables {
256219 fn list_variables ( & mut self ) -> Vec < Variable > {
257220 let mut variables: Vec < Variable > = vec ! [ ] ;
258221 r_task ( || {
259- if let Some ( last_value) = self . last_value ( ) {
260- variables. push ( last_value. var ( ) ) ;
261- }
262-
263222 for binding in self . current_bindings . get ( ) {
264223 variables. push ( PositronVariable :: new ( binding) . var ( ) ) ;
265224 }
@@ -526,45 +485,21 @@ impl RVariables {
526485 }
527486 }
528487
529- /// Gets the value of the special variable '.Last.value' (the value of the
530- /// last expression evaluated at the top level), if enabled.
531- ///
532- /// Returns None in all other cases.
533- fn last_value ( & self ) -> Option < PositronVariable > {
534- // Check the cached value first
535- let show_last_value = match self . show_last_value {
536- LastValue :: Always => true ,
537- LastValue :: UseOption => {
538- // If we aren't always showing the last value, update from the
539- // global option
540- let use_last_value = get_option ( "positron.show_last_value" ) ;
541- match use_last_value. get_bool ( 0 ) {
542- Ok ( Some ( true ) ) => true ,
543- _ => false ,
544- }
545- } ,
546- } ;
547-
548- if show_last_value {
549- match harp:: environment:: last_value ( ) {
550- Ok ( last_robj) => Some ( PositronVariable :: from (
551- String :: from ( ".Last.value" ) ,
552- String :: from ( ".Last.value" ) ,
553- last_robj. sexp ,
554- ) ) ,
555- Err ( err) => {
556- // This isn't a critical error but would also be very
557- // unexpected.
558- log:: error!( "Variables: Could not evaluate .Last.value ({err:?})" ) ;
559- None
560- } ,
561- }
562- } else {
563- // Last value display is disabled
564- None
488+ /// Determines whether or not we should be showing the special `.Last.value` object
489+ fn show_last_value ( & self ) -> bool {
490+ match get_option ( "positron.show_last_value" ) . get_bool ( 0 ) {
491+ Ok ( Some ( true ) ) => true ,
492+ _ => false ,
565493 }
566494 }
567495
496+ fn last_value ( ) -> harp:: Result < Binding > {
497+ Binding :: new (
498+ & Environment :: view ( R_ENVS . base ) ,
499+ RSymbol :: from ( ".Last.value" ) ,
500+ )
501+ }
502+
568503 #[ tracing:: instrument( level = "trace" , skip_all) ]
569504 fn update ( & mut self ) {
570505 r_task ( || {
@@ -580,10 +515,6 @@ impl RVariables {
580515
581516 let mut variables: Vec < Variable > = vec ! [ ] ;
582517
583- if let ( Some ( var) , _) = self . track_last_value ( ) {
584- variables. push ( var) ;
585- }
586-
587518 for binding in self . current_bindings . get ( ) {
588519 variables. push ( PositronVariable :: new ( binding) . var ( ) ) ;
589520 }
@@ -602,12 +533,6 @@ impl RVariables {
602533 let mut assigned: Vec < Variable > = vec ! [ ] ;
603534 let mut removed: Vec < String > = vec ! [ ] ;
604535
605- match self . track_last_value ( ) {
606- ( Some ( var) , _) => assigned. push ( var) ,
607- ( None , true ) => removed. push ( ".Last.value" . to_string ( ) ) ,
608- ( None , false ) => { } ,
609- }
610-
611536 let new_bindings = self . bindings ( ) ;
612537
613538 let mut old_iter = self . current_bindings . get ( ) . iter ( ) ;
@@ -684,20 +609,6 @@ impl RVariables {
684609
685610 // SAFETY: The following methods must be called in an `r_task()`
686611
687- /// Updates `self.showing_last_value` and returns the last value variable
688- /// if it should be shown, along with whether it was previously shown.
689- fn track_last_value ( & mut self ) -> ( Option < Variable > , bool ) {
690- let was_showing = self . showing_last_value ;
691-
692- if let Some ( last_value) = self . last_value ( ) {
693- self . showing_last_value = true ;
694- ( Some ( last_value. var ( ) ) , was_showing)
695- } else {
696- self . showing_last_value = false ;
697- ( None , was_showing)
698- }
699- }
700-
701612 /// Updates `self.env` to the current effective environment if it changed
702613 /// (e.g. entering or exiting debug mode). Returns `true` if switched.
703614 fn update_env ( & mut self ) -> bool {
@@ -721,6 +632,12 @@ impl RVariables {
721632
722633 let mut bindings: Vec < Binding > = env. iter ( ) . filter_map ( |b| b. ok ( ) ) . collect ( ) ;
723634
635+ if self . show_last_value ( ) {
636+ if let Some ( last_value) = Self :: last_value ( ) . log_err ( ) {
637+ bindings. push ( last_value) ;
638+ }
639+ }
640+
724641 bindings. sort_by ( |a, b| a. name . cmp ( & b. name ) ) ;
725642
726643 RThreadSafe :: new ( bindings)
0 commit comments