From f43b3508e99f39da843e95270b9a43b97e7a8ec5 Mon Sep 17 00:00:00 2001 From: Yurong He <43652751+YurongHe@users.noreply.github.com> Date: Fri, 15 Feb 2019 15:53:06 -0800 Subject: [PATCH] Added SQL notebook IntelliSense (#4064) * Added SQL notebook intelliSense * Resolved PR comments. * catch disconnect error --- .../notebook/cellViews/code.component.ts | 19 +++++++------------ src/sql/parts/notebook/models/cell.ts | 8 ++++++++ .../parts/notebook/models/notebookModel.ts | 12 ++++++------ src/sql/parts/notebook/notebook.component.ts | 10 ++++++++++ 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/sql/parts/notebook/cellViews/code.component.ts b/src/sql/parts/notebook/cellViews/code.component.ts index dcd801e70c..f467af1c73 100644 --- a/src/sql/parts/notebook/cellViews/code.component.ts +++ b/src/sql/parts/notebook/cellViews/code.component.ts @@ -10,7 +10,7 @@ import { CommonServiceInterface } from 'sql/services/common/commonServiceInterfa import { AngularDisposable } from 'sql/base/node/lifecycle'; import { QueryTextEditor } from 'sql/parts/modelComponents/queryTextEditor'; import { CellToggleMoreActions } from 'sql/parts/notebook/cellToggleMoreActions'; -import { ICellModel } from 'sql/parts/notebook/models/modelInterfaces'; +import { ICellModel, notebookConstants } from 'sql/parts/notebook/models/modelInterfaces'; import { Taskbar } from 'sql/base/browser/ui/taskbar/taskbar'; import { RunCellAction, CellContext } from 'sql/parts/notebook/cellViews/codeActions'; import { NotebookModel } from 'sql/parts/notebook/models/notebookModel'; @@ -23,8 +23,6 @@ import { IProgressService } from 'vs/platform/progress/common/progress'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { ITextModel } from 'vs/editor/common/model'; import { UntitledEditorInput } from 'vs/workbench/common/editor/untitledEditorInput'; -import URI from 'vs/base/common/uri'; -import { Schemas } from 'vs/base/common/network'; import * as DOM from 'vs/base/browser/dom'; import { IModeService } from 'vs/editor/common/services/modeService'; import { IModelService } from 'vs/editor/common/services/modelService'; @@ -121,6 +119,11 @@ export class CodeComponent extends AngularDisposable implements OnInit, OnChange if (propName === 'activeCellId') { let changedProp = changes[propName]; let isActive = this.cellModel.id === changedProp.currentValue; + if (isActive && this._model.defaultKernel.display_name === notebookConstants.SQL + && this.cellModel.cellType === CellTypes.Code + && this.cellModel.cellUri) { + this._model.notebookOptions.connectionService.connect(this._model.activeConnection, this.cellModel.cellUri.toString()).catch(e => console.log(e)); + } this.toggleMoreActionsButton(isActive); if (this._editor) { this._editor.toggleEditorSelected(isActive); @@ -156,7 +159,7 @@ export class CodeComponent extends AngularDisposable implements OnInit, OnChange this._editor.setVisible(true); this._editor.setMinimumHeight(this._minimumHeight); this._editor.setMaximumHeight(this._maximumHeight); - let uri = this.createUri(); + let uri = this.cellModel.cellUri; this._editorInput = instantiationService.createInstance(UntitledEditorInput, uri, false, this.cellModel.language, '', ''); this._editor.setInput(this._editorInput, undefined); this.setFocusAndScroll(); @@ -210,14 +213,6 @@ export class CodeComponent extends AngularDisposable implements OnInit, OnChange this._cellToggleMoreActions.onInit(this.moreActionsElementRef, this.model, this.cellModel); } - - private createUri(): URI { - let uri = URI.from({ scheme: Schemas.untitled, path: `notebook-editor-${this.cellModel.id}` }); - // Use this to set the internal (immutable) and public (shared with extension) uri properties - this.cellModel.cellUri = uri; - return uri; - } - /// Editor Functions private updateModel() { if (this._editorModel) { diff --git a/src/sql/parts/notebook/models/cell.ts b/src/sql/parts/notebook/models/cell.ts index f52f199073..9e8601e9f9 100644 --- a/src/sql/parts/notebook/models/cell.ts +++ b/src/sql/parts/notebook/models/cell.ts @@ -18,6 +18,7 @@ import { CellTypes, CellType, NotebookChangeType } from 'sql/parts/notebook/mode import { ICellModel } from 'sql/parts/notebook/models/modelInterfaces'; import { NotebookModel } from 'sql/parts/notebook/models/notebookModel'; import { INotificationService, Severity } from 'vs/platform/notification/common/notification'; +import { Schemas } from 'vs/base/common/network'; let modelId = 0; @@ -54,6 +55,7 @@ export class CellModel implements ICellModel { } else { this._isTrusted = false; } + this.createUri(); } public equals(other: ICellModel) { @@ -507,4 +509,10 @@ export class CellModel implements ICellModel { private hasLanguage(): boolean { return !!this._language; } + + private createUri(): void { + let uri = URI.from({ scheme: Schemas.untitled, path: `notebook-editor-${this.id}` }); + // Use this to set the internal (immutable) and public (shared with extension) uri properties + this.cellUri = uri; + } } diff --git a/src/sql/parts/notebook/models/notebookModel.ts b/src/sql/parts/notebook/models/notebookModel.ts index bea819609e..714b5e9ae3 100644 --- a/src/sql/parts/notebook/models/notebookModel.ts +++ b/src/sql/parts/notebook/models/notebookModel.ts @@ -13,7 +13,7 @@ import { Disposable } from 'vs/base/common/lifecycle'; import { CellModel } from './cell'; import { IClientSession, INotebookModel, IDefaultConnection, INotebookModelOptions, ICellModel, notebookConstants, NotebookContentChange } from './modelInterfaces'; -import { NotebookChangeType, CellType } from 'sql/parts/notebook/models/contracts'; +import { NotebookChangeType, CellType, CellTypes } from 'sql/parts/notebook/models/contracts'; import { nbversion } from '../notebookConstants'; import * as notebookUtils from '../notebookUtils'; import { INotebookManager, SQL_NOTEBOOK_PROVIDER, DEFAULT_NOTEBOOK_PROVIDER } from 'sql/workbench/services/notebook/common/notebookService'; @@ -68,7 +68,7 @@ export class NotebookModel extends Disposable implements INotebookModel { private _kernelDisplayNameToNotebookProviderIds: Map = new Map(); private _onValidConnectionSelected = new Emitter(); - constructor(private notebookOptions: INotebookModelOptions, startSessionImmediately?: boolean, private connectionProfile?: IConnectionProfile) { + constructor(public notebookOptions: INotebookModelOptions, startSessionImmediately?: boolean, private connectionProfile?: IConnectionProfile) { super(); if (!notebookOptions || !notebookOptions.notebookUri || !notebookOptions.notebookManagers) { throw new Error('path or notebook service not defined'); @@ -230,7 +230,7 @@ export class NotebookModel extends Disposable implements INotebookModel { return this._onProviderIdChanged.event; } - public get onValidConnectionSelected(): Event{ + public get onValidConnectionSelected(): Event { return this._onValidConnectionSelected.event; } @@ -430,7 +430,7 @@ export class NotebookModel extends Disposable implements INotebookModel { // Ensure that the kernel we try to switch to is a valid kernel; if not, use the default let kernelSpecs = this.getKernelSpecs(); if (kernelSpecs && kernelSpecs.length > 0 && kernelSpecs.findIndex(k => k.name === kernelSpec.name) < 0) { - kernelSpec = kernelSpecs.find(spec => spec.name === this.notebookManager.sessionManager.specs.defaultKernel); + kernelSpec = kernelSpecs.find(spec => spec.name === this.notebookManager.sessionManager.specs.defaultKernel); } if (this._activeClientSession && this._activeClientSession.isReady) { return this._activeClientSession.changeKernel(kernelSpec) @@ -692,8 +692,8 @@ export class NotebookModel extends Disposable implements INotebookModel { private getKernelSpecs(): nb.IKernelSpec[] { if (this.notebookManager && this.notebookManager.sessionManager && this.notebookManager.sessionManager.specs && this.notebookManager.sessionManager.specs.kernels) { - return this.notebookManager.sessionManager.specs.kernels; - } + return this.notebookManager.sessionManager.specs.kernels; + } return []; } diff --git a/src/sql/parts/notebook/notebook.component.ts b/src/sql/parts/notebook/notebook.component.ts index 797533292d..acc1b092b2 100644 --- a/src/sql/parts/notebook/notebook.component.ts +++ b/src/sql/parts/notebook/notebook.component.ts @@ -132,6 +132,7 @@ export class NotebookComponent extends AngularDisposable implements OnInit, OnDe } ngOnDestroy() { + this.disconnect(); this.dispose(); if (this.notebookService) { this.notebookService.removeNotebookEditor(this); @@ -165,6 +166,7 @@ export class NotebookComponent extends AngularDisposable implements OnInit, OnDe } if (cell !== this.model.activeCell) { if (this.model.activeCell) { + this.disconnect(); this.model.activeCell.active = false; } this._model.activeCell = cell; @@ -173,6 +175,14 @@ export class NotebookComponent extends AngularDisposable implements OnInit, OnDe } } + private disconnect() { + if (this._model.defaultKernel.display_name === notebookConstants.SQL) { + if (this._model.activeCell && this._model.activeCell.cellType === CellTypes.Code && this._model.activeCell.cellUri) { + this.connectionManagementService.disconnect(this._model.activeCell.cellUri.toString()).catch(e => console.log(e)); + } + } + } + public unselectActiveCell() { if (this.model && this.model.activeCell) { this.model.activeCell.active = false;