mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 10:58:30 -05:00
- Set markdown as language for markdown cell - Fix issue where after loading language from cell metadata, always override it. This made the "language" feature irrelevant in the cell. - Fixed tests with new behavior (assumption: cell-level language overrides notebook-level definition) and added new test to cover this too
This commit is contained in:
@@ -38,15 +38,15 @@ export class CellModel implements ICellModel {
|
|||||||
constructor(private factory: IModelFactory, cellData?: nb.ICellContents, private _options?: ICellModelOptions) {
|
constructor(private factory: IModelFactory, cellData?: nb.ICellContents, private _options?: ICellModelOptions) {
|
||||||
this.id = `${modelId++}`;
|
this.id = `${modelId++}`;
|
||||||
CellModel.CreateLanguageMappings();
|
CellModel.CreateLanguageMappings();
|
||||||
// Do nothing for now
|
|
||||||
if (cellData) {
|
if (cellData) {
|
||||||
|
// Read in contents if available
|
||||||
this.fromJSON(cellData);
|
this.fromJSON(cellData);
|
||||||
} else {
|
} else {
|
||||||
this._cellType = CellTypes.Code;
|
this._cellType = CellTypes.Code;
|
||||||
this._source = '';
|
this._source = '';
|
||||||
}
|
}
|
||||||
this._isEditMode = this._cellType !== CellTypes.Markdown;
|
this._isEditMode = this._cellType !== CellTypes.Markdown;
|
||||||
this.setDefaultLanguage();
|
this.ensureDefaultLanguage();
|
||||||
if (_options && _options.isTrusted) {
|
if (_options && _options.isTrusted) {
|
||||||
this._isTrusted = true;
|
this._isTrusted = true;
|
||||||
} else {
|
} else {
|
||||||
@@ -284,7 +284,7 @@ export class CellModel implements ICellModel {
|
|||||||
}
|
}
|
||||||
this._cellType = cell.cell_type;
|
this._cellType = cell.cell_type;
|
||||||
this._source = Array.isArray(cell.source) ? cell.source.join('') : cell.source;
|
this._source = Array.isArray(cell.source) ? cell.source.join('') : cell.source;
|
||||||
this._language = (cell.metadata && cell.metadata.language) ? cell.metadata.language : 'python';
|
this.setLanguageFromContents(cell);
|
||||||
if (cell.outputs) {
|
if (cell.outputs) {
|
||||||
for (let output of cell.outputs) {
|
for (let output of cell.outputs) {
|
||||||
// For now, we're assuming it's OK to save these as-is with no modification
|
// For now, we're assuming it's OK to save these as-is with no modification
|
||||||
@@ -293,6 +293,15 @@ export class CellModel implements ICellModel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private setLanguageFromContents(cell: nb.ICellContents): void {
|
||||||
|
if (cell.cell_type === CellTypes.Markdown) {
|
||||||
|
this._language = 'markdown';
|
||||||
|
} else if (cell.metadata && cell.metadata.language) {
|
||||||
|
this._language = cell.metadata.language;
|
||||||
|
}
|
||||||
|
// else skip, we set default language anyhow
|
||||||
|
}
|
||||||
|
|
||||||
private addOutput(output: nb.ICellOutput) {
|
private addOutput(output: nb.ICellOutput) {
|
||||||
this._normalize(output);
|
this._normalize(output);
|
||||||
this._outputs.push(output);
|
this._outputs.push(output);
|
||||||
@@ -327,8 +336,32 @@ export class CellModel implements ICellModel {
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
private setDefaultLanguage(): void {
|
/**
|
||||||
this._language = 'python';
|
* Ensures there is a default language set, if none was already defined.
|
||||||
|
* Will read information from the overall Notebook (passed as options to the model), or
|
||||||
|
* if all else fails default back to python.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private ensureDefaultLanguage(): void {
|
||||||
|
// See if language is already set / is known based on cell type
|
||||||
|
if (this.hasLanguage()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this._cellType === CellTypes.Markdown) {
|
||||||
|
this._language = 'markdown';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// try set it based on overall Notebook language
|
||||||
|
this.trySetLanguageFromLangInfo();
|
||||||
|
|
||||||
|
// fallback to python
|
||||||
|
if (!this._language) {
|
||||||
|
this._language = 'python';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private trySetLanguageFromLangInfo() {
|
||||||
// In languageInfo, set the language to the "name" property
|
// In languageInfo, set the language to the "name" property
|
||||||
// If the "name" property isn't defined, check the "mimeType" property
|
// If the "name" property isn't defined, check the "mimeType" property
|
||||||
// Otherwise, default to python as the language
|
// Otherwise, default to python as the language
|
||||||
@@ -338,16 +371,25 @@ export class CellModel implements ICellModel {
|
|||||||
// check the LanguageMapping to determine if a mapping is necessary (example 'pyspark' -> 'python')
|
// check the LanguageMapping to determine if a mapping is necessary (example 'pyspark' -> 'python')
|
||||||
if (CellModel.LanguageMapping[languageInfo.name]) {
|
if (CellModel.LanguageMapping[languageInfo.name]) {
|
||||||
this._language = CellModel.LanguageMapping[languageInfo.name];
|
this._language = CellModel.LanguageMapping[languageInfo.name];
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
this._language = languageInfo.name;
|
this._language = languageInfo.name;
|
||||||
}
|
}
|
||||||
} else if (languageInfo.mimetype) {
|
}
|
||||||
|
else if (languageInfo.mimetype) {
|
||||||
this._language = languageInfo.mimetype;
|
this._language = languageInfo.mimetype;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let mimeTypePrefix = 'x-';
|
|
||||||
if (this._language.includes(mimeTypePrefix)) {
|
if (this._language) {
|
||||||
this._language = this._language.replace(mimeTypePrefix, '');
|
let mimeTypePrefix = 'x-';
|
||||||
|
if (this._language.includes(mimeTypePrefix)) {
|
||||||
|
this._language = this._language.replace(mimeTypePrefix, '');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private hasLanguage(): boolean {
|
||||||
|
return !!this._language;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ describe('Cell Model', function (): void {
|
|||||||
let cellData: nb.ICellContents = {
|
let cellData: nb.ICellContents = {
|
||||||
cell_type: CellTypes.Code,
|
cell_type: CellTypes.Code,
|
||||||
source: 'print(\'1\')',
|
source: 'print(\'1\')',
|
||||||
metadata: { language: 'python'},
|
metadata: { },
|
||||||
execution_count: 1
|
execution_count: 1
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -105,6 +105,22 @@ describe('Cell Model', function (): void {
|
|||||||
let cell = factory.createCell(cellData, { notebook: notebookModel, isTrusted: false });
|
let cell = factory.createCell(cellData, { notebook: notebookModel, isTrusted: false });
|
||||||
should(cell.language).equal('scala');
|
should(cell.language).equal('scala');
|
||||||
});
|
});
|
||||||
|
it('Should keep cell language as python if cell has language override', async function (): Promise<void> {
|
||||||
|
let cellData: nb.ICellContents = {
|
||||||
|
cell_type: CellTypes.Code,
|
||||||
|
source: 'print(\'1\')',
|
||||||
|
metadata: { language: 'python'},
|
||||||
|
execution_count: 1
|
||||||
|
};
|
||||||
|
|
||||||
|
let notebookModel = new NotebookModelStub({
|
||||||
|
name: 'scala',
|
||||||
|
version: '',
|
||||||
|
mimetype: ''
|
||||||
|
});
|
||||||
|
let cell = factory.createCell(cellData, { notebook: notebookModel, isTrusted: false });
|
||||||
|
should(cell.language).equal('python');
|
||||||
|
});
|
||||||
|
|
||||||
it('Should set cell language to python if no language defined', async function (): Promise<void> {
|
it('Should set cell language to python if no language defined', async function (): Promise<void> {
|
||||||
let cellData: nb.ICellContents = {
|
let cellData: nb.ICellContents = {
|
||||||
@@ -127,7 +143,7 @@ describe('Cell Model', function (): void {
|
|||||||
let cellData: nb.ICellContents = {
|
let cellData: nb.ICellContents = {
|
||||||
cell_type: CellTypes.Code,
|
cell_type: CellTypes.Code,
|
||||||
source: 'std::cout << "hello world";',
|
source: 'std::cout << "hello world";',
|
||||||
metadata: { language: 'python'},
|
metadata: { },
|
||||||
execution_count: 1
|
execution_count: 1
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -144,7 +160,7 @@ describe('Cell Model', function (): void {
|
|||||||
let cellData: nb.ICellContents = {
|
let cellData: nb.ICellContents = {
|
||||||
cell_type: CellTypes.Code,
|
cell_type: CellTypes.Code,
|
||||||
source: 'print(\'1\')',
|
source: 'print(\'1\')',
|
||||||
metadata: { language: 'python'},
|
metadata: { },
|
||||||
execution_count: 1
|
execution_count: 1
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user