Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow click to run in notebooks #235

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,8 @@
"id": "malloy.notebook-renderer-schema",
"mimeTypes": [
"x-application/malloy-schema"
]
],
"requiresMessaging": "optional"
}
]
},
Expand Down
8 changes: 8 additions & 0 deletions src/extension/notebook/malloy_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ export function activateNotebookController(
newUntitledNotebookCommand
)
);

const messageChannel = vscode.notebooks.createRendererMessaging(
'malloy.notebook-renderer-schema'
);
messageChannel.onDidReceiveMessage(event => {
const {type, args} = event.message;
vscode.commands.executeCommand(type, ...args);
});
}

class MalloyController {
Expand Down
47 changes: 42 additions & 5 deletions src/extension/notebook/renderer/schema_entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import React from 'react';
import {StyleSheetManager} from 'styled-components';
import {ActivationFunction} from 'vscode-notebook-renderer';
import {SchemaRenderer} from '../../webviews/components/SchemaRenderer';
import {Explore, SerializedExplore} from '@malloydata/malloy';
import {Explore, Field, SerializedExplore} from '@malloydata/malloy';
import {fieldType} from '../../common/schema';

export const activate: ActivationFunction = () => {
export const activate: ActivationFunction = ({postMessage}) => {
return {
renderOutputItem(info, element) {
let shadow = element.shadowRoot;
Expand All @@ -45,7 +46,10 @@ export const activate: ActivationFunction = () => {

ReactDOM.render(
<StyleSheetManager target={root}>
<SchemaRendererWrapper results={info.json()} />
<SchemaRendererWrapper
results={info.json()}
postMessage={postMessage}
/>
</StyleSheetManager>,
root
);
Expand All @@ -55,6 +59,7 @@ export const activate: ActivationFunction = () => {

interface SchemaRendererWrapperProps {
results: SerializedExplore[];
postMessage?: (message: unknown) => void;
}

/**
Expand All @@ -63,7 +68,10 @@ interface SchemaRendererWrapperProps {
* we use useEffect() to delay rendering the actual contents until
* it's ready.
*/
const SchemaRendererWrapper = ({results}: SchemaRendererWrapperProps) => {
const SchemaRendererWrapper = ({
results,
postMessage,
}: SchemaRendererWrapperProps) => {
const [explores, setExplores] = React.useState<Explore[]>();

React.useEffect(() => {
Expand All @@ -77,5 +85,34 @@ const SchemaRendererWrapper = ({results}: SchemaRendererWrapperProps) => {
return null;
}

return <SchemaRenderer explores={explores} />;
const onFieldClick = (field: Field) => {
const type = fieldType(field);

let path = field.name;
let current: Explore = field.parentExplore;
while (current.parentExplore) {
path = `${current.name}.${path}`;
current = current.parentExplore;
}
const topLevelExplore = current;

if (type === 'query') {
const type = 'malloy.runQuery';
const args = [
`query: ${topLevelExplore.name}->${path}`,
`${topLevelExplore.name}->${path}`,
];
postMessage?.({type, args});
} else {
let path = field.name;
let current: Explore = field.parentExplore;
while (current.parentExplore) {
path = `${current.name}.${path}`;
current = current.parentExplore;
}
navigator.clipboard.writeText(path);
}
};

return <SchemaRenderer explores={explores} onFieldClick={onFieldClick} />;
};
27 changes: 17 additions & 10 deletions src/extension/webviews/components/SchemaRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import OneToOneIcon from '../../../media/one_to_one.svg';
export interface SchemaRendererProps {
explores: Explore[];
defaultShow?: boolean;
onFieldClick?: (field: Field) => void;
}

/**
Expand Down Expand Up @@ -147,6 +148,9 @@ Path: ${path}${path ? '.' : ''}${fieldName}
Type: ${type}`;
}

const sortByName = (a: Field | Explore, b: Field | Explore) =>
a.name.localeCompare(b.name);

/**
* Bucket fields by type and sort by name.
*
Expand All @@ -161,9 +165,6 @@ function bucketFields(fields: Field[]) {
const measures: Field[] = [];
const explores: Explore[] = [];

const sortByName = (a: Field | Explore, b: Field | Explore) =>
a.name.localeCompare(b.name);

for (const field of fields) {
const type = fieldType(field);

Expand Down Expand Up @@ -193,6 +194,7 @@ function bucketFields(fields: Field[]) {
export const SchemaRenderer: React.FC<SchemaRendererProps> = ({
explores,
defaultShow = false,
onFieldClick,
}) => {
if (!explores || !explores.length) {
return <b>No Schema Information</b>;
Expand Down Expand Up @@ -268,15 +270,16 @@ export const SchemaRenderer: React.FC<SchemaRendererProps> = ({

const FieldItem = ({field, path}: FieldProps) => {
const onClick = () => {
const type = fieldType(field);
const fieldName = field.name;
if (type !== 'query') {
navigator.clipboard.writeText(`${path}${path ? '.' : ''}${fieldName}`);
}
onFieldClick?.(field);
};
const clickable = onFieldClick ? 'clickable' : '';

return (
<div className="field" title={buildTitle(field, path)} onClick={onClick}>
<div
className={`field ${clickable}`}
title={buildTitle(field, path)}
onClick={onClick}
>
{getIconElement(fieldType(field), isFieldAggregate(field))}
<span className="field_name">{field.name}</span>
</div>
Expand All @@ -300,7 +303,7 @@ export const SchemaRenderer: React.FC<SchemaRendererProps> = ({
return (
<SchemaTree>
<ul>
{explores.map(explore => (
{explores.sort(sortByName).map(explore => (
<StructItem key={explore.name} explore={explore} path={''} />
))}
</ul>
Expand Down Expand Up @@ -355,6 +358,10 @@ const SchemaTree = styled.div`
border-radius: 8px;
padding: 0.25em 0.75em;
margin: 0.2em;

&.clickable {
cursor: pointer;
}
}

.field_list {
Expand Down
23 changes: 21 additions & 2 deletions src/extension/webviews/query_page/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import {Explore, Result} from '@malloydata/malloy';
import {Explore, Field, Result} from '@malloydata/malloy';
import {HTMLView} from '@malloydata/render';
import React, {
DOMElement,
Expand All @@ -47,6 +47,7 @@ import {CopyButton} from './CopyButton';
import {Scroll} from '../components/Scroll';
import {PrismContainer} from '../components/PrismContainer';
import {SchemaRenderer} from '../components/SchemaRenderer';
import {fieldType} from '../../common/schema';

enum Status {
Ready = 'ready',
Expand Down Expand Up @@ -237,6 +238,20 @@ export const App: React.FC = () => {
[resultKind, results]
);

const onFieldClick = (field: Field) => {
const type = fieldType(field);

if (type !== 'query') {
let path = field.name;
let current: Explore = field.parentExplore;
while (current.parentExplore) {
path = `${current.name}.${path}`;
current = current.parentExplore;
}
navigator.clipboard.writeText(path);
}
};

return (
<div
style={{
Expand Down Expand Up @@ -324,7 +339,11 @@ export const App: React.FC = () => {
)}
{!status.error && results.schema && resultKind === ResultKind.SCHEMA && (
<Scroll>
<SchemaRenderer explores={results.schema} defaultShow={true} />
<SchemaRenderer
explores={results.schema}
defaultShow={true}
onFieldClick={onFieldClick}
/>
</Scroll>
)}
{status.error && (
Expand Down
Loading