|
1 | 1 | import * as vscode from 'vscode'; |
| 2 | +import * as path from 'path'; |
2 | 3 | import { format } from './formatter'; |
3 | 4 |
|
4 | 5 | // Rainbow bracket colors - optimized for readability |
@@ -236,6 +237,101 @@ function updateIndentGuides(editor: vscode.TextEditor | undefined) { |
236 | 237 | } |
237 | 238 | } |
238 | 239 |
|
| 240 | +class ExprFileLinkProvider implements vscode.DocumentLinkProvider { |
| 241 | + provideDocumentLinks( |
| 242 | + document: vscode.TextDocument |
| 243 | + ): vscode.DocumentLink[] | undefined { |
| 244 | + const links: vscode.DocumentLink[] = []; |
| 245 | + const text = document.getText(); |
| 246 | + |
| 247 | + // Match quoted strings containing .expr files |
| 248 | + const regex = /["']([^"']+\.expr)["']/g; |
| 249 | + let match; |
| 250 | + |
| 251 | + while ((match = regex.exec(text)) !== null) { |
| 252 | + const filePath = match[1]; |
| 253 | + const startPos = document.positionAt(match.index + 1); // +1 to skip opening quote |
| 254 | + const endPos = document.positionAt(match.index + 1 + filePath.length); |
| 255 | + const range = new vscode.Range(startPos, endPos); |
| 256 | + |
| 257 | + try { |
| 258 | + // Resolve file path relative to current document |
| 259 | + const currentDir = path.dirname(document.uri.fsPath); |
| 260 | + const fullPath = path.resolve(currentDir, filePath); |
| 261 | + const fileUri = vscode.Uri.file(fullPath); |
| 262 | + |
| 263 | + // Create document link |
| 264 | + const link = new vscode.DocumentLink(range, fileUri); |
| 265 | + link.tooltip = `Open ${filePath}`; |
| 266 | + links.push(link); |
| 267 | + } catch (error) { |
| 268 | + console.error('[ExprLink] Error resolving expr file path:', error); |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + return links; |
| 273 | + } |
| 274 | +} |
| 275 | + |
| 276 | +class ExprFileDefinitionProvider implements vscode.DefinitionProvider { |
| 277 | + provideDefinition( |
| 278 | + document: vscode.TextDocument, |
| 279 | + position: vscode.Position |
| 280 | + ): vscode.ProviderResult<vscode.Definition | vscode.LocationLink[]> { |
| 281 | + console.log('[ExprDef] provideDefinition called at', position.line, position.character); |
| 282 | + |
| 283 | + const line = document.lineAt(position.line); |
| 284 | + const lineText = line.text; |
| 285 | + console.log('[ExprDef] Line text:', lineText); |
| 286 | + |
| 287 | + // Scan the entire line for .expr files and find the one under cursor |
| 288 | + const regex = /["']([^"']+\.expr)["']/g; |
| 289 | + let match; |
| 290 | + |
| 291 | + while ((match = regex.exec(lineText)) !== null) { |
| 292 | + const filePath = match[1]; |
| 293 | + const matchStart = match.index + 1; // +1 to skip opening quote |
| 294 | + const matchEnd = matchStart + filePath.length; |
| 295 | + |
| 296 | + console.log('[ExprDef] Checking match:', filePath, 'range:', matchStart, '-', matchEnd); |
| 297 | + |
| 298 | + // Check if cursor is within this match (including the filename) |
| 299 | + if (position.character >= matchStart && position.character <= matchEnd) { |
| 300 | + console.log('[ExprDef] Cursor is within match!'); |
| 301 | + |
| 302 | + try { |
| 303 | + // Resolve file path relative to current document |
| 304 | + const currentDir = path.dirname(document.uri.fsPath); |
| 305 | + const fullPath = path.resolve(currentDir, filePath); |
| 306 | + const fileUri = vscode.Uri.file(fullPath); |
| 307 | + |
| 308 | + console.log('[ExprDef] Resolved full path:', fullPath); |
| 309 | + |
| 310 | + // Create the range for the entire filename (without quotes) |
| 311 | + const originSelectionRange = new vscode.Range( |
| 312 | + new vscode.Position(position.line, matchStart), |
| 313 | + new vscode.Position(position.line, matchEnd) |
| 314 | + ); |
| 315 | + |
| 316 | + // Return LocationLink with explicit range for better hover UX |
| 317 | + return [{ |
| 318 | + originSelectionRange: originSelectionRange, |
| 319 | + targetUri: fileUri, |
| 320 | + targetRange: new vscode.Range(0, 0, 0, 0), |
| 321 | + targetSelectionRange: new vscode.Range(0, 0, 0, 0) |
| 322 | + }]; |
| 323 | + } catch (error) { |
| 324 | + console.error('[ExprDef] Error resolving expr file path:', error); |
| 325 | + return undefined; |
| 326 | + } |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + console.log('[ExprDef] No match found'); |
| 331 | + return undefined; |
| 332 | + } |
| 333 | +} |
| 334 | + |
239 | 335 | export function activate(context: vscode.ExtensionContext) { |
240 | 336 | console.log('Expr language extension activated'); |
241 | 337 |
|
@@ -311,12 +407,28 @@ export function activate(context: vscode.ExtensionContext) { |
311 | 407 | } |
312 | 408 | }); |
313 | 409 |
|
| 410 | + // Register document link provider for .expr file highlighting |
| 411 | + // Makes .expr files appear as clickable links in all file types |
| 412 | + const linkProvider = vscode.languages.registerDocumentLinkProvider( |
| 413 | + { scheme: 'file' }, |
| 414 | + new ExprFileLinkProvider() |
| 415 | + ); |
| 416 | + |
| 417 | + // Register definition provider for .expr file navigation |
| 418 | + // Works in all file types (markdown, plaintext, etc.) |
| 419 | + const definitionProvider = vscode.languages.registerDefinitionProvider( |
| 420 | + { scheme: 'file' }, |
| 421 | + new ExprFileDefinitionProvider() |
| 422 | + ); |
| 423 | + |
314 | 424 | context.subscriptions.push( |
315 | 425 | formatterProvider, |
316 | 426 | formatCommand, |
317 | 427 | editorChangeListener, |
318 | 428 | documentChangeListener, |
319 | | - configChangeListener |
| 429 | + configChangeListener, |
| 430 | + linkProvider, |
| 431 | + definitionProvider |
320 | 432 | ); |
321 | 433 | } |
322 | 434 |
|
|
0 commit comments