|
6 | 6 |
|
7 | 7 | /** imports */ |
8 | 8 | const vscode = require("vscode"); |
9 | | -const path = require("path"); |
10 | | -const fs = require("fs"); |
| 9 | + |
11 | 10 |
|
12 | 11 | const { |
13 | 12 | irOptimizerActiveFile, |
@@ -41,7 +40,7 @@ const { treeFilesCodeActionProvider, treeFilesDiagnosticCollection } = require(" |
41 | 40 |
|
42 | 41 | const { scaffoldActiveFile, scaffoldContextMenu } = require("./commands/bulloak-scaffold"); |
43 | 42 |
|
44 | | -const { findSolidityFiles } = require("./helpers"); |
| 43 | +const { provideCompletionItems } = require("./helpers"); |
45 | 44 |
|
46 | 45 |
|
47 | 46 | /** global vars */ |
@@ -153,67 +152,7 @@ function onActivate(context) { |
153 | 152 | context.subscriptions.push(scaffoldActiveFileSubscription); |
154 | 153 | context.subscriptions.push(scaffoldContextMenuSubscription); |
155 | 154 |
|
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. |
217 | 156 | context.subscriptions.push(vscode.languages.registerCompletionItemProvider('solidity', { provideCompletionItems }, ['"', "{"])); |
218 | 157 |
|
219 | 158 |
|
|
0 commit comments