Skip to content

Commit

Permalink
WIP: Updating capf with system-tf
Browse files Browse the repository at this point in the history
  • Loading branch information
gmlarumbe committed Aug 21, 2023
1 parent 48caa1e commit 3d5d460
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 17 deletions.
9 changes: 9 additions & 0 deletions misc/notes.org
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,16 @@ DANGER: Still very inefficient, removed funcall in

* Tree-sitter dev
** Pending
*** TODO Check capf
*** TODO Check TODOs in the whole project
*** TODO Check verilog-ts-mode
*** TODO Grab the hashes files thing from laptop
*** TODO Check if a after-save hook can spawn a update-async of this type thing
- Tooslow some times?...
*** TODO Move to notes the thing of doing a per project/dir cache (same as vhdl-ext or projectile)

*** Hashes for cache/updating only modified files
- Create function that removes entries associated to :file in defs/inst/refs tables
*** Tests
- [ ] Refactor/reorganize and prepare to split into separate repo for verilog-ts-mode

Expand Down
110 changes: 96 additions & 14 deletions verilog-ext-capf.el
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,50 @@

(require 'verilog-ext-utils)

;; Fetched from IEEE 1800-2012
;; https://ece.uah.edu/~gaede/cpe526/2012%20System%20Verilog%20Language%20Reference%20Manual.pdf
(defconst verilog-ext-capf-system-tf
'(;; Section 20: Utility system tasks and system functions
;; TODO Where are the $urandom and $urandom_range ?
"$finish" "$stop" "$exit" "$realtime" "$stime" "$time" "$printtimescale"
"$timeformat" "$bitstoreal" "$realtobits" "$bitstoshortreal"
"$shortrealtobits" "$itor" "$rtoi" "$signed" "$unsigned" "$cast" "$bits"
"$isunbounded" "$typename" "$unpacked_dimensions" "$dimensions" "$left"
"$right" "$low" "$high" "$increment" "$size" "$clog2" "$asin" "$ln" "$acos"
"$log10" "$atan" "$exp" "$atan2" "$sqrt" "$hypot" "$pow" "$sinh" "$floor"
"$cosh" "$ceil" "$tanh" "$sin" "$asinh" "$cos" "$acosh" "$tan" "$atanh"
"$countbits" "$countones" "$onehot" "$onehot0" "$isunknown" "$fatal"
"$error" "$warning" "$info" "$fatal" "$error" "$warning" "$info" "$asserton"
"$assertoff" "$assertkill" "$assertcontrol" "$assertpasson" "$assertpassoff"
"$assertfailon" "$assertfailoff" "$assertnonvacuouson" "$assertvacuousoff"
"$sampled" "$rose" "$fell" "$stable" "$changed" "$past" "$past_gclk"
"$rose_gclk" "$fell_gclk" "$stable_gclk" "$changed_gclk" "$future_gclk"
"$rising_gclk" "$falling_gclk" "$steady_gclk" "$changing_gclk"
"$coverage_control" "$coverage_get_max" "$coverage_get" "$coverage_merge"
"$coverage_save" "$get_coverage" "$set_coverage_db_name" "$load_coverage_db"
"$random" "$dist_chi_square" "$dist_erlang" "$dist_exponential"
"$dist_normal" "$dist_poisson" "$dist_t" "$dist_uniform" "$q_initialize"
"$q_add" "$q_remove" "$q_full" "$q_exam" "$async$and$array"
"$async$and$plane" "$async$nand$array" "$async$nand$plane" "$async$or$array"
"$async$or$plane" "$async$nor$array" "$async$nor$plane" "$sync$and$array"
"$sync$and$plane" "$sync$nand$array" "$sync$nand$plane" "$sync$or$array"
"$sync$or$plane" "$sync$nor$array" "$sync$nor$plane" "$system"
;; Section 21: Input/output system tasks and system function
"$display" "$write" "$displayb" "$writeb" "$displayh" "$writeh" "$displayo"
"$writeo" "$strobe" "$monitor" "$strobeb" "$monitorb" "$strobeh" "$monitorh"
"$strobeo" "$monitoro" "$monitoroff" "$monitoron" "$fclose" "$fopen"
"$fdisplay" "$fwrite" "$fdisplayb" "$fwriteb" "$fdisplayh" "$fwriteh"
"$fdisplayo" "$fwriteo" "$fstrobe" "$fmonitor" "$fstrobeb" "$fmonitorb"
"$fstrobeh" "$fmonitorh" "$fstrobeo" "$fmonitoro" "$swrite" "$sformat"
"$swriteb" "$sformatf" "$swriteh" "$fgetc" "$swriteo" "$ungetc" "$fscanf"
"$fgets" "$fread" "$sscanf" "$fseek" "$rewind" "$fflush" "$ftell" "$feof"
"$ferror" "$readmemb" "$readmemh" "$writememb" "$writememh" "$test$plusargs"
"$value$plusargs" "$dumpfile" "$dumpvars" "$dumpoff" "$dumpon" "$dumpall"
"$dumplimit" "$dumpflush" "$dumpports" "$dumpportsoff" "$dumpportson"
"$dumpportsall" "$dumpportslimit" "$dumpportsflush"))

;; TODO: Add completion for directives

