-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8cd7c21
commit c59b75f
Showing
16 changed files
with
282 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
.../cursorless-engine/src/processTargets/modifiers/scopeHandlers/NotebookCellScopeHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { | ||
Range, | ||
type Direction, | ||
type NotebookCell, | ||
type Position, | ||
type ScopeType, | ||
type TextEditor, | ||
} from "@cursorless/common"; | ||
import type { LanguageDefinitions } from "../../../languages/LanguageDefinitions"; | ||
import { ide } from "../../../singletons/ide.singleton"; | ||
import { NotebookCellTarget } from "../../targets"; | ||
import type { TargetScope } from "./scope.types"; | ||
import type { | ||
ScopeHandler, | ||
ScopeIteratorRequirements, | ||
} from "./scopeHandler.types"; | ||
|
||
export class NotebookCellScopeHandler implements ScopeHandler { | ||
public readonly scopeType = { type: "notebookCell" } as const; | ||
public readonly iterationScopeType = { type: "document" } as const; | ||
public readonly includeAdjacentInEvery = false; | ||
|
||
constructor( | ||
private languageDefinitions: LanguageDefinitions, | ||
_scopeType: ScopeType, | ||
private languageId: string, | ||
) {} | ||
|
||
*generateScopes( | ||
editor: TextEditor, | ||
position: Position, | ||
direction: Direction, | ||
hints: ScopeIteratorRequirements, | ||
): Iterable<TargetScope> { | ||
const scopeHandler = this.languageDefinitions | ||
.get(this.languageId) | ||
?.getScopeHandler(this.scopeType); | ||
|
||
if (scopeHandler != null) { | ||
yield* scopeHandler.generateScopeCandidates( | ||
editor, | ||
position, | ||
direction, | ||
hints, | ||
); | ||
} | ||
|
||
const nb = getNotebook(editor); | ||
|
||
if (nb == null) { | ||
return; | ||
} | ||
|
||
const { notebook, cell } = nb; | ||
|
||
if (hints.containment === "required") { | ||
yield createTargetScope(cell); | ||
return; | ||
} | ||
|
||
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(); | ||
})(); | ||
|
||
for (const cell of cells) { | ||
yield createTargetScope(cell); | ||
} | ||
} | ||
} | ||
|
||
function createTargetScope(cell: NotebookCell): TargetScope { | ||
const editor = getEditor(cell); | ||
const contentRange = editor.document.range; | ||
return { | ||
editor, | ||
domain: contentRange, | ||
getTargets: (isReversed: boolean) => [ | ||
new NotebookCellTarget({ | ||
editor, | ||
isReversed, | ||
contentRange, | ||
}), | ||
], | ||
}; | ||
} | ||
|
||
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()) { | ||
return editor; | ||
} | ||
} | ||
throw new Error("Editor not found notebook cell"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
packages/cursorless-engine/src/processTargets/modifiers/scopeTypeStages/NotebookCellStage.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.