Skip to content

Commit 7266085

Browse files
authored
Merge pull request #12 from PraneshASP/staging
♻️ Release `v1.0.0`: `staging` -> `main`
2 parents 68fc639 + 57de7d6 commit 7266085

11 files changed

Lines changed: 237 additions & 22 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.vscode-test/
33
*.vsix
4+
yarn-error.log

CHANGELOG.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ All notable changes to the "vscode-solidity-inspector" extension will be documen
44

55
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
66

7-
## v.0.0.3
8-
- Add support for projects with `foundry.toml` and `hardhat.config.ts` config files
9-
- Add a new feature: `storage-layout` inspector
10-
- Refactoring of the codebase
7+
## [v.1.0.0](https://github.com/PraneshASP/vscode-solidity-inspector/releases/tag/v1.0.0)
8+
- Added a feature to flatten smart contracts.
9+
- Introduced a feature to highlight unused import statements inline to easily detect them.
10+
11+
## [v.0.0.3](https://github.com/PraneshASP/vscode-solidity-inspector/releases/tag/v0.0.3)
12+
- Added support for projects with `foundry.toml` and `hardhat.config.ts` config files
13+
- Introduced a new feature: `storage-layout` inspector
14+
- Extensive refactoring of the codebase
1115

12-
## v.0.01 - v.0.0.2
13-
- Initial release
16+
## [v.0.01 - v.0.0.2](https://github.com/PraneshASP/vscode-solidity-inspector/releases/tag/v0.0.2)
17+
- Initial release of the extension with basic functionalities
18+
19+

README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<img align="right" width="150" height="150" top="100" src="./assets/icon.jpg">
1+
<img align="right" width="150" height="150" top="100" src="./assets/m-icon.jpg">
22

33
# VSCode Solidity Inspector • [![license](https://img.shields.io/badge/MIT-brown.svg?label=license)](https://github.com/PraneshASP/vscode-solidity-inspector/blob/main/LICENSE)
44

@@ -19,6 +19,8 @@ TL;DR, this is my first VSCode extension, so feel free to provide feedback as I
1919
- Forge ir-optimized o/p generation for the current file (or) selected file(s).
2020
- Forge asm-optimized o/p generation for the current file (or) selected file(s).
2121
- Forge storage-layout o/p generation for the current file (or) selected file(s).
22+
- Flatten current file (or) selected file(s).
23+
- Inline highlighting in code editor for unused imports.
2224

2325
---
2426

@@ -33,31 +35,45 @@ The following will need to be installed in order to use this template. Please fo
3335

3436
## Usage
3537

36-
The usage of this extension is straightforward. You can inspect the solidity contracts that are under the foundry project in two ways:
38+
The usage of this extension is straightforward.
3739

38-
### 1.) From the context menu: Select file(s) -> Right click -> `SolidityInspector: ir-inspect selected file(s)`
40+
### 1.) From the context menu: Select file(s) -> Right click -> `SolidityInspector: <ACTION>`
3941

40-
<img src="./assets/context-menu.gif" />
42+
<img src="./assets/context-menu.gif"/>
4143

4244
---
4345

44-
### 2.) From the command pallet: Press `Cmd + Shift + P` -> Search `Solidity Inspector - ir-optimizer`
46+
### 2.) From the command pallet: Press `Cmd + Shift + P` -> Search `Solidity Inspector - <ACTION>`
4547

4648
#### IR-Optimized Output:
4749

48-
<img src="./assets/ir-opt.gif" />
50+
<img src="./assets/ir-opt.gif"/>
4951

5052
---
5153

5254
#### ASM-Optimized Output:
5355

54-
<img src="./assets/asm-opt.gif" />
56+
<img src="./assets/asm-opt.gif"/>
5557

5658
---
5759

5860
#### Storage layout Output:
5961

60-
<img src="./assets/storage-layout.gif" />
62+
<img src="./assets/storage-layout.gif"/>
63+
64+
---
65+
66+
#### Flatten contract Output:
67+
68+
<img src="./assets/flatten.gif"/>
69+
70+
71+
---
72+
73+
#### Highlights unused imports:
74+
75+
https://github.com/PraneshASP/vscode-solidity-inspector/assets/42379522/e4906ad3-69e6-4cea-b986-7712ec342fca
76+
6177