(defun verilog-ext-capf-get-table-entry (table &optional tag)
"Get symbol at point entry from capf TABLE.
Expand All @@ -48,7 +92,13 @@ Only returns the type of the first occurrence in the :locs property of ENTRY."
(let ((locs (plist-get entry :locs)))
(plist-get (car locs) :type)))

(defun verilog-ext-capf--dot-completion-bds ()
(defun verilog-ext-capf-get-completions (table &optional tag)
"Get completion candidates from TABLE.
If optional arg TAG is nil, get completions for symbol at point."
(verilog-ext-capf-get-entry-items (verilog-ext-capf-get-table-entry table tag)))

(defun verilog-ext-capf--dot-completion-bounds ()
"Return bounds of dot completion, if possible, for `completion-at-point'.
Return value is a cons with (start . end) bounds."
Expand All @@ -66,7 +116,7 @@ Return value is a cons with (start . end) bounds."
(when (and start end)
(cons start end))))

(defun verilog-ext-capf--scope-completion-bds ()
(defun verilog-ext-capf--scope-completion-bounds ()
"Return bounds of scope completion, if possible, for `completion-at-point'.
Return value is a cons with (start . end) bounds."
Expand All @@ -84,6 +134,24 @@ Return value is a cons with (start . end) bounds."
(when (and start end)
(cons start end))))

(defun verilog-ext-capf--system-tf-bounds ()
"Return bounds of system tasks and functions, if possible, for `completion-at-point'.
Return value is a cons with (start . end) bounds."
(let (start end)
(cond ((eq (preceding-char) ?$)
(setq start (1- (point)))
(setq end (point)))
((save-excursion
(skip-chars-backward verilog-identifier-sym-re)
(setq start (1- (point)))
(eq (preceding-char) ?$))
(setq end (point)))
(t
nil))
(when (and start end)
(cons start end))))

(defun verilog-ext-capf-annotation-function (cand defs-table inst-table)
"Completion annotation function for candidate CAND.
Expand All @@ -97,8 +165,8 @@ Get candidate type from DEFS-TABLE, or if not found, from INST-TABLE."
("task" "<t>")
(_ type))))

(defun verilog-ext-capf (table inst-table annotation-fn)
"Complete with identifiers present in TABLE.
(cl-defun verilog-ext-capf (&key defs-table inst-table refs-table annotation-fn)
"Complete with identifiers present in DEFS-TABLE, INST-TABLE and REFS-TABLE.
Show annotations using function ANNOTATION-FN.
Expand All @@ -107,33 +175,47 @@ the workspace."
(interactive)
(let* (bounds start end completions)
(cond (;; Dot completion for object methods/attributes and hierarchical references
(setq bounds (verilog-ext-capf--dot-completion-bds))
(setq bounds (verilog-ext-capf--dot-completion-bounds))
(let (table-entry-value block-type)
(save-excursion
(goto-char (car bounds))
(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)
(setq table-entry-value (or (verilog-ext-capf-get-table-entry defs-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 (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)))))))
(setq completions (append (verilog-ext-capf-get-completions defs-table block-type)
(verilog-ext-capf-get-completions inst-table block-type)))))))
(;; Class static methods/members and package items
(setq bounds (verilog-ext-capf--scope-completion-bds))
(setq bounds (verilog-ext-capf--scope-completion-bounds))
(save-excursion
(goto-char (car bounds))
(backward-char 2)
(while (eq (preceding-char) ?\]) ; Skip array indexes
(verilog-ext-backward-sexp))
(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)))))
(setq completions (append (verilog-ext-capf-get-completions defs-table)
(verilog-ext-capf-get-completions inst-table)))))
(;; System tasks and functions
(setq bounds (verilog-ext-capf--system-tf-bounds))
(setq completions verilog-ext-capf-system-tf))
(t ; Fallback, all project completions
(setq bounds (bounds-of-thing-at-point 'symbol))
(setq completions table)))
;; TODO: Refactor this part, a bit ugly still
(let (my-list)
(maphash (lambda (key value)
(push key my-list))
defs-table)
(maphash (lambda (key value)
(push key my-list))
inst-table)
(maphash (lambda (key value)
(push key my-list))
refs-table)
(setq completions (delete-dups my-list
)))))
;; )
;; Return value for `completion-at-point'
(setq start (car bounds))
(setq end (cdr bounds))
Expand Down
7 changes: 4 additions & 3 deletions verilog-ext-workspace.el
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,10 @@ With current-prefix or VERBOSE, dump output log."
"Verilog-ext `completion-at-point' function.
Complete with identifiers of current workspace."
(interactive)
(verilog-ext-capf verilog-ext-workspace-tags-defs-table
verilog-ext-workspace-tags-inst-table
#'verilog-ext-workspace-capf-annotation-function))
(verilog-ext-capf :defs-table verilog-ext-workspace-tags-defs-table
:inst-table verilog-ext-workspace-tags-inst-table
:refs-table verilog-ext-workspace-tags-refs-table
:annotation-fn #'verilog-ext-workspace-capf-annotation-function))

(defun verilog-ext-workspace-capf-set (&optional disable)
"Enable or DISABLE builtin capf function.
Expand Down

0 comments on commit 3d5d460

Please sign in to comment.