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 registering multiple suggestion handlers #11

Merged
merged 5 commits into from
Dec 4, 2024
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
2 changes: 1 addition & 1 deletion .copier-answers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ kind: frontend
labextension_name: jupyter-suggestions
project_short_description: A JupyterLab extension for suggesting changes.
python_name: jupyterlab_suggestions
repository: https://github.com/QuantStack/jupyterlab-suggestions
repository: https://github.com/QuantStack/jupyter-suggestions
test: false

2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The `jlpm` command is JupyterLab's pinned version of

```bash
# Clone the repo to your local environment
git clone https://github.com/QuantStack/jupyterlab-suggestions.git
git clone https://github.com/QuantStack/jupyter-suggestions.git
# Change directory to the jupytercad directory
cd jupyter-suggestions
# Install JupyterLab for jlpm
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# jupyterlab_suggestions

[![Github Actions Status](https://github.com/QuantStack/jupyterlab-suggestions/workflows/Build/badge.svg)](https://github.com/QuantStack/jupyterlab-suggestions/actions/workflows/build.yml)
[![Github Actions Status](https://github.com/QuantStack/jupyter-suggestions/workflows/Build/badge.svg)](https://github.com/QuantStack/jupyter-suggestions/actions/workflows/build.yml)

A JupyterLab extension for suggesting changes.

Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"jupyterlab",
"jupyterlab-extension"
],
"homepage": "https://github.com/QuantStack/jupyterlab-suggestions",
"homepage": "https://github.com/QuantStack/jupyter-suggestions",
"bugs": {
"url": "https://github.com/QuantStack/jupyterlab-suggestions/issues"
"url": "https://github.com/QuantStack/jupyter-suggestions/issues"
},
"license": "BSD-3-Clause",
"author": "QuantStack",
Expand All @@ -21,7 +21,7 @@
],
"repository": {
"type": "git",
"url": "https://github.com/QuantStack/jupyterlab-suggestions.git"
"url": "https://github.com/QuantStack/jupyter-suggestions.git"
},
"workspaces": [
"packages/*",
Expand Down Expand Up @@ -62,6 +62,7 @@
"eslint": "^8.36.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-react-hooks": "^4.6.2",
"lerna": "^7.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^3.0.0",
Expand All @@ -87,7 +88,8 @@
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
"plugin:prettier/recommended",
"plugin:react-hooks/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
Expand All @@ -98,6 +100,7 @@
"@typescript-eslint"
],
"rules": {
"react-hooks/exhaustive-deps": "error",
"@typescript-eslint/naming-convention": [
"error",
{
Expand Down
6 changes: 3 additions & 3 deletions packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"jupyterlab",
"jupyterlab-extension"
],
"homepage": "https://github.com/QuantStack/jupyterlab-suggestions",
"homepage": "https://github.com/QuantStack/jupyter-suggestions",
"bugs": {
"url": "https://github.com/QuantStack/jupyterlab-suggestions/issues"
"url": "https://github.com/QuantStack/jupyter-suggestions/issues"
},
"license": "BSD-3-Clause",
"author": "QuantStack",
Expand All @@ -23,7 +23,7 @@
"style": "style/index.css",
"repository": {
"type": "git",
"url": "https://github.com/QuantStack/jupyterlab-suggestions.git"
"url": "https://github.com/QuantStack/jupyter-suggestions.git"
},
"scripts": {
"build": "tsc -b",
Expand Down
1 change: 1 addition & 0 deletions packages/base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './suggestionsPanel';
export * from './icons';
export * from './tokens';
export * from './localSuggestionsManager';
export * from './registry';
72 changes: 72 additions & 0 deletions packages/base/src/registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { ISignal, Signal } from '@lumino/signaling';
import { ISuggestionsManager, ISuggestionsManagerRegistry } from './types';

export class SuggestionsManagerRegistry implements ISuggestionsManagerRegistry {
constructor() {
this._managers = new Map();
this._managerChanged = new Signal(this);
this._managerRegistered = new Signal(this);
}

get isDisposed() {
return this._isDisposed;
}

get managerChanged(): ISignal<
ISuggestionsManagerRegistry,
ISuggestionsManager
> {
return this._managerChanged;
}
get managerRegistered(): ISignal<ISuggestionsManagerRegistry, string> {
return this._managerRegistered;
}

async register(options: {
id: string;
manager: ISuggestionsManager;
}): Promise<boolean> {
const { id, manager } = options;
if (this._managers.has(id)) {
console.error(`Suggestions manager with id ${id} exists!`);
return false;
}
this._managers.set(id, manager);
this._managerRegistered.emit(id);
return true;
}

async setManager(id: string): Promise<boolean> {
if (!this._managers.has(id)) {
return false;
}
this._currentManager = this._managers.get(id)!;
this._managerChanged.emit(this._currentManager);
return true;
}

async getActivatedManager(): Promise<ISuggestionsManager | undefined> {
return this._currentManager;
}

getAllManagers(): string[] {
return [...this._managers.keys()];
}

dispose(): void {
if (this._isDisposed) {
return;
}
this._managers.forEach(it => it.dispose());
this._managers.clear();
}

private _managers: Map<string, ISuggestionsManager>;
private _managerChanged: Signal<
ISuggestionsManagerRegistry,
ISuggestionsManager
>;
private _managerRegistered: Signal<ISuggestionsManagerRegistry, string>;
private _currentManager: ISuggestionsManager | undefined;
private _isDisposed = false;
}
42 changes: 28 additions & 14 deletions packages/base/src/suggestionsPanel/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import { Cell, ICellModel } from '@jupyterlab/cells';
export class SuggestionsModel implements ISuggestionsModel {
constructor(options: SuggestionsModel.IOptions) {
this.switchNotebook(options.panel);
this._suggestionsManager = options.suggestionsManager;
this._suggestionsManager.suggestionChanged.connect(
this._handleSuggestionChanged,
this
);
this.switchManager(options.suggestionsManager);
}
get filePath(): string {
return this._filePath ?? '-';
Expand Down Expand Up @@ -45,15 +41,15 @@ export class SuggestionsModel implements ISuggestionsModel {
return;
}
this._isDisposed = true;
this._suggestionsManager.suggestionChanged.disconnect(
this._suggestionsManager?.suggestionChanged.disconnect(
this._handleSuggestionChanged
);
Signal.clearData(this);
}

async addSuggestion(): Promise<void> {
const activeCell = this._notebookPanel?.content.activeCell;
if (activeCell && this._notebookPanel) {
if (activeCell && this._notebookPanel && this._suggestionsManager) {
await this._suggestionsManager.addSuggestion({
notebook: this._notebookPanel,
cell: activeCell
Expand All @@ -65,7 +61,7 @@ export class SuggestionsModel implements ISuggestionsModel {
suggestionId: string;
}): Promise<void> {
const { cellId, suggestionId } = options;
if (cellId && this._notebookPanel) {
if (cellId && this._notebookPanel && this._suggestionsManager) {
await this._suggestionsManager.deleteSuggestion({
notebook: this._notebookPanel,
cellId,
Expand All @@ -79,7 +75,7 @@ export class SuggestionsModel implements ISuggestionsModel {
suggestionId: string;
}): Promise<boolean> {
const { cellId, suggestionId } = options;
if (cellId && this._notebookPanel) {
if (cellId && this._notebookPanel && this._suggestionsManager) {
return await this._suggestionsManager.acceptSuggestion({
notebook: this._notebookPanel,
cellId,
Expand All @@ -94,7 +90,7 @@ export class SuggestionsModel implements ISuggestionsModel {
newSource: string;
}): Promise<void> {
const { cellId, suggestionId, newSource } = options;
if (cellId && this._notebookPanel) {
if (cellId && this._notebookPanel && this._suggestionsManager) {
await this._suggestionsManager.updateSuggestion({
notebook: this._notebookPanel,
cellId,
Expand All @@ -104,7 +100,7 @@ export class SuggestionsModel implements ISuggestionsModel {
}
}
async getSuggestion(options: { cellId: string; suggestionId: string }) {
if (!this._filePath) {
if (!this._filePath || !this._suggestionsManager) {
return;
}
return await this._suggestionsManager.getSuggestion({
Expand Down Expand Up @@ -133,7 +129,7 @@ export class SuggestionsModel implements ISuggestionsModel {
if (panel) {
await panel.context.ready;
this._allSuggestions =
await this._suggestionsManager.getAllSuggestions(panel);
await this._suggestionsManager?.getAllSuggestions(panel);
} else {
this._allSuggestions = undefined;
}
Expand All @@ -143,7 +139,25 @@ export class SuggestionsModel implements ISuggestionsModel {
this._filePath = this._notebookPanel?.context.localPath;
this._notebookSwitched.emit();
}
async switchManager(manager: ISuggestionsManager | undefined): Promise<void> {
if (!manager) {
return;
}
this._suggestionsManager?.suggestionChanged.disconnect(
this._handleSuggestionChanged
);

this._suggestionsManager = manager;
this._suggestionsManager.suggestionChanged.connect(
this._handleSuggestionChanged,
this
);
if (this._notebookPanel) {
this._allSuggestions = await this._suggestionsManager?.getAllSuggestions(
this._notebookPanel
);
}
}
private _connectPanelSignal() {
if (!this._notebookPanel) {
return;
Expand Down Expand Up @@ -183,7 +197,7 @@ export class SuggestionsModel implements ISuggestionsModel {
private _notebookPanel: NotebookPanel | null = null;
private _notebookSwitched: Signal<this, void> = new Signal(this);
private _allSuggestions?: IAllSuggestions;
private _suggestionsManager: ISuggestionsManager;
private _suggestionsManager?: ISuggestionsManager;
private _suggestionChanged = new Signal<
ISuggestionsModel,
Omit<ISuggestionChange, 'notebookPath'>
Expand All @@ -197,6 +211,6 @@ export class SuggestionsModel implements ISuggestionsModel {
export namespace SuggestionsModel {
export interface IOptions {
panel: NotebookPanel | null;
suggestionsManager: ISuggestionsManager;
suggestionsManager?: ISuggestionsManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ interface IProps {
acceptCallback: () => Promise<void>;
}
function _SuggestionToolbarReact(props: IProps) {
const { toggleMinimized, state } = props;
const minimizeClick = React.useCallback(() => {
props.toggleMinimized(!props.state.get('minimized'));
}, [props.toggleMinimized, props.state]);
toggleMinimized(!state.get('minimized'));
}, [toggleMinimized, state]);

const [elementState, setElementState] = React.useState({
minimized: props.state.get('minimized')
minimized: state.get('minimized')
});
React.useEffect(() => {
const handler = () => {
const current = props.state.get('minimized');
const current = state.get('minimized');
setElementState(old => ({ ...old, minimized: current }));
};
props.state.changed.connect(handler);
state.changed.connect(handler);
return () => {
props.state.changed.disconnect(handler);
state.changed.disconnect(handler);
};
}, [props.state]);
}, [state]);
return (
<div className={toolbarStyle}>
<ToolbarButtonComponent
Expand Down
11 changes: 10 additions & 1 deletion packages/base/src/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Token } from '@lumino/coreutils';
import { ISuggestionsManager, ISuggestionsModel } from './types';
import {
ISuggestionsManager,
ISuggestionsModel,
ISuggestionsManagerRegistry
} from './types';

export const ISuggestionsModelToken = new Token<ISuggestionsModel>(
'jupyter-suggestions:suggestionsModel'
Expand All @@ -8,3 +12,8 @@ export const ISuggestionsModelToken = new Token<ISuggestionsModel>(
export const ISuggestionsManagerToken = new Token<ISuggestionsManager>(
'jupyter-suggestions:suggestionsManager'
);

export const ISuggestionsManagerRegistryToken =
new Token<ISuggestionsManagerRegistry>(
'jupyter-suggestions:suggestionsManagerRegistry'
);
Loading
Loading