-
Notifications
You must be signed in to change notification settings - Fork 411
Show Run and Debug button when hover on a main method #658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. | ||
|
|
||
| import { CancellationToken, Command, Disposable, Hover, HoverProvider, languages, MarkdownString, Position, ProviderResult, TextDocument, | ||
| Uri, window } from "vscode"; | ||
| import { instrumentOperationAsVsCodeCommand } from "vscode-extension-telemetry-wrapper"; | ||
| import { JAVA_LANGID } from "./constants"; | ||
| import { startDebugging } from "./debugCodeLensProvider"; | ||
| import { resolveElementAtSelection } from "./languageServerPlugin"; | ||
|
|
||
| const JAVA_HOVER_RUN_COMMAND = "java.debug.runHover"; | ||
| const MAIN_METHOD_REGEX = /^(public|static|final|synchronized|\s+){4,}void\s+main\s*\(\s*String\s*\[\s*\]\s*\w+\s*\)\s*($|\{)/; | ||
|
|
||
| export function initializeHoverProvider(): Disposable { | ||
| return new DebugHoverProvider(); | ||
| } | ||
|
|
||
| class DebugHoverProvider implements Disposable { | ||
| private runHoverCommand: Disposable; | ||
| private hoverProvider: Disposable | undefined; | ||
|
|
||
| constructor() { | ||
| this.runHoverCommand = instrumentOperationAsVsCodeCommand(JAVA_HOVER_RUN_COMMAND, async (noDebug: boolean, uri: string, position: any) => { | ||
| const element = await resolveElementAtSelection(uri, position.line, position.character); | ||
| if (element && element.hasMainMethod) { | ||
| startDebugging(element.declaringType, element.projectName, Uri.parse(uri), noDebug); | ||
| } else { | ||
| window.showErrorMessage("The hovered element is not a main method."); | ||
| } | ||
| }); | ||
| this.hoverProvider = languages.registerHoverProvider(JAVA_LANGID, new InternalDebugHoverProvider()); | ||
| } | ||
|
|
||
| public dispose() { | ||
| if (this.runHoverCommand) { | ||
| this.runHoverCommand.dispose(); | ||
| } | ||
|
|
||
| if (this.hoverProvider) { | ||
| this.hoverProvider.dispose(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class InternalDebugHoverProvider implements HoverProvider { | ||
| public provideHover(document: TextDocument, position: Position, token: CancellationToken): ProviderResult<Hover> { | ||
| const range = document.getWordRangeAtPosition(position, /\w+/); | ||
| if (!range || document.getText(range) !== "main") { | ||
| return; | ||
| } | ||
|
|
||
| const line = document.lineAt(position); | ||
| if (MAIN_METHOD_REGEX.test(line.text.trim()) && this.isMainMethod(line.text.trim())) { | ||
| const commands: Command[] = [ | ||
| { | ||
| title: "Run", | ||
| command: JAVA_HOVER_RUN_COMMAND, | ||
| tooltip: "Run Java Program", | ||
| arguments: [ true, document.uri.toString(), { line: position.line, character: position.character }], | ||
| }, | ||
| { | ||
| title: "Debug", | ||
| command: JAVA_HOVER_RUN_COMMAND, | ||
| tooltip: "Debug Java Program", | ||
| arguments: [ false, document.uri.toString(), { line: position.line, character: position.character }], | ||
| }, | ||
| ]; | ||
| const contributed = new MarkdownString(commands.map((command) => this.convertCommandToMarkdown(command)).join(" | ")); | ||
| contributed.isTrusted = true; | ||
| return new Hover(contributed); | ||
| } | ||
| } | ||
|
|
||
| private isMainMethod(line: string): boolean { | ||
| const modifier: string = line.substring(0, line.indexOf("main")); | ||
| const modifiers: string[] = modifier.split(/\s+/); | ||
| return modifiers.indexOf("public") >= 0 && modifiers.indexOf("static") >= 0; | ||
| } | ||
|
|
||
| private convertCommandToMarkdown(command: Command): string { | ||
| return `[${command.title}](command:${command.command}?` + | ||
| `${encodeURIComponent(JSON.stringify(command.arguments || []))} "${command.tooltip || command.command}")`; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.