Skip to content

Commit

Permalink
add support for scoped tags
Browse files Browse the repository at this point in the history
- tags have new scope field, giving the source span
  in which they are in scope
- new option "--locals" adds tags for local declarations and parameters;
  scope format is startline:startcolumn-endline:endcolumn
- export object properties have "global" scope

This depends on narcissus patch for column tracking and source spans.
Patch preview:

add column tracking/line:column srcspans
clausreinke/narcissus@6d1ae3f

First client implementation, scoped tags navigation for Vim:
  https://github.com/clausreinke/scoped_tags
  • Loading branch information
Claus Reinke committed Sep 12, 2011
1 parent d2195fb commit 2815cad
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
16 changes: 12 additions & 4 deletions bin/jsctags.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function usage() {
sys.puts(" -j, --jsonp function use JSONP with a function name");
sys.puts(" -o, --output file synonym for -f");
sys.puts(" --oneprog combine all inputs into one program");
sys.puts(" --locals add scoped tags for local variables");
sys.puts(" -L, --libroot dir add a CommonJS module root (like " +
"require.paths)")
sys.puts(" -W, --warning level set log level (debug/info/warn/" +
Expand All @@ -65,8 +66,8 @@ function usage() {

var opts;
try {
opts = getopt("help|h", "jsonp|j=s", "libroot|L=s@", "oneprog", "output|o|f=s",
"sort|=s", "warning|W=s");
opts = getopt("help|h", "jsonp|j=s", "libroot|L=s@", "oneprog", "locals",
"output|o|f=s", "sort|=s", "warning|W=s");
} catch (e) {
sys.puts(e);
usage();
Expand Down Expand Up @@ -101,6 +102,8 @@ if ('libroot' in opts) {
});
}

var options = opts.locals ? {params: true, localVars: true} : {};

var tags = new Tags();

// Ascend the directory hierarchy to find the CommonJS package this module is
Expand Down Expand Up @@ -172,6 +175,11 @@ function getModuleInfo(fullPath) {
return { commonJS: false };
}

function addOptions(options,addIns) {
for (var a in addIns) options[a] = addIns[a];
return options;
}

var idsSeen = {};
function processPath(p,named) {
var st = fs.statSync(p);
Expand All @@ -189,7 +197,7 @@ function processPath(p,named) {
} else if (ext === ".js" || ext === ".jsm" || named) {
try {
var data = fs.readFileSync(p, "utf8");
tags.scan(data, p, getModuleInfo(p));
tags.scan(data, p, addOptions(getModuleInfo(p),options));
} catch (e) {
if ('lineNumber' in e) {
sys.puts("error:" + p + ":" + e.lineNumber + ": " + e);
Expand Down Expand Up @@ -230,7 +238,7 @@ function processMany() {

for (data = "", i = pcm1; i >= 0; --i) data = fileinfo[i].data + data;
// the decision to pass argv[2] here is arbitrary
tags.scan(data, argv[2], getModuleInfo(argv[2]));
tags.scan(data, argv[2], addOptions(getModuleInfo(argv[2]),options));

function getFileInfo(ln) {
var i = 0, f, s = 0, e = pcm1, floor = Math.floor;
Expand Down
29 changes: 27 additions & 2 deletions lib/cfa2/jscfa.js
Original file line number Diff line number Diff line change
Expand Up @@ -3675,6 +3675,7 @@ function getTags(ast, pathtofile, lines, options) {
tag.type = type;
tag.module = options.module;
tag.lineno = exports_object.lines[tag.name].toString();
tag.scope = "global"; // global modulo tag.module-import
tags.push(tag);
});
}
Expand All @@ -3687,8 +3688,31 @@ function getTags(ast, pathtofile, lines, options) {
kind : "f",
type : funToType(f),
lineno : f.lineno.toString(),
sortno : f.lineno.toString()
sortno : f.lineno.toString(),
scope : f.context.startPos+'-'+f.context.endPos
});
options.params && f.params.forEach(function(param,i){
tags.push({ name : param.name,
tagfile : pathtofile,
addr : regexify(lines[f.lineno - 1]),
kind : "vp", // distinguish params from other vars?
type : summaries[addr].type[0][i+1].toType(),
lineno : f.lineno.toString(),
sortno : f.lineno.toString(),
scope : f.startPos+'-'+f.endPos
});
});
options.localVars && f.body.varDecls.forEach(function(vd){
tags.push({ name : vd.name,
tagfile : pathtofile,
addr : regexify(lines[vd.lineno - 1]),
kind : "v",
type : flags[vd.addr] ? heap[vd.addr].toType() : "(local)", // local types?
lineno : vd.lineno.toString(),
sortno : vd.lineno.toString(),
scope : f.body.startPos+'-'+f.body.endPos
});
});
}
ast.varDecls.forEach(function(vd) {
tags.push({ name : vd.name,
Expand All @@ -3697,7 +3721,8 @@ function getTags(ast, pathtofile, lines, options) {
kind : "v",
type : heap[vd.addr].toType(),
lineno : vd.lineno.toString(),
sortno : vd.lineno.toString()
sortno : vd.lineno.toString(),
scope : ast.startPos+'-'+ast.endPos // TODO: non-module scripts?
});
});
return tags;
Expand Down

0 comments on commit 2815cad

Please sign in to comment.