1+ import { JupyterFrontEnd } from '@jupyterlab/application' ;
12import { 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
47export 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
0 commit comments