Skip to content

Commit

Permalink
Java: Move source navigation special case to adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
LDAP committed Sep 17, 2022
1 parent ef9bbec commit aac31cb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
8 changes: 7 additions & 1 deletion modules/adapters/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# "command": "vscode.java.startDebugSession"
# }

from ..typecheck import Optional, Dict, Any
from ..typecheck import Optional, Dict, Any, Tuple
from ..import core
from ..import dap
from .import util
Expand Down Expand Up @@ -83,6 +83,12 @@ def configuration_snippets(self) -> Optional[list]:
def configuration_schema(self) -> Optional[dict]:
return util.vscode.configuration_schema(self.type)

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})

Expand Down
11 changes: 10 additions & 1 deletion modules/dap/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from ..typecheck import *

from ..import core
from ..import dap
from .transport import Transport

import sublime
Expand Down Expand Up @@ -77,6 +78,14 @@ def settings(self, debugger: Debugger) -> list[Any]:
def ui(self, debugger: Debugger) -> Any|None:
...

async def on_navigate_to_source(self, source: dap.SourceLocation) -> Optional[Tuple[str, str]]:
"""
Allows the adapter to supply content when navigating to source.
Returns: None to keep the default behavior, else a tuple (content, mime_type)
"""
return None


class Configuration(Dict[str, Any]):
def __init__(self, name: str, index: int, type: str, request: str, all: dict[str, Any]):
super().__init__(all)
Expand Down Expand Up @@ -161,4 +170,4 @@ def _expand_variables_and_platform(json: dict[str, Any], variables: dict[str, st
if variables := variables:
json = sublime.expand_variables(json, variables)

return json
return json
46 changes: 16 additions & 30 deletions modules/source_navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
syntax_name_for_mime_type: dict[str|None, str] = {
'text/plain': 'text.plain',
'text/javascript': 'source.js',
'text/java': 'source.java',
'text/x-lldb.disassembly': 'source.disassembly',
}

Expand Down Expand Up @@ -99,22 +100,27 @@ def clear_generated_view(self):
self.generated_view = None

async def navigate_to_source(self, source: dap.SourceLocation, move_cursor: bool = False) -> sublime.View:
# TODO: Prevent circular import using interface
from .adapters.java import Java
# Check if adapter want to provide content
if self.debugger.session:
adapter_content = await self.debugger.session.adapter_configuration.on_navigate_to_source(source)
else:
adapter_content = None

# if we aren't going to reuse the previous generated view throw away any generated view
if not source.source.sourceReference:
if adapter_content or source.source.sourceReference:
self.clear_generated_view()

line = (source.line or 1) - 1
column = (source.column or 1) - 1

if source.source.sourceReference:
session = self.debugger.session
if not session:
raise core.Error('No Active Debug Session')

content, mime_type = await session.get_source(source.source)
if adapter_content or source.source.sourceReference:
if adapter_content:
content, mime_type = adapter_content
else:
session = self.debugger.session
if not session:
raise core.Error('No Active Debug Session')
content, mime_type = await session.get_source(source.source)

# the generated view was closed (no buffer) throw it away
if self.generated_view and not self.generated_view.buffer_id():
Expand All @@ -126,7 +132,6 @@ async def navigate_to_source(self, source: dap.SourceLocation, move_cursor: bool
view.set_name(source.source.name or "")
view.set_read_only(False)


syntax = syntax_name_for_mime_type.get(mime_type, 'text.plain')
view.assign_syntax(sublime.find_syntax_by_scope(syntax)[0])

Expand All @@ -135,26 +140,7 @@ async def navigate_to_source(self, source: dap.SourceLocation, move_cursor: bool
view.set_read_only(True)
view.set_scratch(True)
elif source.source.path:
if source.source.path.startswith("jdt:") and self.debugger.session and isinstance(self.debugger.session.adapter_configuration, Java):
# the generated view was closed (no buffer) throw it away
if self.generated_view and not self.generated_view.buffer_id():
self.clear_generated_view()

view = self.generated_view or self.project.window.new_file()
self.project.window.set_view_index(view, 0, len(self.project.window.views_in_group(0)))
self.generated_view = view
view.set_name(source.source.name or "")
view.set_read_only(False)

view.assign_syntax(sublime.find_syntax_by_scope("source.java")[0])
content = await self.debugger.session.adapter_configuration.get_class_content_for_uri(source.source.path)

replace_contents(view, content)

view.set_read_only(True)
view.set_scratch(True)
else:
view = await core.sublime_open_file_async(self.project.window, source.source.path, group=0)
view = await core.sublime_open_file_async(self.project.window, source.source.path, group=0)
else:
raise core.Error('source has no reference or path')

Expand Down

0 comments on commit aac31cb

Please sign in to comment.