Skip to content

Commit a723b25

Browse files
Migrate html scopes (#2044)
## 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 --------- Co-authored-by: Pokey Rule <755842+pokey@users.noreply.github.com>
1 parent 6660914 commit a723b25

5 files changed

Lines changed: 126 additions & 60 deletions

File tree

packages/cursorless-engine/src/languages/getTextFragmentExtractor.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { notSupported } from "../util/nodeMatchers";
55
import { getNodeInternalRange, getNodeRange } from "../util/nodeSelectors";
66
import { LegacyLanguageId } from "./LegacyLanguageId";
77
import { getNodeMatcher } from "./getNodeMatcher";
8-
import { stringTextFragmentExtractor as htmlStringTextFragmentExtractor } from "./html";
98
import { stringTextFragmentExtractor as rubyStringTextFragmentExtractor } from "./ruby";
109
import { stringTextFragmentExtractor as scssStringTextFragmentExtractor } from "./scss";
1110

@@ -126,10 +125,6 @@ const textFragmentExtractors: Record<
126125
"css",
127126
scssStringTextFragmentExtractor,
128127
),
129-
html: constructDefaultTextFragmentExtractor(
130-
"html",
131-
htmlStringTextFragmentExtractor,
132-
),
133128
latex: fullDocumentTextFragmentExtractor,
134129
ruby: constructDefaultTextFragmentExtractor(
135130
"ruby",
@@ -144,8 +139,4 @@ const textFragmentExtractors: Record<
144139
scssStringTextFragmentExtractor,
145140
),
146141
rust: constructDefaultTextFragmentExtractor("rust"),
147-
xml: constructDefaultTextFragmentExtractor(
148-
"xml",
149-
htmlStringTextFragmentExtractor,
150-
),
151142
};
Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,11 @@
1-
import type { SyntaxNode } from "web-tree-sitter";
21
import { SimpleScopeTypeType } from "@cursorless/common";
3-
import { NodeMatcherAlternative, SelectionWithEditor } from "../typings/Types";
4-
import { typedNodeFinder } from "../util/nodeFinders";
5-
import {
6-
createPatternMatchers,
7-
leadingMatcher,
8-
matcher,
9-
patternMatcher,
10-
} from "../util/nodeMatchers";
11-
import { xmlElementExtractor, getNodeRange } from "../util/nodeSelectors";
12-
13-
const attribute = "*?.attribute!";
14-
15-
const getStartTag = patternMatcher(`*?.start_tag!`);
16-
const getEndTag = patternMatcher(`*?.end_tag!`);
17-
18-
const getTags = (selection: SelectionWithEditor, node: SyntaxNode) => {
19-
const startTag = getStartTag(selection, node);
20-
const endTag = getEndTag(selection, node);
21-
return startTag != null && endTag != null ? startTag.concat(endTag) : null;
22-
};
2+
import { NodeMatcherAlternative } from "../typings/Types";
3+
import { createPatternMatchers } from "../util/nodeMatchers";
234

245
const nodeMatchers: Partial<
256
Record<SimpleScopeTypeType, NodeMatcherAlternative>
267
> = {
27-
xmlElement: matcher(
28-
typedNodeFinder("element", "script_element", "style_element"),
29-
xmlElementExtractor,
30-
),
31-
xmlBothTags: getTags,
32-
xmlStartTag: getStartTag,
33-
xmlEndTag: getEndTag,
34-
attribute: attribute,
35-
collectionItem: attribute,
36-
name: "*?.tag_name!",
37-
collectionKey: ["*?.attribute_name!"],
38-
value: leadingMatcher(
39-
["*?.quoted_attribute_value!.attribute_value", "*?.attribute_value!"],
40-
["="],
41-
),
42-
string: "quoted_attribute_value",
43-
comment: "comment",
8+
collectionItem: "*?.attribute!",
449
};
4510

