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

Fixes for tree-sitter-julia v0.22 #22

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
99 changes: 49 additions & 50 deletions julia-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,12 @@ Otherwise, the indentation is:
:version "29.1"
:type 'boolean)

(defcustom julia-ts-align-parameter-list-to-first-sibling nil
"Align the parameter list to the first sibling.

If it is set to t, the following indentation is used:

function myfunc(a, b,
c, d)

Otherwise, the indentation is:

function myfunc(a, b,
c, d)"
:version "29.1"
:type 'boolean)
;; As of the grammar version 0.22, it uses the argument_list node for
;; both function definitions and calls.
(define-obsolete-variable-alias
'julia-ts-align-parameter-list-to-first-sibling
'julia-ts-align-argument-list-to-first-sibling
"0.3")

(defcustom julia-ts-align-curly-brace-expressions-to-first-sibling nil
"Align curly brace expressions to the first sibling.
Expand Down Expand Up @@ -162,9 +154,9 @@ Otherwise, the indentation is:
(assignment
(field_expression (identifier) "." (identifier) @font-lock-variable-name-face)
(operator))
(assignment (bare_tuple (identifier) @font-lock-variable-name-face))
(assignment (open_tuple (identifier) @font-lock-variable-name-face))
(assignment
(bare_tuple
(open_tuple
(field_expression (identifier) "." (identifier) @font-lock-variable-name-face))
(operator))
(local_statement (identifier) @font-lock-variable-name-face)
Expand All @@ -187,15 +179,25 @@ Otherwise, the indentation is:
:language 'julia
:feature 'definition
`((function_definition
name: (identifier) @font-lock-function-name-face)
(signature
(call_expression (identifier) @font-lock-function-name-face)))
(function_definition
name: (field_expression (identifier) "." (identifier) @font-lock-function-name-face))
(signature
(call_expression
(field_expression
(identifier) "." (identifier) @font-lock-function-name-face))))
(assignment
(call_expression (identifier) @font-lock-function-name-face)
(operator))
(assignment
(call_expression
(field_expression
(identifier) "." (identifier) @font-lock-function-name-face))
(operator))
(macro_definition
name: (identifier) @font-lock-function-name-face)
(short_function_definition
name: (identifier) @font-lock-function-name-face)
(short_function_definition
name: (field_expression (identifier) "." (identifier) @font-lock-function-name-face)))
(signature
(call_expression (identifier) @font-lock-function-name-face)))
(struct_definition name: (identifier) @font-lock-type-face))

:language 'julia
:feature 'error
Expand Down Expand Up @@ -240,7 +242,6 @@ Otherwise, the indentation is:
(for_binding ["=" "∈"] @font-lock-type-face)
(function_expression "->" @font-lock-type-face)
(operator) @font-lock-type-face
(slurp_parameter "..." @font-lock-type-face)
(splat_expression "..." @font-lock-type-face)
(ternary_expression ["?" ":"] @font-lock-type-face)
(["." "::"] @font-lock-type-face))
Expand All @@ -261,8 +262,11 @@ Otherwise, the indentation is:
:override t
`((type_clause (operator) (_) @font-lock-type-face)
(typed_expression (_) "::" (_) @font-lock-type-face)
(typed_parameter
type: (_) @font-lock-type-face)
(type_parameter_list
(binary_expression
(identifier) (operator) (identifier) @font-lock-type-face))
(unary_typed_expression
"::" (_) @font-lock-type-face)
(where_clause "where"
(curly_expression "{"
(binary_expression (identifier)
Expand Down Expand Up @@ -320,28 +324,12 @@ Otherwise, the indentation is:
`((julia-ts--parent-is-and-sibling-on-same-line "argument_list" 1) parent-bol julia-ts-indent-offset))
((julia-ts--parent-is-and-sibling-not-on-same-line "argument_list" 1) parent-bol julia-ts-indent-offset)

;; Alignment of parameter lists.
,(if julia-ts-align-parameter-list-to-first-sibling
`((julia-ts--parent-is-and-sibling-on-same-line "parameter_list" 1) first-sibling 1)
`((julia-ts--parent-is-and-sibling-on-same-line "parameter_list" 1) parent-bol julia-ts-indent-offset))
((julia-ts--parent-is-and-sibling-not-on-same-line "parameter_list" 1) parent-bol julia-ts-indent-offset)

;; Alignment of type parameter lists.
,(if julia-ts-align-argument-list-to-first-sibling
`((julia-ts--parent-is-and-sibling-on-same-line "type_parameter_list" 1) first-sibling 1)
`((julia-ts--parent-is-and-sibling-on-same-line "type_parameter_list" 1) parent-bol julia-ts-indent-offset))
((julia-ts--parent-is-and-sibling-not-on-same-line "type_parameter_list" 1) parent-bol julia-ts-indent-offset)

;; The keyword parameters is a child of parameter list. Hence, we need to
;; consider its grand parent to perform the alignment.
,@(if julia-ts-align-parameter-list-to-first-sibling
(list '((n-p-gp nil "keyword_parameters" "parameter_list")
julia-ts--grand-parent-first-sibling
1))
(list '((n-p-gp nil "keyword_parameters" "parameter_list")
julia-ts--grand-parent-bol
julia-ts-indent-offset)))

;; Match if the node is inside an assignment.
;; Note that if the user wants to align the assignment expressions on the
;; first sibling, we should only check if the first sibling is not on the
Expand All @@ -359,13 +347,22 @@ Otherwise, the indentation is:
"Return the defun name of NODE.
Return nil if there is no name or if NODE is not a defun node."
(pcase (treesit-node-type node)
((or "abstract_definition"
"function_definition"
"short_function_definition"
"struct_definition")
((or "abstract_definition" "struct_definition")
(treesit-node-text
(treesit-node-child-by-field-name node "name")
t))))
t))
("function_definition"
(when-let*
((node1 (julia-ts--child-of-type node "signature"))
(node2 (julia-ts--child-of-type node1 "call_expression"))
(node3 (treesit-node-child node2 0)))
(treesit-node-text node3)))))

(defun julia-ts--child-of-type (node type)
(car (treesit-filter-child
node
(lambda (child)
(equal (treesit-node-type child) type)))))

;;;###autoload
(add-to-list 'auto-mode-alist '("\\.jl\\'" . julia-ts-mode))
Expand Down Expand Up @@ -396,13 +393,15 @@ Return nil if there is no name or if NODE is not a defun node."
;; Navigation.
(setq-local treesit-defun-type-regexp
(rx (or "function_definition"
"struct_definition")))
"struct_definition"
"abstract_definition")))
(setq-local treesit-defun-name-function #'julia-ts--defun-name)

;; Imenu.
(setq-local treesit-simple-imenu-settings
`(("Function" "\\`function_definition\\|short_function_definition\\'" nil nil)
("Struct" "\\`struct_definition\\'" nil nil)))
`(("Function" "\\`function_definition\\'" nil nil)
("Struct" "\\`struct_definition\\'" nil nil)
("Type" "\\`abstract_definition\\'" nil nil)))

;; Fontification
(setq-local treesit-font-lock-settings julia-ts--treesit-font-lock-settings)
Expand Down