0

For example, a tcl file, how to analyse the file and get each variable's value?

It can be done by reading each line, check if there is "set XXX ...", then get variable name and its value. After analysing the whole file, we will get a list of variables and its definition.

Currently only need to support tcl file.
Not sure if there is an easy way to do this.

2
  • 2
    Typical plugins run language specific servers in the background and communicate with them, or make use of external tools’ output. Even so, with this type of static analysis I think stating the exact value of a variable is likely to be impossible in the general case.
    – D. Ben Knoble
    Aug 24, 2018 at 2:19
  • Have you looked at a code analysis tool or linter specifically for Tcl?
    – Herb
    Aug 24, 2018 at 20:23

2 Answers 2

1

LSP approaches are likely to be better.

If no LSP server for Tcl exists, check whether universal-ctags supports local variables in Tcl -- I've just checked that exuberant-ctags doesn't. Once universal-ctags has been properly installed, you can check this with ctags --list-kinds | less + /Tcl

If so, you can then run ctags on your local file with --kinds-Tcl=l (if l is the right kind), make sure that within Vim the tag file produced is registered to the &tags option, then filter() the tag entries (taglist()) to extract those with the l (?) kind.

BTW, I almost forgot, you'll also need to extract the boundaries of function definitions (there is a end field that universal-ctags can provide, unlike ex-ctags) in order to know the functions your local variables belong to.

Note that I'm providing an already fully integrated way to extract local variables though my lh-dev and lh-tags plugins -- as I'm in the process of refactoring these two, if you're interested, drop me an email.

1

In command mode enter

:vimgrep "^\s*set\s" % || :copen

This produces an output like

desktop/a.tcl|11 col 1| set set
desktop/a.tcl|14 col 1| set example {1 2 3}
desktop/a.tcl|21 col 1| set pos [lsearch -exact $set $el]
desktop/a.tcl|22 col 1| set set [lreplace $set $pos $pos]
desktop/a.tcl|25 col 1| set set
desktop/a.tcl|32 col 1| set res {}
desktop/a.tcl|34 col 1| set res  

in the quickfix window.

It extracts all lines starting with optional whitespace (^\s), the word set, and at least one whitespace (\s) into the quickfix list, then opens the quickfix window (|| :copen) to show the list.

(Source code is the "Sets as lists" example from https://en.wikibooks.org/wiki/Tcl_Programming/Examples)


Update:

To make things more generic, you can put this line in your Vim configuration file (~/.vimrc if you are on Linux):

command! -nargs=1 FindFirstWord :vimgrep "^\s*<args>\s" % || :copen

Then you can search any "first word" like this:

:FindFirstWord proc

with result:

1 desktop/a.tcl|1 col 1| proc set'contains {set el} {expr {[lsearch -exact $set $el]>=0}}
2 desktop/a.tcl|6 col 1| proc set'add {_set args} {
3 desktop/a.tcl|18 col 1| proc set'remove {_set args} {
4 desktop/a.tcl|30 col 1| proc set'intersection {a b} {
5 desktop/a.tcl|38 col 1| proc set'union {a b} {
6 desktop/a.tcl|46 col 1| proc set'difference {a b} {

for the same sample code from above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.