Skip to content

Commit

Permalink
Support markdown code blocks as "cell" (#2410)
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey authored Jun 14, 2024
1 parent 9251a02 commit d2a1845
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 28 deletions.
57 changes: 57 additions & 0 deletions data/fixtures/scopes/markdown/notebookCell.scope
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions packages/common/src/scopeSupportFacets/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ const { supported, unsupported, notApplicable } = ScopeSupportFacetLevel;
export const markdownScopeSupport: LanguageScopeSupportFacetMap = {
"comment.line": supported,
"comment.block": supported,
notebookCell: supported,
};
Original file line number Diff line number Diff line change
Expand Up @@ -628,4 +628,9 @@ export const scopeSupportFacetInfos: Record<
scopeType: "type",
isIteration: true,
},

notebookCell: {
description: "A cell in a notebook or a markdown code block",
scopeType: "notebookCell",
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ const scopeSupportFacets = [
"type.typeArgument",
"type.typeArgument.iteration",

"notebookCell",

// FIXME: Still in legacy
// section
// selector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 {}
34 changes: 34 additions & 0 deletions queries/markdown.scm
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

0 comments on commit d2a1845

Please sign in to comment.