Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Fixes "JS Quick Open doesn't recognize functions with multiline arguments" #1085

Merged
merged 13 commits into from
Jun 22, 2012
Merged
2 changes: 2 additions & 0 deletions src/brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ define(function (require, exports, module) {
DocumentManager = require("document/DocumentManager"),
EditorManager = require("editor/EditorManager"),
CSSInlineEditor = require("editor/CSSInlineEditor"),
JSUtils = require("language/JSUtils"),
WorkingSetView = require("project/WorkingSetView"),
DocumentCommandHandlers = require("document/DocumentCommandHandlers"),
FileViewController = require("project/FileViewController"),
Expand Down Expand Up @@ -200,6 +201,7 @@ define(function (require, exports, module) {
WorkingSetView : WorkingSetView,
JSLintUtils : JSLintUtils,
PerfUtils : PerfUtils,
JSUtils : JSUtils,
CommandManager : require("command/CommandManager"),
FileSyncManager : FileSyncManager,
FileIndexManager : FileIndexManager,
Expand Down
4 changes: 1 addition & 3 deletions src/extensions/default/JavaScriptQuickEdit/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ define(function (require, exports, module) {
var MultiRangeInlineEditor = brackets.getModule("editor/MultiRangeInlineEditor").MultiRangeInlineEditor,
FileIndexManager = brackets.getModule("project/FileIndexManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
JSUtils = brackets.getModule("language/JSUtils"),
PerfUtils = brackets.getModule("utils/PerfUtils");

// Local modules
var JSUtils = require("JSUtils");

/**
* Return the token string that is at the specified position.
*
Expand Down
487 changes: 10 additions & 477 deletions src/extensions/default/JavaScriptQuickEdit/unittests.js

Large diffs are not rendered by default.

70 changes: 14 additions & 56 deletions src/extensions/default/QuickOpenJavaScript/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ define(function (require, exports, module) {

var EditorManager = brackets.getModule("editor/EditorManager"),
QuickOpen = brackets.getModule("search/QuickOpen"),
JSUtils = brackets.getModule("language/JSUtils"),
DocumentManager = brackets.getModule("document/DocumentManager");


Expand Down Expand Up @@ -86,70 +87,27 @@ define(function (require, exports, module) {
return fileLocation;
}

/**
* Populates the functionList array
*/

function createFunctionList() {
var doc = DocumentManager.getCurrentDocument();
if (!doc) {
return;
}

if (!functionList) {
functionList = [];
var docText = doc.getText();

// TODO: use a shared JS language intelligence module
// TODO: this doesn't handle functions with params that spread across lines
//
// Note, the global switch on the regex expressions below is NOT used because this code assumes
// one function per line. exec() is not called repeatedly for each line.
var regexA = new RegExp(/(function\b)([^)]+)\b\([^)]*\)/i); // recognizes the form: function functionName()
var regexB = new RegExp(/(\w+)\s*=\s*function\s*(\([^)]*\))/i); // recognizes the form: functionName = function()
var regexC = new RegExp(/((\w+)\s*:\s*function\s*\([^)]*\))/i); // recognizes the form: functionName: function()
var infoA, infoB, infoC, i, line;
var funcName, chFrom, chTo;

var lines = docText.split("\n");

for (i = 0; i < lines.length; i++) {
line = lines[i];

infoA = regexA.exec(line);
if (infoA) {
funcName = $.trim(infoA[0]);
chFrom = line.indexOf(funcName);
chTo = chFrom + funcName.length;
functionList.push(new FileLocation(null, i, chFrom, chTo, funcName));
}

infoB = regexB.exec(line);
if (infoB) {
var pattern = $.trim(infoB[1]);
var params = infoB[2];
var dotIndex = pattern.lastIndexOf(".");
if (dotIndex !== -1) {
funcName = pattern.slice(dotIndex + 1, pattern.length);
} else {
funcName = pattern;
}

chFrom = line.indexOf(funcName);
chTo = chFrom + funcName.length;
functionList.push(new FileLocation(null, i, chFrom, chTo, funcName + params));
}

infoC = regexC.exec(line);
if (infoC) {
funcName = $.trim(infoC[1]);
chFrom = line.indexOf(funcName);
chTo = chFrom + funcName.length;
functionList.push(new FileLocation(null, i, chFrom, chTo, funcName));
}
}
}
functionList = [];
var docText = doc.getText();
var lines = docText.split("\n");
var functions = JSUtils.findAllMatchingFunctionsInText(docText, "*");
functions.forEach(function (funcEntry) {
var chFrom = lines[funcEntry.lineStart].indexOf(funcEntry.name);
var chTo = chFrom + funcEntry.name.length;
functionList.push(new FileLocation(null, funcEntry.lineStart, chFrom, chTo, funcEntry.name));
});

}



/**
* @param {string} query what the user is searching for
* @returns {Array.<string>} sorted and filtered results that match the query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ define(function (require, exports, module) {
'use strict';

// Load brackets modules
var Async = brackets.getModule("utils/Async"),
DocumentManager = brackets.getModule("document/DocumentManager"),
ChangedDocumentTracker = brackets.getModule("document/ChangedDocumentTracker"),
NativeFileSystem = brackets.getModule("file/NativeFileSystem").NativeFileSystem,
PerfUtils = brackets.getModule("utils/PerfUtils"),
StringUtils = brackets.getModule("utils/StringUtils");
var Async = require("utils/Async"),
DocumentManager = require("document/DocumentManager"),
ChangedDocumentTracker = require("document/ChangedDocumentTracker"),
NativeFileSystem = require("file/NativeFileSystem").NativeFileSystem,
PerfUtils = require("utils/PerfUtils"),
StringUtils = require("utils/StringUtils");

/**
* Tracks dirty documents between invocations of findMatchingFunctions.
Expand Down Expand Up @@ -396,7 +396,7 @@ define(function (require, exports, module) {
* @return {Array.<{offset:number, functionName:string}>}
* Array of objects containing the start offset for each matched function name.
*/
function _findAllMatchingFunctionsInText(text, functionName) {
function findAllMatchingFunctionsInText(text, functionName) {
var allFunctions = _findAllFunctionsInText(text);
var result = [];
var lines = text.split("\n");
Expand All @@ -421,7 +421,7 @@ define(function (require, exports, module) {
PerfUtils.createPerfMeasurement("JSUTILS_REGEXP", "RegExp search for all functions");
PerfUtils.createPerfMeasurement("JSUTILS_END_OFFSET", "Find end offset for a single matched function");

exports._findAllMatchingFunctionsInText = _findAllMatchingFunctionsInText; // For testing only
exports.findAllMatchingFunctionsInText = findAllMatchingFunctionsInText;
exports._getFunctionEndOffset = _getFunctionEndOffset; // For testing only
exports.findMatchingFunctions = findMatchingFunctions;
});
1 change: 1 addition & 0 deletions test/UnitTestSuite.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ define(function (require, exports, module) {
require("spec/CodeHintUtils-test");
require("spec/CommandManager-test");
require("spec/CSSUtils-test");
require("spec/JSUtils-test");
require("spec/Document-test");
require("spec/DocumentCommandHandlers-test");
require("spec/Editor-test");
Expand Down
2 changes: 1 addition & 1 deletion test/spec/CSSUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ define(function (require, exports, module) {
ProjectManager,
brackets;

beforeEach(function () {
beforeEach(function () {
SpecRunnerUtils.createTestWindowAndRun(this, function (testWindow) {
// Load module instances from brackets.test
brackets = testWindow.brackets;
Expand Down
31 changes: 31 additions & 0 deletions test/spec/JSUtils-test-files/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function edit1() {
// this function starts at line 0, char 0
}

/*
* simple functions
*/
var edit2 = function () {

};

edit3: function () {

}

/*
* parameterized functions
*/
function edit4(p1) {
}

var edit5 = function (p1, p2) {

};

edit6: function (longParameterName1,
longParameterName2,
longParameterName3) {

}

22 changes: 22 additions & 0 deletions test/spec/JSUtils-test-files/test1inline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{0}}function func1() {
// comment
}

/*
* comment
*/
{{1}}var func2 = function() {

{{2}} func3: function() {
/* comment */
}

}

function func4() {
return func1();
}

function func5() {
return true;
}
7 changes: 7 additions & 0 deletions test/spec/JSUtils-test-files/test1main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
/*global func1: false, func2: false, func3: false */

var a = {{0}}func1();
var b = fun{{1}}c2();
var c = func3{{2}}();
Loading