Skip to content

Commit

Permalink
submenu for file extensions is added
Browse files Browse the repository at this point in the history
  • Loading branch information
Meriem-BenIsmail committed Mar 20, 2024
1 parent 4fb78e1 commit bb2e881
Showing 1 changed file with 36 additions and 40 deletions.
76 changes: 36 additions & 40 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,21 @@ function addKernelMenuItems(
const { commands } = app;
const menu = new Menu({ commands: app.commands });
menu.title.label = 'Available Kernels Menu';
mainMenu.addMenu(menu, true);
mainMenu.addMenu(menu);
Object.entries(serviceManager.kernelspecs.specs.kernelspecs).forEach(
([key, value]: [string, any]) => {
const kernelMenu = new Menu({ commands: app.commands });
kernelMenu.title.label = value.display_name;
const language =
serviceManager.kernelspecs.specs.kernelspecs[key].language;
const defaultLanguages = EditorLanguageRegistry.getDefaultLanguages();
let fileExtensions: string[] | any;

defaultLanguages.forEach(item => {
if (item.name.toLocaleLowerCase() === language.toLocaleLowerCase()) {
fileExtensions = item.extensions;
}
});

const startNotebookCommand = `widgets:start-notebook-${key}`;
commands.addCommand(startNotebookCommand, {
Expand Down Expand Up @@ -97,50 +107,36 @@ function addKernelMenuItems(
}
});

const createFileCommand = `widgets:create-file-${key}`;
commands.addCommand(createFileCommand, {
label: `New ${key} file`,
execute: async () => {
const language =
serviceManager.kernelspecs.specs.kernelspecs[key].language;
const defaultLanguages = EditorLanguageRegistry.getDefaultLanguages();
let fileExtensions: any;
kernelMenu.addItem({ command: startNotebookCommand });
kernelMenu.addItem({ command: startConsoleCommand });

defaultLanguages.forEach(item => {
if (
item.name.toLocaleLowerCase() === language.toLocaleLowerCase()
) {
fileExtensions = item.extensions;
const fileSubMenu = new Menu({ commands: app.commands });
fileSubMenu.title.label = `Open a ${key} file`;
fileExtensions.forEach((extension: string) => {
const openFileCommand = `widgets:open-file-${key}-${extension}`;
commands.addCommand(openFileCommand, {
label: `${extension} file`,
execute: async () => {
try {
const model = await serviceManager.contents.newUntitled({
type: 'file',
path: '.',
ext: extension,
language: language
});

app.commands.execute('docmanager:open', {
path: model.path
});
} catch (error) {
console.error('Error creating untitled file:', error);
}
});

console.log(`File extensions for kernel '${key}':`, fileExtensions);

const randomExtension =
fileExtensions[Math.floor(Math.random() * fileExtensions.length)];
try {
const model = await serviceManager.contents.newUntitled({
type: 'file',
path: '.',
ext: randomExtension,
language: language
});

app.commands.execute('docmanager:open', {
path: model.path
});
} catch (error) {
console.error('Error creating untitled file:', error);
}
console.log(
`File with extension '${randomExtension}' created successfully in kernel '${key}'.`
);
}
});
fileSubMenu.addItem({ command: openFileCommand });
});

kernelMenu.addItem({ command: startNotebookCommand });
kernelMenu.addItem({ command: startConsoleCommand });
kernelMenu.addItem({ command: createFileCommand });
kernelMenu.addItem({ type: 'submenu', submenu: fileSubMenu });

menu.addItem({ type: 'submenu', submenu: kernelMenu });
}
Expand Down

0 comments on commit bb2e881

Please sign in to comment.