Skip to content

Commit 00ffdf5

Browse files
committed
fix #3781 -- include type and signature info when tab completing with jupyter
- used antd tags with color instead of color letter on one side and word on right: it just seems a lot cleaner to me - also show signature via a tooltip, which jupyterlab doesn't do for some reason
1 parent 7647852 commit 00ffdf5

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

src/packages/frontend/jupyter/complete.tsx

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
declare const $: any;
77

8-
import { CSSProperties, useEffect, useRef } from "react";
8+
import { Tag, Tooltip } from "antd";
9+
import { CSSProperties, useEffect, useMemo, useRef } from "react";
910
import { Map } from "immutable";
1011
import useNotebookFrameActions from "@cocalc/frontend/frame-editors/jupyter-editor/cell-notebook/hook";
1112

@@ -14,7 +15,7 @@ export interface Actions {
1415
select_complete: (
1516
id: string,
1617
item: string,
17-
complete?: Map<string, any>
18+
complete?: Map<string, any>,
1819
) => void;
1920
complete_handle_key: (_: string, keyCode: number) => void;
2021
clear_complete: () => void;
@@ -44,6 +45,25 @@ export function Complete({ actions, id, complete }: Props) {
4445
frameActions.current?.set_mode("edit");
4546
};
4647
}, []);
48+
const typeInfo = useMemo(() => {
49+
const types = complete?.getIn(["metadata", "_jupyter_types_experimental"]);
50+
if (types == null) {
51+
return {};
52+
}
53+
const typeInfo: { [text: string]: { type: string; signature: string } } =
54+
{};
55+
// @ts-ignore
56+
for (const info of types) {
57+
const text = info.get("text");
58+
if (typeInfo[text] == null) {
59+
typeInfo[text] = {
60+
type: info.get("type"),
61+
signature: info.get("signature"),
62+
};
63+
}
64+
}
65+
return typeInfo;
66+
}, [complete]);
4767

4868
function select(item: string): void {
4969
// Save contents of editor to the store so that completion properly *places* the
@@ -57,8 +77,31 @@ export function Complete({ actions, id, complete }: Props) {
5777
function renderItem(item: string) {
5878
return (
5979
<li key={item}>
60-
<a role="menuitem" tabIndex={-1} onClick={() => select(item)}>
80+
<a
81+
role="menuitem"
82+
style={{ display: "flex", fontSize: "13px" }}
83+
tabIndex={-1}
84+
onClick={() => select(item)}
85+
>
6186
{item}
87+
{typeInfo[item]?.type ? (
88+
<Tooltip title={`${item}${typeInfo[item].signature}`}>
89+
<div style={{ flex: 1 }}>
90+
<div
91+
style={{
92+
float: "right",
93+
marginLeft: "30px",
94+
color: "#0000008a",
95+
fontFamily: "monospace",
96+
}}
97+
>
98+
<Tag color={typeToColor[typeInfo[item].type]}>
99+
{typeInfo[item].type}
100+
</Tag>
101+
</div>
102+
</div>
103+
</Tooltip>
104+
) : null}
62105
</a>
63106
</li>
64107
);
@@ -104,3 +147,16 @@ export function Complete({ actions, id, complete }: Props) {
104147
</div>
105148
);
106149
}
150+
151+
const typeToColor = {
152+
function: "blue",
153+
statement: "green",
154+
module: "cyan",
155+
class: "orange",
156+
instance: "magenta",
157+
"<unknown>": "red",
158+
path: "gold",
159+
keyword: "purple",
160+
magic: "geekblue",
161+
param: "volcano",
162+
};

src/packages/jupyter/redux/actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,6 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
10641064
};
10651065

10661066
undo = (): void => {
1067-
console.trace("jupyter redux undo");
10681067
if (this.syncdb != null) {
10691068
this.syncdb.undo();
10701069
}
@@ -1703,6 +1702,8 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
17031702
}
17041703
// For some reason, sometimes complete.matches are not unique, which is annoying/confusing,
17051704
// and breaks an assumption in our react code too.
1705+
// I think the reason is e.g., a filename and a variable could be the same. We're not
1706+
// worrying about that now.
17061707
complete.matches = Array.from(new Set(complete.matches));
17071708
// sort in a way that matches how JupyterLab sorts completions, which
17081709
// is case insensitive with % magics at the bottom

0 commit comments

Comments
 (0)