4611
export const patternMatchers = createPatternMatchers(nodeMatchers);
47-
48-
const textFragmentTypes = ["attribute_value", "raw_text", "text"];
49-
50-
export function stringTextFragmentExtractor(
51-
node: SyntaxNode,
52-
_selection: SelectionWithEditor,
53-
) {
54-
if (textFragmentTypes.includes(node.type)) {
55-
return getNodeRange(node);
56-
}
57-
58-
return null;
59-
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
languageId: html
2+
command:
3+
version: 6
4+
spokenForm: chuck value
5+
action:
6+
name: remove
7+
target:
8+
type: primitive
9+
modifiers:
10+
- type: containingScope
11+
scopeType: {type: value}
12+
usePrePhraseSnapshot: true
13+
initialState:
14+
documentContents: <html value=2></html>
15+
selections:
16+
- anchor: {line: 0, character: 6}
17+
active: {line: 0, character: 6}
18+
marks: {}
19+
finalState:
20+
documentContents: <html value></html>
21+
selections:
22+
- anchor: {line: 0, character: 6}
23+
active: {line: 0, character: 6}

queries/html.scm

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
;;!! <aaa>
2+
;;! ^^^
3+
;;! -----
4+
(start_tag
5+
(tag_name) @name
6+
) @_.domain
7+
8+
;;!! </aaa>
9+
;;! ^^^
10+
;;! ------
11+
(end_tag
12+
(tag_name) @name
13+
) @_.domain
14+
15+
;;!! <aaa id="me">
16+
;;! ^^^^^^^
17+
(attribute) @attribute
18+
19+
;;!! <aaa id="me">
20+
;;! ^^
21+
(attribute
22+
(attribute_name) @collectionKey @collectionKey.trailing.start.endOf
23+
[
24+
(quoted_attribute_value)
25+
(attribute_value)
26+
] ? @collectionKey.trailing.end.startOf
27+
) @_.domain
28+
29+
;;!! <aaa value=2>
30+
;;! ^
31+
;;!! <aaa id="me">
32+
;;! ^^^^
33+
(attribute
34+
(attribute_name) @value.leading.start.endOf
35+
[
36+
(quoted_attribute_value)
37+
(attribute_value)
38+
] @value @value.leading.end.startOf
39+
) @value.domain
40+
41+
;;!! <aaa id="me">
42+
;;! ^^
43+
(quoted_attribute_value
44+
(attribute_value) @textFragment
45+
) @string
46+
47+
;;!! <aaa>
48+
;;! ^^^^^
49+
(start_tag) @attribute.iteration @collectionKey.iteration @value.iteration
50+
51+
;;!! <!-- comment -->
52+
;;! ^^^^^^^^^^^^^^^^
53+
(comment) @comment @textFragment
54+
55+
;;!! <aaa>text</aaa>
56+
;;! ^^^^
57+
(text) @textFragment
58+
59+
;;!! <script>hello</script>
60+
;;! ^^^^^
61+
(raw_text) @textFragment
62+
63+
;; Use parent wildcard to get all three kinds of elements: element, script_element, style_element
64+
65+
;;!! <aaa>text</aaa>
66+
;;! ^^^^^^^^^^^^^^^
67+
;;! ^^^^
68+
(_
69+
(start_tag) @xmlElement.interior.start.endOf
70+
(end_tag) @xmlElement.interior.end.startOf
71+
) @xmlElement
72+
73+
;;!! <aaa>text</aaa>
74+
;;! ^^^^^ ^^^^^^
75+
;;! ---------------
76+
(_
77+
(start_tag) @xmlStartTag
78+
(end_tag) @xmlEndTag
79+
) @_.domain
80+
81+
(_
82+
[
83+
(start_tag)
84+
(end_tag)
85+
] @xmlBothTags
86+
(#allow-multiple! @xmlBothTags)
87+
) @_.domain
88+
89+
(_
90+
(start_tag) @xmlElement.iteration.start.endOf @xmlBothTags.iteration.start.endOf
91+
(element)
92+
(end_tag) @xmlElement.iteration.end.startOf @xmlBothTags.iteration.end.startOf
93+
)
94+
95+
(_
96+
(start_tag) @xmlStartTag.iteration.start.endOf @xmlEndTag.iteration.start.endOf
97+
(element)
98+
(end_tag) @xmlStartTag.iteration.end.startOf @xmlEndTag.iteration.end.startOf
99+
)

queries/xml.scm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
;; import html.scm

0 commit comments

Comments
 (0)