-
Notifications
You must be signed in to change notification settings - Fork 44
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
Java adapter #106
Merged
Merged
Java adapter #106
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
75472ef
Initial java configuration
daveleroy ca1cad6
Implement java adapter
LDAP 5c15016
Merge branch 'master' into Implement-java-adapter
LDAP 2775766
Remove debug output
LDAP d131bf1
Fix Exception on jdtls server
LDAP 99d72ec
Resolve classPaths and mainClass
LDAP b126fc4
Show errors
LDAP c482ce7
Fix modulePaths and incompatible class version
LDAP a0fc030
Allow failure when resolving mainClass/classPaths if set in configura…
LDAP 4805618
Fix for debug-java out-of-specification response
LDAP a0d9d2c
Merge branch 'master' into java-adapter
LDAP 846c839
Update Java adapter to new API
LDAP 43f91c2
Merge branch 'master' into java-adapter
LDAP 27cdc87
Merge branch 'master' into java-adapter
LDAP dbe7182
Update Java adapter to new API
LDAP ef9bbec
Java: Move LSP config logic into debugger
LDAP aac31cb
Java: Move source navigation special case to adapter
LDAP 71fabb5
Update README
LDAP 83650c2
Java: Format
LDAP 85f3cfd
Merge branch 'master' into java-adapter
LDAP fe97c7e
Java: Adapt to new installer interface
LDAP 43c28ba
Java: Add jdtls connection timeout
LDAP 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 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
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,145 @@ | ||
from ..typecheck import Optional, Dict, Any, Tuple | ||
from ..import core | ||
from ..import dap | ||
from .import util | ||
|
||
import sublime | ||
import sublime_plugin | ||
import os | ||
import json | ||
|
||
|
||
class Java(dap.AdapterConfiguration): | ||
jdtls_bridge: Dict[int, core.Future] = {} | ||
jdtls_bridge_current_id = 0 | ||
|
||
type = 'java' | ||
docs = 'https://github.com/redhat-developer/vscode-java/blob/master/README.md' | ||
|
||
installer = util.OpenVsxInstaller( | ||
type='java', | ||
repo='vscjava/vscode-java-debug' | ||
) | ||
|
||
async def start(self, log, configuration): | ||
# Make sure LSP and LSP-JDTLS are installed | ||
pc_settings = sublime.load_settings('Package Control.sublime-settings') | ||
installed_packages = pc_settings.get('installed_packages', []) | ||
if 'LSP-jdtls' not in installed_packages or 'LSP' not in installed_packages: | ||
raise core.Error('LSP and LSP-jdtls required to debug Java!') | ||
|
||
# Get configuration from LSP | ||
lsp_config = await self.get_configuration_from_lsp() | ||
|
||
# Configure debugger | ||
if 'cwd' not in configuration: | ||
configuration['cwd'], _ = os.path.split(sublime.active_window().project_file_name()) | ||
if 'mainClass' not in configuration or not configuration['mainClass']: | ||
configuration['mainClass'] = lsp_config['mainClass'] | ||
if 'classPaths' not in configuration: | ||
configuration['classPaths'] = lsp_config['classPaths'] | ||
if 'modulePaths' not in configuration: | ||
configuration['modulePaths'] = lsp_config['modulePaths'] | ||
if 'console' not in configuration: | ||
configuration['console'] = 'internalConsole' | ||
if lsp_config['enablePreview']: | ||
if 'vmArgs' in configuration: | ||
configuration['vmArgs'] += ' --enable-preview' | ||
else: | ||
configuration['vmArgs'] = '--enable-preview' | ||
|
||
# Start debugging session on the LSP side | ||
port = await self.lsp_execute_command('vscode.java.startDebugSession') | ||
|
||
return dap.SocketTransport(log, 'localhost', port) | ||
|
||
async def on_navigate_to_source(self, source: dap.SourceLocation) -> Optional[Tuple[str, str]]: | ||
if not source.source.path or not source.source.path.startswith('jdt:'): | ||
return None | ||
content = await self.get_class_content_for_uri(source.source.path) | ||
return content, 'text/java' | ||
|
||
async def get_class_content_for_uri(self, uri): | ||
return await self.lsp_request('java/classFileContents', {'uri': uri}) | ||
|
||
async def get_configuration_from_lsp(self) -> dict: | ||
lsp_config = {} | ||
|
||
mainclass_resp = await self.lsp_execute_command('vscode.java.resolveMainClass') | ||
if not mainclass_resp or 'mainClass' not in mainclass_resp[0]: | ||
raise core.Error('Failed to resolve main class') | ||
else: | ||
lsp_config['mainClass'] = mainclass_resp[0]['mainClass'] | ||
lsp_config['projectName'] = mainclass_resp[0].get('projectName', '') | ||
|
||
classpath_response = await self.lsp_execute_command( | ||
'vscode.java.resolveClasspath', [lsp_config['mainClass'], lsp_config['projectName']] | ||
) | ||
if not classpath_response[0] and not classpath_response[1]: | ||
raise core.Error('Failed to resolve classpaths/modulepaths') | ||
else: | ||
lsp_config['modulePaths'] = classpath_response[0] | ||
lsp_config['classPaths'] = classpath_response[1] | ||
|
||
# See https://github.com/microsoft/vscode-java-debug/blob/b2a48319952b1af8a4a328fc95d2891de947df94/src/configurationProvider.ts#L297 | ||
lsp_config['enablePreview'] = await self.lsp_execute_command( | ||
'vscode.java.checkProjectSettings', | ||
[ | ||
json.dumps( | ||
{ | ||
'className': lsp_config['mainClass'], | ||
'projectName': lsp_config['projectName'], | ||
'inheritedOptions': True, | ||
'expectedOptions': { | ||
'org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures': 'enabled' | ||
}, | ||
} | ||
) | ||
] | ||
) | ||
|
||
return lsp_config | ||
|
||
async def lsp_execute_command(self, command, arguments=None): | ||
request_params = { 'command': command } | ||
if arguments: | ||
request_params['arguments'] = arguments | ||
return await self.lsp_request('workspace/executeCommand', request_params) | ||
|
||
async def lsp_request(self, method, params) -> Any: | ||
''' | ||
Returns the response or raises an exception. | ||
''' | ||
future = core.Future() | ||
|
||
id = Java.jdtls_bridge_current_id | ||
Java.jdtls_bridge_current_id += 1 | ||
Java.jdtls_bridge[id] = future | ||
|
||
# Send a request to JDTLS. | ||
# NOTE: the active window might not match the debugger window but generally will | ||
# TODO: a way to get the actual window. | ||
sublime.active_window().run_command( | ||
'debugger_jdtls_bridge_request', | ||
{'id': id, 'callback_command': 'debugger_jdtls_bridge_response', 'method': method, 'params': params} | ||
) | ||
sublime.set_timeout(lambda: future.cancel(), 2500) | ||
try: | ||
command_response = await future | ||
except core.CancelledError: | ||
raise core.Error('Unable to connect to LSP-jdtls (timed out)') | ||
|
||
del Java.jdtls_bridge[id] | ||
|
||
if command_response['error']: | ||
raise core.Error(command_response['error']) | ||
return command_response['resp'] | ||
|
||
|
||
class DebuggerJdtlsBridgeResponseCommand(sublime_plugin.WindowCommand): | ||
def run(self, **args): | ||
future = Java.jdtls_bridge.get(args['id']) | ||
if not future: | ||
print('Unable to find a future for this id') | ||
return | ||
future.set_result(args) |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might change that next week to only request if the user did not provide certain config option, by splitting in multiple methods. I should also use the correct main class in the checkProjectSettings command below.