Skip to content

Commit

Permalink
Merge pull request #2 from purcell/main
Browse files Browse the repository at this point in the history
Misc tweaks
  • Loading branch information
dmitrig authored Aug 20, 2023
2 parents 5762593 + 492c9ee commit bb8c86b
Showing 1 changed file with 50 additions and 31 deletions.
81 changes: 50 additions & 31 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 @@ -239,13 +239,22 @@
(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.")
(let ((infix-operators '("asr" "land" "lor" "lsl" "lsr" "lxor" "or" "mod")))
(seq-remove (lambda (k) (seq-position infix-operators k))
(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.
Infix operators are parsed and fontified separately.")

(defvar ocaml-ts-mode--constants
'((unit) "true" "false")
Expand Down Expand Up @@ -295,7 +304,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))

:language language
:feature 'keyword
Expand All @@ -314,6 +328,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 +362,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 +542,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

0 comments on commit bb8c86b

Please sign in to comment.