Skip to content

Commit

Permalink
feat(treesitter): add Move language syntax highlighting and parser setup
Browse files Browse the repository at this point in the history
- Add tree-sitter parser configuration for Move language
- Add syntax highlighting queries for Move language features
  • Loading branch information
fishman committed Dec 23, 2024
1 parent 172425e commit 940066d
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 5 deletions.
19 changes: 19 additions & 0 deletions ftplugin/move.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@
local config = require('movelang.config.internal')
local compat = require('movelang.compat')

local function setup_move_parser()
local parser_config = require('nvim-treesitter.parsers').get_parser_configs()

-- Only register if not already registered
if not parser_config.move then
parser_config['move'] = {
install_info = {
url = 'https://github.com/fishman/tree-sitter-move',
files = { 'src/parser.c' },
branch = 'main',
},
filetype = 'move',
maintainers = { '@fishman' },
}
end
end

if not vim.g.loaded_movelang then
setup_move_parser()

require('movelang.config.check').check_for_lspconfig_conflict(vim.schedule_wrap(function(warn)
vim.notify_once(warn, vim.log.levels.WARN)
end))
Expand Down
7 changes: 2 additions & 5 deletions lua/movelang/move_analyzer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ M.get_active_movelang_clients = function(bufnr, filter)
client_filter.bufnr = bufnr
end
local clients = vim.lsp.get_clients(client_filter)
if filter and filter.exclude_rustc_target then
if filter then
clients = vim.tbl_filter(function(client)
local move_target = vim.tbl_get(client, 'config', 'settings', 'move-analyzer', 'move', 'target')
if filter.exclude_rustc_target == rustc.DEFAULT_RUSTC_TARGET and move_target == nil then
return false
end
return move_target ~= filter.exclude_rustc_target
return move_target
end, clients)
end

Expand Down
149 changes: 149 additions & 0 deletions queries/move/highlights.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
;; Highlights file for Move

;; Types
(type_parameters) @type
(type_parameter) @type
(type_parameter_identifier) @type
(apply_type) @type
(ref_type) @type.ref
(primitive_type) @type.builtin

;; Comments
(line_comment) @comment
(block_comment) @comment

;; Annotations
(annotation) @annotation
(annotation_item) @annotation.item

;; Constants
(constant name: (constant_identifier) @constant.name)
(constant expr: (num_literal) @constant.value)
((identifier) @constant.name
(#match? @constant.name "^[A-Z][A-Z\\d_]+$'"))

;; Function definitions
(function_definition name: (function_identifier) @function)
(macro_function_definition name: (function_identifier) @macro)
(native_function_definition name: (function_identifier) @function)
(usual_spec_function name: (function_identifier) @function)
(function_parameter name: (variable_identifier) @variable.parameter)

;; Module definitions
(module_identity address: (module_identifier) @namespace.module.address)
(module_identity module: (module_identifier) @namespace.module.name)

;; Function calls
(call_expression (name_expression access: (module_access module: (module_identifier) @namespace.module.name)))
(call_expression (name_expression access: (module_access member: (identifier) @function.call)))


(label (identifier) @label)

;; Macro calls
(macro_call_expression access: (macro_module_access) @macro.call)

;; Literals
(num_literal) @number
(bool_literal) @boolean
(hex_string_literal) @string.hex
(byte_string_literal) @string.byte
(address_literal) @number.address

;; Binders

;; Uses
(use_member member: (identifier) @include.member)
(use_module alias: (module_identifier) @namespace.module.name)

(use_fun (module_access module: (module_identifier) @namespace.module.name))
(use_fun (module_access member: (identifier) @include.member))

(function_identifier) @function.name

;; Friends
; (friend_access local_module: (identifier) @namespace.module.name)

;; Structs
(struct_definition name: (struct_identifier) @type.definition.struct)
(ability) @type.ability
(field_annotation field: (field_identifier) @field.identifier)
(field_identifier) @field.identifier

;; Enums
(enum_definition name: (enum_identifier) @type.definition.struct)
(variant variant_name: (variant_identifier) @constructor.name)

;; Packs
(pack_expression (name_expression access: (module_access) @constructor.name))

;; Unpacks
;; TODO: go into variants
(bind_unpack (name_expression) @type.name)
(module_access "$" (identifier) @macro.variable)
"$" @macro.variable

(module_access module: (module_identifier) member: (identifier) @constructor.name)

(abort_expression) @keyword
(mut_ref) @keyword

;; Lambdas
; (lambda_binding bind: (bind_var (variable_identifier) @variable.parameter))
; (lambda_bindings (bind_var (variable_identifier) @variable.parameter))


;; Operators
(binary_operator) @operator
(unary_op) @operator
"=>" @operator
"@" @operator
"->" @operator

;; Specs
(spec_block target: (identifier) @function.spec.target)
(spec_pragma) @preproc.annotation.spec
(spec_condition kind: (condition_kind) @condition.spec)
(condition_properties) @preproc.spec.condition.properties

; (match_expression "match") @keyword

;; Spec keywords
; "opaque" @keyword
; "aborts_if" @keyword
; "abstract" @keyword
[
"pragma"
] @keyword

;; Source Language Keywords
[
"fun"
"return"
"if"
"else"
"while"
"native"
"struct"
"use"
"public"
"package"
"spec"
"module"
"abort"
"const"
"let"
"has"
"as"
"&"
"friend"
"entry"
"mut"
"macro"
"enum"
"break"
"continue"
"loop"
] @keyword

"match" @keyword
53 changes: 53 additions & 0 deletions queries/move/locals.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
; Function Scope
; (function_definition
; body: (block) @scope
; parameters: (function_parameters (function_parameter name: (variable_identifier) @definition.var)))

(function_definition body: (block) @scope)

(function_parameter name: (variable_identifier) @definition.var)
(function_parameter name: (variable_identifier) @local.definition)

(identifier) @local.reference


; Module and Struct Scope
(module_definition
(module_body) @scope)

; (struct_definition
; (struct_def_fields) @scope)

; Spec Block Scope
(spec_block
body: (spec_body) @scope)

; Local Variable Declarations in Function Blocks
; (let_statement
; binds: (bind_list
; (bind_var (variable_identifier) @definition.var)
; (bind_unpack
; (bind_field (field_identifier) @definition.var))))

; Local Variable Declarations in Spec Blocks
; (spec_block
; (spec_variable name: (identifier) @definition.var))

; Parameters in Spec Functions
; (spec_function
; parameters: (function_parameters (function_parameter name: (identifier) @definition.var)))
;
; ; Type Parameters
; (type_parameters
; (type_parameter name: (identifier) @definition.type)))
;
; ; Struct Fields
; (struct_definition
; (struct_def_fields
; (field_annotation
; field: (identifier) @definition.field)))
;
; ; Function and Module Identifiers
; (function_definition name: (identifier) @definition.function)
; (module_definition name: (identifier) @definition.module)
;

0 comments on commit 940066d

Please sign in to comment.