Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@
}
]
},
{
"name": "scopes/markdown/collectionItem.unenclosed.iteration2",
"languageId": "markdown",
"facet": "collectionItem.unenclosed.iteration",
"code": "- First item\n Paragraph\n- Second item",
"scopes": [
{
"targets": [
{
"content": "0:0-2:13"
}
],
"domain": "0:0-2:13"
}
]
},
{
"name": "scopes/markdown/collectionItem.unenclosed.multiLine",
"languageId": "markdown",
Expand Down Expand Up @@ -51,13 +67,39 @@
"targets": [
{
"content": "2:2-2:42",
"removal": "2:0-2:42"
"removal": "1:39-2:42"
}
],
"domain": "2:0-2:42"
}
]
},
{
"name": "scopes/markdown/collectionItem.unenclosed.multiLine2",
"languageId": "markdown",
"facet": "collectionItem.unenclosed.multiLine",
"code": "- First item\n Paragraph\n- Second item",
"scopes": [
{
"targets": [
{
"content": "0:2-1:11",
"removal": "0:0-2:0"
}
],
"domain": "0:0-1:11"
},
{
"targets": [
{
"content": "2:2-2:13",
"removal": "1:11-2:13"
}
],
"domain": "2:0-2:13"
}
]
},
{
"name": "scopes/markdown/comment.block",
"languageId": "markdown",
Expand Down
4 changes: 4 additions & 0 deletions packages/app-web-docs/src/docs/user/languages/markdown.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ Below are visualizations of all our scope tests for this language. These were cr

<ScopeVisualizer fixtureName="scopes/markdown/collectionItem.unenclosed.multiLine" languageId="markdown" />

<ScopeVisualizer fixtureName="scopes/markdown/collectionItem.unenclosed.multiLine2" languageId="markdown" />

#### 2. Collection item: Unenclosed (iteration)

<i>Iteration scope for items in a comma-separated list without enclosing delimiters</i>

<ScopeVisualizer fixtureName="scopes/markdown/collectionItem.unenclosed.iteration" languageId="markdown" />

<ScopeVisualizer fixtureName="scopes/markdown/collectionItem.unenclosed.iteration2" languageId="markdown" />

### Comment

#### 1. Comment: Block
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ Default: `item`

<ScopeVisualizer fixtureName="scopes/markdown/collectionItem.unenclosed.multiLine" />

<ScopeVisualizer fixtureName="scopes/markdown/collectionItem.unenclosed.multiLine2" />

##### Scala

<ScopeVisualizer fixtureName="scopes/scala/collectionItem/collectionItem.unenclosed.multiLine" />
Expand Down Expand Up @@ -255,6 +257,8 @@ Default: `item`

<ScopeVisualizer fixtureName="scopes/markdown/collectionItem.unenclosed.iteration" />

<ScopeVisualizer fixtureName="scopes/markdown/collectionItem.unenclosed.iteration2" />

##### Python

<ScopeVisualizer fixtureName="scopes/python/collectionItem/collectionItem.unenclosed.iteration" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,22 @@
}
]
},
{
"name": "scopes/markdown/collectionItem.unenclosed.iteration2",
"languageId": "markdown",
"facet": "collectionItem.unenclosed.iteration",
"code": "- First item\n Paragraph\n- Second item",
"scopes": [
{
"targets": [
{
"content": "0:0-2:13"
}
],
"domain": "0:0-2:13"
}
]
},
{
"name": "scopes/markdown/collectionItem.unenclosed.multiLine",
"languageId": "markdown",
Expand Down Expand Up @@ -1527,13 +1543,39 @@
"targets": [
{
"content": "2:2-2:42",
"removal": "2:0-2:42"
"removal": "1:39-2:42"
}
],
"domain": "2:0-2:42"
}
]
},
{
"name": "scopes/markdown/collectionItem.unenclosed.multiLine2",
"languageId": "markdown",
"facet": "collectionItem.unenclosed.multiLine",
"code": "- First item\n Paragraph\n- Second item",
"scopes": [
{
"targets": [
{
"content": "0:2-1:11",
"removal": "0:0-2:0"
}
],
"domain": "0:0-1:11"
},
{
"targets": [
{
"content": "2:2-2:13",
"removal": "1:11-2:13"
}
],
"domain": "2:0-2:13"
}
]
},
{
"name": "scopes/plaintext/collectionItem/collectionItem.textual.iteration",
"languageId": "plaintext",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ export abstract class QueryPredicateOperator<T extends HasSchema> {
*/
abstract readonly schema: InferSchemaType<T>;

/**
* Whether it is ok for a node argument to be missing. If true, then the
* operator will just accept the pattern if the given node is missing. If
* false, then the operator will throw an error if the node is missing.
*
* This is useful if we want to set some flag on a node, but only if it's
* present.
*/
protected allowMissingNode: boolean = false;

/**
* Given a list of operands, return whether the operator accepts the given
* operands. This is where the actual logic of the operator is implemented.
Expand All @@ -47,21 +57,6 @@ export abstract class QueryPredicateOperator<T extends HasSchema> {
...args: AcceptFunctionArgs<z.infer<InferSchemaType<T>>>
): boolean;

/**
* Whether it is ok for a node argument to be missing. If true, then the
* operator will just accept the pattern if the given node is missing. If
* false, then the operator will throw an error if the node is missing.
*
* This is useful if we want to set some flag on a node, but only if it's
* present.
*
* @returns A boolean indicating whether it is ok for a node argument to be
* missing.
*/
protected allowMissingNode(): boolean {
return false;
}

/**
* Given a list of operands, return a predicate function that can be used to
* test whether a given match satisfies the predicate.
Expand All @@ -84,7 +79,7 @@ export abstract class QueryPredicateOperator<T extends HasSchema> {
} catch (error) {
if (
error instanceof CaptureNotFoundError &&
this.allowMissingNode()
this.allowMissingNode
) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ suite("queryPredicateOperators", () => {
"shrink-to-match!": [captureOperand, "hello"],
"grow-to-named-siblings!": [captureOperand],
"trim-end!": [captureOperand, conditionOperand],
"trim-end-optional!": [captureOperand],
"document-range!": [captureOperand, conditionOperand],
"allow-multiple!": [captureOperand, conditionOperand],
"insertion-delimiter!": [captureOperand, ", "],
Expand Down Expand Up @@ -448,17 +449,15 @@ suite("queryPredicateOperators", () => {
}

test("allow-multiple! accepts missing captures while range operators require them", () => {
for (const name of [
"allow-multiple!",
"trim-end!",
"document-range!",
] as const) {
const optionalNames = ["allow-multiple!", "trim-end-optional!"];
const requiredNames = ["trim-end!", "document-range!"];
for (const name of [...optionalNames, ...requiredNames] as const) {
const operator = queryPredicateOperators.find((op) => op.name === name)!;
const result = operator.createPredicate([
{ type: "capture", name: "missing" },
]);
assert.ok(result.success);
if (name === "allow-multiple!") {
if (optionalNames.includes(name)) {
assert.equal(result.predicate({ captures: [] }), true);
} else {
assert.throws(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,25 +318,42 @@ class TrimEnd extends QueryPredicateOperator<TrimEnd> {

run(...captures: MutableQueryCapture[]) {
for (const capture of captures) {
const { document, range } = capture;
const text = getNode(capture).text;
const whitespaceLength = text.length - text.trimEnd().length;

if (whitespaceLength > 0) {
setRange(
capture,
new Range(
range.start,
adjustPosition(document, range.end, -whitespaceLength),
),
);
}
trimEnd(capture);
}
return true;
}
}

/**
* A predicate that behaves the same as {@link TrimEnd} but allows the node to be missing.
*/
class TrimEndOptional extends QueryPredicateOperator<TrimEndOptional> {
name = "trim-end-optional!" as const;
schema = z.tuple([q.node]);
allowMissingNode = true;

run(capture: MutableQueryCapture) {
trimEnd(capture);
return true;
}
}

function trimEnd(capture: MutableQueryCapture): void {
const { document, range } = capture;
const text = getNode(capture).text;
const whitespaceLength = text.length - text.trimEnd().length;

if (whitespaceLength > 0) {
setRange(
capture,
new Range(
range.start,
adjustPosition(document, range.end, -whitespaceLength),
),
);
}
}

/**
* A predicate operator that sets the range to the full document.
*/
Expand Down Expand Up @@ -367,10 +384,7 @@ class DocumentRange extends QueryPredicateOperator<DocumentRange> {
class AllowMultiple extends QueryPredicateOperator<AllowMultiple> {
name = "allow-multiple!" as const;
schema = z.tuple([q.node]).rest(q.node);

protected allowMissingNode(): boolean {
return true;
}
allowMissingNode = true;

run(...captures: MutableQueryCapture[]) {
for (const capture of captures) {
Expand Down Expand Up @@ -477,6 +491,7 @@ export const queryPredicateOperators = [
new Type(),
new NotType(),
new TrimEnd(),
new TrimEndOptional(),
new DocumentRange(),
new NotParentType(),
new NotChildType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ initialState:
finalState:
documentContents: |-
- values
- 0
- 1
- 0- 1
- 2
selections:
- anchor: {line: 2, character: 4}
active: {line: 2, character: 4}
- anchor: {line: 1, character: 7}
active: {line: 1, character: 7}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ finalState:
documentContents: |-
- values
- 0

selections:
- anchor: {line: 2, character: 4}
active: {line: 2, character: 4}
- anchor: {line: 1, character: 7}
active: {line: 1, character: 7}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- First item
Paragraph
- Second item
---

[Content] =
[Domain] = 0:0-2:13
>------------
0| - First item
1| Paragraph
2| - Second item
-------------<
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,20 @@
>----------------------------------------<
2| - This is the first level of a list again.

[#3 Removal] =
[#3 Domain] = 2:0-2:42
>------------------------------------------<
[#3 Removal] = 1:39-2:42
>
1| - This is the second level of a list.
2| - This is the first level of a list again.
------------------------------------------<

[#3 Leading delimiter] = 2:0-2:0
><
[#3 Leading delimiter] = 1:39-2:0
>
1| - This is the second level of a list.
2| - This is the first level of a list again.
<

[#3 Domain] = 2:0-2:42
>------------------------------------------<
2| - This is the first level of a list again.

[#3 Insertion delimiter] = "\n"
Loading
Loading