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

Commit 2245e01

Browse files
author
Narciso Jaramillo
committed
Merge pull request #3847 from jeffkenton/master
Use Tern jump-to-definition search for quickeditt
2 parents 03ecacc + 59587ec commit 2245e01

6 files changed

Lines changed: 95 additions & 34 deletions

File tree

src/editor/EditorManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ define(function (require, exports, module) {
214214
function registerInlineEditProvider(provider) {
215215
_inlineEditProviders.push(provider);
216216
}
217-
217+
218218
/**
219219
* Registers a new inline docs provider. When Quick Docs is invoked each registered provider is
220220
* asked if it wants to provide inline docs given the current editor and cursor location.

src/editor/MultiRangeInlineEditor.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ define(function (require, exports, module) {
126126
this.$editorsDiv.on("mousewheel.MultiRangeInlineEditor", function (e) {
127127
e.stopPropagation();
128128
});
129-
129+
130130
// Outer container for border-left and scrolling
131131
this.$relatedContainer = $(window.document.createElement("div")).addClass("related-container");
132132

133133
// List "selection" highlight
134134
this.$selectedMarker = $(window.document.createElement("div")).appendTo(this.$relatedContainer).addClass("selection");
135-
135+
136136
// Inner container
137137
this.$related = $(window.document.createElement("div")).appendTo(this.$relatedContainer).addClass("related");
138138

@@ -165,8 +165,10 @@ define(function (require, exports, module) {
165165
// select the first range
166166
self.setSelectedIndex(0);
167167

168-
// attach to main container
169-
this.$htmlContent.append(this.$relatedContainer).append(this.$editorsDiv);
168+
if (this._ranges.length > 1) { // attach to main container
169+
this.$htmlContent.append(this.$relatedContainer);
170+
}
171+
this.$htmlContent.append(this.$editorsDiv);
170172

171173
// Listen for clicks directly on us, so we can set focus back to the editor
172174
var clickHandler = this._onClick.bind(this);

src/extensions/default/JavaScriptCodeHints/ScopeManager.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,14 @@ define(function (require, exports, module) {
403403
}
404404
}
405405

406+
/**
407+
* @param {string} file a relative path
408+
* @return {string} returns the path we resolved when we tried to parse the file, or undefined
409+
*/
410+
function getResolvedPath(file) {
411+
return resolvedFiles[file];
412+
}
413+
406414
/**
407415
* Get a Promise for the definition from TernJS, for the file & offset passed in.
408416
* @return {jQuery.Promise} - a promise that will resolve to definition when
@@ -453,9 +461,8 @@ define(function (require, exports, module) {
453461

454462
var $deferredJump = getPendingRequest(file, offset, MessageIds.TERN_JUMPTODEF_MSG);
455463

456-
// pendingTernRequests[file] = null;
457-
458464
if ($deferredJump) {
465+
response.fullPath = getResolvedPath(response.resultFile);
459466
$deferredJump.resolveWith(null, [response]);
460467
}
461468
}
@@ -624,14 +631,6 @@ define(function (require, exports, module) {
624631
}
625632
}
626633

627-
/**
628-
* @param {string} file a relative path
629-
* @return {string} returns the path we resolved when we tried to parse the file, or undefined
630-
*/
631-
function getResolvedPath(file) {
632-
return resolvedFiles[file];
633-
}
634-
635634
/**
636635
* Handle a request from the worker for text of a file
637636
*

src/extensions/default/JavaScriptCodeHints/main.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,22 @@ define(function (require, exports, module) {
591591
}
592592
}
593593

594+
/*
595+
* Helper for QuickEdit jump-to-definition request.
596+
*/
597+
function quickEditHelper() {
598+
var offset = session.getOffset(),
599+
response = ScopeManager.requestJumptoDef(session, session.editor.document, offset);
600+
601+
return response;
602+
}
603+
594604
// Register command handler
595605
CommandManager.register(Strings.CMD_JUMPTO_DEFINITION, JUMPTO_DEFINITION, handleJumpToDefinition);
596606

607+
// Register quickEditHelper.
608+
brackets._jsCodeHintsHelper = quickEditHelper;
609+
597610
// Add the menu item
598611
var menu = Menus.getMenu(Menus.AppMenuBar.NAVIGATE_MENU);
599612
if (menu) {

src/extensions/default/JavaScriptCodeHints/tern-worker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ importScripts("thirdparty/requirejs/require.js");
143143

144144
var request = buildRequest(dir, file, "definition", offset, text);
145145
request.query.lineCharPositions = true;
146+
// request.query.typeOnly = true; // FIXME: tern doesn't work exactly right yet.
146147
ternServer.request(request, function (error, data) {
147148
if (error) {
148149
_log("Error returned from Tern 'definition' request: " + error);

src/extensions/default/JavaScriptQuickEdit/main.js

Lines changed: 65 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ define(function (require, exports, module) {
3232
var MultiRangeInlineEditor = brackets.getModule("editor/MultiRangeInlineEditor").MultiRangeInlineEditor,
3333
FileIndexManager = brackets.getModule("project/FileIndexManager"),
3434
EditorManager = brackets.getModule("editor/EditorManager"),
35+
DocumentManager = brackets.getModule("document/DocumentManager"),
3536
JSUtils = brackets.getModule("language/JSUtils"),
3637
PerfUtils = brackets.getModule("utils/PerfUtils");
3738

@@ -103,26 +104,71 @@ define(function (require, exports, module) {
103104
* or null if we're not going to provide anything.
104105
*/
105106
function _createInlineEditor(hostEditor, functionName) {
107+
// Use Tern jump-to-definition helper, if it's available, to find InlineEditor target.
108+
var helper = brackets._jsCodeHintsHelper;
109+
if (helper === null) {
110+
return null;
111+
}
112+
106113
var result = new $.Deferred();
107114
PerfUtils.markStart(PerfUtils.JAVASCRIPT_INLINE_CREATE);
108-
109-
_findInProject(functionName).done(function (functions) {
110-
if (functions && functions.length > 0) {
111-
var jsInlineEditor = new MultiRangeInlineEditor(functions);
112-
jsInlineEditor.load(hostEditor);
113-
114-
PerfUtils.addMeasurement(PerfUtils.JAVASCRIPT_INLINE_CREATE);
115-
result.resolve(jsInlineEditor);
116-
} else {
117-
// No matching functions were found
118-
PerfUtils.addMeasurement(PerfUtils.JAVASCRIPT_INLINE_CREATE);
115+
116+
var response = helper();
117+
if (response.hasOwnProperty("promise")) {
118+
response.promise.done(function (jumpResp) {
119+
var resolvedPath = jumpResp.fullPath;
120+
if (resolvedPath) {
121+
122+
// Tern doesn't always return entire function extent.
123+
// Use QuickEdit search now that we know which file to look at.
124+
var fileInfos = [];
125+
fileInfos.push({name: jumpResp.resultFile, fullPath: resolvedPath});
126+
JSUtils.findMatchingFunctions(functionName, fileInfos)
127+
.done(function (functions) {
128+
if (functions && functions.length > 0) {
129+
var jsInlineEditor = new MultiRangeInlineEditor(functions);
130+
jsInlineEditor.load(hostEditor);
131+
132+
PerfUtils.addMeasurement(PerfUtils.JAVASCRIPT_INLINE_CREATE);
133+
result.resolve(jsInlineEditor);
134+
} else {
135+
// No matching functions were found
136+
PerfUtils.addMeasurement(PerfUtils.JAVASCRIPT_INLINE_CREATE);
137+
result.reject();
138+
}
139+
})
140+
.fail(function () {
141+
PerfUtils.addMeasurement(PerfUtils.JAVASCRIPT_INLINE_CREATE);
142+
result.reject();
143+
});
144+
145+
} else { // no result from Tern. Fall back to _findInProject().
146+
147+
_findInProject(functionName).done(function (functions) {
148+
if (functions && functions.length > 0) {
149+
var jsInlineEditor = new MultiRangeInlineEditor(functions);
150+
jsInlineEditor.load(hostEditor);
151+
152+
PerfUtils.addMeasurement(PerfUtils.JAVASCRIPT_INLINE_CREATE);
153+
result.resolve(jsInlineEditor);
154+
} else {
155+
// No matching functions were found
156+
PerfUtils.addMeasurement(PerfUtils.JAVASCRIPT_INLINE_CREATE);
157+
result.reject();
158+
}
159+
}).fail(function () {
160+
PerfUtils.finalizeMeasurement(PerfUtils.JAVASCRIPT_INLINE_CREATE);
161+
result.reject();
162+
});
163+
}
164+
165+
}).fail(function () {
166+
PerfUtils.finalizeMeasurement(PerfUtils.JAVASCRIPT_INLINE_CREATE);
119167
result.reject();
120-
}
121-
}).fail(function () {
122-
PerfUtils.finalizeMeasurement(PerfUtils.JAVASCRIPT_INLINE_CREATE);
123-
result.reject();
124-
});
125-
168+
});
169+
170+
}
171+
126172
return result.promise();
127173
}
128174

@@ -147,14 +193,14 @@ define(function (require, exports, module) {
147193
if (sel.start.line !== sel.end.line) {
148194
return null;
149195
}
150-
196+
151197
// Always use the selection start for determining the function name. The pos
152198
// parameter is usually the selection end.
153199
var functionName = _getFunctionName(hostEditor, sel.start);
154200
if (!functionName) {
155201
return null;
156202
}
157-
203+
158204
return _createInlineEditor(hostEditor, functionName);
159205
}
160206

0 commit comments

Comments
 (0)