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

LSP Find References Feature #14693

Merged
merged 14 commits into from
Apr 15, 2019
1 change: 1 addition & 0 deletions src/brackets.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ define(function (require, exports, module) {
//load language features
require("features/ParameterHintsManager");
require("features/JumpToDefManager");
require("features/FindReferencesManager");

// Load modules that self-register and just need to get included in the main project
require("command/DefaultMenus");
Expand Down
8 changes: 6 additions & 2 deletions src/extensions/default/PhpTooling/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ define(function (require, exports, module) {
CodeHintManager = brackets.getModule("editor/CodeHintManager"),
ParameterHintManager = brackets.getModule("features/ParameterHintsManager"),
JumpToDefManager = brackets.getModule("features/JumpToDefManager"),
FindReferencesManager = brackets.getModule("features/FindReferencesManager"),
CodeInspection = brackets.getModule("language/CodeInspection"),
DefaultProviders = brackets.getModule("languageTools/DefaultProviders"),
CodeHintsProvider = require("CodeHintsProvider").CodeHintsProvider,
Expand Down Expand Up @@ -61,7 +62,8 @@ define(function (require, exports, module) {
chProvider,
phProvider,
lProvider,
jdProvider;
jdProvider,
refProvider;

PreferencesManager.definePreference("php", "object", phpConfig, {
description: Strings.DESCRIPTION_PHP_TOOLING_CONFIGURATION
Expand Down Expand Up @@ -101,11 +103,13 @@ define(function (require, exports, module) {
chProvider = new CodeHintsProvider(_client),
phProvider = new DefaultProviders.ParameterHintsProvider(_client),
lProvider = new DefaultProviders.LintingProvider(_client),
jdProvider = new DefaultProviders.JumpToDefProvider(_client);
jdProvider = new DefaultProviders.JumpToDefProvider(_client),
refProvider = new DefaultProviders.ReferencesProvider(_client);

JumpToDefManager.registerJumpToDefProvider(jdProvider, ["php"], 0);
CodeHintManager.registerHintProvider(chProvider, ["php"], 0);
ParameterHintManager.registerHintProvider(phProvider, ["php"], 0);
FindReferencesManager.registerFindReferencesProvider(refProvider, ["php"], 0);
CodeInspection.register(["php"], {
name: "",
scanFileAsync: lProvider.getInspectionResultsAsync.bind(lProvider)
Expand Down
151 changes: 151 additions & 0 deletions src/features/FindReferencesManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright (c) 2013 - present Adobe Systems Incorporated. All rights reserved.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the banner. Copyright (c) 2019 - present Adobe. All rights reserved.

*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

define(function (require, exports, module) {
"use strict";

var AppInit = require("utils/AppInit"),
CommandManager = require("command/CommandManager"),
EditorManager = require("editor/EditorManager"),
Menus = require("command/Menus"),
ProviderRegistrationHandler = require("features/PriorityBasedRegistration").RegistrationHandler,
SearchResultsView = require("search/SearchResultsView").SearchResultsView,
SearchModel = require("search/SearchModel").SearchModel,
Strings = require("strings");

var _providerRegistrationHandler = new ProviderRegistrationHandler(),
registerFindReferencesProvider = _providerRegistrationHandler.registerProvider.bind(
_providerRegistrationHandler
),
removeFindReferencesProvider = _providerRegistrationHandler.removeProvider.bind(_providerRegistrationHandler);

var SHOW_FIND_REFERENCES_CMD_ID = "showrReferences",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Typo "showReferences"

KeyboardPrefs = JSON.parse(require("text!features/keyboard.json"));

var searchModel = new SearchModel(),
_resultsView;

function _getReferences(provider, hostEditor, pos) {
var result = new $.Deferred();

provider.getReferences(hostEditor, pos)
.done(function (rcvdObj) {

searchModel.results = rcvdObj.results;
searchModel.numFiles = rcvdObj.numFiles;
searchModel.numMatches = rcvdObj.numMatches;
searchModel.allResultsAvailable = true;
searchModel.setQueryInfo({query: rcvdObj.queryInfo, caseSensitive: true, isRegExp: false});
//searchModel.fireChanged();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented code.

result.resolve();
}).fail(function (){
result.reject();
});

return result.promise();

}

function _openReferencesPanel() {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty line.

var editor = EditorManager.getActiveEditor(),
pos = editor ? editor.getCursorPos() : null,
referencespromise,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use camel case.

result = new $.Deferred(),
errorMsg,
referencesProvider,
defaultErrorMsg = "";

var language = editor.getLanguageForSelection(),
enabledProviders = _providerRegistrationHandler.getProvidersForLanguageId(language.getId());

enabledProviders.some(function (item, index) {
if (item.provider.hasReferences(editor)) {
referencesProvider = item.provider;
return true;
}
});

referencespromise = _getReferences(referencesProvider, editor, pos);

// Use default error message if none other provided
errorMsg = errorMsg || defaultErrorMsg;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are errorMsg and defaultErrorMsg being set?


// If one of them will provide a widget, show it inline once ready
if (referencespromise) {
referencespromise.done(function () {
_resultsView.open();
}).fail(function () {
editor.displayErrorMessageAtCursor(errorMsg);
result.reject();
});
} else {
editor.displayErrorMessageAtCursor(errorMsg);
result.reject();
}

return result.promise();
}

/**
* @private
* Clears any previous search information, removing update listeners and clearing the model.
*/
function _clearSearch() {
searchModel.clear();
}

AppInit.appReady(function () {

var model = searchModel;
_resultsView = new SearchResultsView(
model,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Directly use searchModel here.

"reference-in-files-results",
"reference-in-files.results",
"reference"
);
_resultsView
.on("close", function () {
_clearSearch();
})
.on("getNextPage", function () {
if (searchModel.hasResults()) {
_resultsView.showNextPage();
}
})
.on("getLastPage", function () {
if (searchModel.hasResults()) {
_resultsView.showLastPage();
}
});

CommandManager.register(Strings.FIND_ALL_REFERENCES, SHOW_FIND_REFERENCES_CMD_ID, _openReferencesPanel);
Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU).addMenuItem(
SHOW_FIND_REFERENCES_CMD_ID,
KeyboardPrefs.findAllReferences
);
});

exports.registerFindReferencesProvider = registerFindReferencesProvider;
exports.removeFindReferencesProvider = removeFindReferencesProvider;
});
13 changes: 13 additions & 0 deletions src/features/keyboard.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"findAllReferences": [
{
"key": "Ctrl-Shift-R", "platform": "mac"
},
{
"key": "Ctrl-Shift-R", "platform": "win"
},
{
"key": "Ctrl-Shift-R", "platform": "linux"
}
]
}
66 changes: 66 additions & 0 deletions src/languageTools/DefaultProviders.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ define(function (require, exports, module) {
var _ = brackets.getModule("thirdparty/lodash");

var EditorManager = require('editor/EditorManager'),
DocumentManager = require('document/DocumentManager'),
ExtensionUtils = require("utils/ExtensionUtils"),
CommandManager = require("command/CommandManager"),
Commands = require("command/Commands"),
Expand Down Expand Up @@ -402,8 +403,73 @@ define(function (require, exports, module) {
return this._results.get(filePath);
};

function ReferencesProvider(client) {
this.client = client;
}

ReferencesProvider.prototype.hasReferences = function() {
return true;
};

ReferencesProvider.prototype.getReferences = function() {
var editor = EditorManager.getActiveEditor(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why aren't you using the hostEditor and pos that is being passed by manager?

pos = editor.getCursorPos(),
docPath = editor.document.file._path,
result = $.Deferred();

if (this.client) {
this.client.findReferences({
filePath: docPath,
cursorPos: pos
}).done(function(msgObj){
if(msgObj ) {
var referenceModel = {};
referenceModel.results = {};
referenceModel.numFiles = 0;
var fulfilled = 0,
queryInfo = "";
msgObj.forEach((element, i) => {
var filePath = PathConverters.uriToPath(element.uri);
DocumentManager.getDocumentForPath(filePath)
.done(function(doc) {
var startRange = {line: element.range.start.line, ch: element.range.start.character};
var endRange = {line: element.range.end.line, ch: element.range.end.character};
var match = {
start: startRange,
end: endRange,
line: doc.getLine(element.range.start.line)
};
if(!referenceModel.results[filePath]) {
referenceModel.numFiles = referenceModel.numFiles + 1;
referenceModel.results[filePath] = {"matches": []};
}
if(!queryInfo) {
referenceModel.queryInfo = doc.getRange(startRange, endRange);
}
referenceModel.results[filePath]["matches"].push(match);
}).always(function() {
fulfilled++;
if(fulfilled === msgObj.length) {
referenceModel.numMatches = msgObj.length;
referenceModel.allResultsAvailable = true;
result.resolve(referenceModel);
}
});
});
} else {
result.reject();
}
}).fail(function(){
result.reject();
});
return result.promise();
}
return result.reject();
};

exports.CodeHintsProvider = CodeHintsProvider;
exports.ParameterHintsProvider = ParameterHintsProvider;
exports.JumpToDefProvider = JumpToDefProvider;
exports.LintingProvider = LintingProvider;
exports.ReferencesProvider = ReferencesProvider;
});
5 changes: 4 additions & 1 deletion src/nls/root/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -888,5 +888,8 @@ define({
"OPEN_PREFERENNCES" : "Open Preferences",

//Strings for LanguageTools Preferences
LANGUAGE_TOOLS_PREFERENCES : "Preferences for Language Tools"
"LANGUAGE_TOOLS_PREFERENCES" : "Preferences for Language Tools",
"FIND_ALL_REFERENCES" : "Find All References",
"FIND_IN_FILES_REFERENCES" : "references",
"FIND_IN_FILES_REFERENCE" : "reference"
});
14 changes: 12 additions & 2 deletions src/search/SearchResultsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ define(function (require, exports, module) {
* @param {SearchModel} model The model that this view is showing.
* @param {string} panelID The CSS ID to use for the panel.
* @param {string} panelName The name to use for the panel, as passed to WorkspaceManager.createBottomPanel().
* @param {string} type type to indentify if it is reference search or string match serach
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: identify

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need to fix this.

*/
function SearchResultsView(model, panelID, panelName) {
function SearchResultsView(model, panelID, panelName, type) {
var panelHtml = Mustache.render(searchPanelTemplate, {panelID: panelID});

this._panel = WorkspaceManager.createBottomPanel(panelName, $(panelHtml), 100);
this._$summary = this._panel.$panel.find(".title");
this._$table = this._panel.$panel.find(".table-container");
this._model = model;
this._searchResultsType = type;
}
EventDispatcher.makeEventDispatcher(SearchResultsView.prototype);

Expand Down Expand Up @@ -116,6 +118,9 @@ define(function (require, exports, module) {
/** @type {number} The ID we use for timeouts when handling model changes. */
SearchResultsView.prototype._timeoutID = null;

/** @type {string} The ID we use to check id it is refrence serach or match search */
SearchResultsView.prototype._searchResultsType = null;

/**
* @private
* Handles when model changes. Updates the view, buffering changes if necessary so as not to churn too much.
Expand Down Expand Up @@ -344,9 +349,14 @@ define(function (require, exports, module) {
SearchResultsView.prototype._showSummary = function () {
var count = this._model.countFilesMatches(),
lastIndex = this._getLastIndex(count.matches),
typeStr = (count.matches > 1) ? Strings.FIND_IN_FILES_MATCHES : Strings.FIND_IN_FILES_MATCH,
filesStr,
summary;

if(this._searchResultsType) {
typeStr = (count.matches > 1) ? Strings.FIND_IN_FILES_REFERENCES : Strings.FIND_IN_FILES_REFERENCE;
}

filesStr = StringUtils.format(
Strings.FIND_NUM_FILES,
count.files,
Expand All @@ -358,7 +368,7 @@ define(function (require, exports, module) {
Strings.FIND_TITLE_SUMMARY,
this._model.exceedsMaximum ? Strings.FIND_IN_FILES_MORE_THAN : "",
String(count.matches),
(count.matches > 1) ? Strings.FIND_IN_FILES_MATCHES : Strings.FIND_IN_FILES_MATCH,
typeStr,
filesStr
);

Expand Down