-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Vimspector config substitutions for convenience
The following variables are now available for automatic substitution in Vimspector config based on the currently loaded Java file. Note, these only work if the Java file contains a main method. - ClassPaths - MainClass - ModulePaths - ProjectName The variable substitution names are configurable via the plugin settings. - java.debug.vimspector.substitution.classPaths - java.debug.vimspector.substitution.mainClass - java.debug.vimspector.substitution.modulePaths - java.debug.vimspector.substitution.projectName
- Loading branch information
1 parent
db96c05
commit a3eca5a
Showing
5 changed files
with
189 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,16 +1,21 @@ | ||
import { commands } from 'coc.nvim'; | ||
|
||
export namespace Commands { | ||
/** | ||
* vscode-java-debug command to start a debug session. | ||
*/ | ||
export const JAVA_START_DEBUG_SESSION = 'vscode.java.startDebugSession'; | ||
|
||
/** | ||
* coc command to start a java debugger session and connect vimspector to it. | ||
*/ | ||
export const JAVA_START_DEBUGSESSION = 'vscode.java.startDebugSession'; | ||
|
||
export const JAVA_RESOLVE_CLASSPATH = 'vscode.java.resolveClasspath'; | ||
|
||
export const JAVA_RESOLVE_MAINMETHOD = 'vscode.java.resolveMainMethod'; | ||
|
||
export const JAVA_DEBUG_VIMSPECTOR_START = 'java.debug.vimspector.start'; | ||
|
||
/** | ||
* Execute Workspace Command | ||
*/ | ||
export const JAVA_DEBUG_RESOLVE_MAINMETHOD = 'java.debug.resolveMainMethod'; | ||
|
||
export const JAVA_DEBUG_RESOLVE_CLASSPATH = 'java.debug.resolveClasspath'; | ||
|
||
export const EXECUTE_WORKSPACE_COMMAND = 'java.execute.workspaceCommand'; | ||
} | ||
|
||
export async function executeCommand(...rest: any[]) { | ||
return commands.executeCommand(Commands.EXECUTE_WORKSPACE_COMMAND, ...rest); | ||
} |
This file contains 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,58 @@ | ||
import { TextDocument, Uri, window, workspace } from 'coc.nvim'; | ||
import { Commands, executeCommand } from './commands'; | ||
import { IClassPath, IMainClassOption, MainMethodResult } from './protocol'; | ||
|
||
export async function resolveMainMethodCurrentFile(): Promise<IMainClassOption | undefined> { | ||
const mainMethods = await resolveMainMethodsCurrentFile(); | ||
if (mainMethods.length === 1) { | ||
return mainMethods[0]; | ||
} else if (mainMethods.length > 1) { | ||
return await pickMainMethod(mainMethods); | ||
} | ||
return undefined; | ||
} | ||
|
||
export async function resolveMainMethodsCurrentFile(): Promise<MainMethodResult> { | ||
const { document } = await workspace.getCurrentState(); | ||
return resolveMainMethod(document); | ||
} | ||
|
||
async function resolveMainMethod(document: TextDocument): Promise<MainMethodResult> { | ||
const resourcePath = getJavaResourcePath(document); | ||
return await executeCommand(Commands.JAVA_RESOLVE_MAINMETHOD, resourcePath); | ||
} | ||
|
||
export async function resolveClassPathCurrentFile(): Promise<IClassPath> { | ||
const mainMethod = await resolveMainMethodCurrentFile(); | ||
if (mainMethod) { | ||
return resolveClassPathMainMethod(mainMethod); | ||
} | ||
return { modulePaths: [], classPaths: [] }; | ||
} | ||
|
||
export async function resolveClassPathMainMethod(mainMethod: IMainClassOption): Promise<IClassPath> { | ||
const classPath: any[] = await resolveClasspath(mainMethod.mainClass, mainMethod.projectName || ''); | ||
const [modulePaths, classPaths] = classPath; | ||
return { modulePaths, classPaths }; | ||
} | ||
|
||
async function resolveClasspath(mainClass: string, projectName: string, scope?: string): Promise<any[]> { | ||
return executeCommand(Commands.JAVA_RESOLVE_CLASSPATH, mainClass, projectName, scope); | ||
} | ||
|
||
function getJavaResourcePath(document: TextDocument): string | undefined { | ||
const resource = Uri.parse(document.uri); | ||
if (resource.scheme === 'file' && resource.fsPath.endsWith('.java')) { | ||
return resource.toString(); | ||
} | ||
return undefined; | ||
} | ||
|
||
export async function pickMainMethod(mainMethods: MainMethodResult): Promise<IMainClassOption> { | ||
const items = mainMethods.map((method) => { | ||
return method.mainClass; | ||
}); | ||
const selected = await window.showQuickpick(items, 'Choose a main method.'); | ||
// Choose the first one if none is selected. | ||
return mainMethods[selected >= 0 ? selected : 0]; | ||
} |
This file contains 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 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,27 @@ | ||
import { Range } from 'coc.nvim'; | ||
|
||
export interface IMainClassOption { | ||
readonly mainClass: string; | ||
readonly projectName?: string; | ||
readonly filePath?: string; | ||
} | ||
|
||
export interface IMainMethod extends IMainClassOption { | ||
readonly range: Range; | ||
} | ||
|
||
export type MainMethodResult = Array<IMainClassOption>; | ||
|
||
// See https://github.com/microsoft/vscode-java-debug#options | ||
export interface ISubstitutionVar { | ||
readonly adapterPort: string; | ||
readonly classPaths: string; | ||
readonly mainClass: string; | ||
readonly modulePaths: string; | ||
readonly projectName: string; | ||
} | ||
|
||
export interface IClassPath { | ||
readonly modulePaths: string[]; | ||
readonly classPaths: string[]; | ||
} |