Skip to content

Commit

Permalink
Merge pull request #64 from ConsenSys/feat/findref
Browse files Browse the repository at this point in the history
Feature: Find Reference (naive)
  • Loading branch information
tintinweb authored Sep 29, 2020
2 parents 07d9a73 + a02e11a commit fea5cec
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 20 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## v0.0.30
- new: We've finally implemented support for `Right Click → Find All References` for solidity source files!
- Please note that this currently performs a lexical search of all source-code files containing the word under the cursor (including comments). This may be subject to change to return more specific results in the future.
<br><img width="360" alt="image" src="https://user-images.githubusercontent.com/2865694/94445596-eb132a00-01a7-11eb-9098-32958d58ebd6.gif">

- update: dependencies surya / solidity parser

## v0.0.29
- sort top level contracts list by filename
- fix: VSCode-Error: Proposed API is only available when running out of dev or with the following command line switch... #59
Expand Down
8 changes: 8 additions & 0 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {Commands} = require('./features/commands');
const {SolidityCodeLensProvider} = require('./features/codelens');
const settings = require('./settings');
const {Cockpit} = require('./features/cockpit.js');
const {SolidityReferenceProvider} = require('./features/references');

const {WhatsNewHandler} = require('./features/whatsnew/whatsNew');

Expand Down Expand Up @@ -925,6 +926,13 @@ function onActivate(context) {
)
);
}

context.subscriptions.push(
vscode.languages.registerReferenceProvider(
docSel,
new SolidityReferenceProvider()
)
);
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/features/cockpit.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,17 @@ class FTraceView extends BaseView {
functionName = "<Fallback>";
}

let retj = surya.ftrace(contractName + "::" + functionName, 'all', files, { jsonOutput: true }, true);
let retj = {};
try {
retj = surya.ftrace(contractName + "::" + functionName, 'all', files, { jsonOutput: true }, true);
} catch (e) {
//console.error(e);
retj = {"💣💥 - sorry! we've encountered an unrecoverable error :/ Please file an issue in our github repository and provide (mention codebase). thanks!":null};
}
this.dataProvider.documentUri = documentUri;
this.dataProvider.data = retj;
this.refresh();

}
}

Expand Down
12 changes: 8 additions & 4 deletions src/features/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,14 @@ class Commands{
} else if (args[1] === ""){
args[1] = "<Fallback>";
}

ret = surya.ftrace(args[0] + "::" + args[1], args[2] || 'all', files, {}, true);
vscode.workspace.openTextDocument({content: ret, language: "markdown"})
.then(doc => vscode.window.showTextDocument(doc, vscode.ViewColumn.Beside));
try {
ret = surya.ftrace(args[0] + "::" + args[1], args[2] || 'all', files, {}, true);
vscode.workspace.openTextDocument({content: ret, language: "markdown"})
.then(doc => vscode.window.showTextDocument(doc, vscode.ViewColumn.Beside));
} catch (e) {
console.error(e);
}

break;
case "mdreport":
ret = surya.mdreport(files);
Expand Down
104 changes: 104 additions & 0 deletions src/features/references.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* @author github.com/tintinweb
* @license MIT
*
*
* */
//API REF: https://code.visualstudio.com/api/references/vscode-api#ReferenceProvider

const vscode = require('vscode');
const settings = require('../settings.js');
const fs = require('fs').promises;

function getIndicesOf(searchStr, str, caseSensitive) {
var searchStrLen = searchStr.length;
if (searchStrLen == 0) {
return [];
}
var startIndex = 0, index, indices = [];
if (!caseSensitive) {
str = str.toLowerCase();
searchStr = searchStr.toLowerCase();
}
while ((index = str.indexOf(searchStr, startIndex)) > -1) {
indices.push(index);
startIndex = index + searchStrLen;
}
return indices;
}

function getIndicesOfRex(searchRex, str) {
var regEx = new RegExp("\\b" + searchRex + "\\b", "g");
let match;
let results = [];
while (match = regEx.exec(str)) {
results.push({ index: match.index, len: match[0].trim().length });
}
return results;
}