6278
---
6379

assets/context-menu.gif

15.1 KB
Loading

assets/flatten.gif

1.28 MB
Loading

assets/m-icon.jpg

657 Bytes
Loading

assets/vscode-unused.mp4

1.28 MB
Binary file not shown.

package.json

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-solidity-inspector",
33
"displayName": "Solidity Inspector",
44
"description": "A vscode extension used to inspect Solidity files",
5-
"version": "0.0.3",
5+
"version": "1.0.0",
66
"engines": {
77
"vscode": "^1.72.0"
88
},
@@ -25,15 +25,10 @@
2525
"url": "https://github.com/PraneshASP/vscode-solidity-inspector"
2626
},
2727
"icon": "assets/m-icon.jpg",
28+
"main": "./src/extension.js",
2829
"activationEvents": [
29-
"onCommand:vscode-solidity-inspector.activeFile.irOptimizer",
30-
"onCommand:vscode-solidity-inspector.contextMenu.irOptimizer",
31-
"onCommand:vscode-solidity-inspector.activeFile.asmOptimizer",
32-
"onCommand:vscode-solidity-inspector.contextMenu.asmOptimizer",
33-
"onCommand:vscode-solidity-inspector.activeFile.storageLayout",
34-
"onCommand:vscode-solidity-inspector.contextMenu.storageLayout"
30+
"onLanguage:solidity"
3531
],
36-
"main": "./src/extension.js",
3732
"contributes": {
3833
"commands": [
3934
{
@@ -65,6 +60,21 @@
6560
"command": "vscode-solidity-inspector.contextMenu.storageLayout",
6661
"title": "SolidityInspector: storage-layout for selected file(s)",
6762
"category": "Solidity Inspector"
63+
},
64+
{
65+
"command": "vscode-solidity-inspector.activeFile.flatten",
66+
"title": "flatten",
67+
"category": "Solidity Inspector"
68+
},
69+
{
70+
"command": "vscode-solidity-inspector.contextMenu.flatten",
71+
"title": "SolidityInspector: flatten selected file(s)",
72+
"category": "Solidity Inspector"
73+
},
74+
{
75+
"command": "vscode-solidity-inspector.activeFile.highlightUnusedImports",
76+
"title": "SolidityInspector: highlight unused imports",
77+
"category": "Solidity Inspector"
6878
}
6979
],
7080
"configuration": {
@@ -88,6 +98,11 @@
8898
"group": "solidity",
8999
"command": "vscode-solidity-inspector.contextMenu.storageLayout",
90100
"when": "resourceLangId == solidity && resourceFilename =~ /\\.sol/"
101+
},
102+
{
103+
"group": "solidity",
104+
"command": "vscode-solidity-inspector.contextMenu.flatten",
105+
"when": "resourceLangId == solidity && resourceFilename =~ /\\.sol/"
91106
}
92107
]
93108
}

