Skip to content

Commit 59845aa

Browse files
authored
Merge pull request #182 from jahn96/defaultLanguage
Default language
2 parents 4aad202 + b6d4427 commit 59845aa

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

src/CodeSnippetDisplay.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
pythonIcon,
2727
fileIcon,
2828
rKernelIcon,
29+
markdownIcon,
2930
} from '@jupyterlab/ui-components';
3031
import { CodeEditor, IEditorServices } from '@jupyterlab/codeeditor';
3132
import * as nbformat from '@jupyterlab/nbformat';
@@ -669,6 +670,18 @@ export class CodeSnippetDisplay extends React.Component<
669670
/>
670671
);
671672
}
673+
case 'Gfm': {
674+
return (
675+
<markdownIcon.react
676+
tag="span"
677+
height="16px"
678+
width="16px"
679+
right="7px"
680+
top="5px"
681+
margin-right="3px"
682+
/>
683+
);
684+
}
672685
case 'Java': {
673686
return (
674687
<javaIcon.react

src/CodeSnippetInputDialog.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ export interface IFileContainer extends JSONObject {
5353
export function CodeSnippetInputDialog(
5454
codeSnippetWidget: CodeSnippetWidget,
5555
code: string[],
56+
language: string,
5657
idx: number
5758
): Promise<Contents.IModel | null> {
5859
const tags: string[] = [];
5960
const codeSnippetManager = CodeSnippetService.getCodeSnippetService();
6061

6162
const snippets = codeSnippetManager.snippets;
6263

64+
// get all active tags
6365
for (const snippet of snippets) {
6466
if (snippet.tags) {
6567
for (const tag of snippet.tags) {
@@ -70,14 +72,15 @@ export function CodeSnippetInputDialog(
7072
}
7173
}
7274

73-
const body: InputHandler = new InputHandler(tags);
75+
const body: InputHandler = new InputHandler(tags, language);
7476

7577
return showInputDialog(
7678
codeSnippetWidget,
7779
tags,
7880
idx,
7981
codeSnippetManager,
8082
code,
83+
language,
8184
body
8285
);
8386
}
@@ -91,6 +94,7 @@ export function showInputDialog(
9194
idx: number,
9295
codeSnippetManager: CodeSnippetService,
9396
code: string[],
97+
language: string,
9498
body: InputHandler
9599
): Promise<Contents.IModel | null> {
96100
return showCodeSnippetForm({
@@ -112,6 +116,7 @@ export function showInputDialog(
112116
idx,
113117
codeSnippetManager,
114118
code,
119+
language,
115120
body
116121
);
117122
} else {
@@ -255,8 +260,8 @@ class InputHandler extends Widget {
255260
* Construct a new "code snippet" dialog.
256261
* readonly inputNode: HTMLInputElement; <--- in Widget class
257262
*/
258-
constructor(tags: string[]) {
259-
super({ node: Private.createInputNode(tags) });
263+
constructor(tags: string[], language: string) {
264+
super({ node: Private.createInputNode(tags, language) });
260265
this.addClass(FILE_DIALOG_CLASS);
261266
}
262267

@@ -300,7 +305,7 @@ class Private {
300305
/**
301306
* Create the node for a code snippet form handler. This is what's creating all of the elements to be displayed.
302307
*/
303-
static createInputNode(tags: string[]): HTMLElement {
308+
static createInputNode(tags: string[], language: string): HTMLElement {
304309
Private.allTags = tags;
305310
const body = document.createElement('form');
306311

@@ -324,15 +329,17 @@ class Private {
324329
const languageInput = document.createElement('input');
325330
languageInput.className = CODE_SNIPPET_DIALOG_INPUT;
326331
languageInput.setAttribute('list', 'languages');
332+
// capitalize the first character
333+
languageInput.value = language[0].toUpperCase() + language.slice(1);
327334
languageInput.required = true;
328335
const languageOption = document.createElement('datalist');
329336
languageOption.id = 'languages';
330337
languageOption.onblur = Private.handleOnBlur;
331338

332339
SUPPORTED_LANGUAGES.sort();
333-
for (const language of SUPPORTED_LANGUAGES) {
340+
for (const supportedLanguage of SUPPORTED_LANGUAGES) {
334341
const option = document.createElement('option');
335-
option.value = language;
342+
option.value = supportedLanguage;
336343
languageOption.appendChild(option);
337344
}
338345

src/CodeSnippetLanguages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export const SUPPORTED_LANGUAGES = [
6767
'F#',
6868
'Go',
6969
'Galileo',
70+
'Gfm',
7071
'Erlang',
7172
'PARI/GP',
7273
'Aldor',

src/CodeSnippetWidget.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { CodeSnippetDisplay } from './CodeSnippetDisplay';
3131
import { CodeSnippetInputDialog } from './CodeSnippetInputDialog';
3232

3333
import React from 'react';
34+
import { Notebook } from '@jupyterlab/notebook';
3435

3536
/**
3637
* A class used to indicate a snippet item.
@@ -245,6 +246,13 @@ export class CodeSnippetWidget extends ReactWidget {
245246
* Handle the `'lm-drop'` event for the widget.
246247
*/
247248
private async _evtDrop(event: IDragEvent): Promise<void> {
249+
// TODO: get language from kernel
250+
const notebook: Notebook = event.mimeData.getData('internal:cells')[0]
251+
.parent;
252+
253+
const language = notebook.model.defaultKernelLanguage;
254+
console.log(language);
255+
248256
const data = this.findCellData(event.mimeData);
249257
if (data === undefined) {
250258
return;
@@ -300,7 +308,8 @@ export class CodeSnippetWidget extends ReactWidget {
300308
} else {
301309
// Handle the case where we are copying cells
302310
event.dropAction = 'copy';
303-
CodeSnippetInputDialog(this, data, idx);
311+
312+
CodeSnippetInputDialog(this, data, language, idx);
304313
}
305314

306315
// Reorder snippet just to make sure id's are in order.

src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ import {
4040
ICodeSnippetEditorMetadata,
4141
} from './CodeSnippetEditor';
4242
import { CodeSnippetService } from './CodeSnippetService';
43+
import { NotebookPanel } from '@jupyterlab/notebook';
44+
import { DocumentWidget } from '@jupyterlab/docregistry';
45+
// import { NotebookPanel } from '@jupyterlab/notebook';
46+
// import { ServerConnection, SettingManager } from '@jupyterlab/services';
47+
// import { URLExt } from '@jupyterlab/coreutils';
4348

4449
const CODE_SNIPPET_EXTENSION_ID = 'code-snippet-extension';
4550

@@ -178,6 +183,16 @@ function activateCodeSnippet(
178183
isToggled: () => toggled,
179184
iconClass: 'some-css-icon-class',
180185
execute: () => {
186+
let language = '';
187+
// get the language of document or notebook
188+
if (app.shell.currentWidget instanceof NotebookPanel) {
189+
language = (app.shell.currentWidget as NotebookPanel).sessionContext
190+
.kernelPreference.language;
191+
} else if (app.shell.currentWidget instanceof DocumentWidget) {
192+
language = (app.shell.currentWidget as DocumentWidget).context.model
193+
.defaultKernelLanguage;
194+
}
195+
181196
const highlightedCode = getSelectedText();
182197
if (highlightedCode === '') {
183198
//if user just right-clicks cell(s) to save
@@ -202,12 +217,14 @@ function activateCodeSnippet(
202217
CodeSnippetInputDialog(
203218
codeSnippetWidget,
204219
resultArray,
220+
language,
205221
codeSnippetWidget.codeSnippetManager.snippets.length
206222
);
207223
} else {
208224
CodeSnippetInputDialog(
209225
codeSnippetWidget,
210226
highlightedCode.split('\n'),
227+
language,
211228
codeSnippetWidget.codeSnippetManager.snippets.length
212229
);
213230
}

0 commit comments

Comments
 (0)