Skip to content

Commit c25e776

Browse files
committed
chore: 🛠️ refactor code
1 parent 504d3ad commit c25e776

File tree

2 files changed

+5
-69
lines changed

2 files changed

+5
-69
lines changed

src/extension.js

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
/** imports */
88
const vscode = require("vscode");
9-
const path = require("path");
10-
const fs = require("fs");
9+
1110

1211
const {
1312
irOptimizerActiveFile,
@@ -41,7 +40,7 @@ const { treeFilesCodeActionProvider, treeFilesDiagnosticCollection } = require("
4140

4241
const { scaffoldActiveFile, scaffoldContextMenu } = require("./commands/bulloak-scaffold");
4342

44-
const { findSolidityFiles } = require("./helpers");
43+
const { provideCompletionItems } = require("./helpers");
4544

4645

4746
/** global vars */
@@ -153,67 +152,7 @@ function onActivate(context) {
153152
context.subscriptions.push(scaffoldActiveFileSubscription);
154153
context.subscriptions.push(scaffoldContextMenuSubscription);
155154

156-
157-
158-
async function provideCompletionItems(document, position) {
159-
const linePrefix = document.lineAt(position).text.substring(0, position.character);
160-
if (!linePrefix.startsWith('import')) {
161-
return undefined;
162-
}
163-
164-
// Step 1: Read and parse remappings
165-
const workspaceFolders = vscode.workspace.workspaceFolders;
166-
if (!workspaceFolders) return undefined; // No workspace folder open
167-
168-
const rootPath = workspaceFolders[0].uri.fsPath;
169-
const remappingsPath = path.join(rootPath, 'remappings.txt');
170-
let remappings = {};
171-
172-
if (fs.existsSync(remappingsPath)) {
173-
const remappingsContent = fs.readFileSync(remappingsPath, 'utf8');
174-
remappingsContent.split('\n').forEach(line => {
175-
const [key, val] = line.split('=');
176-
remappings[key.trim()] = val.trim();
177-
});
178-
}
179-
180-
// Step 2: Apply remappings to import paths
181-
const solidityFiles = await findSolidityFiles();
182-
const currentFilePath = document.uri.fsPath;
183-
const currentDir = path.dirname(currentFilePath);
184-
185-
186-
const completionItems = solidityFiles.map(file => {
187-
let filePath = vscode.workspace.asRelativePath(file, false);
188-
let remappedPath = filePath; // Start with the original path
189-
190-
// Apply remappings
191-
Object.entries(remappings).forEach(([key, value]) => {
192-
if (filePath.startsWith(value)) {
193-
// Replace the matching part of the path with the remapping key
194-
remappedPath = filePath.replace(value, key);
195-
}
196-
});
197-
198-
// Determine if the path was remapped
199-
const isRemapped = remappedPath !== filePath;
200-
201-
// Use the remapped path if available, otherwise use the relative path
202-
const finalPath = isRemapped ? remappedPath : `./${path.relative(currentDir, file).split(path.sep).join('/')}`;
203-
204-
const contractName = path.basename(file, '.sol');
205-
const completionItem = new vscode.CompletionItem(`${contractName} from "${finalPath}"`, vscode.CompletionItemKind.File);
206-
207-
// Format the insert text based on whether the path was remapped
208-
completionItem.insertText = `{${contractName}} from "${finalPath}";`;
209-
210-
return completionItem;
211-
});
212-
213-
214-
return completionItems;
215-
}
216-
155+
// Import suggestions.
217156
context.subscriptions.push(vscode.languages.registerCompletionItemProvider('solidity', { provideCompletionItems }, ['"', "{"]));
218157

219158

src/helpers.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ function newWindowBeside(content) {
2525
}
2626

2727

28-
async function provideImportSuggestions(document, position) {
28+
async function provideCompletionItems(document, position) {
2929
const linePrefix = document.lineAt(position).text.substring(0, position.character);
30-
console.log("lineprefix", linePrefix);
3130
if (!linePrefix.startsWith('import')) {
3231
return undefined;
3332
}
@@ -80,8 +79,6 @@ async function provideImportSuggestions(document, position) {
8079

8180
return completionItem;
8281
});
83-
84-
8582
return completionItems;
8683
}
8784

@@ -195,4 +192,4 @@ const networkMap = {
195192

196193

197194

198-
module.exports = { newWindowBeside, getContractRootDir, LANGID, networkMap, provideImportSuggestions, findSolidityFiles };
195+
module.exports = { newWindowBeside, getContractRootDir, LANGID, networkMap, provideCompletionItems, findSolidityFiles };

0 commit comments

Comments
 (0)