Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions src/CodeSnippetService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { JupyterFrontEnd } from '@jupyterlab/application';
import { Settings } from '@jupyterlab/settingregistry';
import { JSONArray, PartialJSONValue } from '@lumino/coreutils';
import { JSONArray, JSONExt, PartialJSONValue } from '@lumino/coreutils';

import { CodeSnippetWidget } from './CodeSnippetWidget';

export interface ICodeSnippet {
name: string;
Expand All @@ -16,15 +19,34 @@ export class CodeSnippetService {
private static codeSnippetService: CodeSnippetService;
private codeSnippetList: ICodeSnippet[];

private constructor(settings: Settings) {
private constructor(settings: Settings, app: JupyterFrontEnd) {
this.settingManager = settings;

// just in case when user changes the snippets using settingsEditor
this.settingManager.changed.connect((plugin) => {
this.settingManager.changed.connect(async (plugin) => {
const newCodeSnippetList = plugin.get('snippets').user;
this.codeSnippetList = this.convertToICodeSnippetList(
newCodeSnippetList as JSONArray
);

if (
!JSONExt.deepEqual(
newCodeSnippetList,
(this.codeSnippetList as unknown) as PartialJSONValue
)
) {
this.codeSnippetList = this.convertToICodeSnippetList(
newCodeSnippetList as JSONArray
);

const leftWidgets = app.shell.widgets('left').iter();

let current = leftWidgets.next();
while (current) {
if (current instanceof CodeSnippetWidget) {
current.updateCodeSnippetWidget();
break;
}
current = leftWidgets.next();
}
}
});

const defaultSnippets = this.convertToICodeSnippetList(
Expand Down Expand Up @@ -56,9 +78,9 @@ export class CodeSnippetService {
return snippetList;
}

static init(settings: Settings): void {
static init(settings: Settings, app: JupyterFrontEnd): void {
if (!this.codeSnippetService) {
this.codeSnippetService = new CodeSnippetService(settings);
this.codeSnippetService = new CodeSnippetService(settings, app);
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/CodeSnippetWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { IDragEvent } from '@lumino/dragdrop';
import { MimeData } from '@lumino/coreutils';

import { CodeSnippetService, ICodeSnippet } from './CodeSnippetService';
// import { CodeSnippetWidgetModel } from './CodeSnippetWidgetModel';
import { CodeSnippetDisplay } from './CodeSnippetDisplay';
import { CodeSnippetInputDialog } from './CodeSnippetInputDialog';

Expand Down Expand Up @@ -73,7 +72,6 @@ export class CodeSnippetWidget extends ReactWidget {
this.app = app;
this.editorServices = editorServices;
this.getCurrentWidget = getCurrentWidget;
// this._codeSnippetWidgetModel = new CodeSnippetWidgetModel([]);
this.renderCodeSnippetsSignal = new Signal<this, ICodeSnippet[]>(this);
this.codeSnippetManager = CodeSnippetService.getCodeSnippetService();

Expand Down
6 changes: 1 addition & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ import codeSnippetIconSVGstr from '../style/icon/jupyter_snippeticon.svg';

import { CodeSnippetInputDialog } from './CodeSnippetInputDialog';
import { CodeSnippetWidget } from './CodeSnippetWidget';
// import { CodeSnippetContentsService } from './CodeSnippetContentsService';
import {
CodeSnippetEditor,
ICodeSnippetEditorMetadata,
} from './CodeSnippetEditor';
import { CodeSnippetService } from './CodeSnippetService';
import { NotebookPanel } from '@jupyterlab/notebook';
import { DocumentWidget } from '@jupyterlab/docregistry';
// import { NotebookPanel } from '@jupyterlab/notebook';
// import { ServerConnection, SettingManager } from '@jupyterlab/services';
// import { URLExt } from '@jupyterlab/coreutils';

const CODE_SNIPPET_EXTENSION_ID = 'code-snippet-extension';

Expand Down Expand Up @@ -303,7 +299,7 @@ const codeSnippetExtensionSetting: JupyterFrontEndPlugin<void> = {
void settingRegistry
.load(CODE_SNIPPET_SETTING_ID)
.then((settings) => {
CodeSnippetService.init(settings as Settings);
CodeSnippetService.init(settings as Settings, app);
console.log('JupyterLab extension code-snippets setting is activated!');
})
.catch((e) => console.log(e));
Expand Down