function indicesToVscodeRange(uri, indices, data, cb) {
const lineIndices = getIndicesOf("\n", data, true);
indices.forEach(i => {
//find line
let lineIndex;

let lineNumber = lineIndices.findIndex(li => i.index < li);
if (lineNumber <= 0) {
lineNumber = 0; //maybe firstline
lineIndex = 0; //firstline.
} else {
lineNumber--; // its the previous line
lineIndex = lineIndices[lineNumber];
}

if (i.index > 0) {
i.index--; //vscode columns start at 1
}

let pos = new vscode.Range(
new vscode.Position(lineNumber + 1, i.index - lineIndex),
new vscode.Position(lineNumber + 1, i.index - lineIndex + i.len)
);
cb(vscode.Location(uri, pos));
});
}

class SolidityReferenceProvider {

provideReferences(document, position, context, token) {
console.log("providing references ...");
return new Promise(async (resolve, reject) => {
var locations = [];

const range = document.getWordRangeAtPosition(position);
if (!range || range.length <= 0) {
return reject();
}
const word = document.getText(range);

/*
DANGER: UGLY HACK AHEAD. @todo: replace this with a clean solution. this is just an initial hack to make "find references" work.
*/
await vscode.workspace.findFiles("**/*.sol", settings.DEFAULT_FINDFILES_EXCLUDES, 500)
.then(async uris => {

await Promise.all(uris.map(async (uri) => {
let data = await fs.readFile(uri.fsPath, 'utf8');
if (!data) {
return;
}
const indicesRex = getIndicesOfRex(word, data);
indicesToVscodeRange(uri, indicesRex, data, (elem) => locations.push(elem));
}));
return resolve(locations);
});

});
}
}


module.exports = {
SolidityReferenceProvider: SolidityReferenceProvider
};
22 changes: 7 additions & 15 deletions src/features/whatsnew/whatsNew.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,12 @@ Thanks for using **Solidity Visual Developer** 🤜🤛
The complete changelog can be found [here](https://github.com/ConsenSys/vscode-solidity-auditor/blob/master/CHANGELOG.md).
#### v0.0.29
- sort top level contracts list by filename
- fix: VSCode-Error: Proposed API is only available when running out of dev or with the following command line switch... #59
#### v0.0.28
- new: integration with [tintinweb.vscode-ethover](https://marketplace.visualstudio.com/items?itemName=tintinweb.vscode-ethover) (uninstall to disable)
- ethereum address hover
- open address in etherscan, fetch bytecode, verified contract
- disassemble or decompile bytecode
- registers \`.evmtrace\` and \`.evm\` language handlers to decorate disassemblies or bytecode
- customizations/ApiKey: see settings
<img width="360" alt="image" src="https://user-images.githubusercontent.com/2865694/86650152-bd707780-bfe2-11ea-819d-a9e3dacb2034.gif">
- update: \`surya\` to \`0.4.1-dev.2\`
#### v0.0.30
- new: We've finally implemented support for \`Right Click → Find All References\` for solidity source files!
- Please note that this currently performs a lexical search of all source-code files containing the word under the cursor (including comments). This may be subject to change to return more specific results in the future.
<br><img width="360" alt="image" src="https://user-images.githubusercontent.com/2865694/94445596-eb132a00-01a7-11eb-9098-32958d58ebd6.gif">
- update: dependencies surya / solidity parser
<sub>
Note: This notification is only shown once per release. Disable future notification? \`settings → solidity-va.whatsNew.disabled : true\`
Expand Down Expand Up @@ -97,7 +89,7 @@ class WhatsNewHandler {
let webview = new InteractiveWebviewGenerator(context, "whats_new");
webview.revealOrCreatePreview(vscode.ViewColumn.Beside, doc)
.then(webpanel => {
webpanel.getPanel().postMessage({
webpanel.getPanel().webview.postMessage({
command:"render",
value:{
markdown:MESSAGE,
Expand Down

0 comments on commit fea5cec

Please sign in to comment.