Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate html scopes #2044

Merged
merged 7 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -5,7 +5,6 @@ import { notSupported } from "../util/nodeMatchers";
import { getNodeInternalRange, getNodeRange } from "../util/nodeSelectors";
import { LegacyLanguageId } from "./LegacyLanguageId";
import { getNodeMatcher } from "./getNodeMatcher";
import { stringTextFragmentExtractor as htmlStringTextFragmentExtractor } from "./html";
import { stringTextFragmentExtractor as rubyStringTextFragmentExtractor } from "./ruby";
import { stringTextFragmentExtractor as scssStringTextFragmentExtractor } from "./scss";

Expand Down Expand Up @@ -126,10 +125,6 @@ const textFragmentExtractors: Record<
"css",
scssStringTextFragmentExtractor,
),
html: constructDefaultTextFragmentExtractor(
"html",
htmlStringTextFragmentExtractor,
),
latex: fullDocumentTextFragmentExtractor,
ruby: constructDefaultTextFragmentExtractor(
"ruby",
Expand All @@ -144,8 +139,4 @@ const textFragmentExtractors: Record<
scssStringTextFragmentExtractor,
),
rust: constructDefaultTextFragmentExtractor("rust"),
xml: constructDefaultTextFragmentExtractor(
"xml",
htmlStringTextFragmentExtractor,
),
};
54 changes: 3 additions & 51 deletions packages/cursorless-engine/src/languages/html.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,11 @@
import type { SyntaxNode } from "web-tree-sitter";
import { SimpleScopeTypeType } from "@cursorless/common";
import { NodeMatcherAlternative, SelectionWithEditor } from "../typings/Types";
import { typedNodeFinder } from "../util/nodeFinders";
import {
createPatternMatchers,
leadingMatcher,
matcher,
patternMatcher,
} from "../util/nodeMatchers";
import { xmlElementExtractor, getNodeRange } from "../util/nodeSelectors";

const attribute = "*?.attribute!";

const getStartTag = patternMatcher(`*?.start_tag!`);
const getEndTag = patternMatcher(`*?.end_tag!`);

const getTags = (selection: SelectionWithEditor, node: SyntaxNode) => {
const startTag = getStartTag(selection, node);
const endTag = getEndTag(selection, node);
return startTag != null && endTag != null ? startTag.concat(endTag) : null;
};
import { NodeMatcherAlternative } from "../typings/Types";
import { createPatternMatchers } from "../util/nodeMatchers";

const nodeMatchers: Partial<
Record<SimpleScopeTypeType, NodeMatcherAlternative>
> = {
xmlElement: matcher(
typedNodeFinder("element", "script_element", "style_element"),
xmlElementExtractor,
),
xmlBothTags: getTags,
xmlStartTag: getStartTag,
xmlEndTag: getEndTag,
attribute: attribute,
collectionItem: attribute,
name: "*?.tag_name!",
collectionKey: ["*?.attribute_name!"],
value: leadingMatcher(
["*?.quoted_attribute_value!.attribute_value", "*?.attribute_value!"],
["="],
),
string: "quoted_attribute_value",
comment: "comment",
collectionItem: "*?.attribute!",
};

export const patternMatchers = createPatternMatchers(nodeMatchers);

const textFragmentTypes = ["attribute_value", "raw_text", "text"];

export function stringTextFragmentExtractor(
node: SyntaxNode,
_selection: SelectionWithEditor,
) {
if (textFragmentTypes.includes(node.type)) {
return getNodeRange(node);
}

return null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
languageId: html
command:
version: 6
spokenForm: chuck value
action:
name: remove
target:
type: primitive
modifiers:
- type: containingScope
scopeType: {type: value}
usePrePhraseSnapshot: true
initialState:
documentContents: <html value=2></html>
selections:
- anchor: {line: 0, character: 6}
active: {line: 0, character: 6}
marks: {}
finalState:
documentContents: <html value></html>
selections:
- anchor: {line: 0, character: 6}
active: {line: 0, character: 6}
93 changes: 93 additions & 0 deletions queries/html.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
;;!! <aaa>
;;! ^^^
;;! -----
(start_tag
(tag_name) @name
) @_.domain

;;!! </aaa>
;;! ^^^
;;! ------
(end_tag
(tag_name) @name
) @_.domain

;;!! <aaa id="me">
;;! ^^^^^^^
(attribute) @attribute

;;!! <aaa id="me">
;;! ^^
(attribute
(attribute_name) @collectionKey @collectionKey.trailing.start.endOf
[
(quoted_attribute_value)
(attribute_value)
] ? @collectionKey.trailing.end.startOf
) @_.domain

;;!! <aaa value=2>
;;! ^
;;!! <aaa id="me">
;;! ^^^^
(attribute
(attribute_name) @value.leading.start.endOf
[
(quoted_attribute_value)
(attribute_value)
] @value @value.leading.end.startOf
) @value.domain

;;!! <aaa id="me">
;;! ^^
(quoted_attribute_value
(attribute_value) @textFragment
) @string

;;!! <aaa>
;;! ^^^^^
(start_tag) @attribute.iteration @collectionKey.iteration @value.iteration

;;!! <!-- comment -->
;;! ^^^^^^^^^^^^^^^^
(comment) @comment @textFragment

;;!! <aaa>text</aaa>
;;! ^^^^
(text) @textFragment

;;!! <script>hello</script>
;;! ^^^^^
(raw_text) @textFragment

;; Use parent wildcard to get all three kinds of elements: element, script_element, style_element

;;!! <aaa>text</aaa>
;;! ^^^^^^^^^^^^^^^
;;! ^^^^
(_
(start_tag) @xmlElement.interior.start.endOf
(end_tag) @xmlElement.interior.end.startOf
) @xmlElement

;;!! <aaa>text</aaa>
;;! ^^^^^ ^^^^^^
;;! ---------------
(_
(start_tag) @xmlStartTag
(end_tag) @xmlEndTag
) @_.domain

(_
[
(start_tag)
(end_tag)
] @xmlBothTags
(#allow-multiple! @xmlBothTags)
) @_.domain

(_
(start_tag) @xmlElement.iteration.start.endOf @xmlBothTags.iteration.start.endOf
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're missing startTag and endTag

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

(element)
pokey marked this conversation as resolved.
Show resolved Hide resolved
(end_tag) @xmlElement.iteration.end.startOf @xmlBothTags.iteration.end.startOf
)
1 change: 1 addition & 0 deletions queries/xml.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
;; import html.scm