Skip to content

Commit 9a3450e

Browse files
authored
Merge pull request #187 from jahn96/setting
Make snippets configurable in advanced settings #121
2 parents 20288b5 + c75dd6f commit 9a3450e

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

src/CodeSnippetService.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { JupyterFrontEnd } from '@jupyterlab/application';
12
import { Settings } from '@jupyterlab/settingregistry';
2-
import { JSONArray, PartialJSONValue } from '@lumino/coreutils';
3+
import { JSONArray, JSONExt, PartialJSONValue } from '@lumino/coreutils';
4+
5+
import { CodeSnippetWidget } from './CodeSnippetWidget';
36

47
export interface ICodeSnippet {
58
name: string;
@@ -16,15 +19,34 @@ export class CodeSnippetService {
1619
private static codeSnippetService: CodeSnippetService;
1720
private codeSnippetList: ICodeSnippet[];
1821

19-
private constructor(settings: Settings) {
22+
private constructor(settings: Settings, app: JupyterFrontEnd) {
2023
this.settingManager = settings;
2124

2225
// just in case when user changes the snippets using settingsEditor
23-
this.settingManager.changed.connect((plugin) => {
26+
this.settingManager.changed.connect(async (plugin) => {
2427
const newCodeSnippetList = plugin.get('snippets').user;
25-
this.codeSnippetList = this.convertToICodeSnippetList(
26-
newCodeSnippetList as JSONArray
27-
);
28+
29+
if (
30+
!JSONExt.deepEqual(
31+
newCodeSnippetList,
32+
(this.codeSnippetList as unknown) as PartialJSONValue
33+
)
34+
) {
35+
this.codeSnippetList = this.convertToICodeSnippetList(
36+
newCodeSnippetList as JSONArray
37+
);
38+
39+
const leftWidgets = app.shell.widgets('left').iter();
40+
41+
let current = leftWidgets.next();
42+
while (current) {
43+
if (current instanceof CodeSnippetWidget) {
44+
current.updateCodeSnippetWidget();
45+
break;
46+
}
47+
current = leftWidgets.next();
48+
}
49+
}
2850
});
2951

3052
const defaultSnippets = this.convertToICodeSnippetList(
@@ -56,9 +78,9 @@ export class CodeSnippetService {
5678
return snippetList;
5779
}
5880

59-
static init(settings: Settings): void {
81+
static init(settings: Settings, app: JupyterFrontEnd): void {
6082
if (!this.codeSnippetService) {
61-
this.codeSnippetService = new CodeSnippetService(settings);
83+
this.codeSnippetService = new CodeSnippetService(settings, app);
6284
}
6385
}
6486

src/CodeSnippetWidget.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import { IDragEvent } from '@lumino/dragdrop';
2626
import { MimeData } from '@lumino/coreutils';
2727

2828
import { CodeSnippetService, ICodeSnippet } from './CodeSnippetService';
29-
// import { CodeSnippetWidgetModel } from './CodeSnippetWidgetModel';
3029
import { CodeSnippetDisplay } from './CodeSnippetDisplay';
3130
import { CodeSnippetInputDialog } from './CodeSnippetInputDialog';
3231

@@ -73,7 +72,6 @@ export class CodeSnippetWidget extends ReactWidget {
7372
this.app = app;
7473
this.editorServices = editorServices;
7574
this.getCurrentWidget = getCurrentWidget;
76-
// this._codeSnippetWidgetModel = new CodeSnippetWidgetModel([]);
7775
this.renderCodeSnippetsSignal = new Signal<this, ICodeSnippet[]>(this);
7876
this.codeSnippetManager = CodeSnippetService.getCodeSnippetService();
7977

src/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,13 @@ import codeSnippetIconSVGstr from '../style/icon/jupyter_snippeticon.svg';
3434

3535
import { CodeSnippetInputDialog } from './CodeSnippetInputDialog';
3636
import { CodeSnippetWidget } from './CodeSnippetWidget';
37-
// import { CodeSnippetContentsService } from './CodeSnippetContentsService';
3837
import {
3938
CodeSnippetEditor,
4039
ICodeSnippetEditorMetadata,
4140
} from './CodeSnippetEditor';
4241
import { CodeSnippetService } from './CodeSnippetService';
4342
import { NotebookPanel } from '@jupyterlab/notebook';
4443
import { DocumentWidget } from '@jupyterlab/docregistry';
45-
// import { NotebookPanel } from '@jupyterlab/notebook';
46-
// import { ServerConnection, SettingManager } from '@jupyterlab/services';
47-
// import { URLExt } from '@jupyterlab/coreutils';
4844

4945
const CODE_SNIPPET_EXTENSION_ID = 'code-snippet-extension';
5046

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

0 commit comments

Comments
 (0)