Skip to content

Commit d748867

Browse files
jmcpherslionel-DavisVaughan
authored
Send profvis output to editor (#988)
* send profvis output to editor * Update crates/ark/src/modules/positron/html_widgets.R Co-authored-by: Lionel Henry <[email protected]> * revert srcfile change * Update crates/ark/src/viewer.rs Co-authored-by: Davis Vaughan <[email protected]> * clarify htmltools dependency * enforce standalone mode --------- Co-authored-by: Lionel Henry <[email protected]> Co-authored-by: Davis Vaughan <[email protected]>
1 parent af8b084 commit d748867

File tree

8 files changed

+85
-14
lines changed

8 files changed

+85
-14
lines changed

crates/amalthea/src/comm/connections_comm.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ pub struct ObjectSchema {
1818
pub name: String,
1919

2020
/// The object type (table, catalog, schema)
21-
pub kind: String
21+
pub kind: String,
22+
23+
/// Indicates if the object has children that can be listed. This property
24+
/// is optional and when omitted, it is assumed that the object may have
25+
/// children unless its kind is 'field'.
26+
pub has_children: Option<bool>
2227
}
2328

2429
/// FieldSchema in Schemas

crates/amalthea/src/comm/ui_comm.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ pub struct Range {
9898
pub end: Position
9999
}
100100

101+
/// Source information for preview content
102+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
103+
pub struct PreviewSource {
104+
/// The type of source that opened the preview
105+
#[serde(rename = "type")]
106+
pub preview_source_type: PreviewSourceType,
107+
108+
/// The ID of the source (session_id or terminal process ID)
109+
pub id: String
110+
}
111+
101112
/// Possible values for Kind in OpenEditor
102113
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, strum_macros::Display, strum_macros::EnumString)]
103114
pub enum OpenEditorKind {
@@ -110,6 +121,34 @@ pub enum OpenEditorKind {
110121
Uri
111122
}
112123

124+
/// Possible values for Destination in ShowHtmlFile
125+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, strum_macros::Display, strum_macros::EnumString)]
126+
pub enum ShowHtmlFileDestination {
127+
#[serde(rename = "plot")]
128+
#[strum(to_string = "plot")]
129+
Plot,
130+
131+
#[serde(rename = "viewer")]
132+
#[strum(to_string = "viewer")]
133+
Viewer,
134+
135+
#[serde(rename = "editor")]
136+
#[strum(to_string = "editor")]
137+
Editor
138+
}
139+
140+
/// Possible values for Type in PreviewSource
141+
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, strum_macros::Display, strum_macros::EnumString)]
142+
pub enum PreviewSourceType {
143+
#[serde(rename = "runtime")]
144+
#[strum(to_string = "runtime")]
145+
Runtime,
146+
147+
#[serde(rename = "terminal")]
148+
#[strum(to_string = "terminal")]
149+
Terminal
150+
}
151+
113152
/// Parameters for the DidChangePlotsRenderSettings method.
114153
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
115154
pub struct DidChangePlotsRenderSettingsParams {
@@ -305,6 +344,9 @@ pub struct ModifyEditorSelectionsParams {
305344
pub struct ShowUrlParams {
306345
/// The URL to display
307346
pub url: String,
347+
348+
/// Optional source information for the URL
349+
pub source: Option<PreviewSource>,
308350
}
309351

310352
/// Parameters for the ShowHtmlFile method.
@@ -317,8 +359,9 @@ pub struct ShowHtmlFileParams {
317359
/// superseded by the title in the HTML file.
318360
pub title: String,
319361

320-
/// Whether the HTML file is a plot-like object
321-
pub is_plot: bool,
362+
/// Where the file should be shown in Positron: as an interactive plot, in
363+
/// the viewer pane, or in a new editor tab.
364+
pub destination: ShowHtmlFileDestination,
322365

323366
/// The desired height of the HTML viewer, in pixels. The special value 0
324367
/// indicates that no particular height is desired, and -1 indicates that

crates/ark/src/connections/r_connection.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ impl RConnection {
126126
.map(|(name, kind)| ObjectSchema {
127127
name: name.clone(),
128128
kind: kind.clone(),
129+
has_children: None,
129130
})
130131
.collect::<Vec<_>>();
131132

crates/ark/src/modules/positron/html_widgets.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# html_widgets.R
33
#
4-
# Copyright (C) 2023-2024 Posit Software, PBC. All rights reserved.
4+
# Copyright (C) 2023-2025 Posit Software, PBC. All rights reserved.
55
#
66
#
77
#' @export
@@ -14,7 +14,7 @@
1414
tmp_file <- htmltools::html_print(rendered, viewer = NULL)
1515

1616
# Guess whether this is a plot-like widget based on its sizing policy.
17-
is_plot <- isTRUE(x$sizingPolicy$knitr$figure)
17+
destination <- if (isTRUE(x$sizingPolicy$knitr$figure)) "plot" else "viewer"
1818

1919
# Derive the height of the viewer pane from the sizing policy of the widget.
2020
height <- .ps.validate.viewer.height(x$sizingPolicy$viewer$paneHeight)
@@ -30,7 +30,7 @@
3030

3131
# Pass the widget to the viewer. Positron will assemble the final HTML
3232
# document from these components.
33-
.ps.Call("ps_html_viewer", tmp_file, label, height, is_plot)
33+
.ps.Call("ps_html_viewer", tmp_file, label, height, destination)
3434
}
3535

3636
#' @export

crates/ark/src/modules/positron/options.R

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# options.R
33
#
4-
# Copyright (C) 2023-2024 Posit Software, PBC. All rights reserved.
4+
# Copyright (C) 2023-2025 Posit Software, PBC. All rights reserved.
55
#
66
#
77

@@ -36,3 +36,16 @@ options(plumber.docs.callback = function(url) {
3636
options(shiny.launch.browser = function(url) {
3737
.ps.ui.showUrl(url)
3838
})
39+
40+
# Show Profvis output in the viewer
41+
options(profvis.print = function(x) {
42+
# Render the widget to a tag list to create standalone HTML output.
43+
# (htmltools is a Profvis dependency so it's guaranteed to be available)
44+
rendered <- htmltools::as.tags(x, standalone = TRUE)
45+
46+
# Render the HTML content to a temporary file
47+
tmp_file <- htmltools::html_print(rendered, viewer = NULL)
48+
49+
# Pass the file to the viewer
50+
.ps.Call("ps_html_viewer", tmp_file, "R Profile", -1L, "editor")
51+
})

crates/ark/src/ui/events.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ pub unsafe extern "C-unwind" fn ps_ui_set_selection_ranges(ranges: SEXP) -> anyh
108108
pub fn send_show_url_event(url: &str) -> anyhow::Result<()> {
109109
let params = ShowUrlParams {
110110
url: url.to_string(),
111+
source: None,
111112
};
112113
let event = UiFrontendEvent::ShowUrl(params);
113114

crates/ark/src/viewer.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//
66
//
77

8+
use amalthea::comm::ui_comm::ShowHtmlFileDestination;
89
use amalthea::comm::ui_comm::ShowHtmlFileParams;
910
use amalthea::comm::ui_comm::UiFrontendEvent;
1011
use amalthea::socket::iopub::IOPubMessage;
@@ -53,7 +54,7 @@ pub unsafe extern "C-unwind" fn ps_html_viewer(
5354
url: SEXP,
5455
label: SEXP,
5556
height: SEXP,
56-
is_plot: SEXP,
57+
destination: SEXP,
5758
) -> anyhow::Result<SEXP> {
5859
// Convert url to a string; note that we are only passed URLs that
5960
// correspond to files in the temporary directory.
@@ -76,12 +77,18 @@ pub unsafe extern "C-unwind" fn ps_html_viewer(
7677
}
7778
},
7879
SessionMode::Console => {
79-
let is_plot = RObject::view(is_plot).to::<bool>();
80-
let is_plot = match is_plot {
81-
Ok(is_plot) => is_plot,
80+
let destination = match RObject::view(destination).to::<String>() {
81+
Ok(s) => s.parse::<ShowHtmlFileDestination>().unwrap_or_else(|_| {
82+
log::warn!(
83+
"`destination` must be one of 'plot', 'editor', or 'viewer', using 'viewer' as a fallback."
84+
);
85+
ShowHtmlFileDestination::Viewer
86+
}),
8287
Err(err) => {
83-
log::warn!("Can't convert `is_plot` into a bool, using `false` as a fallback: {err:?}");
84-
false
88+
log::warn!(
89+
"Can't convert `destination` to a string, using 'viewer' as a fallback: {err}"
90+
);
91+
ShowHtmlFileDestination::Viewer
8592
},
8693
};
8794

@@ -98,7 +105,7 @@ pub unsafe extern "C-unwind" fn ps_html_viewer(
98105
path,
99106
title: label,
100107
height,
101-
is_plot,
108+
destination,
102109
};
103110

104111
let event = UiFrontendEvent::ShowHtmlFile(params);

crates/ark/tests/connections.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ fn obj(name: &str, kind: &str) -> ObjectSchema {
7070
ObjectSchema {
7171
name: String::from(name),
7272
kind: String::from(kind),
73+
has_children: None,
7374
}
7475
}
7576

0 commit comments

Comments
 (0)