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 all 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
2 changes: 1 addition & 1 deletion src/vs/editor/common/cursor/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
59 changes: 25 additions & 34 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 { 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 @@ -646,44 +646,35 @@ 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()) {
return;
}
context.selectedCells.forEach(async cellViewModel => {
const textModel = await cellViewModel.resolveTextModel();

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

const selection = editor.getSelection();

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, textModel.getLineCount(), textModel.getLineMaxColumn(textModel.getLineCount())), // comment the entire cell
textModel.getOptions().tabSize,
Type.Toggle,
commentsOptions.insertSpace,
commentsOptions.ignoreEmptyLines,
commentsOptions.insertSpace ?? true,
commentsOptions.ignoreEmptyLines ?? true,
false
));
);

// 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.pushUndoStop();
editor.executeCommands(COMMENT_SELECTED_CELLS_ID, commands);
editor.pushUndoStop();
CommandExecutor.executeCommands(textModel, cellEditorSelections, [cellCommentCommand]);

editor.setSelection(selection);
});
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
}

});
4 changes: 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Disposable, IDisposable, IReference, MutableDisposable, dispose } from
import { Mimes } from 'vs/base/common/mime';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { IEditorCommentsOptions } from 'vs/editor/common/config/editorOptions';
import { IPosition } from 'vs/editor/common/core/position';
import { IRange, Range } from 'vs/editor/common/core/range';
import { Selection } from 'vs/editor/common/core/selection';
Expand Down Expand Up @@ -85,6 +86,15 @@ export abstract class BaseCellViewModel extends Disposable {
this._onDidChangeState.fire({ cellLineNumberChanged: true });
}

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

public set commentOptions(newOptions: IEditorCommentsOptions) {
this._commentOptions = newOptions;
}

private _focusMode: CellFocusMode = CellFocusMode.Container;
get focusMode() {
return this._focusMode;
Expand Down Expand Up @@ -208,6 +218,13 @@ export abstract class BaseCellViewModel extends Disposable {
if (this.model.collapseState?.outputCollapsed) {
this._outputCollapsed = true;
}

this._commentOptions = this._configurationService.getValue<IEditorCommentsOptions>('editor.comments', { overrideIdentifier: this.language });
this._register(this._configurationService.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('editor.comments')) {
this._commentOptions = this._configurationService.getValue<IEditorCommentsOptions>('editor.comments', { overrideIdentifier: this.language });
}
}));
}


Expand Down Expand Up @@ -510,12 +527,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
Loading