Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Script Editor language-specific subclasses #2388

Merged
merged 7 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/python-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@jupyterlab/launcher": "^3.0.0",
"@jupyterlab/mainmenu": "^3.0.0",
"@jupyterlab/settingregistry": "^3.0.0",
"@jupyterlab/ui-components": "^3.0.0",
"@lumino/coreutils": "^1.5.3"
},
"devDependencies": {
Expand Down
100 changes: 100 additions & 0 deletions packages/python-editor/src/PythonEditor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2018-2021 Elyra Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ScriptEditor } from '@elyra/script-editor';
import { pyIcon } from '@elyra/ui-components';

import { CodeEditor, IEditorServices } from '@jupyterlab/codeeditor';
import {
ABCWidgetFactory,
DocumentRegistry,
DocumentWidget
} from '@jupyterlab/docregistry';
import { FileEditor } from '@jupyterlab/fileeditor';
import { LabIcon } from '@jupyterlab/ui-components';

export class PythonEditor extends ScriptEditor {
/**
* Construct a new Python Editor widget.
*/
constructor(
options: DocumentWidget.IOptions<FileEditor, DocumentRegistry.ICodeModel>
) {
super(options);
}
getLanguage(): string {
return 'python';
}

getIcon(): LabIcon {
return pyIcon;
}
}

/**
* A widget factory for Python Editors.
*/
export class PythonEditorFactory extends ABCWidgetFactory<
PythonEditor,
DocumentRegistry.ICodeModel
> {
/**
* Construct a new editor widget factory.
*/
constructor(options: PythonEditorFactory.IOptions) {
super(options.factoryOptions);
this._services = options.editorServices;
}

/**
* Create a new widget given a context.
*/
protected createNewWidget(
context: DocumentRegistry.CodeContext
): PythonEditor {
const newDocumentEditor = this._services.factoryService.newDocumentEditor;
const factory: CodeEditor.Factory = options => {
return newDocumentEditor(options);
};
const content = new FileEditor({
factory,
context,
mimeTypeService: this._services.mimeTypeService
});
return new PythonEditor({ content, context });
}

private _services: IEditorServices;
}

/**
* The namespace for `PythonEditorFactory` class statics.
*/
export namespace PythonEditorFactory {
/**
* The options used to create an editor widget factory.
*/
export interface IOptions {
/**
* The editor services used by the factory.
*/
editorServices: IEditorServices;

/**
* The factory options associated with the factory.
*/
factoryOptions: DocumentRegistry.IWidgetFactoryOptions<PythonEditor>;
}
}
11 changes: 6 additions & 5 deletions packages/python-editor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import { ScriptEditorFactory, ScriptEditor } from '@elyra/script-editor';
import { pyIcon } from '@elyra/ui-components';

