-
-
Notifications
You must be signed in to change notification settings - Fork 63
ADFA-2448: Add a document outline sidebar for Java, Kotlin and XML #1804
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
Open
Daniel-ADFA
wants to merge
20
commits into
stage
Choose a base branch
from
feat/ADFA-2448-document-outline
base: stage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6943a9d
ADFA-2448: Add outline symbol model and containment tree builder
alome007 d6e2f45
ADFA-2448: Add outline tree-sitter queries for java, kotlin, xml
alome007 81c1eb6
ADFA-2448: Add tree-sitter outline extraction provider
alome007 dbb0a1a
ADFA-2448: Add outline UDF state types and view model
alome007 931f720
ADFA-2448: Add outline Compose panel and row flattening
alome007 7695fd3
style: spotless reformat EditorSidebarActions, no functional change
alome007 1f8fa62
ADFA-2448: Add outline sidebar panel with symbol navigation
alome007 5258438
ADFA-2448: Read outline change snapshots from FileManager
alome007 63b90d4
ADFA-2448: Color outline badges by symbol kind group
alome007 d902119
ADFA-2448: Scroll the outline target into view after the drawer closes
alome007 1b611d5
ADFA-2448: Use two-letter mnemonic badges for outline symbol kinds
alome007 017a64c
ADFA-2448: Densify outline rows with inline top-aligned details
alome007 bbbf6f1
ADFA-2448: Draw hierarchy connector lines in the outline
alome007 dd1e536
ADFA-2448: Center the tapped symbol in the viewport
alome007 3fefb06
ADFA-2448: Keep the outline pipeline alive through parse failures
alome007 f7b8597
ADFA-2448: Track only the active document and harden the outline pipe…
alome007 0208cfc
ADFA-2448: Fix outline extraction gaps, collapse-state race and stale…
alome007 01a4300
ADFA-2448: Stop parsing for a hidden panel, scope Kotlin functions, r…
alome007 44bbe8d
ADFA-2448: Keep the signature beside the name when it fits
alome007 0ce2bd3
ADFA-2448: Hold the deferred scroll as state and translate the row de…
alome007 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
28 changes: 28 additions & 0 deletions
28
app/src/main/java/com/itsaky/androidide/actions/sidebar/OutlineSidebarAction.kt
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,28 @@ | ||
| package com.itsaky.androidide.actions.sidebar | ||
|
|
||
| import android.content.Context | ||
| import androidx.core.content.ContextCompat | ||
| import androidx.fragment.app.Fragment | ||
| import com.itsaky.androidide.R | ||
| import com.itsaky.androidide.fragments.sidebar.OutlineFragment | ||
| import com.itsaky.androidide.idetooltips.TooltipTag | ||
| import kotlin.reflect.KClass | ||
|
|
||
| class OutlineSidebarAction( | ||
| context: Context, | ||
| override val order: Int, | ||
| ) : AbstractSidebarAction() { | ||
| companion object { | ||
| const val ID = "ide.editor.sidebar.outline" | ||
| } | ||
|
|
||
| override val id: String = ID | ||
| override val fragmentClass: KClass<out Fragment> = OutlineFragment::class | ||
|
|
||
| init { | ||
| label = context.getString(R.string.title_document_outline) | ||
| icon = ContextCompat.getDrawable(context, R.drawable.ic_outline) | ||
| } | ||
|
|
||
| override fun retrieveTooltipTag(isAlternateContext: Boolean) = TooltipTag.OUTLINE_SIDEBAR | ||
|
Daniel-ADFA marked this conversation as resolved.
|
||
| } | ||
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
190 changes: 190 additions & 0 deletions
190
app/src/main/java/com/itsaky/androidide/fragments/sidebar/OutlineFragment.kt
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,190 @@ | ||
| package com.itsaky.androidide.fragments.sidebar | ||
|
|
||
| import android.os.Bundle | ||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import androidx.compose.ui.platform.ComposeView | ||
| import androidx.compose.ui.platform.ViewCompositionStrategy | ||
| import androidx.core.view.GravityCompat | ||
| import androidx.drawerlayout.widget.DrawerLayout | ||
| import androidx.fragment.app.Fragment | ||
| import androidx.fragment.app.activityViewModels | ||
| import androidx.lifecycle.Lifecycle | ||
| import androidx.lifecycle.lifecycleScope | ||
| import androidx.lifecycle.repeatOnLifecycle | ||
| import com.itsaky.androidide.activities.editor.EditorHandlerActivity | ||
| import com.itsaky.androidide.common.compose.IdeTheme | ||
| import com.itsaky.androidide.editor.ui.IDEEditor | ||
| import com.itsaky.androidide.eventbus.events.editor.DocumentChangeEvent | ||
| import com.itsaky.androidide.eventbus.events.editor.DocumentOpenEvent | ||
| import com.itsaky.androidide.models.Position | ||
| import com.itsaky.androidide.ui.models.OutlineUiEffect | ||
| import com.itsaky.androidide.ui.outline.OutlinePanel | ||
| import com.itsaky.androidide.viewmodel.EditorViewModel | ||
| import com.itsaky.androidide.viewmodel.OutlineViewModel | ||
| import kotlinx.coroutines.launch | ||
| import org.greenrobot.eventbus.EventBus | ||
| import org.greenrobot.eventbus.Subscribe | ||
| import org.greenrobot.eventbus.ThreadMode.MAIN | ||
| import org.koin.androidx.viewmodel.ext.android.activityViewModel | ||
| import java.nio.file.Path | ||
|
|
||
| class OutlineFragment : Fragment() { | ||
| private val viewModel: OutlineViewModel by activityViewModel() | ||
| private val editorViewModel: EditorViewModel by activityViewModels() | ||
| private var drawer: DrawerLayout? = null | ||
|
|
||
| private var pendingScroll: Pair<IDEEditor, Position>? = null | ||
|
|
||
| private val drawerListener = | ||
| object : DrawerLayout.SimpleDrawerListener() { | ||
| override fun onDrawerOpened(drawerView: View) { | ||
| pendingScroll = null | ||
| seedFromCurrentEditor() | ||
| } | ||
|
|
||
| override fun onDrawerClosed(drawerView: View) { | ||
| val (editor, position) = pendingScroll ?: return | ||
| pendingScroll = null | ||
| if (editor.isValidPosition(position, true)) { | ||
| centerPositionInView(editor, position) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onCreateView( | ||
| inflater: LayoutInflater, | ||
| container: ViewGroup?, | ||
| savedInstanceState: Bundle?, | ||
| ): View = | ||
| ComposeView(requireContext()).apply { | ||
| setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) | ||
| setContent { | ||
| IdeTheme { | ||
| OutlinePanel(viewModel) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onViewCreated( | ||
| view: View, | ||
| savedInstanceState: Bundle?, | ||
| ) { | ||
| super.onViewCreated(view, savedInstanceState) | ||
| drawer = | ||
| (activity as? EditorHandlerActivity)?.binding?.editorDrawerLayout?.also { | ||
| it.addDrawerListener(drawerListener) | ||
| } | ||
| editorViewModel.currentFile.observe(viewLifecycleOwner) { view.post { seedFromCurrentEditor() } } | ||
| viewLifecycleOwner.lifecycleScope.launch { | ||
| viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
| viewModel.effects.collect { effect -> | ||
| when (effect) { | ||
| is OutlineUiEffect.NavigateTo -> navigateTo(effect.position) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun onDestroyView() { | ||
| drawer?.removeDrawerListener(drawerListener) | ||
| drawer = null | ||
| pendingScroll = null | ||
| viewModel.onNoEditor() | ||
| super.onDestroyView() | ||
| } | ||
|
|
||
| override fun onStart() { | ||
| super.onStart() | ||
| if (!EventBus.getDefault().isRegistered(this)) { | ||
| EventBus.getDefault().register(this) | ||
| } | ||
| } | ||
|
|
||
| override fun onStop() { | ||
| EventBus.getDefault().unregister(this) | ||
| super.onStop() | ||
| } | ||
|
|
||
| @Subscribe(threadMode = MAIN) | ||
| fun onDocumentChanged(event: DocumentChangeEvent) { | ||
|
Daniel-ADFA marked this conversation as resolved.
|
||
| if (!isPanelVisible()) return | ||
| val editor = currentEditor() ?: return | ||
| val file = editor.file ?: return | ||
| if (normalized(file.toPath()) != normalized(event.changedFile)) return | ||
| viewModel.onSnapshot( | ||
| path = normalized(event.changedFile), | ||
| extension = file.extension, | ||
| text = event.newText ?: editor.text.toString(), | ||
| immediate = false, | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Subscribe(threadMode = MAIN) | ||
| fun onDocumentOpened(event: DocumentOpenEvent) { | ||
|
Daniel-ADFA marked this conversation as resolved.
|
||
| if (!isPanelVisible()) return | ||
| val file = currentEditor()?.file ?: return | ||
| if (normalized(file.toPath()) != normalized(event.openedFile)) return | ||
| viewModel.onSnapshot( | ||
| path = normalized(event.openedFile), | ||
| extension = extensionOf(event.openedFile), | ||
| text = event.text, | ||
| immediate = true, | ||
| ) | ||
| } | ||
|
|
||
| private fun seedFromCurrentEditor() { | ||
| if (!isPanelVisible()) return | ||
| val editor = currentEditor() | ||
| val file = editor?.file | ||
| if (editor == null || file == null) { | ||
| viewModel.onNoEditor() | ||
| return | ||
| } | ||
| viewModel.onSnapshot( | ||
| path = normalized(file.toPath()), | ||
| extension = file.extension, | ||
| text = editor.text.toString(), | ||
| immediate = true, | ||
| ) | ||
| } | ||
|
|
||
| private fun currentEditor() = (activity as? EditorHandlerActivity)?.getCurrentEditor()?.editor | ||
|
|
||
| private fun isPanelVisible(): Boolean = drawer?.isDrawerOpen(GravityCompat.START) == true | ||
|
|
||
| private fun normalized(path: Path): String = path.toAbsolutePath().normalize().toString() | ||
|
|
||
| private fun navigateTo(position: Position) { | ||
| val drawer = drawer ?: return | ||
| val editor = currentEditor() | ||
| if (editor == null || !editor.isValidPosition(position, true)) { | ||
| drawer.closeDrawer(GravityCompat.START) | ||
| return | ||
| } | ||
| editor.setSelection(position) | ||
| if (!drawer.isDrawerOpen(GravityCompat.START)) { | ||
| centerPositionInView(editor, position) | ||
| return | ||
| } | ||
| pendingScroll = editor to position | ||
| drawer.closeDrawer(GravityCompat.START) | ||
| } | ||
|
|
||
| private fun centerPositionInView( | ||
| editor: IDEEditor, | ||
| position: Position, | ||
| ) { | ||
| val rowY = editor.layout.getCharLayoutOffset(position.line, position.column)[0] | ||
|
Daniel-ADFA marked this conversation as resolved.
|
||
| val targetY = | ||
| (rowY - editor.height / 2f) | ||
| .toInt() | ||
| .coerceIn(0, editor.scrollMaxY) | ||
| editor.scroller.startScroll(editor.offsetX, editor.offsetY, 0, targetY - editor.offsetY, 0) | ||
| editor.postInvalidate() | ||
| } | ||
|
|
||
| private fun extensionOf(path: Path): String = path.fileName.toString().substringAfterLast('.', "") | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.