src/commands/flatten.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const vscode = require("vscode");
2+
const cp = require("child_process");
3+
const { newWindowBeside } = require("../helpers");
4+
5+
async function flattenActiveFile(args) {
6+
let activeDoc = vscode.window.activeTextEditor.document;
7+
let activeFile = activeDoc.fileName;
8+
if (activeFile.endsWith(".sol")) {
9+
const contractPathArray = activeFile.split("/");
10+
let contractName = contractPathArray[contractPathArray.length - 1];
11+
contractName = contractName.substring(0, contractName.length - 4);
12+
contractPathArray.pop();
13+
14+
let activeFileDirArray = activeFile.split('/');
15+
activeFileDirArray.pop();
16+
let activeFileDir = activeFileDirArray.join("/");
17+
cp.exec(
18+
`cd ${activeFileDir} && forge flatten ${contractName}.sol`,
19+
(err, stdout, stderr) => {
20+
if (err) {
21+
vscode.window.showErrorMessage(
22+
`[Operation failed] ${contractName}\n${err.message}\n\nNOTE: Please make sure to install 'forge' before using this extension`
23+
);
24+
console.error(err);
25+
}
26+
newWindowBeside(stdout);
27+
}
28+
);
29+
} else {
30+
vscode.window.showErrorMessage(
31+
`[Operation failed] ${activeFile}\n\nOnly .sol files in a Foundry project are supported for now.`
32+
);
33+
}
34+
}
35+
36+
async function flattenContextMenu(clickedFile, selectedFiles) {
37+
for (let index = 0; index < selectedFiles.length; index++) {
38+
const activeFile = selectedFiles[index].path;
39+
if (!activeFile.endsWith(".sol")) continue;
40+
const contractPathArray = activeFile.split("/");
41+
let contractName = contractPathArray[contractPathArray.length - 1];
42+
contractName = contractName.substring(0, contractName.length - 4);
43+
44+
contractPathArray.pop();
45+
46+
let activeFileDirArray = activeFile.split('/');
47+
activeFileDirArray.pop();
48+
let activeFileDir = activeFileDirArray.join("/");
49+
cp.exec(
50+
`cd ${activeFileDir} && forge flatten ${contractName}.sol`,
51+
(err, stdout, stderr) => {
52+
if (err) {
53+
vscode.window.showErrorMessage(
54+
`[Operation failed] ${contractName}\n${err.message}\n\nNOTE: Please make sure to install 'forge' before using this extension`
55+
);
56+
}
57+
newWindowBeside(stdout);
58+
}
59+
);
60+
}
61+
}
62+
63+
module.exports = { flattenActiveFile, flattenContextMenu };
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const vscode = require("vscode");
2+
3+
const decorationType = vscode.window.createTextEditorDecorationType({
4+
backgroundColor: 'rgba(255, 0, 0, 0.3)'
5+
});
6+
function extractImports(importStatement) {
7+
const itemsRegex = /import\s*\{(.*?)\}\s*from.*/;
8+
const fileRegex = /\/([^\/"]+)\.sol\";/;
9+
10+
const itemsMatch = importStatement.match(itemsRegex);
11+
if (itemsMatch) {
12+
// This was an import with named items.
13+
// Split the match into individual items, removing leading/trailing whitespace.
14+
const items = itemsMatch[1].split(',').map(item => item.trim());
15+
return items;
16+
} else {
17+
// This was an import with a file.
18+
const fileMatch = importStatement.match(fileRegex);
19+
if (fileMatch) {
20+
// Extract the contract/file name from the path.
21+
const filePathParts = fileMatch[1].split('/');
22+
const fileNameParts = filePathParts[filePathParts.length - 1].split('.');
23+
const contractName = fileNameParts[0];
24+
return [contractName];
25+
} else {
26+
// This import statement didn't match either format.
27+
return [];
28+
}
29+
}
30+
}
31+
32+
// Highlight unused imports for the active Solidity editor when the extension is activated
33+
async function unusedImportsActiveFile(editor) {
34+
if (editor && editor.document.languageId == "solidity") {
35+
editor.setDecorations(decorationType, []);
36+
37+
const text = editor.document.getText();
38+
const importRegex = /import\s+((?:\{.+?\}\s+from\s+)?(?:\".*?\"|'.*?'));/g;
39+
const imports = text.match(importRegex) || [];
40+
const unusedImportDecorations = [];
41+
42+
for (const importStatement of imports) {
43+
const imports = extractImports(importStatement);
44+
for (const item of imports) {
45+
const filePath = item;
46+
const regex = new RegExp(item, 'g');
47+
const itemOccurancesInImportStatement = (importStatement.replace(/\.sol\b/g, '').match(regex) || []).length;
48+
const totalOccurrencesOfItem = (text.match(new RegExp(filePath, 'g')) || []).length;
49+
if (totalOccurrencesOfItem == itemOccurancesInImportStatement) {
50+
const lineIndex = editor.document.getText().split('\n').findIndex(line => line.includes(importStatement));
51+
const range = new vscode.Range(editor.document.lineAt(lineIndex).range.start, editor.document.lineAt(lineIndex).range.end);
52+
53+
unusedImportDecorations.push({ range, ...{ hoverMessage: "Unused import" } });
54+
}
55+
}
56+
}
57+
editor.setDecorations(decorationType, unusedImportDecorations);
58+
}
59+
}
60+
61+
62+
63+
module.exports = { unusedImportsActiveFile };

0 commit comments

Comments
 (0)