Files
azuredatastudio/src/sql/parts/notebook/models/cellMagicMapper.ts
Kevin Cunnane 1f501f4553 Improve cell language detection and add support for language magics (#4081)
* Move to using notebook language by default, with override in cell
* Update cell language on kernel change
* Tweak language logic so that it prefers code mirror mode, then falls back since this was failing some notebooks
* Add new package.json contribution to define language magics. These result in cell language changing. Language is cleared out on removing the language magic
* Added support for executing Python, R and Java in the SQL Kernel to prove this out. It converts to the sp_execute_external_script format

TODO in future PR:

* Need to hook up completion item support for magics (issue #4078)
* Should add indicator at the bottom of a cell when an alternate language has been detected (issue #4079)
* On executing Python, R or Java, should add some output showing the generated code (issue #4080)
2019-02-19 17:05:56 -08:00

56 lines
1.8 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { ICellMagicMapper, ILanguageMagic } from 'sql/parts/notebook/models/modelInterfaces';
const defaultKernel = '*';
export class CellMagicMapper implements ICellMagicMapper {
private kernelToMagicMap = new Map<string,ILanguageMagic[]>();
constructor(languageMagics: ILanguageMagic[]) {
if (languageMagics) {
for (let magic of languageMagics) {
if (!magic.kernels || magic.kernels.length === 0) {
this.addKernelMapping(defaultKernel, magic);
}
if (magic.kernels) {
for (let kernel of magic.kernels) {
this.addKernelMapping(kernel.toLowerCase(), magic);
}
}
}
}
}
private addKernelMapping(kernelId: string, magic: ILanguageMagic): void {
let magics = this.kernelToMagicMap.get(kernelId) || [];
magics.push(magic);
this.kernelToMagicMap.set(kernelId, magics);
}
private findMagicForKernel(searchText: string, kernelId: string): ILanguageMagic | undefined {
if (kernelId === undefined || !searchText) {
return undefined;
}
searchText = searchText.toLowerCase();
let kernelMagics = this.kernelToMagicMap.get(kernelId) || [];
if (kernelMagics) {
return kernelMagics.find(m => m.magic.toLowerCase() === searchText);
}
return undefined;
}
toLanguageMagic(magic: string, kernelId: string): ILanguageMagic {
let languageMagic = this.findMagicForKernel(magic, kernelId.toLowerCase());
if (!languageMagic) {
languageMagic = this.findMagicForKernel(magic, defaultKernel);
}
return languageMagic;
}
}