Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Nov 23, 2024
1 parent c59b75f commit cd1f0be
Showing 1 changed file with 49 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,44 +45,64 @@ export class NotebookCellScopeHandler implements ScopeHandler {
);
}

const nb = getNotebook(editor);
const cells = getNotebookCells(editor, position, direction, hints);

if (nb == null) {
return;
for (const cell of cells) {
yield createTargetScope(cell);
}
}
}

const { notebook, cell } = nb;
function getNotebookCells(
editor: TextEditor,
position: Position,
direction: Direction,
hints: ScopeIteratorRequirements,
) {
const nb = getNotebook(editor);

if (hints.containment === "required") {
yield createTargetScope(cell);
return;
if (nb == null) {
return [];
}

const { notebook, cell } = nb;

if (hints.containment === "required") {
return [cell];
}

if (
hints.containment === "disallowed" ||
hints.containment === "disallowedIfStrict"
) {
return direction === "forward"
? notebook.cells.slice(cell.index + 1)
: notebook.cells.slice(0, cell.index).reverse();
}

// Every scope
if (hints.distalPosition != null) {
const searchRange = new Range(position, hints.distalPosition);
if (searchRange.isRangeEqual(editor.document.range)) {
return notebook.cells;
}
}

const cells = (() => {
if (
hints.containment === "disallowed" ||
hints.containment === "disallowedIfStrict"
) {
return direction === "forward"
? notebook.cells.slice(cell.index + 1)
: notebook.cells.slice(0, cell.index).reverse();
}
// Every scope
if (hints.distalPosition != null) {
const searchRange = new Range(position, hints.distalPosition);
if (searchRange.isRangeEqual(editor.document.range)) {
return notebook.cells;
}
}
return direction === "forward"
? notebook.cells.slice(cell.index)
: notebook.cells.slice(0, cell.index + 1).reverse();
})();
return direction === "forward"
? notebook.cells.slice(cell.index)
: notebook.cells.slice(0, cell.index + 1).reverse();
}

for (const cell of cells) {
yield createTargetScope(cell);
function getNotebook(editor: TextEditor) {
const uri = editor.document.uri.toString();
for (const notebook of ide().visibleNotebookEditors) {
for (const cell of notebook.cells) {
if (cell.document.uri.toString() === uri) {
return { notebook, cell };
}
}
}
return undefined;
}

function createTargetScope(cell: NotebookCell): TargetScope {
Expand All @@ -101,18 +121,6 @@ function createTargetScope(cell: NotebookCell): TargetScope {
};
}

function getNotebook(editor: TextEditor) {
const uri = editor.document.uri.toString();
for (const notebook of ide().visibleNotebookEditors) {
for (const cell of notebook.cells) {
if (cell.document.uri.toString() === uri) {
return { notebook, cell };
}
}
}
return undefined;
}

function getEditor(cell: NotebookCell) {
for (const editor of ide().visibleTextEditors) {
if (editor.document.uri.toString() === cell.document.uri.toString()) {
Expand Down

0 comments on commit cd1f0be

Please sign in to comment.