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

Support commenting cells outside of viewport + range tracking #225187

Merged
merged 8 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 3 additions & 3 deletions src/vs/editor/common/cursor/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,14 +723,14 @@ class AutoClosedAction {
}
}

interface IExecContext {
export interface IExecContext {
readonly model: ITextModel;
readonly selectionsBefore: Selection[];
readonly trackedRanges: string[];
readonly trackedRangesDirection: SelectionDirection[];
}

interface ICommandData {
export interface ICommandData {
operations: IIdentifiedSingleEditOperation[];
hadTrackedEditOperation: boolean;
}
Expand All @@ -740,7 +740,7 @@ interface ICommandsData {
hadTrackedEditOperation: boolean;
}

class CommandExecutor {
export class CommandExecutor {

public static executeCommands(model: ITextModel, selectionsBefore: Selection[], commands: (editorCommon.ICommand | null)[]): Selection[] | null {

Expand Down
63 changes: 32 additions & 31 deletions src/vs/workbench/contrib/notebook/browser/controller/editActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { Mimes } from 'vs/base/common/mime';
import { URI } from 'vs/base/common/uri';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { Selection } from 'vs/editor/common/core/selection';
import { ICommand } from 'vs/editor/common/editorCommon';
import { CommandExecutor } from 'vs/editor/common/cursor/cursor';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { ILanguageService } from 'vs/editor/common/languages/language';
import { ILanguageConfigurationService } from 'vs/editor/common/languages/languageConfigurationRegistry';
import { ITextModel, TrackedRangeStickiness } from 'vs/editor/common/model';
import { getIconClasses } from 'vs/editor/common/services/getIconClasses';
import { IModelService } from 'vs/editor/common/services/model';
import { LineCommentCommand, Type } from 'vs/editor/contrib/comment/browser/lineCommentCommand';
Expand Down Expand Up @@ -40,6 +40,7 @@ import { INotebookKernelService } from 'vs/workbench/contrib/notebook/common/not
import { ICellRange } from 'vs/workbench/contrib/notebook/common/notebookRange';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { ILanguageDetectionService } from 'vs/workbench/services/languageDetection/common/languageDetectionWorkerService';
import { TextModel } from 'vs/editor/common/model/textModel';
Yoyokrazy marked this conversation as resolved.
Show resolved Hide resolved

const CLEAR_ALL_CELLS_OUTPUTS_COMMAND_ID = 'notebook.clearAllCellsOutputs';
const EDIT_CELL_COMMAND_ID = 'notebook.cell.edit';
Expand Down Expand Up @@ -646,44 +647,44 @@ registerAction2(class CommentSelectedCellsAction extends NotebookMultiCellAction
async runWithContext(accessor: ServicesAccessor, context: INotebookCommandContext): Promise<void> {
const languageConfigurationService = accessor.get(ILanguageConfigurationService);

const selectedCellEditors: ICodeEditor[] = [];
context.selectedCells.forEach(cell => {
const findContext = { notebookEditor: context.notebookEditor, cell };
const foundEditor = findTargetCellEditor(findContext, cell);
if (foundEditor) {
selectedCellEditors.push(foundEditor);
}
});


selectedCellEditors.forEach(editor => {
if (!editor.hasModel()) {
context.selectedCells.forEach(async cellViewModel => {
const cellTextModel = cellViewModel.model;
if (!cellTextModel) {
Yoyokrazy marked this conversation as resolved.
Show resolved Hide resolved
return;
}
const textBuffer = cellTextModel.textBuffer;

const model = editor.getModel();
const commands: ICommand[] = [];
const modelOptions = model.getOptions();
const commentsOptions = editor.getOption(EditorOption.comments);

const selection = editor.getSelection();
let textModel: ITextModel | TextModel | undefined = cellTextModel.textModel;
Yoyokrazy marked this conversation as resolved.
Show resolved Hide resolved
if (!textModel) {
textModel = await cellViewModel.resolveTextModel();
}
Yoyokrazy marked this conversation as resolved.
Show resolved Hide resolved

commands.push(new LineCommentCommand(
const commentsOptions = cellViewModel.commentOptions;
const cellCommentCommand = new LineCommentCommand(
languageConfigurationService,
new Selection(1, 1, model.getLineCount(), model.getLineMaxColumn(model.getLineCount())),
modelOptions.indentSize,
new Selection(1, 1, textBuffer.getLineCount(), textBuffer.getLineLength(textBuffer.getLineCount())), // comment the entire cell
textModel.getOptions().tabSize,
Type.Toggle,
commentsOptions.insertSpace,
commentsOptions.ignoreEmptyLines,
commentsOptions.insertSpace ?? true,
commentsOptions.ignoreEmptyLines ?? true,
false
));
);

editor.pushUndoStop();
editor.executeCommands(COMMENT_SELECTED_CELLS_ID, commands);
editor.pushUndoStop();
// store any selections that are in the cell, allows them to be shifted by comments and preserved
const cellEditorSelections = cellViewModel.getSelections();
const initialTrackedRangesIDs: string[] = cellEditorSelections.map(selection => {
return textModel._setTrackedRange(null, selection, TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges);
});

editor.setSelection(selection);
});
CommandExecutor.executeCommands(textModel, cellEditorSelections, [cellCommentCommand]);

const newTrackedSelections = initialTrackedRangesIDs.map(i => {
return textModel._getTrackedRange(i);
}).filter(r => !!r).map((range,) => {
return new Selection(range.startLineNumber, range.startColumn, range.endLineNumber, range.endColumn);
});
cellViewModel.setSelections(newTrackedSelections ?? []);
}); // end of cells forEach
}

});
5 changes: 4 additions & 1 deletion src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { INotebookKernel } from 'vs/workbench/contrib/notebook/common/notebookKe
import { NotebookOptions } from 'vs/workbench/contrib/notebook/browser/notebookOptions';
import { cellRangesToIndexes, ICellRange, reduceCellRanges } from 'vs/workbench/contrib/notebook/common/notebookRange';
import { IWebviewElement } from 'vs/workbench/contrib/webview/browser/webview';
import { IEditorOptions } from 'vs/editor/common/config/editorOptions';
import { IEditorCommentsOptions, IEditorOptions } from 'vs/editor/common/config/editorOptions';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { IObservable } from 'vs/base/common/observable';
Expand Down Expand Up @@ -256,6 +256,7 @@ export interface ICellViewModel extends IGenericCellViewModel {
readonly mime: string;
cellKind: CellKind;
lineNumbers: 'on' | 'off' | 'inherit';
commentOptions: IEditorCommentsOptions;
chatHeight: number;
commentHeight: number;
focusMode: CellFocusMode;
Expand All @@ -271,6 +272,7 @@ export interface ICellViewModel extends IGenericCellViewModel {
hasModel(): this is IEditableCellViewModel;
resolveTextModel(): Promise<ITextModel>;
getSelections(): Selection[];
setSelections(selections: Selection[]): void;
getSelectionsStartPosition(): IPosition[] | undefined;
getCellDecorations(): INotebookCellDecorationOptions[];
getCellStatusBarItems(): INotebookCellStatusBarItem[];
Expand Down Expand Up @@ -770,6 +772,7 @@ export interface INotebookEditorPane extends IEditorPaneWithSelection {

export interface IBaseCellEditorOptions extends IDisposable {
readonly value: IEditorOptions;
// readonly commentsValue: IEditorCommentsOptions;
readonly onDidChange: Event<void>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ export abstract class BaseCellViewModel extends Disposable {
this._editorViewStates = this._textEditor.saveViewState();
}

//! todo@Yoyokrazy update view state --> update cursor
//! get and save view states need to be public
Yoyokrazy marked this conversation as resolved.
Show resolved Hide resolved

private saveTransientState() {
if (!this._textEditor || !this._textEditor.hasModel()) {
return;
Expand Down Expand Up @@ -510,12 +513,24 @@ export abstract class BaseCellViewModel extends Disposable {

setSelections(selections: Selection[]) {
if (selections.length) {
this._textEditor?.setSelections(selections);
if (this._textEditor) {
this._textEditor?.setSelections(selections);
} else if (this._editorViewStates) {
this._editorViewStates.cursorState = selections.map(selection => {
return {
inSelectionMode: !selection.isEmpty(),
selectionStart: selection.getStartPosition(),
position: selection.getEndPosition(),
};
});
}
}
}

getSelections() {
return this._textEditor?.getSelections() || [];
return this._textEditor?.getSelections()
?? this._editorViewStates?.cursorState.map(state => new Selection(state.selectionStart.lineNumber, state.selectionStart.column, state.position.lineNumber, state.position.column))
?? [];
}

getSelectionsStartPosition(): IPosition[] | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { CellKind, INotebookFindOptions, NotebookCellOutputsSplice } from 'vs/wo
import { ICellExecutionError, ICellExecutionStateChangedEvent } from 'vs/workbench/contrib/notebook/common/notebookExecutionStateService';
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
import { BaseCellViewModel } from './baseCellViewModel';
import { IEditorCommentsOptions } from 'vs/editor/common/config/editorOptions';

export const outputDisplayLimit = 500;

Expand Down Expand Up @@ -137,6 +138,16 @@ export class CodeCellViewModel extends BaseCellViewModel implements ICellViewMod

readonly excecutionError = observableValue<ICellExecutionError | undefined>('excecutionError', undefined);

private _commentOptions: IEditorCommentsOptions;
public get commentOptions(): IEditorCommentsOptions {
return this._commentOptions;
}

public set commentOptions(newOptions: IEditorCommentsOptions) {
this._commentOptions = newOptions;
this._onDidChangeState.fire({ contentChanged: true });
}

constructor(
viewType: string,
model: NotebookCellTextModel,
Expand Down Expand Up @@ -196,6 +207,14 @@ export class CodeCellViewModel extends BaseCellViewModel implements ICellViewMod
layoutState: CellLayoutState.Uninitialized,
estimatedHasHorizontalScrolling: false
};

this._commentOptions = configurationService.getValue<IEditorCommentsOptions>('editor.comments');
this._register(configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('editor.comments')) {
this._commentOptions = configurationService.getValue<IEditorCommentsOptions>('editor.comments');
this._onDidChangeState.fire({ contentChanged: true });
}
}));
}

updateExecutionState(e: ICellExecutionStateChangedEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
import { NotebookOptionsChangeEvent } from 'vs/workbench/contrib/notebook/browser/notebookOptions';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { NotebookCellStateChangedEvent, NotebookLayoutInfo } from 'vs/workbench/contrib/notebook/browser/notebookViewEvents';
import { IEditorCommentsOptions } from 'vs/editor/common/config/editorOptions';

export class MarkupCellViewModel extends BaseCellViewModel implements ICellViewModel {

Expand Down Expand Up @@ -111,6 +112,16 @@ export class MarkupCellViewModel extends BaseCellViewModel implements ICellViewM
this._onDidChangeState.fire({ cellIsHoveredChanged: true });
}

private _commentOptions: IEditorCommentsOptions;
public get commentOptions(): IEditorCommentsOptions {
return this._commentOptions;
}

public set commentOptions(newOptions: IEditorCommentsOptions) {
this._commentOptions = newOptions;
this._onDidChangeState.fire({ contentChanged: true });
}

constructor(
viewType: string,
model: NotebookCellTextModel,
Expand Down Expand Up @@ -143,6 +154,14 @@ export class MarkupCellViewModel extends BaseCellViewModel implements ICellViewM
statusBarHeight: 0
};

this._commentOptions = configurationService.getValue<IEditorCommentsOptions>('editor.comments');
Yoyokrazy marked this conversation as resolved.
Show resolved Hide resolved
this._register(configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('editor.comments')) {
this._commentOptions = configurationService.getValue<IEditorCommentsOptions>('editor.comments');
this._onDidChangeState.fire({ contentChanged: true });
}
}));

this._register(this.onDidChangeState(e => {
this.viewContext.eventDispatcher.emit([new NotebookCellStateChangedEvent(e, this.model)]);

Expand Down
Loading