From 4bd7f91c54afee8c5fdfb61c49356f4c356eee1f Mon Sep 17 00:00:00 2001 From: Pokey Rule <755842+pokey@users.noreply.github.com> Date: Fri, 14 Jun 2024 14:49:05 +0100 Subject: [PATCH 1/2] Support markdown code blocks as `"cell"` --- .../scopes/markdown/notebookCell.scope | 57 +++++++++++++++++++ .../scopeSupportFacetInfos.ts | 5 ++ .../scopeSupportFacets.types.ts | 2 + .../canonicalizeAndValidateCommand.ts | 38 ++++--------- queries/markdown.scm | 34 +++++++++++ 5 files changed, 108 insertions(+), 28 deletions(-) create mode 100644 data/fixtures/scopes/markdown/notebookCell.scope diff --git a/data/fixtures/scopes/markdown/notebookCell.scope b/data/fixtures/scopes/markdown/notebookCell.scope new file mode 100644 index 0000000000..bc17667883 --- /dev/null +++ b/data/fixtures/scopes/markdown/notebookCell.scope @@ -0,0 +1,57 @@ +```python +def foo(): + pass +``` + +``` +hello +``` + +--- + +[#1 Content] = +[#1 Removal] = +[#1 Domain] = 0:0-3:3 + >--------- +0| ```python +1| def foo(): +2| pass +3| ``` + ---< + +[#1 Interior: Content] = 1:0-2:8 + >---------- +1| def foo(): +2| pass + --------< +[#1 Interior: Removal] = 0:9-3:0 + > +0| ```python +1| def foo(): +2| pass +3| ``` + < + +[#1 Insertion delimiter] = "\n\n" + + +[#2 Content] = +[#2 Removal] = +[#2 Domain] = 5:0-7:3 + >--- +5| ``` +6| hello +7| ``` + ---< + +[#2 Interior: Content] = 6:0-6:5 + >-----< +6| hello +[#2 Interior: Removal] = 5:3-7:0 + > +5| ``` +6| hello +7| ``` + < + +[#2 Insertion delimiter] = "\n\n" diff --git a/packages/common/src/scopeSupportFacets/scopeSupportFacetInfos.ts b/packages/common/src/scopeSupportFacets/scopeSupportFacetInfos.ts index 3f99a46a93..18da96a5f5 100644 --- a/packages/common/src/scopeSupportFacets/scopeSupportFacetInfos.ts +++ b/packages/common/src/scopeSupportFacets/scopeSupportFacetInfos.ts @@ -500,4 +500,9 @@ export const scopeSupportFacetInfos: Record< scopeType: "type", isIteration: true, }, + + notebookCell: { + description: "A cell in a notebook or a markdown code block", + scopeType: "notebookCell", + }, }; diff --git a/packages/common/src/scopeSupportFacets/scopeSupportFacets.types.ts b/packages/common/src/scopeSupportFacets/scopeSupportFacets.types.ts index a48b50260e..ac2df0ecd8 100644 --- a/packages/common/src/scopeSupportFacets/scopeSupportFacets.types.ts +++ b/packages/common/src/scopeSupportFacets/scopeSupportFacets.types.ts @@ -125,6 +125,8 @@ const scopeSupportFacets = [ "type.typeArgument", "type.typeArgument.iteration", + "notebookCell", + // FIXME: Still in legacy // section // selector diff --git a/packages/cursorless-engine/src/core/commandVersionUpgrades/canonicalizeAndValidateCommand.ts b/packages/cursorless-engine/src/core/commandVersionUpgrades/canonicalizeAndValidateCommand.ts index 584e20c1f8..ab2b44f846 100644 --- a/packages/cursorless-engine/src/core/commandVersionUpgrades/canonicalizeAndValidateCommand.ts +++ b/packages/cursorless-engine/src/core/commandVersionUpgrades/canonicalizeAndValidateCommand.ts @@ -5,13 +5,10 @@ import { CommandLatest, EnforceUndefined, LATEST_VERSION, - Modifier, OutdatedExtensionError, PartialTargetDescriptor, - SimpleScopeTypeType, } from "@cursorless/common"; import { getPartialTargetDescriptors } from "../../util/getPartialTargetDescriptors"; -import { getPartialPrimitiveTargets } from "../../util/getPrimitiveTargets"; import canonicalizeTargetsInPlace from "./canonicalizeTargetsInPlace"; import { upgradeV0ToV1 } from "./upgradeV0ToV1"; import { upgradeV1ToV2 } from "./upgradeV1ToV2"; @@ -90,29 +87,14 @@ function upgradeCommand(command: Command): CommandLatest { return command; } +/** + * Validates the given action. Today, this function is a no-op, but in the + * future it may perform additional validation. + * + * @param _actionName The name of the action + * @param _partialTargets The partial targets of the action + */ function validateCommand( - actionName: ActionType, - partialTargets: PartialTargetDescriptor[], -): void { - if ( - usesScopeType("notebookCell", partialTargets) && - !["editNewLineBefore", "editNewLineAfter"].includes(actionName) - ) { - throw new Error( - "The notebookCell scope type is currently only supported with the actions editNewLineAbove and editNewLineBelow", - ); - } -} - -function usesScopeType( - scopeTypeType: SimpleScopeTypeType, - partialTargets: PartialTargetDescriptor[], -) { - return getPartialPrimitiveTargets(partialTargets).some((partialTarget) => - partialTarget.modifiers?.find( - (mod: Modifier) => - (mod.type === "containingScope" || mod.type === "everyScope") && - mod.scopeType.type === scopeTypeType, - ), - ); -} + _actionName: ActionType, + _partialTargets: PartialTargetDescriptor[], +): void {} diff --git a/queries/markdown.scm b/queries/markdown.scm index c4fe4d5489..537bfc2ecf 100644 --- a/queries/markdown.scm +++ b/queries/markdown.scm @@ -41,3 +41,37 @@ ) (list) @collectionItem.iteration + +;;!! ``` +;;! ^^^ +;;!! hello +;;! ----- +;;! ##### +;;!! ``` +;;! ^^^ +( + (fenced_code_block + (fenced_code_block_delimiter) @_.interior.start.endOf + . + (block_continuation) + (fenced_code_block_delimiter) @_.interior.end.startOf + ) @notebookCell + (#trim-end! @notebookCell) + (#insertion-delimiter! @notebookCell "\n\n") +) + +;;!! ```python +;;! ^^^^^^^^^ +;;!! pass +;;! ---- +;;! #### +;;!! ``` +;;! ^^^ +( + (fenced_code_block + (info_string) @_.interior.start.endOf + (fenced_code_block_delimiter) @_.interior.end.startOf + ) @notebookCell + (#trim-end! @notebookCell) + (#insertion-delimiter! @notebookCell "\n\n") +) From a1922a31f99d6347ee7e7ada9359e8ead2bd9409 Mon Sep 17 00:00:00 2001 From: Pokey Rule <755842+pokey@users.noreply.github.com> Date: Fri, 14 Jun 2024 16:49:16 +0100 Subject: [PATCH 2/2] Add facet --- packages/common/src/scopeSupportFacets/markdown.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/common/src/scopeSupportFacets/markdown.ts b/packages/common/src/scopeSupportFacets/markdown.ts index 259173abc5..60fae8aa1b 100644 --- a/packages/common/src/scopeSupportFacets/markdown.ts +++ b/packages/common/src/scopeSupportFacets/markdown.ts @@ -11,4 +11,5 @@ const { supported, unsupported, notApplicable } = ScopeSupportFacetLevel; export const markdownScopeSupport: LanguageScopeSupportFacetMap = { "comment.line": supported, "comment.block": supported, + notebookCell: supported, };