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

Syntax improvements for defs and symbols #75

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 10 additions & 21 deletions grammars/clojure.cson
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
}
]
'dynamic-variables':
'match': '\\*[\\w\\.\\-\\_\\:\\+\\=\\>\\<\\!\\?\\d]+\\*'
'match': '\\*[0-9#\'A-Z_a-z!$%&+\\-.:<=>?|]+\\*'
'name': 'meta.symbol.dynamic.clojure'
'map':
'begin': '(\\{)'
Expand Down Expand Up @@ -279,7 +279,7 @@
'patterns': [
{
# ns, declare and everything that starts with def* or namespace/def*
'begin': '(?<=\\()(ns|declare|def[\\w\\d._:+=><!?*-]*|[\\w._:+=><!?*-][\\w\\d._:+=><!?*-]*/def[\\w\\d._:+=><!?*-]*)\\s+'
'begin': '(?<=\\()(ns|declare|def[0-9#\'A-Z_a-z!$%&+\\-.:<=>?|*]*|[A-Z_a-z!$%&+\\-.:<=>?|*][0-9#\'A-Z_a-z!$%&+\\-.:<=>?|*]*/def[0-9#\'A-Z_a-z!$%&+\\-.:<=>?|*]*)\\s+'
'beginCaptures':
'1':
'name': 'keyword.control.clojure'
Expand All @@ -290,14 +290,14 @@
# there may be some metadata before an actual definition
'include': '#metadata'
}
{ # dynamic variables are rendered diferently
'include': '#dynamic-variables'
}
{
# recognizing a symbol as being defined here
# copied and pasted from #symbol, screw it
'match': '([\\w\\.\\-\\_\\:\\+\\=\\>\\<\\!\\?\\*][\\w\\.\\-\\_\\:\\+\\=\\>\\<\\!\\?\\*\\d]+)'
'name': 'entity.global.clojure'
'match': '([A-Z_a-z!$%&+\\-.:<=>?|*][0-9#\'A-Z_a-z!$%&+\\-.:<=>?|*]*)'
'name': 'entity.name.definition.clojure'
}
{ # dynamic variables are rendered diferently
'include': '#dynamic-variables'
}
{
'include': '$self'
Expand All @@ -322,17 +322,6 @@
{
'include': '#sexp'
}
{
'match': '(?<=\\()(.+?)(?=\\s|\\))'
Copy link
Contributor

Choose a reason for hiding this comment

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

And why was this removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was placing the same entity.name.function to all function calls inside defn. Because the token is the same, there was no way to distinguish between function declaration and function call. I checked with other syntaxes and they seem to put entity.name.* on definitions only as well

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, I think this is worth keeping. I checked language-javascript, language-java, and language-php and all use entity.name.function for function declarations and function calls but have an additional meta scope to differentiate between whether it's a declaration or call (usually meta.function vs meta.{function|method}-call).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe you’re right. I was looking at Ruby. Returned it back

'captures':
'1':
'name': 'entity.name.function.clojure'
'patterns': [
{
'include': '$self'
}
]
}
{
'include': '$self'
}
Expand Down Expand Up @@ -365,7 +354,7 @@
'patterns': [
{ # copied from #symbol, plus a / at the end. Matches the "app/" part of
# "app/*config*"
'match': '([\\w\\.\\-\\_\\:\\+\\=\\>\\<\\!\\?\\*][\\w\\.\\-\\_\\:\\+\\=\\>\\<\\!\\?\\*\\d]+)/'
'match': '([A-Z_a-z!$%&+\\-.:<=>?|*][0-9#\'A-Z_a-z!$%&+\\-.:<=>?|*]*)/'
'captures':
'1':
'name': 'meta.symbol.namespace.clojure'
Expand All @@ -374,12 +363,12 @@
'symbol':
'patterns': [
{
'match': '([\\w\\.\\-\\_\\:\\+\\=\\>\\<\\!\\?\\*][\\w\\.\\-\\_\\:\\+\\=\\>\\<\\!\\?\\*\\d]+)'
'match': '([A-Z_a-z!$%&+\\-.:<=>?|*][0-9#\'A-Z_a-z!$%&+\\-.:<=>?|*]*)'
'name': 'meta.symbol.clojure'
}
]
'var':
'match': '(?<=(\\s|\\(|\\[|\\{)\\#)\'[a-zA-Z0-9\\.\\-\\_\\:\\+\\=\\>\\<\\/\\!\\?\\*]+(?=(\\s|\\)|\\]|\\}))'
'match': '(?<=(\\s|\\(|\\[|\\{)\\#)\'[A-Z_a-z!$%&+\\-.:<=>?|*][0-9#\'A-Z_a-z!$%&+\\-.:<=>?|*/]*(?=(\\s|\\)|\\]|\\}))'
'name': 'meta.var.clojure'
'vector':
'begin': '(\\[)'
Expand Down
24 changes: 14 additions & 10 deletions spec/clojure-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,16 @@ describe "Clojure grammar", ->

it "tokenizes global definitions", ->
macros = ["ns", "declare", "def", "defn", "defn-", "defroutes", "compojure/defroutes", "rum.core/defc123-", "some.nested-ns/def-nested->symbol!?*", "def+!.?abc8:<>", "ns/def+!.?abc8:<>"]
symbols = ["foo", "f", "f'", "*f*"]

for macro in macros
{tokens} = grammar.tokenizeLine "(#{macro} foo 'bar)"
expect(tokens[1]).toEqual value: macro, scopes: ["source.clojure", "meta.expression.clojure", "meta.definition.global.clojure", "keyword.control.clojure"]
expect(tokens[3]).toEqual value: "foo", scopes: ["source.clojure", "meta.expression.clojure", "meta.definition.global.clojure", "entity.global.clojure"]
for symbol in symbols
{tokens} = grammar.tokenizeLine "(#{macro} #{symbol} 'bar)"
expect(tokens[1]).toEqual value: macro, scopes: ["source.clojure", "meta.expression.clojure", "meta.definition.global.clojure", "keyword.control.clojure"]
expect(tokens[3]).toEqual value: symbol, scopes: ["source.clojure", "meta.expression.clojure", "meta.definition.global.clojure", "entity.name.definition.clojure"]

it "tokenizes dynamic variables", ->
mutables = ["*ns*", "*foo-bar*"]
mutables = ["*ns*", "*foo-bar*", "*1#*"]

for mutable in mutables
{tokens} = grammar.tokenizeLine mutable
Expand All @@ -147,18 +149,20 @@ describe "Clojure grammar", ->

for expr in expressions
{tokens} = grammar.tokenizeLine expr
expect(tokens[1]).toEqual value: "foo", scopes: ["source.clojure", "meta.expression.clojure", "entity.name.function.clojure"]
expect(tokens[1]).toEqual value: "foo", scopes: ["source.clojure", "meta.expression.clojure", "meta.symbol.clojure"]

it "tokenizes vars", ->
{tokens} = grammar.tokenizeLine "(func #'foo)"
expect(tokens[2]).toEqual value: " #", scopes: ["source.clojure", "meta.expression.clojure"]
expect(tokens[3]).toEqual value: "'foo", scopes: ["source.clojure", "meta.expression.clojure", "meta.var.clojure"]

it "tokenizes symbols", ->
{tokens} = grammar.tokenizeLine "foo/bar"
expect(tokens[0]).toEqual value: "foo", scopes: ["source.clojure", "meta.symbol.namespace.clojure"]
expect(tokens[1]).toEqual value: "/", scopes: ["source.clojure"]
expect(tokens[2]).toEqual value: "bar", scopes: ["source.clojure", "meta.symbol.clojure"]
for ns in ["foo", "f", "f1", "ns.ns"]
for symbol in ["bar", "b", "b2", "b'", "bar.bar"]
{tokens} = grammar.tokenizeLine "#{ns}/#{symbol}"
expect(tokens[0]).toEqual value: ns, scopes: ["source.clojure", "meta.symbol.namespace.clojure"]
expect(tokens[1]).toEqual value: "/", scopes: ["source.clojure"]
expect(tokens[2]).toEqual value: symbol, scopes: ["source.clojure", "meta.symbol.clojure"]

testMetaSection = (metaScope, puncScope, startsWith, endsWith) ->
# Entire expression on one line.
Expand Down Expand Up @@ -209,7 +213,7 @@ describe "Clojure grammar", ->
{tokens} = grammar.tokenizeLine "((foo bar) baz)"
expect(tokens[0]).toEqual value: "(", scopes: ["source.clojure", "meta.expression.clojure", "punctuation.section.expression.begin.clojure"]
expect(tokens[1]).toEqual value: "(", scopes: ["source.clojure", "meta.expression.clojure", "meta.expression.clojure", "punctuation.section.expression.begin.clojure"]
expect(tokens[2]).toEqual value: "foo", scopes: ["source.clojure", "meta.expression.clojure", "meta.expression.clojure", "entity.name.function.clojure"]
expect(tokens[2]).toEqual value: "foo", scopes: ["source.clojure", "meta.expression.clojure", "meta.expression.clojure", "meta.symbol.clojure"]
expect(tokens[3]).toEqual value: " ", scopes: ["source.clojure", "meta.expression.clojure", "meta.expression.clojure"]
expect(tokens[4]).toEqual value: "bar", scopes: ["source.clojure", "meta.expression.clojure", "meta.expression.clojure", "meta.symbol.clojure"]
expect(tokens[5]).toEqual value: ")", scopes: ["source.clojure", "meta.expression.clojure", "meta.expression.clojure", "punctuation.section.expression.end.clojure"]
Expand Down