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

Misc tweaks #2

Merged
merged 6 commits into from
Aug 20, 2023
Merged
Changes from 5 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
79 changes: 47 additions & 32 deletions ocaml-ts-mode.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
;;; ocaml-ts-mode.el --- tree-sitter support for OCaml -*- lexical-binding: t; -*-
;;; ocaml-ts-mode.el --- Major mode for OCaml using tree-sitter -*- lexical-binding: t; -*-

;; Version: 0.1.0
;; Author: dmitrig
Expand Down Expand Up @@ -89,16 +89,16 @@
(defconst ocaml-ts-mode--types-regexp
(regexp-opt
'("type_variable"
"type_constructor_path"
"constructed_type"
;; "polymorphic_variant_type"
;; "package_type"
"hash_type"
;; "object_type"
;; "parenthesized_type"
"tuple_type"
"function_type"
"aliased_type")
"type_constructor_path"
"constructed_type"
;; "polymorphic_variant_type"
;; "package_type"
"hash_type"
;; "object_type"
;; "parenthesized_type"
"tuple_type"
"function_type"
"aliased_type")
'symbols)
"All node types in _type.")

Expand Down Expand Up @@ -189,7 +189,7 @@
(when-let* ((line (line-number-at-pos (treesit-node-start node)))
(parent (ocaml-ts-mode--parent-mod node type))
(pline (line-number-at-pos (treesit-node-start parent)))
((= pline line)))
((= pline line)))
parent))

(defun ocaml-ts-mode--anchor-dangleable-parent (node &rest _)
Expand Down Expand Up @@ -238,14 +238,19 @@

(catch-all ,dangle-parent (,default-ofs))))))

(defvar ocaml-ts-mode--keywords
'("and" "as" "assert" "begin" "class" "constraint" "do" "done"
"downto" "else" "end" "exception" "external" "for" "fun"
"function" "functor" "if" "in" "include" "inherit" "initializer"
"lazy" "let" "match" "method" "module" "mutable" "new" "nonrec"
"object" "of" "open" "private" "rec" "sig" "struct" "then" "to"
"try" "type" "val" "virtual" "when" "while" "with")
"OCaml keywords for tree-sitter font-locking.")
(defvar ocaml-ts-mode--keywords (string-split "
and as assert asr begin class
constraint do done downto else end
exception external false for fun function
functor if in include inherit initializer
land lazy let lor lsl lsr
lxor match method mod module mutable
new nonrec object of open or
private rec sig struct then to
true try type val virtual when
while with")
"OCaml keywords for tree-sitter font-locking.
List taken directly from https://v2.ocaml.org/manual/lex.html.")
dmitrig marked this conversation as resolved.
Show resolved Hide resolved

(defvar ocaml-ts-mode--constants
'((unit) "true" "false")
Expand Down Expand Up @@ -295,7 +300,12 @@
;; signatures and misc
(instance_variable_name) @font-lock-variable-name-face
(value_specification (value_name) @font-lock-variable-name-face)
(external (value_name) @font-lock-variable-name-face))
(external (value_name) @font-lock-variable-name-face)
;; assignment of bindings in various circumstances
(type_binding ["="] @font-lock-keyword-face)
(let_binding ["="] @font-lock-keyword-face)
(field_expression ["="] @font-lock-keyword-face)
(for_expression ["="] @font-lock-keyword-face))
dmitrig marked this conversation as resolved.
Show resolved Hide resolved

:language language
:feature 'keyword
Expand All @@ -314,6 +324,11 @@
:override t
'([(string) (quoted_string) (character)] @font-lock-string-face)

:language language
:feature 'number
:override t
'((number) @font-lock-number-face)

:language language
:feature 'builtin
`([";;"] @font-lock-preprocessor-face
Expand Down Expand Up @@ -343,16 +358,16 @@
:feature 'type
'([(type_constructor) (type_variable) (hash_type)
(class_name) (class_type_name)] @font-lock-type-face
(function_type "->" @font-lock-type-face)
(tuple_type "*" @font-lock-type-face)
(polymorphic_variant_type ["[>" "[<" ">" "|" "[" "]"] @font-lock-type-face)
(object_type ["<" ">" ";" ".."] @font-lock-type-face)
(constructor_declaration ["->" "*"] @font-lock-type-face)
(record_declaration ["{" "}" ";"] @font-lock-type-face)
(parenthesized_type ["(" ")"] @font-lock-type-face)
(polymorphic_type "." @font-lock-type-face)
(module_name) @font-lock-type-face
(module_type_name) @font-lock-type-face)))
(function_type "->" @font-lock-type-face)
(tuple_type "*" @font-lock-type-face)
(polymorphic_variant_type ["[>" "[<" ">" "|" "[" "]"] @font-lock-type-face)
(object_type ["<" ">" ";" ".."] @font-lock-type-face)
(constructor_declaration ["->" "*"] @font-lock-type-face)
(record_declaration ["{" "}" ";"] @font-lock-type-face)
(parenthesized_type ["(" ")"] @font-lock-type-face)
(polymorphic_type "." @font-lock-type-face)
(module_name) @font-lock-type-face
(module_type_name) @font-lock-type-face)))

(defvar ocaml-ts-mode--defun-type-regexp
(regexp-opt '("type_binding"
Expand Down Expand Up @@ -523,7 +538,7 @@ Return nil if there is no name or if NODE is not a defun node."
(ocaml-ts-mode--font-lock-settings 'ocaml))
(setq-local treesit-font-lock-feature-list
'((comment definition)
(keyword string)
(keyword string number)
(attribute builtin constant type)))

;; Imenu.
Expand Down