import {
Expand All @@ -33,6 +32,8 @@ import { ISettingRegistry } from '@jupyterlab/settingregistry';

import { JSONObject } from '@lumino/coreutils';

import { PythonEditorFactory, PythonEditor } from './PythonEditor';

const PYTHON_FACTORY = 'Python Editor';
const PYTHON = 'python';
const PYTHON_EDITOR_NAMESPACE = 'elyra-python-editor-extension';
Expand Down Expand Up @@ -70,7 +71,7 @@ const extension: JupyterFrontEndPlugin<void> = {
) => {
console.log('Elyra - python-editor extension is activated!');

const factory = new ScriptEditorFactory({
const factory = new PythonEditorFactory({
editorServices,
factoryOptions: {
name: PYTHON_FACTORY,
Expand All @@ -91,9 +92,9 @@ const extension: JupyterFrontEndPlugin<void> = {
const { restored } = app;

/**
* Track ScriptEditor widget on page refresh
* Track PythonEditor widget on page refresh
*/
const tracker = new WidgetTracker<ScriptEditor>({
const tracker = new WidgetTracker<PythonEditor>({
namespace: PYTHON_EDITOR_NAMESPACE
});

Expand Down Expand Up @@ -136,7 +137,7 @@ const extension: JupyterFrontEndPlugin<void> = {
/**
* Update the settings of a widget. Adapted from fileeditor-extension.
*/
const updateWidget = (widget: ScriptEditor): void => {
const updateWidget = (widget: PythonEditor): void => {
if (!editorTracker.has(widget)) {
(editorTracker as WidgetTracker<IDocumentWidget<FileEditor>>).add(
widget
Expand Down
96 changes: 96 additions & 0 deletions packages/r-editor/src/REditor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright 2018-2021 Elyra Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ScriptEditor } from '@elyra/script-editor';

import { CodeEditor, IEditorServices } from '@jupyterlab/codeeditor';
import {
ABCWidgetFactory,
DocumentRegistry,
DocumentWidget
} from '@jupyterlab/docregistry';
import { FileEditor } from '@jupyterlab/fileeditor';

export class REditor extends ScriptEditor {
/**
* Construct a new R Editor widget.
*/
constructor(
options: DocumentWidget.IOptions<FileEditor, DocumentRegistry.ICodeModel>
) {
super(options);
}
getLanguage(): string {
return 'R';
}

getIcon(): string {
return 'rIcon';
}
}

/**
* A widget factory for R Editors.
*/
export class REditorFactory extends ABCWidgetFactory<
REditor,
DocumentRegistry.ICodeModel
> {
/**
* Construct a new editor widget factory.
*/
constructor(options: REditorFactory.IOptions) {
super(options.factoryOptions);
this._services = options.editorServices;
}

/**
* Create a new widget given a context.
*/
protected createNewWidget(context: DocumentRegistry.CodeContext): REditor {
const newDocumentEditor = this._services.factoryService.newDocumentEditor;
const factory: CodeEditor.Factory = options => {
return newDocumentEditor(options);
};
const content = new FileEditor({
factory,
context,
mimeTypeService: this._services.mimeTypeService
});
return new REditor({ content, context });
}

private _services: IEditorServices;
}

/**
* The namespace for `REditorFactory` class statics.
*/
export namespace REditorFactory {
/**
* The options used to create an editor widget factory.
*/
export interface IOptions {
/**
* The editor services used by the factory.
*/
editorServices: IEditorServices;

/**
* The factory options associated with the factory.
*/
factoryOptions: DocumentRegistry.IWidgetFactoryOptions<REditor>;
}
}
11 changes: 6 additions & 5 deletions packages/r-editor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import { ScriptEditorFactory, ScriptEditor } from '@elyra/script-editor';
import { rIcon } from '@elyra/ui-components';

import {
Expand All @@ -33,6 +32,8 @@ import { ISettingRegistry } from '@jupyterlab/settingregistry';

import { JSONObject } from '@lumino/coreutils';

import { REditorFactory, REditor } from './REditor';

const R_FACTORY = 'R Editor';
const R = 'r';
const R_EDITOR_NAMESPACE = 'elyra-r-script-editor-extension';
Expand Down Expand Up @@ -70,7 +71,7 @@ const extension: JupyterFrontEndPlugin<void> = {
) => {
console.log('Elyra - r-editor extension is activated!');

const factory = new ScriptEditorFactory({
const factory = new REditorFactory({
editorServices,
factoryOptions: {
name: R_FACTORY,
Expand All @@ -91,9 +92,9 @@ const extension: JupyterFrontEndPlugin<void> = {
const { restored } = app;

/**
* Track ScriptEditor widget on page refresh
* Track REditor widget on page refresh
*/
const tracker = new WidgetTracker<ScriptEditor>({
const tracker = new WidgetTracker<REditor>({
namespace: R_EDITOR_NAMESPACE
});

Expand Down Expand Up @@ -136,7 +137,7 @@ const extension: JupyterFrontEndPlugin<void> = {
/**
* Update the settings of a widget. Adapted from fileeditor-extension.
*/
const updateWidget = (widget: ScriptEditor): void => {
const updateWidget = (widget: REditor): void => {
if (!editorTracker.has(widget)) {
(editorTracker as WidgetTracker<IDocumentWidget<FileEditor>>).add(
widget
Expand Down
1 change: 0 additions & 1 deletion packages/script-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"lab:uninstall": "jupyter labextension unlink --no-build"
},
"dependencies": {
"@elyra/ui-components": "3.5.0-dev",
"@jupyterlab/apputils": "^3.0.0",
"@jupyterlab/codeeditor": "^3.0.0",
"@jupyterlab/docregistry": "^3.0.0",
Expand Down
Loading