Skip to content

Commit 01b79fc

Browse files
feat(lsp): show jump target picker when multiple definitions are found
1 parent bdefb46 commit 01b79fc

2 files changed

Lines changed: 36 additions & 30 deletions

File tree

src/languageTools/DefaultProviders.js

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ define(function (require, exports, module) {
4141
Strings = require("strings"),
4242
StringUtils = require("utils/StringUtils"),
4343
Metrics = require("utils/Metrics"),
44+
ModalBar = require("widgets/ModalBar").ModalBar,
4445
marked = require("thirdparty/marked.min"),
4546
matcher = new StringMatch.StringMatcher({
4647
preferPrefixMatches: true
@@ -850,41 +851,30 @@ define(function (require, exports, module) {
850851
metricLabel = this.client._metricLabel,
851852
$deferredHints = $.Deferred();
852853

854+
function jumpToLocation(location) {
855+
var startCurPos = { line: location.range.start.line, ch: location.range.start.character };
856+
if (location.uri !== docPathUri) {
857+
CommandManager.execute(Commands.FILE_OPEN, { fullPath: PathConverters.uriToPath(location.uri) })
858+
.done(function () { setJumpPosition(startCurPos); $deferredHints.resolve(); })
859+
.fail(function () { $deferredHints.reject(); });
860+
} else { //definition is in current document
861+
setJumpPosition(startCurPos);
862+
$deferredHints.resolve();
863+
}
864+
}
865+
853866
this.client.gotoDefinition({
854867
filePath: docPath,
855868
cursorPos: pos
856869
}).done(function (msgObj) {
857-
//For Older servers
858-
if (Array.isArray(msgObj)) {
859-
msgObj = msgObj[msgObj.length - 1];
870+
if (Array.isArray(msgObj) && msgObj.length > 1) {
871+
showJumpTargetPicker(msgObj, jumpToLocation, $deferredHints.reject);
872+
return;
860873
}
861-
862-
if (msgObj && msgObj.range) {
863-
var docUri = msgObj.uri,
864-
startCurPos = {};
865-
startCurPos.line = msgObj.range.start.line;
866-
startCurPos.ch = msgObj.range.start.character;
867-
868-
if (docUri !== docPathUri) {
869-
let documentPath = PathConverters.uriToPath(docUri);
870-
CommandManager.execute(Commands.FILE_OPEN, {
871-
fullPath: documentPath
872-
})
873-
.done(function () {
874-
setJumpPosition(startCurPos);
875-
$deferredHints.resolve();
876-
})
877-
.fail(function () {
878-
$deferredHints.reject();
879-
});
880-
} else { //definition is in current document
881-
setJumpPosition(startCurPos);
882-
$deferredHints.resolve();
883-
}
884-
} else {
885-
// No definition at this position (servers answer null/[] - e.g. tsserver while it
886-
// is still loading the project). MUST settle: an unresolved deferred here leaves
887-
// the NAVIGATE_JUMPTO_DEFINITION command promise pending forever.
874+
var location = Array.isArray(msgObj) ? msgObj[0] : msgObj;
875+
if (location && location.range) {
876+
jumpToLocation(location);
877+
} else { // no definition - settle promise
888878
$deferredHints.reject();
889879
}
890880
}).fail(function (err) {
@@ -898,6 +888,20 @@ define(function (require, exports, module) {
898888
return $deferredHints;
899889
};
900890

891+
function showJumpTargetPicker(locations, onPick, onCancel) {
892+
var $bar = $('<div style="padding:4px;"></div>').text(Strings.JUMP_TO_DEFINITION_MULTIPLE_PROMPT);
893+
locations.forEach(function (loc, i) {
894+
$("<button type='button' class='btn'>")
895+
.text(PathConverters.uriToPath(loc.uri).split(/[\\/]/).pop() + ":" + (loc.range.start.line + 1) + ":" + (loc.range.start.character + 1))
896+
.click(function () {
897+
modalBar.close(false, false, ModalBar.CLOSE_API); onPick(loc);
898+
}).appendTo($bar);
899+
});
900+
901+
var modalBar = new ModalBar($bar, true, false);
902+
modalBar.on("close", function (_evt, reason) { if (reason !== ModalBar.CLOSE_API) { onCancel(); } });
903+
}
904+
901905
function LintingProvider() {
902906
this._results = new Map();
903907
this._promiseMap = new Map();

src/nls/root/strings.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,8 @@ define({
15751575
"COLOR_EDITOR_USED_COLOR_TIP_PLURAL": "{0} (Used {1} times)",
15761576
"EDIT": "Edit",
15771577

1578+
"JUMP_TO_DEFINITION_MULTIPLE_PROMPT": "Multiple definitions. Select one:",
1579+
15781580
// extensions/default/JavaScriptCodeHints
15791581
"CMD_JUMPTO_DEFINITION": "Go to Definition",
15801582
"CMD_SHOW_PARAMETER_HINT": "Show Parameter Hint",

0 commit comments

Comments
 (0)