Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix: broken 'Close all files' behavior (fixes #1549)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsaky committed Dec 16, 2023
1 parent 3010fef commit 7aa4a8f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ import com.itsaky.androidide.preferences.internal.launchAppAfterInstall
import com.itsaky.androidide.projects.IProjectManager
import com.itsaky.androidide.projects.ProjectManagerImpl
import com.itsaky.androidide.tasks.cancelIfActive
import com.itsaky.androidide.ui.ContentTranslatingDrawerLayout
import com.itsaky.androidide.ui.CodeEditorView
import com.itsaky.androidide.ui.ContentTranslatingDrawerLayout
import com.itsaky.androidide.uidesigner.UIDesignerActivity
import com.itsaky.androidide.utils.ActionMenuUtils.createMenu
import com.itsaky.androidide.utils.ApkInstallationSessionCallback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.util.Collections

/** ViewModel for data used in [com.itsaky.androidide.activities.editor.EditorActivityKt] */
@Suppress("PropertyName")
Expand Down Expand Up @@ -126,42 +127,41 @@ class EditorViewModel : ViewModel() {
}

internal var files: MutableList<File>
get() = this._files.value ?: EMPTY_FILES
get() = this._files.value ?: Collections.emptyList()
set(value) {
this._files.value = value
}

companion object {

private val EMPTY_FILES = mutableListOf<File>()
private inline fun updateFiles(crossinline action: (files: MutableList<File>) -> Unit) {
val files = this.files
action(files)
this.files = files
}

/**
* Add the given file to the list of opened files.
*
* @param file The file that has been opened.
*/
fun addFile(file: File) {
fun addFile(file: File) = updateFiles { files ->
files.add(file)
this.files = files
}

/**
* Remove the file at the given index from the list of opened files.
*
* @param index The index of the closed file.
*/
fun removeFile(index: Int) {
fun removeFile(index: Int) = updateFiles { files ->
files.removeAt(index)
this.files = files

if (this.files.isEmpty()) {
if (files.isEmpty()) {
mCurrentFile.value = null
}
}

fun removeAllFiles() {
this.files = EMPTY_FILES
fun removeAllFiles() = updateFiles { files ->
files.clear()
setCurrentFile(-1, null)
}

Expand All @@ -170,9 +170,8 @@ class EditorViewModel : ViewModel() {
mCurrentFile.value = index to file
}

fun updateFile(index: Int, newFile: File) {
fun updateFile(index: Int, newFile: File) = updateFiles { files ->
files[index] = newFile
this.files = files
}

/**
Expand Down Expand Up @@ -200,7 +199,7 @@ class EditorViewModel : ViewModel() {
* @return The list of opened files.
*/
fun getOpenedFiles(): List<File> {
return files
return Collections.unmodifiableList(files)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import io.github.rosemoe.sora.widget.IDEEditorSearcher
import io.github.rosemoe.sora.widget.component.EditorAutoCompletion
import io.github.rosemoe.sora.widget.component.EditorBuiltinComponent
import io.github.rosemoe.sora.widget.component.EditorTextActionWindow
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -133,7 +134,7 @@ open class IDEEditor @JvmOverloads constructor(context: Context, attrs: Attribut
*
* All the jobs in this scope are cancelled when the editor is released.
*/
val editorScope = CoroutineScope(Dispatchers.Default)
val editorScope = CoroutineScope(Dispatchers.Default + CoroutineName("IDEEditor"))

protected val eventDispatcher = EditorEventDispatcher()

Expand Down Expand Up @@ -270,7 +271,7 @@ open class IDEEditor @JvmOverloads constructor(context: Context, attrs: Attribut
this.sigHelpCancelChecker = it
}

editorScope.launch {
editorScope.launch(Dispatchers.Default) {
cancelChecker.job = coroutineContext[Job]
val params = SignatureHelpParams(file.toPath(), cursorLSPPosition, cancelChecker)
val help = languageServer.signatureHelp(params)
Expand Down

0 comments on commit 7aa4a8f

Please sign in to comment.