Skip to content

Commit

Permalink
WIP: Improvements in tags, do not add instances as defs
Browse files Browse the repository at this point in the history
  • Loading branch information
gmlarumbe committed Aug 17, 2023
1 parent 287f5dd commit 44df727
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions verilog-ext-tags.el
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@


;;;; Common
(defun verilog-ext-tags-table-add-entry (table tag type desc &optional file parent)
(defun verilog-ext-tags-table-add-entry (table tag type desc &optional file parent only-items)
"Add entry for TAG in hash-table TABLE.
It is needed to provide TYPE, description DESC and FILE properties to add the
Expand All @@ -54,26 +54,30 @@ If there is no entry in the table for TAG add one. Otherwise update the
existing one with current location properties."
(let ((tag-value (gethash tag table))
locs-plist loc-new parent-value parent-items)
(if (not tag-value)
;; Add tag with properties
(puthash tag `(:items nil :locs (,(verilog-ext-tags-tag-properties type desc file))) table)
;; Otherwise update existing tag properties
(setq locs-plist (plist-get tag-value :locs))
(setq loc-new (verilog-ext-tags-tag-properties type desc file))
(unless (member loc-new locs-plist)
(push loc-new locs-plist)
(plist-put tag-value :locs locs-plist)
(puthash tag `(:items ,(plist-get tag-value :items) :locs ,locs-plist) table)))
;; First add parent and populate its items if it was provided.
;; Create if it did not exist.
(when parent
(setq parent-value (or (gethash parent table)
(puthash parent (list :items nil :locs nil) table)))
(setq parent-items (plist-get parent-value :items))
(unless (member tag parent-items)
(plist-put parent-value :items `(,@parent-items ,tag))
(puthash parent parent-value table)))
;; Next add the tag if it was not present in the table or update existing
;; tag properties if it was present.
(unless only-items
(if (not tag-value)
(puthash tag `(:items nil :locs (,(verilog-ext-tags-tag-properties type desc file))) table)
(setq locs-plist (plist-get tag-value :locs))
(setq loc-new (verilog-ext-tags-tag-properties type desc file))
(unless (member loc-new locs-plist)
(push loc-new locs-plist)
(plist-put tag-value :locs locs-plist)
(puthash tag `(:items ,(plist-get tag-value :items) :locs ,locs-plist) table))))
;; Return table contents
table))

(defmacro verilog-ext-tags-table-push-tag (table tag type desc &optional file parent)
(defmacro verilog-ext-tags-table-push-tag (table tag type desc &optional file parent only-items)
"Push TAG in hash table TABLE.
TAG is of string type TYPE and with string description DESC and located in FILE
Expand All @@ -82,7 +86,7 @@ for `xref'.
Optional arg PARENT is the module where TAG is defined/instantiated for dot
completion."
(declare (indent 0) (debug t))
`(setq ,table (verilog-ext-tags-table-add-entry ,table ,tag ,type ,desc ,file ,parent)))
`(setq ,table (verilog-ext-tags-table-add-entry ,table ,tag ,type ,desc ,file ,parent ,only-items)))

(defun verilog-ext-tags-tag-properties (type desc &optional file)
"Return :locs properties for current tag.
Expand Down Expand Up @@ -124,9 +128,6 @@ buffer."
(declare (indent 0) (debug t))
`(setq ,table (verilog-ext-tags-get-references ,table ,defs-table ,file ,start ,limit)))

;; TODO: Do not add instances as definitions. Just add items to its parent!
;; Add arg to `verilog-ext-tags-table-add-entry' to only add items in the second part of the function
;; and set it to non-nil in caller function if it's a "module|interface_instantiation"
(defun verilog-ext-tags-get-definitions (tag-type table &optional file start limit parent)
"Add definitions of TAG-TYPE to hash-table TABLE for FILE.
Expand Down Expand Up @@ -169,7 +170,7 @@ 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)))
(verilog-ext-tags-table-push-tag table tag type desc file parent :only-items)))
("structs" (while (setq data (verilog-ext-find-struct))
(setq tag (alist-get 'name data))
(setq type "struct")
Expand Down Expand Up @@ -264,15 +265,13 @@ Limit search between START and LIMIT if provided."
"task_declaration"
"class_constructor_declaration"
"local_parameter_declaration"
"data_declaration"
;; "data_declaration" ; TODO: This one is too generic. E.g: doesn't allow parsing of multiple variables declarations in one-line
"variable_decl_assignment"
"class_property"
;; INFO: These two below are only included add items to the defs table for capf
"module_instantiation"
"interface_instantiation")))

;; TODO: Do not add instances as definitions. Just add items to its parent!
;; Add arg to `verilog-ext-tags-table-add-entry' to only add items in the second part of the function
;; and set it to non-nil in caller function if it's a "module|interface_instantiation"
(defun verilog-ext-tags-table-push-definitions-ts (table &optional file)
"Push definitions found in FILE inside hash table TABLE using tree-sitter."
(let* ((node (treesit-buffer-root-node 'verilog))
Expand All @@ -293,7 +292,8 @@ PARENT is passed as an argument to build the :items prop list of TABLE."
(subtrees (mapc (lambda (leaf)
(verilog-ext-tags-table-push-definitions-ts--recurse table leaf ts-node file))
children))
(type (treesit-node-type ts-node)))
(type (treesit-node-type ts-node))
(only-items (and type (string-match "\\(module\\|interface\\)_instantiation" type))))
(cond
((null ts-node)
subtrees)
Expand All @@ -307,7 +307,8 @@ PARENT is passed as an argument to build the :items prop list of TABLE."
type
(verilog-ext-tags-desc)
file
(verilog-ts--node-identifier-name parent))))))
(verilog-ts--node-identifier-name parent)
only-items)))))

(defun verilog-ext-tags-table-push-references-ts (table &optional file)
"Push references found in FILE inside hash table TABLE using tree-sitter."
Expand Down

0 comments on commit 44df727

Please sign in to comment.