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

Notebook cell scope provider #2693

Merged
merged 7 commits into from
Jan 19, 2025
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
30 changes: 0 additions & 30 deletions data/fixtures/recorded/selectionTypes/drinkCell.yml

This file was deleted.

41 changes: 0 additions & 41 deletions data/fixtures/recorded/selectionTypes/drinkCellEach.yml

This file was deleted.

29 changes: 0 additions & 29 deletions data/fixtures/recorded/selectionTypes/pourCell.yml

This file was deleted.

41 changes: 0 additions & 41 deletions data/fixtures/recorded/selectionTypes/pourCellEach.yml

This file was deleted.

7 changes: 6 additions & 1 deletion packages/common/src/ide/PassthroughIDEBase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { GeneralizedRange } from "../types/GeneralizedRange";
import type { NotebookEditor } from "../types/NotebookEditor";
import type { TextDocument } from "../types/TextDocument";
import type { EditableTextEditor, TextEditor } from "../types/TextEditor";
import type { Capabilities } from "./types/Capabilities";
Expand All @@ -17,9 +18,9 @@ import type {
RunMode,
WorkspaceFolder,
} from "./types/ide.types";
import type { KeyValueStore } from "./types/KeyValueStore";
import type { Messages } from "./types/Messages";
import type { QuickPickOptions } from "./types/QuickPickOptions";
import type { KeyValueStore } from "./types/KeyValueStore";

export default class PassthroughIDEBase implements IDE {
configuration: Configuration;
Expand Down Expand Up @@ -123,6 +124,10 @@ export default class PassthroughIDEBase implements IDE {
return this.original.visibleTextEditors;
}

public get visibleNotebookEditors(): NotebookEditor[] {
return this.original.visibleNotebookEditors;
}

public get cursorlessVersion(): string {
return this.original.cursorlessVersion;
}
Expand Down
6 changes: 5 additions & 1 deletion packages/common/src/ide/fake/FakeIDE.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pull } from "lodash-es";
import type { EditableTextEditor, TextEditor } from "../..";
import type { EditableTextEditor, NotebookEditor, TextEditor } from "../..";
import type { GeneralizedRange } from "../../types/GeneralizedRange";
import type { TextDocument } from "../../types/TextDocument";
import type { TextDocumentChangeEvent } from "../types/Events";
Expand Down Expand Up @@ -82,6 +82,10 @@ export class FakeIDE implements IDE {
throw Error("Not implemented");
}

get visibleNotebookEditors(): NotebookEditor[] {
throw Error("Not implemented");
}

public getEditableTextEditor(_editor: TextEditor): EditableTextEditor {
throw Error("Not implemented");
}
Expand Down
10 changes: 8 additions & 2 deletions packages/common/src/ide/types/ide.types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { URI } from "vscode-uri";
import type {
EditableTextEditor,
InputBoxOptions,
NotebookEditor,
TextDocument,
TextEditor,
} from "../..";
import type { URI } from "vscode-uri";
import type { GeneralizedRange } from "../../types/GeneralizedRange";
import type { Capabilities } from "./Capabilities";
import type { Clipboard } from "./Clipboard";
Expand All @@ -16,9 +17,9 @@ import type {
TextEditorVisibleRangesChangeEvent,
} from "./events.types";
import type { FlashDescriptor } from "./FlashDescriptor";
import type { KeyValueStore } from "./KeyValueStore";
import type { Messages } from "./Messages";
import type { QuickPickOptions } from "./QuickPickOptions";
import type { KeyValueStore } from "./KeyValueStore";

