(
preserveFocus: boolean = false,
)
| 422 | |
| 423 | // quickly open help for selection |
| 424 | public async openHelpForSelection( |
| 425 | preserveFocus: boolean = false, |
| 426 | ): Promise<boolean> { |
| 427 | // only use if we failed to show help page: |
| 428 | let errMsg = ''; |
| 429 | |
| 430 | const editor = vscode.window.activeTextEditor; |
| 431 | if (editor) { |
| 432 | // the text to show help for: |
| 433 | let txt = ''; |
| 434 | if (editor.selection.isEmpty) { |
| 435 | // no text selected -> find word at current cursor position |
| 436 | // use regex including ":" to capture package/namespace (e.g. base::print) |
| 437 | const re = /([a-zA-Z0-9._:])+/; |
| 438 | const range = editor.document.getWordRangeAtPosition( |
| 439 | editor.selection.start, |
| 440 | re, |
| 441 | ); |
| 442 | // check if the cursor is at a word (else: whitespace -> ignore) |
| 443 | if (range) { |
| 444 | txt = editor.document.getText(range); |
| 445 | } |
| 446 | } else { |
| 447 | // use selected text |
| 448 | txt = editor.document.getText(editor.selection); |
| 449 | } |
| 450 | txt = txt.trim(); |
| 451 | if (txt) { |
| 452 | const success = await this.openHelpByAlias(txt, preserveFocus); |
| 453 | if (!success) { |
| 454 | errMsg = `Failed to open help for "${txt}"!`; |
| 455 | } |
| 456 | } else { |
| 457 | errMsg = 'Cannot show help: No valid text selected!'; |
| 458 | } |
| 459 | } else { |
| 460 | errMsg = 'No editor active!'; |
| 461 | } |
| 462 | if (errMsg) { |
| 463 | void vscode.window.showErrorMessage(errMsg); |
| 464 | return false; |
| 465 | } |
| 466 | return true; |
| 467 | } |
| 468 | |
| 469 | // quickly open help page by alias |
| 470 | public async openHelpByAlias( |
no test coverage detected