Permalink
Browse files

Merge pull request #24 from ajaxorg/ui/searchreplace

Updates for search in files
  • Loading branch information...
2 parents 1a55b4f + ed5bb7b commit 535243c7243975685c23f65f5c11c04e66b884ba @mikedeboer committed May 23, 2012
Showing with 53 additions and 99 deletions.
  1. +53 −99 lib/DAV/plugins/codesearch.js
@@ -30,6 +30,7 @@ jsDAV_Codesearch_Plugin.IGNORE_DIRS = {
".hg" : "Mercurial",
".pc" : "quilt",
".svn" : "Subversion",
+ "_MTN" : "Monotone",
"blib" : "Perl module building",
"CVS" : "CVS",
"RCS" : "RCS",
@@ -69,7 +70,7 @@ jsDAV_Codesearch_Plugin.MAPPINGS = {
"jsp" : ["jsp", "jspx", "jhtm", "jhtml"],
"lisp" : ["lisp", "lsp"],
"lua" : ["lua"],
- "make" : ["makefile"],
+ "make" : ["makefile", "Makefile"],
"mason" : ["mas", "mhtml", "mpl", "mtxt"],
"markdown" : ["md", "markdown"],
"objc" : ["m", "h"],
@@ -114,7 +115,10 @@ jsDAV_Codesearch_Plugin.PATTERN_DIR = Util.escapeRegExp(dirs.join("|"));
jsDAV_Codesearch_Plugin.PATTERN_EDIR = dirs.join(",");
jsDAV_Codesearch_Plugin.GREP_CMD = GnuTools.GREP_CMD;
jsDAV_Codesearch_Plugin.PERL_CMD = "perl";
-jsDAV_Codesearch_Plugin.MAXSIZE = 68157440; // 65MB
+
+var filecount;
+var count;
+var prevFile;
(function() {
this.initialize = function() {
@@ -126,8 +130,12 @@ jsDAV_Codesearch_Plugin.MAXSIZE = 68157440; // 65MB
return e.next();
e.stop();
+ filecount = 0;
+ count = 0;
+ prevFile = null;
+
var uri = this.handler.getRequestUri();
- var options = this.parseOptions(dom);
+ var options = this.parseOptions(dom);
var self = this;
options.uri = uri;
this.handler.server.tree.getNodeForPath(uri, function(err, node) {
@@ -146,7 +154,7 @@ jsDAV_Codesearch_Plugin.MAXSIZE = 68157440; // 65MB
e.stop();
});
});
- };
+ }
this.parseOptions = function(dom) {
var options = {};
@@ -166,29 +174,22 @@ jsDAV_Codesearch_Plugin.MAXSIZE = 68157440; // 65MB
var include = "";
- if (!Util.empty(options.pattern)) {
- var patterns = options.pattern.split(",");
-
- if (patterns.length > 0) {
- patterns.forEach(function (p, idx) {
- patterns[idx] = p.split('.').pop(); // trim "*.", e.g. *.js -> js, foo.js.cs -> cs
- });
-
- patterns.push("");
- include = "*.{" + patterns.join(",") + "}";
- }
- else {
- include = "*.{" + jsDAV_Codesearch_Plugin.PATTERN_EXT + "}";
- }
+ if (!Util.empty(options.pattern)) { // handles grep peculiarities with --include
+ if (options.pattern.split(",").length > 1)
+ include = "{" + options.pattern + "}";
+ else
+ include = options.pattern;
}
else {
- include = "*.{" + jsDAV_Codesearch_Plugin.PATTERN_EXT + "}";
+ include = "\\*{" + jsDAV_Codesearch_Plugin.PATTERN_EXT + "}";
}
if (options.maxresults) {
cmd += "-m " + parseInt(options.maxresults, 10);
}
+ if (Util.isTrue(options.wholeword))
+ cmd += " -w"
var query = options.query;
// grep has a funny way of handling new lines (that is to say, it's non-existent)
// if we're not doing a regex search, then we must split everything between the
@@ -223,120 +224,73 @@ jsDAV_Codesearch_Plugin.MAXSIZE = 68157440; // 65MB
Util.log("search command: " + cmd);
}
- // Print header
-
- cbwrite('<?xml version="1.0" encoding="utf-8"?><d:multistatus count="many"');
-
- // Adding in default namespaces
- var prefix;
- for (var namespace in this.handler.xmlNamespaces) {
- prefix = this.handler.xmlNamespaces[namespace];
- cbwrite(' xmlns:' + prefix + '="' + namespace + '"');
+ try {
+ this.grep = Spawn("/bin/bash", ["-c", cmd]);
}
-
- cbwrite('>');
-
- var optionsDesc = [];
- if (Util.isTrue(options.casesensitive)) {
- optionsDesc.push("case sensitive");
+ catch (e) {
+ return cbend(null, "Could not spawn grep process");
}
- if (Util.isTrue(options.regexp)) {
- optionsDesc.push("regexp");
- }
-
- if (optionsDesc.length > 0) {
- optionsDesc = "(" + optionsDesc.join(", ") + ")";
- }
- else {
- optionsDesc = "";
- }
-
- // TODO move this down at the end with the count
- cbwrite('<d:querydetail query="' + Util.escapeXml(options.query) + '" replacement="' + Util.escapeXml(options.replacement) + '" options="' + optionsDesc + '"/>');
- var grep = Spawn("/bin/bash", ["-c", cmd]);
+ var err = "", out = "";
- grep.stdout.setEncoding("utf8");
- grep.stderr.setEncoding("utf8");
+ this.grep.stdout.setEncoding("utf8");
+ this.grep.stderr.setEncoding("utf8");
var buffer = '';
- var count = 0;
- grep.stdout.on("data", function(data) {
+
+ this.grep.stdout.on("data", function(data) {
if (!Util.empty(data)) {
buffer += data;
if(data.indexOf("\n") !== -1) {
- count += self.parseSearchResult(data, node.path, options, cbwrite);
+ count += self.parseSearchResult(data, node.path, options, prevFile, cbwrite);
buffer = '';
}
}
});
- grep.stderr.on("data", function(data) {
+ this.grep.stderr.on("data", function(data) {
if (!Util.empty(data)) {
buffer += data;
if(data.indexOf("\n") !== -1) {
- count += self.parseSearchResult(data, node.path, options, cbwrite);
+ count += self.parseSearchResult(data, node.path, options, prevFile, cbwrite);
buffer = '';
}
}
});
- grep.on("exit", function(code, signal) {
- if(signal == "SIGTERM") {
- cbwrite('<d:maxreached/>');
- }
-
-
- cbend(null, '</d:multistatus>');
+ this.grep.on("exit", function(code, signal) {
+ cbend(null, '\nResults: {"count": '+ count + ', "filecount":' + filecount + '}');
});
};
- function truncate(s, options) {
- var len = options.maxexcerptlength || 200;
- s = Util.trim(s);
- if (s.length <= len)
- return s;
- var res,
- pos = s.indexOf(options.query);
- if (pos > -1) {
- if ((pos + options.query.length) < len)
- res = s.substr(0, len - 3) + "...";
- else
- res = "..." + s.substr(pos - (Math.floor(len / 2) - options.query.length), len - 6) + "...";
- }
- else {
- res = s.substr(0, len - 3) + "...";
- }
- return res;
- }
-
- this.parseSearchResult = function(res, basePath, options, cbwrite) {
- var lastFile = null, parts, file, lineno;
+ this.parseSearchResult = function(res, basePath, options, prevFile, cbwrite) {
+ var parts, file, lineno, result = "";
var aLines = (typeof res == "string" ? res : "").split(/([\n\r]+)/g);
var i = 0;
var count = 0;
- var filecount = 0;
var l = aLines.length;
-
+
for (; i < l; ++i) {
parts = aLines[i].split(":");
if (parts.length < 3) continue;
- file = parts.shift();
+
+ file = encodeURI(options.uri + Util.rtrim(parts.shift().replace(basePath, "")), "/");
+
lineno = parseInt(parts.shift(), 10);
- if (!lineno)
- continue;
- ++count;
- if (file !== lastFile) {
+ if (!lineno) continue;
+
+ ++count;
+ if (file !== prevFile) {
filecount++;
- if (lastFile)
- cbwrite('</d:response>');
- cbwrite('<d:response path="' + encodeURI(options.uri + Util.rtrim(file.replace(basePath, "")), "/") + '" query="' +
- Util.escapeXml(options.query) + '">');
- lastFile = file;
+ if (prevFile)
+ result += "\n\n";
+ result += file + ":";
+ prevFile = file;
}
- cbwrite('<d:excerpt line="' + lineno + '">' +
- Util.escapeXml(truncate(parts.join(":"), options)) + '</d:excerpt>');
+
+ result += "\n\t" + lineno + ": " + parts.join(":");
}
- if(count > 0)
- cbwrite('</d:response>');
+
+ cbwrite(result);
+
return count;
};
}).call(jsDAV_Codesearch_Plugin.prototype = new jsDAV_ServerPlugin());

0 comments on commit 535243c

Please sign in to comment.