export type RunMode = "production" | "development" | "test";
export type HighlightId = string;
Expand Down Expand Up @@ -79,6 +80,11 @@ export interface IDE {
*/
readonly visibleTextEditors: TextEditor[];

/**
* The currently visible notebook editors or an empty array.
*/
readonly visibleNotebookEditors: NotebookEditor[];

/**
* The capabilities of the IDE
*/
Expand Down
6 changes: 4 additions & 2 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ export * from "./ide/types/Clipboard";
export * from "./ide/types/CommandHistoryStorage";
export * from "./ide/types/CommandId";
export * from "./ide/types/Configuration";
export * from "./ide/types/events.types";
export * from "./ide/types/Events";
export * from "./ide/types/events.types";
export * from "./ide/types/FileSystem.types";
export * from "./ide/types/FlashDescriptor";
export * from "./ide/types/Hats";
export * from "./ide/types/HatStability";
export * from "./ide/types/hatStyles.types";
export * from "./ide/types/ide.types";
export * from "./ide/types/KeyValueStore";
export * from "./ide/types/Messages";
export * from "./ide/types/Paths";
export * from "./ide/types/QuickPickOptions";
export * from "./ide/types/RawTreeSitterQueryProvider";
export * from "./ide/types/KeyValueStore";
export * from "./ide/types/TutorialContentProvider";
export * from "./ide/util/messages";
export * from "./scopeSupportFacets/languageScopeSupport";
Expand Down Expand Up @@ -66,6 +66,8 @@ export * from "./types/Edit";
export * from "./types/GeneralizedRange";
export * from "./types/HatTokenMap";
export * from "./types/InputBoxOptions";
export * from "./types/NotebookCell";
export * from "./types/NotebookEditor";
export * from "./types/Position";
export * from "./types/Range";
export * from "./types/RangeExpansionBehavior";
Expand Down
32 changes: 32 additions & 0 deletions packages/common/src/types/NotebookCell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { TextDocument } from "./TextDocument";

export interface NotebookCell {
/**
* The index of this cell in its containing notebook. The
* index is updated when a cell is moved within its notebook. The index is `-1`
* when the cell has been removed from its notebook.
*/
readonly index: number;

/**
* The kind of this cell.
*/
readonly kind: NotebookCellKind;

/**
* The {@link TextDocument} of this cell.
*/
readonly document: TextDocument;
}

export enum NotebookCellKind {
/**
* A markup-cell is formatted source that is used for display.
*/
Markup = 1,

/**
* A code-cell.
*/
Code = 2,
}
22 changes: 22 additions & 0 deletions packages/common/src/types/NotebookEditor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { URI } from "vscode-uri";
import type { NotebookCell } from "./NotebookCell";

export interface NotebookEditor {
/**
* The associated uri for this document.
*
* *Note* that most documents use the `file`-scheme, which means they are files on disk. However, **not** all documents are
* saved on disk and therefore the `scheme` must be checked before trying to access the underlying file or siblings on disk.
*/
readonly uri: URI;

/**
* The number of cells in the notebook.
*/
readonly cellCount: number;

/**
* The cells of this notebook.
*/
readonly cells: NotebookCell[];
}
6 changes: 1 addition & 5 deletions packages/common/src/types/TextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,8 @@ export interface EditableTextEditor extends TextEditor {

/**
* Edit a new new notebook cell above.
* @return A promise that resolves to a function that must be applied to any
* selections that should be updated as result of this operation. This is a
* horrible hack to work around the fact that in vscode the promise resolves
* before the edits have actually been performed.
*/
editNewNotebookCellAbove(): Promise<(selection: Selection) => Selection>;
editNewNotebookCellAbove(): Promise<void>;

/**
* Edit a new new notebook cell below.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Selection } from "@cursorless/common";
import { ide } from "../../singletons/ide.singleton";
import type { Destination } from "../../typings/target.types";
import { createThatMark, ensureSingleTarget } from "../../util/targetUtils";
Expand All @@ -23,18 +22,13 @@ export async function runEditNewNotebookCellTargets(

await actions.setSelection.run([destination.target]);

let modifyThatMark = (selection: Selection) => selection;
if (isAbove) {
modifyThatMark = await editor.editNewNotebookCellAbove();
await editor.editNewNotebookCellAbove();
} else {
await editor.editNewNotebookCellBelow();
}

const thatMark = createThatMark([destination.target.thatTarget]);

// Apply horrible hack to work around the fact that in vscode the promise
// resolves before the edits have actually been performed.
thatMark[0].selection = modifyThatMark(thatMark[0].selection);

return { thatSelections: thatMark };
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import type {
SimpleEveryScopeModifier,
} from "./modifiers/scopeTypeStages/LegacyContainingSyntaxScopeStage";
import { LegacyContainingSyntaxScopeStage } from "./modifiers/scopeTypeStages/LegacyContainingSyntaxScopeStage";
import { NotebookCellStage } from "./modifiers/scopeTypeStages/NotebookCellStage";

export class ModifierStageFactoryImpl implements ModifierStageFactory {
constructor(
Expand Down Expand Up @@ -135,8 +134,6 @@ export class ModifierStageFactoryImpl implements ModifierStageFactory {
modifier: ContainingScopeModifier | EveryScopeModifier,
): ModifierStage {
switch (modifier.scopeType.type) {
case "notebookCell":
return new NotebookCellStage(modifier);
default:
// Default to containing syntax scope using tree sitter
return new LegacyContainingSyntaxScopeStage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export function createContinuousRangeTarget(
includeStart: boolean,
includeEnd: boolean,
): Target {
if (startTarget.editor !== endTarget.editor) {
throw Error("Continuous targets must be in the same editor");
}

if (includeStart && includeEnd && isSameType(startTarget, endTarget)) {
const richTarget = startTarget.maybeCreateRichRangeTarget(
isReversed,
Expand Down
Loading
Loading