mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-17 17:22:42 -05:00
Split notebookcell (#17185)
* Initial Split code * minor change * minor changes * Added split cell button to initActionBar, created split cell class to pss cell context. * added changes * fixed index * Split Cell Working in markdown mode * Fixed highlighting * Preserve the edit state * Added new icon and updated styles and cellToolbar component with new icon name. * Addressed PR * Addressed PR * Added back isEditMode flag * Moved split action to after edit toggle. * Fixed typo * Addressed PR * Addressed PR * Removed deletion of the cell * fixed the comments Co-authored-by: Hale Rankin <harankin@microsoft.com>
This commit is contained in:
@@ -14,7 +14,7 @@ import { NotebookChangeType, CellType, CellTypes } from 'sql/workbench/services/
|
||||
import { KernelsLanguage, nbversion } from 'sql/workbench/services/notebook/common/notebookConstants';
|
||||
import * as notebookUtils from 'sql/workbench/services/notebook/browser/models/notebookUtils';
|
||||
import * as TelemetryKeys from 'sql/platform/telemetry/common/telemetryKeys';
|
||||
import { IExecuteManager, SQL_NOTEBOOK_PROVIDER, DEFAULT_NOTEBOOK_PROVIDER, ISerializationManager } from 'sql/workbench/services/notebook/browser/notebookService';
|
||||
import { IExecuteManager, SQL_NOTEBOOK_PROVIDER, DEFAULT_NOTEBOOK_PROVIDER, ISerializationManager, INotebookService } from 'sql/workbench/services/notebook/browser/notebookService';
|
||||
import { NotebookContexts } from 'sql/workbench/services/notebook/browser/models/notebookContexts';
|
||||
import { IConnectionProfile } from 'sql/platform/connection/common/interfaces';
|
||||
import { INotification, Severity, INotificationService } from 'vs/platform/notification/common/notification';
|
||||
@@ -32,6 +32,9 @@ import { IConnectionManagementService } from 'sql/platform/connection/common/con
|
||||
import { values } from 'vs/base/common/collections';
|
||||
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
|
||||
import { isUUID } from 'vs/base/common/uuid';
|
||||
import { TextModel } from 'vs/editor/common/model/textModel';
|
||||
import { QueryTextEditor } from 'sql/workbench/browser/modelComponents/queryTextEditor';
|
||||
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget';
|
||||
|
||||
/*
|
||||
* Used to control whether a message in a dialog/wizard is displayed as an error,
|
||||
@@ -121,7 +124,6 @@ export class NotebookModel extends Disposable implements INotebookModel {
|
||||
@IConnectionManagementService private connectionManagementService: IConnectionManagementService,
|
||||
@IConfigurationService private configurationService: IConfigurationService,
|
||||
@ICapabilitiesService private _capabilitiesService?: ICapabilitiesService
|
||||
|
||||
) {
|
||||
super();
|
||||
if (!_notebookOptions || !_notebookOptions.notebookUri || !_notebookOptions.executeManagers) {
|
||||
@@ -538,8 +540,127 @@ export class NotebookModel extends Disposable implements INotebookModel {
|
||||
if (this.inErrorState) {
|
||||
return undefined;
|
||||
}
|
||||
let cell = this.createCell(cellType);
|
||||
|
||||
let cell = this.createCell(cellType);
|
||||
return this.insertCell(cell, index);
|
||||
}
|
||||
|
||||
public splitCell(cellType: CellType, notebookService: INotebookService, index?: number): ICellModel | undefined {
|
||||
if (this.inErrorState) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let notebookEditor = notebookService.findNotebookEditor(this.notebookUri);
|
||||
let cellEditorProvider = notebookEditor.cellEditors.find(e => e.cellGuid() === this.cells[index].cellGuid);
|
||||
//Only split the cell if the markdown editor is open.
|
||||
//TODO: Need to handle splitting of cell if the selection is on webview
|
||||
if (cellEditorProvider) {
|
||||
let editor = cellEditorProvider.getEditor() as QueryTextEditor;
|
||||
if (editor) {
|
||||
let editorControl = editor.getControl() as CodeEditorWidget;
|
||||
|
||||
let model = editorControl.getModel() as TextModel;
|
||||
let range = model.getFullModelRange();
|
||||
let selection = editorControl.getSelection();
|
||||
let source = this.cells[index].source;
|
||||
let newCell = undefined, tailCell = undefined, partialSource = undefined;
|
||||
let newCellIndex = index;
|
||||
let tailCellIndex = index;
|
||||
|
||||
// Save UI state
|
||||
let showMarkdown = this.cells[index].showMarkdown;
|
||||
let showPreview = this.cells[index].showPreview;
|
||||
|
||||
//Get selection value from current cell
|
||||
let newCellContent = model.getValueInRange(selection);
|
||||
|
||||
//Get content after selection
|
||||
let tailRange = range.setStartPosition(selection.endLineNumber, selection.endColumn);
|
||||
let tailCellContent = model.getValueInRange(tailRange);
|
||||
|
||||
//Get content before selection
|
||||
let headRange = range.setEndPosition(selection.startLineNumber, selection.startColumn);
|
||||
let headContent = model.getValueInRange(headRange);
|
||||
|
||||
// If the selection is equal to entire content then do nothing
|
||||
if (headContent.length === 0 && tailCellContent.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
//Set content before selection if the selection is not the same as original content
|
||||
if (headContent.length) {
|
||||
let headsource = source.slice(range.startLineNumber - 1, selection.startLineNumber - 1);
|
||||
if (selection.startColumn > 1) {
|
||||
partialSource = source.slice(selection.startLineNumber - 1, selection.startLineNumber)[0].slice(0, selection.startColumn - 1);
|
||||
headsource = headsource.concat(partialSource.toString());
|
||||
}
|
||||
this.cells[index].source = headsource;
|
||||
}
|
||||
|
||||
if (newCellContent.length) {
|
||||
let newSource = source.slice(selection.startLineNumber - 1, selection.endLineNumber) as string[];
|
||||
if (selection.startColumn > 1) {
|
||||
partialSource = source.slice(selection.startLineNumber - 1)[0].slice(selection.startColumn - 1);
|
||||
newSource.splice(0, 1, partialSource);
|
||||
}
|
||||
if (selection.endColumn !== source[selection.endLineNumber - 1].length) {
|
||||
let splicestart = 0;
|
||||
if (selection.startLineNumber === selection.endLineNumber) {
|
||||
splicestart = selection.startColumn - 1;
|
||||
}
|
||||
let partial = source.slice(selection.endLineNumber - 1, selection.endLineNumber)[0].slice(splicestart, selection.endColumn - 1);
|
||||
newSource.splice(newSource.length - 1, 1, partial);
|
||||
}
|
||||
//If the selection is not from the start of the cell, create a new cell.
|
||||
if (headContent.length) {
|
||||
newCell = this.createCell(cellType);
|
||||
newCell.source = newSource;
|
||||
newCellIndex++;
|
||||
this.insertCell(newCell, newCellIndex);
|
||||
}
|
||||
else { //update the existing cell
|
||||
this.cells[index].source = newSource;
|
||||
}
|
||||
}
|
||||
|
||||
if (tailCellContent.length) {
|
||||
//tail cell will be of original cell type.
|
||||
tailCell = this.createCell(this._cells[index].cellType);
|
||||
let tailSource = source.slice(tailRange.startLineNumber - 1) as string[];
|
||||
if (selection.endColumn > 1) {
|
||||
partialSource = source.slice(tailRange.startLineNumber - 1, tailRange.startLineNumber)[0].slice(tailRange.startColumn - 1);
|
||||
tailSource.splice(0, 1, partialSource);
|
||||
}
|
||||
tailCell.source = tailSource;
|
||||
tailCellIndex = newCellIndex + 1;
|
||||
this.insertCell(tailCell, tailCellIndex);
|
||||
}
|
||||
|
||||
let activeCell = newCell ? newCell : (headContent.length ? tailCell : this.cells[index]);
|
||||
let activeCellIndex = newCell ? newCellIndex : (headContent.length ? tailCellIndex : index);
|
||||
|
||||
//make new cell Active
|
||||
this.updateActiveCell(activeCell);
|
||||
activeCell.isEditMode = true;
|
||||
this._contentChangedEmitter.fire({
|
||||
changeType: NotebookChangeType.CellsModified,
|
||||
cells: [activeCell],
|
||||
cellIndex: activeCellIndex
|
||||
});
|
||||
activeCell.showMarkdown = showMarkdown;
|
||||
activeCell.showPreview = showPreview;
|
||||
|
||||
//return inserted cell
|
||||
return activeCell;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public insertCell(cell: ICellModel, index?: number): ICellModel | undefined {
|
||||
if (this.inErrorState) {
|
||||
return undefined;
|
||||
}
|
||||
if (index !== undefined && index !== null && index >= 0 && index < this._cells.length) {
|
||||
this._cells.splice(index, 0, cell);
|
||||
} else {
|
||||
@@ -554,7 +675,6 @@ export class NotebookModel extends Disposable implements INotebookModel {
|
||||
cells: [cell],
|
||||
cellIndex: index
|
||||
});
|
||||
|
||||
return cell;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user