Skip to content

Commit

Permalink
WIP: Add inst-table for capf hierarchical refs completion
Browse files Browse the repository at this point in the history
  • Loading branch information
gmlarumbe committed Aug 18, 2023
1 parent 578cc98 commit bcfff28
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
10 changes: 10 additions & 0 deletions ts-mode/verilog-ts-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,19 @@ Defaults to .v, .vh, .sv and .svh."
((string-match "class_method" (treesit-node-type node))
(or (treesit-node-text (treesit-search-subtree node "\\(function\\|task\\)_identifier") :no-prop)
"new"))
;; ((string-match "\\(module\\|interface\\)_instantiation" (treesit-node-type node))
;; (let ((type (treesit-node-text (treesit-search-subtree node "simple_identifier") :no-prop))
;; (name (treesit-node-text (treesit-search-subtree node "instance_identifier") :no-prop)))
;; (cons type name)))
(t
(treesit-node-text (treesit-search-subtree node "simple_identifier") :no-prop)))))

(defun verilog-ts--node-instance-name (node)
"Return identifier name of NODE."
(when (and node
(string-match "\\(module\\|interface\\)_instantiation" (treesit-node-type node)))
(treesit-node-text (treesit-search-subtree node "instance_identifier") :no-prop)))

(defun verilog-ts--highest-node-at-pos (pos)
"Return highest node in the hierarchy that starts at POS.
Expand Down
12 changes: 8 additions & 4 deletions verilog-ext-capf.el
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,23 @@ the workspace."
(backward-char)
(while (eq (preceding-char) ?\]) ; Skip array indexes
(verilog-ext-backward-sexp))
(setq table-entry-value (or (verilog-ext-capf-get-table-entry table) ; Search for definitions of objects and instances
(verilog-ext-capf-get-table-entry inst-table))) ; Works for hierarchical references
(setq table-entry-value (or (verilog-ext-capf-get-table-entry table)
(verilog-ext-capf-get-table-entry inst-table))) ; Search for definitions of objects and instances
(when table-entry-value
(setq block-type (verilog-ext-capf-get-entry-type table-entry-value))
(setq completions (verilog-ext-capf-get-entry-items (verilog-ext-capf-get-table-entry table block-type)))))))
(setq completions (append (verilog-ext-capf-get-entry-items (verilog-ext-capf-get-table-entry table block-type))
(verilog-ext-capf-get-entry-items (verilog-ext-capf-get-table-entry inst-table block-type))))))))
;; (setq completions (verilog-ext-capf-get-entry-items (verilog-ext-capf-get-table-entry inst-table block-type)))))))
(;; Class static methods/members and package items
(setq bounds (verilog-ext-capf--scope-completion-bds))
(save-excursion
(goto-char (car bounds))
(backward-char 2)
(while (eq (preceding-char) ?\]) ; Skip array indexes
(verilog-ext-backward-sexp))
(setq completions (verilog-ext-capf-get-entry-items (verilog-ext-capf-get-table-entry table)))))
(setq completions (append (verilog-ext-capf-get-entry-items (verilog-ext-capf-get-table-entry table))
(verilog-ext-capf-get-entry-items (verilog-ext-capf-get-table-entry inst-table))))))
;; (setq completions (verilog-ext-capf-get-entry-items (verilog-ext-capf-get-table-entry inst-table)))))
(t ; Fallback, all project completions
(setq bounds (bounds-of-thing-at-point 'symbol))
(setq completions table)))
Expand Down
41 changes: 27 additions & 14 deletions verilog-ext-tags.el
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,8 @@ completion."
(setq tag (match-string-no-properties 2))
(setq type (match-string-no-properties 1))
(setq desc (verilog-ext-tags-desc))
(verilog-ext-tags-table-push-tag table tag type desc file parent :only-items)
(verilog-ext-tags-table-push-tag inst-table tag type desc file parent)
))
;; (verilog-ext-tags-table-push-tag table tag type desc file parent :only-items)
(verilog-ext-tags-table-push-tag inst-table tag type nil file parent)))
("structs" (while (setq data (verilog-ext-find-struct))
(setq tag (alist-get 'name data))
(setq type "struct")
Expand Down Expand Up @@ -279,6 +278,7 @@ Limit search between START and LIMIT if provided."
"task_declaration"
"class_constructor_declaration"
"local_parameter_declaration"
"ansi_port_declaration"
;; "data_declaration" ; INFO: This one is too generic. E.g: doesn't allow parsing of multiple variables declarations in one-line
"variable_decl_assignment"
"net_decl_assignment"
Expand All @@ -287,16 +287,16 @@ Limit search between START and LIMIT if provided."
"module_instantiation"
"interface_instantiation")))

(defun verilog-ext-tags-table-push-definitions-ts (table &optional file)
(defun verilog-ext-tags-table-push-definitions-ts (table &optional file inst-table)
"Push definitions found in FILE inside hash table TABLE using tree-sitter."
(let* ((node (treesit-buffer-root-node 'verilog))
(tree (treesit-induce-sparse-tree
node
verilog-ext-tags-definitions-ts-re
nil 1000)))
(verilog-ext-tags-table-push-definitions-ts--recurse table tree nil file)))
(verilog-ext-tags-table-push-definitions-ts--recurse table tree nil file inst-table)))

(defun verilog-ext-tags-table-push-definitions-ts--recurse (table node parent &optional file)
(defun verilog-ext-tags-table-push-definitions-ts--recurse (table node parent &optional file inst-table)
"Push definitions found in FILE inside hash table TABLE using tree-sitter.
Traverse the tree starting at NODE.
Expand All @@ -305,7 +305,7 @@ PARENT is passed as an argument to build the :items prop list of TABLE."
(let* ((ts-node (car node))
(children (cdr node))
(subtrees (mapc (lambda (leaf)
(verilog-ext-tags-table-push-definitions-ts--recurse table leaf ts-node file))
(verilog-ext-tags-table-push-definitions-ts--recurse table leaf ts-node file inst-table))
children))
(type (pcase (treesit-node-type ts-node)
("list_of_variable_decl_assignments" "variable_decl_assignment") ; Rename to avoid grouping problems for xref
Expand All @@ -316,13 +316,26 @@ PARENT is passed as an argument to build the :items prop list of TABLE."
subtrees)
(t
(goto-char (treesit-node-start ts-node))
(verilog-ext-tags-table-push-tag table
(verilog-ts--node-identifier-name ts-node)
type
(verilog-ext-tags-desc)
file
(verilog-ts--node-identifier-name parent)
only-items)))))
(if (not only-items)
(verilog-ext-tags-table-push-tag table
(verilog-ts--node-identifier-name ts-node) ; TODO: Here it is not relevant the return value I guess? for instances?
type
(verilog-ext-tags-desc)
file
(verilog-ts--node-identifier-name parent)
;; only-items
)
;; TODO: Use variables so that pushing does not repeat code between both `verilog-ext-tags-table-push-tag'
;; (when only-items ; TODO: Rename, only-items equals there is an instance
(verilog-ext-tags-table-push-tag inst-table
(verilog-ts--node-instance-name ts-node) ; TODO: Fix the function, so that it returns properly the name and type
(verilog-ts--node-identifier-name ts-node)
;; (verilog-ext-tags-desc)
nil
file
(verilog-ts--node-identifier-name parent) ; TODO: Is it relevant the parent thing here?
;; (verilog-ts--node-instance-name parent)
nil))))))

(defun verilog-ext-tags-table-push-references-ts (table &optional defs-table file)
"Push references found in FILE inside hash table TABLE using tree-sitter.
Expand Down
5 changes: 3 additions & 2 deletions verilog-ext-workspace.el
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ With current-prefix or VERBOSE, dump output log."
(num-files (length files))
(num-files-processed 0)
(table (make-hash-table :test #'equal))
(inst-table verilog-ext-workspace-tags-inst-table)
(inst-table (make-hash-table :test #'equal))
(log-file (file-name-concat verilog-ext-workspace-cache-dir "tags.log"))
msg progress)
(when verbose
Expand All @@ -269,7 +269,7 @@ With current-prefix or VERBOSE, dump output log."
(cond (;; Tree-sitter
(eq verilog-ext-tags-backend 'tree-sitter)
(verilog-ts-mode)
(verilog-ext-tags-table-push-definitions-ts table file))
(verilog-ext-tags-table-push-definitions-ts table file inst-table))
(;; Builtin
(eq verilog-ext-tags-backend 'builtin)
(verilog-mode)
Expand All @@ -287,6 +287,7 @@ With current-prefix or VERBOSE, dump output log."
(setq num-files-processed (1+ num-files-processed))))
(setq verilog-ext-workspace-tags-defs-table table)
(setq verilog-ext-workspace-cache-tags-defs table) ; Update cache
(setq verilog-ext-workspace-tags-inst-table inst-table) ; TODO: Add cache for instances?
(verilog-ext-workspace-serialize-cache 'tags-defs)
;; References
(setq table (make-hash-table :test #'equal)) ; Clean table
Expand Down

0 comments on commit bcfff28

Please sign in to comment.