|
| 1 | +import { |
| 2 | + Range, |
| 3 | + type Direction, |
| 4 | + type NotebookCell, |
| 5 | + type Position, |
| 6 | + type ScopeType, |
| 7 | + type TextEditor, |
| 8 | +} from "@cursorless/common"; |
| 9 | +import type { LanguageDefinitions } from "../../../languages/LanguageDefinitions"; |
| 10 | +import { ide } from "../../../singletons/ide.singleton"; |
| 11 | +import { NotebookCellTarget } from "../../targets"; |
| 12 | +import type { TargetScope } from "./scope.types"; |
| 13 | +import type { |
| 14 | + ScopeHandler, |
| 15 | + ScopeIteratorRequirements, |
| 16 | +} from "./scopeHandler.types"; |
| 17 | + |
| 18 | +export class NotebookCellScopeHandler implements ScopeHandler { |
| 19 | + public readonly scopeType = { type: "notebookCell" } as const; |
| 20 | + public readonly iterationScopeType = { type: "document" } as const; |
| 21 | + public readonly includeAdjacentInEvery = false; |
| 22 | + |
| 23 | + constructor( |
| 24 | + private languageDefinitions: LanguageDefinitions, |
| 25 | + _scopeType: ScopeType, |
| 26 | + private languageId: string, |
| 27 | + ) {} |
| 28 | + |
| 29 | + *generateScopes( |
| 30 | + editor: TextEditor, |
| 31 | + position: Position, |
| 32 | + direction: Direction, |
| 33 | + hints: ScopeIteratorRequirements, |
| 34 | + ): Iterable<TargetScope> { |
| 35 | + const scopeHandler = this.languageDefinitions |
| 36 | + .get(this.languageId) |
| 37 | + ?.getScopeHandler(this.scopeType); |
| 38 | + |
| 39 | + if (scopeHandler != null) { |
| 40 | + yield* scopeHandler.generateScopeCandidates( |
| 41 | + editor, |
| 42 | + position, |
| 43 | + direction, |
| 44 | + hints, |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + const nb = getNotebook(editor); |
| 49 | + |
| 50 | + if (nb == null) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const { notebook, cell } = nb; |
| 55 | + |
| 56 | + if (hints.containment === "required") { |
| 57 | + yield createTargetScope(cell); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 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 | + })(); |
| 81 | + |
| 82 | + for (const cell of cells) { |
| 83 | + yield createTargetScope(cell); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +function createTargetScope(cell: NotebookCell): TargetScope { |
| 89 | + const editor = getEditor(cell); |
| 90 | + const contentRange = editor.document.range; |
| 91 | + return { |
| 92 | + editor, |
| 93 | + domain: contentRange, |
| 94 | + getTargets: (isReversed: boolean) => [ |
| 95 | + new NotebookCellTarget({ |
| 96 | + editor, |
| 97 | + isReversed, |
| 98 | + contentRange, |
| 99 | + }), |
| 100 | + ], |
| 101 | + }; |
| 102 | +} |
| 103 | + |
| 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 | + |
| 116 | +function getEditor(cell: NotebookCell) { |
| 117 | + for (const editor of ide().visibleTextEditors) { |
| 118 | + if (editor.document.uri.toString() === cell.document.uri.toString()) { |
| 119 | + return editor; |
| 120 | + } |
| 121 | + } |
| 122 | + throw new Error("Editor not found notebook cell"); |
| 123 | +} |
0 commit comments