Skip to content

Commit cd1f0be

Browse files
Refactor
1 parent c59b75f commit cd1f0be

File tree

1 file changed

+49
-41
lines changed

1 file changed

+49
-41
lines changed

packages/cursorless-engine/src/processTargets/modifiers/scopeHandlers/NotebookCellScopeHandler.ts

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -45,44 +45,64 @@ export class NotebookCellScopeHandler implements ScopeHandler {
4545
);
4646
}
4747

48-
const nb = getNotebook(editor);
48+
const cells = getNotebookCells(editor, position, direction, hints);
4949

50-
if (nb == null) {
51-
return;
50+
for (const cell of cells) {
51+
yield createTargetScope(cell);
5252
}
53+
}
54+
}
5355

54-
const { notebook, cell } = nb;
56+
function getNotebookCells(
57+
editor: TextEditor,
58+
position: Position,
59+
direction: Direction,
60+
hints: ScopeIteratorRequirements,
61+
) {
62+
const nb = getNotebook(editor);
5563

56-
if (hints.containment === "required") {
57-
yield createTargetScope(cell);
58-
return;
64+
if (nb == null) {
65+
return [];
66+
}
67+
68+
const { notebook, cell } = nb;
69+
70+
if (hints.containment === "required") {
71+
return [cell];
72+
}
73+
74+
if (
75+
hints.containment === "disallowed" ||
76+
hints.containment === "disallowedIfStrict"
77+
) {
78+
return direction === "forward"
79+
? notebook.cells.slice(cell.index + 1)
80+
: notebook.cells.slice(0, cell.index).reverse();
81+
}
82+
83+
// Every scope
84+
if (hints.distalPosition != null) {
85+
const searchRange = new Range(position, hints.distalPosition);
86+
if (searchRange.isRangeEqual(editor.document.range)) {
87+
return notebook.cells;
5988
}
89+
}
6090

61-
const cells = (() => {
62-
if (
63-
hints.containment === "disallowed" ||
64-
hints.containment === "disallowedIfStrict"
65-
) {
66-
return direction === "forward"
67-
? notebook.cells.slice(cell.index + 1)
68-
: notebook.cells.slice(0, cell.index).reverse();
69-
}
70-
// Every scope
71-
if (hints.distalPosition != null) {
72-
const searchRange = new Range(position, hints.distalPosition);
73-
if (searchRange.isRangeEqual(editor.document.range)) {
74-
return notebook.cells;
75-
}
76-
}
77-
return direction === "forward"
78-
? notebook.cells.slice(cell.index)
79-
: notebook.cells.slice(0, cell.index + 1).reverse();
80-
})();
91+
return direction === "forward"
92+
? notebook.cells.slice(cell.index)
93+
: notebook.cells.slice(0, cell.index + 1).reverse();
94+
}
8195

82-
for (const cell of cells) {
83-
yield createTargetScope(cell);
96+
function getNotebook(editor: TextEditor) {
97+
const uri = editor.document.uri.toString();
98+
for (const notebook of ide().visibleNotebookEditors) {
99+
for (const cell of notebook.cells) {
100+
if (cell.document.uri.toString() === uri) {
101+
return { notebook, cell };
102+
}
84103
}
85104
}
105+
return undefined;
86106
}
87107

88108
function createTargetScope(cell: NotebookCell): TargetScope {
@@ -101,18 +121,6 @@ function createTargetScope(cell: NotebookCell): TargetScope {
101121
};
102122
}
103123

104-
function getNotebook(editor: TextEditor) {
105-
const uri = editor.document.uri.toString();
106-
for (const notebook of ide().visibleNotebookEditors) {
107-
for (const cell of notebook.cells) {
108-
if (cell.document.uri.toString() === uri) {
109-
return { notebook, cell };
110-
}
111-
}
112-
}
113-
return undefined;
114-
}
115-
116124
function getEditor(cell: NotebookCell) {
117125
for (const editor of ide().visibleTextEditors) {
118126
if (editor.document.uri.toString() === cell.document.uri.toString()) {

0 commit comments

Comments
 (0)