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

Highlight field access differently from calls #73

Merged
merged 1 commit into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 30 additions & 8 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,30 @@

; Calls

; * function call
; * local function call
(call
target: [
; local
(identifier) @function
; remote
(dot
right: (identifier) @function)
])
target: (identifier) @function)

; * remote function call
(call
target: (dot
right: (identifier) @function))

; * field without parentheses or block
(call
target: (dot
right: (identifier) @property)
.)

; * remote call without parentheses or block (overrides above)
(call
target: (dot
left: [
(alias)
(atom)
]
right: (identifier) @function)
.)

; * definition keyword
(call
Expand Down Expand Up @@ -140,6 +155,13 @@
right: (identifier) @variable))
(#match? @keyword "^(def|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp|defp)$"))

; * pipe into field without parentheses (function call)
(binary_operator
operator: "|>"
right: (call
target: (dot
right: (identifier) @function)))
Copy link
Member

Choose a reason for hiding this comment

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

For what is worth, Elixir will likely emit runtime warnings for this in the future (i.e. Elixir agrees it is a function call but it will treat the code without parens as deprecated).

Copy link
Member Author

Choose a reason for hiding this comment

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

You CALL!

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean the same for foo |> bar?

Copy link
Member

Choose a reason for hiding this comment

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

We will warn on foo |> bar in the future as well. But first we want to change the formatter to automatically add the parens.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good. I think it's fine to keep highlighting for now and align once is changes upstream. Highlighting rules are an easy change.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, to be clear, it will always be a call. So this is correct. We will just emit warnings too.


; Operators

; * capture operand
Expand Down
34 changes: 34 additions & 0 deletions test/highlight/calls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,37 @@ end)
# ^ operator
# ^ number
# ^ punctuation.bracket

map.key1
# ^ variable
# ^ property

map.key1.key2
# ^ variable
# ^ property
# ^ property

DateTime.utc_now.day
# ^ module
# ^ function
# ^ property

arg |> mod.func
# ^ variable
# ^ operator
# ^ variable
# ^ function

Mod.fun do
# ^ module
# ^ function
# ^ keyword
end
# ^ keyword

mod.fun do
# ^ variable
# ^ function
# ^ keyword
end
# ^ keyword
2 changes: 1 addition & 1 deletion test/highlight/kernel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ case __ENV__.line do
# ^ keyword
# ^ constant.builtin
# ^ operator
# ^ function
# ^ property
# ^ keyword
x when is_integer(x) -> x
# <- variable
Expand Down
Loading