Skip to content

Commit d2a1845

Browse files
authored
Support markdown code blocks as "cell" (#2410)
- Fixes #1992 ## Checklist - [x] I have added [tests](https://www.cursorless.org/docs/contributing/test-case-recorder/) - [-] I have updated the [docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and [cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet) - [-] I have not broken the cheatsheet
1 parent 9251a02 commit d2a1845

File tree

6 files changed

+109
-28
lines changed

6 files changed

+109
-28
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
```python
2+
def foo():
3+
pass
4+
```
5+
6+
```
7+
hello
8+
```
9+
10+
---
11+
12+
[#1 Content] =
13+
[#1 Removal] =
14+
[#1 Domain] = 0:0-3:3
15+
>---------
16+
0| ```python
17+
1| def foo():
18+
2| pass
19+
3| ```
20+
---<
21+
22+
[#1 Interior: Content] = 1:0-2:8
23+
>----------
24+
1| def foo():
25+
2| pass
26+
--------<
27+
[#1 Interior: Removal] = 0:9-3:0
28+
>
29+
0| ```python
30+
1| def foo():
31+
2| pass
32+
3| ```
33+
<
34+
35+
[#1 Insertion delimiter] = "\n\n"
36+
37+
38+
[#2 Content] =
39+
[#2 Removal] =
40+
[#2 Domain] = 5:0-7:3
41+
>---
42+
5| ```
43+
6| hello
44+
7| ```
45+
---<
46+
47+
[#2 Interior: Content] = 6:0-6:5
48+
>-----<
49+
6| hello
50+
[#2 Interior: Removal] = 5:3-7:0
51+
>
52+
5| ```
53+
6| hello
54+
7| ```
55+
<
56+
57+
[#2 Insertion delimiter] = "\n\n"

packages/common/src/scopeSupportFacets/markdown.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ const { supported, unsupported, notApplicable } = ScopeSupportFacetLevel;
1111
export const markdownScopeSupport: LanguageScopeSupportFacetMap = {
1212
"comment.line": supported,
1313
"comment.block": supported,
14+
notebookCell: supported,
1415
};

packages/common/src/scopeSupportFacets/scopeSupportFacetInfos.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,4 +628,9 @@ export const scopeSupportFacetInfos: Record<
628628
scopeType: "type",
629629
isIteration: true,
630630
},
631+
632+
notebookCell: {
633+
description: "A cell in a notebook or a markdown code block",
634+
scopeType: "notebookCell",
635+
},
631636
};

packages/common/src/scopeSupportFacets/scopeSupportFacets.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ const scopeSupportFacets = [
155155
"type.typeArgument",
156156
"type.typeArgument.iteration",
157157

158+
"notebookCell",
159+
158160
// FIXME: Still in legacy
159161
// section
160162
// selector

packages/cursorless-engine/src/core/commandVersionUpgrades/canonicalizeAndValidateCommand.ts

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ import {
55
CommandLatest,
66
EnforceUndefined,
77
LATEST_VERSION,
8-
Modifier,
98
OutdatedExtensionError,
109
PartialTargetDescriptor,
11-
SimpleScopeTypeType,
1210
} from "@cursorless/common";
1311
import { getPartialTargetDescriptors } from "../../util/getPartialTargetDescriptors";
14-
import { getPartialPrimitiveTargets } from "../../util/getPrimitiveTargets";
1512
import canonicalizeTargetsInPlace from "./canonicalizeTargetsInPlace";
1613
import { upgradeV0ToV1 } from "./upgradeV0ToV1";
1714
import { upgradeV1ToV2 } from "./upgradeV1ToV2";
@@ -90,29 +87,14 @@ function upgradeCommand(command: Command): CommandLatest {
9087
return command;
9188
}
9289

90+
/**
91+
* Validates the given action. Today, this function is a no-op, but in the
92+
* future it may perform additional validation.
93+
*
94+
* @param _actionName The name of the action
95+
* @param _partialTargets The partial targets of the action
96+
*/
9397
function validateCommand(
94-
actionName: ActionType,
95-
partialTargets: PartialTargetDescriptor[],
96-
): void {
97-
if (
98-
usesScopeType("notebookCell", partialTargets) &&
99-
!["editNewLineBefore", "editNewLineAfter"].includes(actionName)
100-
) {
101-
throw new Error(
102-
"The notebookCell scope type is currently only supported with the actions editNewLineAbove and editNewLineBelow",
103-
);
104-
}
105-
}
106-
107-
function usesScopeType(
108-
scopeTypeType: SimpleScopeTypeType,
109-
partialTargets: PartialTargetDescriptor[],
110-
) {
111-
return getPartialPrimitiveTargets(partialTargets).some((partialTarget) =>
112-
partialTarget.modifiers?.find(
113-
(mod: Modifier) =>
114-
(mod.type === "containingScope" || mod.type === "everyScope") &&
115-
mod.scopeType.type === scopeTypeType,
116-
),
117-
);
118-
}
98+
_actionName: ActionType,
99+
_partialTargets: PartialTargetDescriptor[],
100+
): void {}

queries/markdown.scm

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,37 @@
4141
)
4242

4343
(list) @collectionItem.iteration
44+
45+
;;!! ```
46+
;;! ^^^
47+
;;!! hello
48+
;;! -----
49+
;;! #####
50+
;;!! ```
51+
;;! ^^^
52+
(
53+
(fenced_code_block
54+
(fenced_code_block_delimiter) @_.interior.start.endOf
55+
.
56+
(block_continuation)
57+
(fenced_code_block_delimiter) @_.interior.end.startOf
58+
) @notebookCell
59+
(#trim-end! @notebookCell)
60+
(#insertion-delimiter! @notebookCell "\n\n")
61+
)
62+
63+
;;!! ```python
64+
;;! ^^^^^^^^^
65+
;;!! pass
66+
;;! ----
67+
;;! ####
68+
;;!! ```
69+
;;! ^^^
70+
(
71+
(fenced_code_block
72+
(info_string) @_.interior.start.endOf
73+
(fenced_code_block_delimiter) @_.interior.end.startOf
74+
) @notebookCell
75+
(#trim-end! @notebookCell)
76+
(#insertion-delimiter! @notebookCell "\n\n")
77+
)

0 commit comments

Comments
 (0)