Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 91f3b07

Browse files
committed
Merge pull request #231 from savetheclocktower/apd-punctuation
Add scopes for punctuation
2 parents e2eeb38 + 62613e6 commit 91f3b07

4 files changed

Lines changed: 106 additions & 4 deletions

File tree

grammars/tree-sitter-html.cson

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,23 @@ scopes:
3434
'attribute_name': 'entity.other.attribute-name'
3535
'attribute_value': 'string.html'
3636
'comment': 'comment.block.html'
37+
38+
'
39+
start_tag > "<",
40+
end_tag > "</"
41+
': 'punctuation.definition.tag.begin'
42+
'
43+
start_tag > ">",
44+
end_tag > ">"
45+
': 'punctuation.definition.tag.end'
46+
47+
'attribute > "="': 'punctuation.separator.key-value.html'
48+
49+
# quoted_attribute_value has three child nodes: ", attribute_value, and ".
50+
# Target the first and last.
51+
# Single quotes and double quotes are targeted in separate selectors because
52+
# of quote-escaping difficulties.
53+
"quoted_attribute_value > '\"':nth-child(0)": 'punctuation.definition.string.begin'
54+
'quoted_attribute_value > "\'":nth-child(0)': 'punctuation.definition.string.begin'
55+
"quoted_attribute_value > '\"':nth-child(2)": 'punctuation.definition.string.end'
56+
'quoted_attribute_value > "\'":nth-child(2)': 'punctuation.definition.string.end'

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
},
2222
"dependencies": {
2323
"atom-grammar-test": "^0.6.3",
24-
"tree-sitter-html": "^0.15.0",
25-
"tree-sitter-embedded-template": "^0.15.0"
24+
"tree-sitter-embedded-template": "^0.15.0",
25+
"tree-sitter-html": "^0.15.0"
2626
},
2727
"devDependencies": {
28-
"coffeelint": "^1.10.1"
28+
"coffeelint": "^1.10.1",
29+
"dedent": "^0.7.0"
2930
}
3031
}

spec/html-spec.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
path = require 'path'
22
grammarTest = require 'atom-grammar-test'
33

4-
describe 'HTML grammar', ->
4+
describe 'TextMate HTML grammar', ->
55
grammar = null
66

77
beforeEach ->

spec/tree-sitter-spec.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const dedent = require('dedent')
2+
3+
describe('Tree-sitter HTML grammar', () => {
4+
5+
beforeEach(async () => {
6+
atom.config.set('core.useTreeSitterParsers', true)
7+
await atom.packages.activatePackage('language-html')
8+
})
9+
10+
it('tokenizes punctuation in HTML tags and attributes', async () => {
11+
const editor = await atom.workspace.open(`test.html`)
12+
13+
editor.setText(dedent`
14+
<html lang="en">
15+
<head>
16+
<meta charset='utf-8'>
17+
<meta name='"' content="This'll test single and double quotes.">
18+
</head>
19+
<body>
20+
</html>
21+
`)
22+
23+
// Tag punctuation.
24+
expect(editor.scopeDescriptorForBufferPosition([0, 0]).toString()).toBe(
25+
'.text.html.basic .source.html .punctuation.definition.tag.begin'
26+
)
27+
28+
expect(editor.scopeDescriptorForBufferPosition([0, 15]).toString()).toBe(
29+
'.text.html.basic .source.html .punctuation.definition.tag.end'
30+
)
31+
32+
expect(editor.scopeDescriptorForBufferPosition([6, 0]).toString()).toBe(
33+
'.text.html.basic .source.html .punctuation.definition.tag.begin'
34+
)
35+
36+
expect(editor.scopeDescriptorForBufferPosition([6, 6]).toString()).toBe(
37+
'.text.html.basic .source.html .punctuation.definition.tag.end'
38+
)
39+
40+
// Attribute-value pair punctuation.
41+
expect(editor.scopeDescriptorForBufferPosition([0, 10]).toString()).toBe(
42+
'.text.html.basic .source.html .punctuation.separator.key-value.html'
43+
)
44+
45+
expect(editor.scopeDescriptorForBufferPosition([2, 18]).toString()).toBe(
46+
'.text.html.basic .source.html .punctuation.definition.string.begin'
47+
)
48+
49+
expect(editor.scopeDescriptorForBufferPosition([2, 24]).toString()).toBe(
50+
'.text.html.basic .source.html .punctuation.definition.string.end'
51+
)
52+
53+
// Ensure an attribute value delimited by single-quotes won't mark a
54+
// double-quote in the value as punctuation.
55+
expect(editor.scopeDescriptorForBufferPosition([3, 15]).toString()).toBe(
56+
'.text.html.basic .source.html .punctuation.definition.string.begin'
57+
)
58+
59+
expect(editor.scopeDescriptorForBufferPosition([3, 16]).toString()).toBe(
60+
'.text.html.basic .source.html .string.html'
61+
)
62+
63+
expect(editor.scopeDescriptorForBufferPosition([3, 17]).toString()).toBe(
64+
'.text.html.basic .source.html .punctuation.definition.string.end'
65+
)
66+
67+
// Ensure an attribute value delimited by double-quotes won't mark a
68+
// single-quote in the value as punctuation.
69+
expect(editor.scopeDescriptorForBufferPosition([3, 27]).toString()).toBe(
70+
'.text.html.basic .source.html .punctuation.definition.string.begin'
71+
)
72+
73+
expect(editor.scopeDescriptorForBufferPosition([3, 32]).toString()).toBe(
74+
'.text.html.basic .source.html .string.html'
75+
)
76+
77+
expect(editor.scopeDescriptorForBufferPosition([3, 66]).toString()).toBe(
78+
'.text.html.basic .source.html .punctuation.definition.string.end'
79+
)
80+
})
81+
})

0 commit comments

Comments
 (0)