Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import com.itsaky.androidide.models.Range
import com.itsaky.androidide.models.SaveResult
import com.itsaky.androidide.plugins.manager.fragment.PluginFragmentFactory
import com.itsaky.androidide.plugins.manager.ui.PluginEditorTabManager
import com.itsaky.androidide.preferences.internal.GeneralPreferences
import com.itsaky.androidide.projects.ProjectManagerImpl
import com.itsaky.androidide.projects.builder.BuildResult
import com.itsaky.androidide.tasks.executeAsync
Expand All @@ -75,7 +74,6 @@ import com.itsaky.androidide.utils.flashSuccess
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.adfa.constants.CONTENT_KEY
import org.greenrobot.eventbus.Subscribe
Expand Down Expand Up @@ -454,37 +452,35 @@ open class EditorHandlerActivity :
file: File,
selection: Range?,
) {
openFile(file, selection)
lifecycleScope.launch {
val editorView = openFile(file, selection)

getEditorForFile(file)?.editor?.also { editor ->
editor.postInLifecycle {
if (selection == null) {
editor.setSelection(0, 0)
return@postInLifecycle
editorView?.editor?.also { editor ->
editor.postInLifecycle {
if (selection == null) {
editor.setSelection(0, 0)
return@postInLifecycle
}
editor.validateRange(selection)
editor.setSelection(selection)
}

editor.validateRange(selection)
editor.setSelection(selection)
}
}
}

override fun openFile(
override suspend fun openFile(
file: File,
selection: Range?,
): CodeEditorView? {
): CodeEditorView? = withContext(Dispatchers.Main) {
val range = selection ?: Range.NONE
val isImage = runBlocking {
withContext(Dispatchers.IO) { ImageUtils.isImage(file) }
}

val isImage = withContext(Dispatchers.IO) { ImageUtils.isImage(file) }
if (isImage) {
openImage(this, file)
return null
openImage(this@EditorHandlerActivity, file)
return@withContext null
}

val fileIndex = openFileAndGetIndex(file, range)
if (fileIndex < 0) return null
if (fileIndex < 0) return@withContext null

editorViewModel.startDrawerOpened = false
editorViewModel.displayedFileIndex = fileIndex
Expand All @@ -495,14 +491,24 @@ open class EditorHandlerActivity :
tab.select()
}

return try {
return@withContext try {
getEditorAtIndex(fileIndex)
} catch (th: Throwable) {
log.error("Unable to get editor at file index {}", fileIndex, th)
null
}
}

fun openFileAsync(
file: File,
selection: Range? = null,
onResult: (CodeEditorView?) -> Unit
) {
lifecycleScope.launch {
onResult(openFile(file, selection))
}
}

override fun openFileAndGetIndex(
file: File,
selection: Range?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.itsaky.androidide.handlers

import android.content.Context
import androidx.core.view.GravityCompat
import androidx.lifecycle.lifecycleScope
import com.itsaky.androidide.actions.ActionData
import com.itsaky.androidide.actions.ActionItem.Location.EDITOR_FILE_TREE
import com.itsaky.androidide.actions.ActionMenu
Expand All @@ -36,6 +37,7 @@ import com.itsaky.androidide.idetooltips.TooltipManager
import com.itsaky.androidide.models.SheetOption
import com.itsaky.androidide.utils.flashError
import com.unnamed.b.atv.model.TreeNode
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode.MAIN
Expand Down Expand Up @@ -87,7 +89,9 @@ class FileTreeActionHandler : BaseEventHandler() {
return
}

context.openFile(event.file)
context.lifecycleScope.launch {
context.openFile(event.file)
}
}

@Subscribe(threadMode = MAIN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ interface IEditorHandler {
fun getEditorAtIndex(index: Int) : CodeEditorView?
fun getEditorForFile(file: File) : CodeEditorView?

fun openFile(file: File) : CodeEditorView? = openFile(file, null)
fun openFile(file: File, selection: Range?) : CodeEditorView?
suspend fun openFile(file: File) : CodeEditorView? = openFile(file, null)
suspend fun openFile(file: File, selection: Range?) : CodeEditorView?
fun openFileAndSelect(file: File, selection: Range?)
fun openFileAndGetIndex(file: File, selection: Range?) : Int

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,12 @@ private Boolean applyActionEdits(@Nullable final IDEEditor editor, final CodeAct
} else {
// Edit is in some other file which is not opened
// open that file and perform the edit
openedFrag = activity.openFile(file);
if (openedFrag != null && openedFrag.getEditor() != null) {
editInEditor(openedFrag.getEditor(), edit);
}
activity.openFileAsync(file, null, openedEditor -> {
if (openedEditor != null && openedEditor.getEditor() != null) {
editInEditor(openedEditor.getEditor(), edit);
}
return kotlin.Unit.INSTANCE;
});
}
}
}
Expand Down