diff --git a/common/src/main/java/com/itsaky/androidide/tasks/coroutineUtils.kt b/common/src/main/java/com/itsaky/androidide/tasks/coroutineUtils.kt index 1505d54a07..3ec4dae548 100644 --- a/common/src/main/java/com/itsaky/androidide/tasks/coroutineUtils.kt +++ b/common/src/main/java/com/itsaky/androidide/tasks/coroutineUtils.kt @@ -21,6 +21,7 @@ import com.itsaky.androidide.flashbar.Flashbar import com.itsaky.androidide.progress.ICancelChecker import com.itsaky.androidide.utils.flashProgress import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.InternalCoroutinesApi import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.Dispatchers @@ -38,9 +39,30 @@ import kotlin.coroutines.EmptyCoroutineContext * An [ICancelChecker] which when cancelled, cancels the corresponding [Job]. */ class JobCancelChecker @JvmOverloads constructor( - var job: Job? = null + job: Job? = null ) : ICancelChecker.Default() { + /** + * The job this checker follows. Cancelling the job cancels the checker, so + * [isCancelled] and [invokeOnCancel] listeners see cancellations that did not + * originate from [cancel]. + */ + var job: Job? = null + set(value) { + field = value + // onCancelling: the analysis to abort runs synchronously inside the job body, + // so waiting for completion would signal only after that work finished. + // Also fires on failure, which is intended: a failed job's work is over. + @OptIn(InternalCoroutinesApi::class) + value?.invokeOnCompletion(onCancelling = true, invokeImmediately = true) { cause -> + if (cause != null) cancel() + } + } + + init { + this.job = job + } + override fun cancel() { job?.cancel("Cancelled by user") job = null @@ -48,7 +70,8 @@ class JobCancelChecker @JvmOverloads constructor( } override fun abortIfCancelled() { - job?.ensureActive() + // cancel() drops the job reference, so fall back to the flag once it is gone. + job?.ensureActive() ?: super.abortIfCancelled() } } diff --git a/common/src/test/java/com/itsaky/androidide/tasks/JobCancelCheckerTest.kt b/common/src/test/java/com/itsaky/androidide/tasks/JobCancelCheckerTest.kt new file mode 100644 index 0000000000..805d479ae9 --- /dev/null +++ b/common/src/test/java/com/itsaky/androidide/tasks/JobCancelCheckerTest.kt @@ -0,0 +1,118 @@ +package com.itsaky.androidide.tasks + +import com.google.common.truth.Truth.assertThat +import kotlinx.coroutines.CancellationException +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.NonCancellable +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext +import org.junit.Assert.assertThrows +import org.junit.Test + +class JobCancelCheckerTest { + @Test + fun `cancelling the constructor job cancels the checker and fires listeners`() { + val job = Job() + val checker = JobCancelChecker(job) + var fired = false + checker.invokeOnCancel { fired = true } + + job.cancel() + + assertThat(checker.isCancelled()).isTrue() + assertThat(fired).isTrue() + } + + @Test + fun `cancelling a job assigned after construction cancels the checker`() { + val checker = JobCancelChecker() + val job = Job() + checker.job = job + + job.cancel() + + assertThat(checker.isCancelled()).isTrue() + } + + @Test + fun `a job that completes normally does not cancel the checker`() { + val job = Job() + val checker = JobCancelChecker(job) + + job.complete() + + assertThat(checker.isCancelled()).isFalse() + } + + @Test + fun `a cancelled job that is still running reports cancelled before it completes`() = + runBlocking { + val parent = Job() + val gate = runUncancellableChild(parent) + val checker = JobCancelChecker(parent) + var fired = false + checker.invokeOnCancel { fired = true } + + parent.cancel() + + assertThat(parent.isCompleted).isFalse() + assertThat(checker.isCancelled()).isTrue() + assertThat(fired).isTrue() + gate.complete(Unit) + } + + @Test + fun `a job assigned later that is cancelled while still running reports cancelled before it completes`() = + runBlocking { + val parent = Job() + val gate = runUncancellableChild(parent) + val checker = JobCancelChecker() + var fired = false + checker.invokeOnCancel { fired = true } + checker.job = parent + + parent.cancel() + + assertThat(parent.isCompleted).isFalse() + assertThat(checker.isCancelled()).isTrue() + assertThat(fired).isTrue() + gate.complete(Unit) + } + + @Test + fun `abortIfCancelled throws after the job is cancelled`() { + val job = Job() + val checker = JobCancelChecker(job) + + job.cancel() + + assertThrows(CancellationException::class.java) { checker.abortIfCancelled() } + } + + @Test + fun `abortIfCancelled throws after the checker is cancelled`() { + val checker = JobCancelChecker(Job()) + + checker.cancel() + + assertThrows(CancellationException::class.java) { checker.abortIfCancelled() } + } + + /** A plain child Job completes as soon as its parent is cancelled, so use a body that ignores cancellation. */ + private suspend fun runUncancellableChild(parent: Job): CompletableDeferred { + val gate = CompletableDeferred() + val started = CompletableDeferred() + CoroutineScope(parent + Dispatchers.Default).launch { + withContext(NonCancellable) { + started.complete(Unit) + gate.await() + } + } + started.await() + return gate + } +} diff --git a/common/src/test/java/com/itsaky/androidide/utils/KeyedDebouncingActionTest.kt b/common/src/test/java/com/itsaky/androidide/utils/KeyedDebouncingActionTest.kt new file mode 100644 index 0000000000..5b7863f8f3 --- /dev/null +++ b/common/src/test/java/com/itsaky/androidide/utils/KeyedDebouncingActionTest.kt @@ -0,0 +1,44 @@ +package com.itsaky.androidide.utils + +import com.google.common.truth.Truth.assertThat +import kotlinx.coroutines.CompletableDeferred +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.cancel +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeout +import org.junit.Test +import kotlin.time.Duration.Companion.milliseconds + +class KeyedDebouncingActionTest { + @Test + fun `rescheduling the same key cancels the running action's checker`() = + runBlocking { + val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default) + val started = CompletableDeferred() + val cancelled = CompletableDeferred() + val debouncer = + KeyedDebouncingAction( + scope = scope, + debounceDuration = 10.milliseconds, + action = { _, checker -> + checker.invokeOnCancel { cancelled.complete(Unit) } + started.complete(Unit) + awaitCancellation() + }, + ) + + try { + debouncer.schedule("a") + withTimeout(2_000) { started.await() } + debouncer.schedule("a") + + withTimeout(2_000) { cancelled.await() } + assertThat(cancelled.isCompleted).isTrue() + } finally { + scope.cancel() + } + } +} diff --git a/docs/adr/0011-command-analysis-priority.md b/docs/adr/0011-command-analysis-priority.md index 5db2e7ed62..8cab8cd3ad 100644 --- a/docs/adr/0011-command-analysis-priority.md +++ b/docs/adr/0011-command-analysis-priority.md @@ -37,6 +37,8 @@ INDEXING < DIAGNOSTICS < COMMAND < INTERACTIVE - Keystroke-driven features (completion, signature help) stay on `INTERACTIVE` and therefore still win against a command. - A command preempted by `INTERACTIVE` retries. Long-running commands take their session **per unit of work** - for find usages, per candidate file - so a preemption costs one file, not the whole request. +> **Update (2026-11-13, ADFA-6855):** `INTERACTIVE` is no longer the only thing that preempts a command. Before an edit to an open Kotlin document is applied, `AnalysisScheduler.preemptingAnalysis` raises a barrier that preempts the current holder *whatever its priority* and turns away every waiting or new requester with `AnalysisPreemptedException` until the edit's `project.write` is done. So a command, and an `INTERACTIVE` request too, can be preempted by an edit to any open file. The retry rule above therefore applies to every user-invoked request, including the `INTERACTIVE` refactoring planners (extract variable, extract method, inline variable), which now wrap their pipeline in `retryingOnPreemption`. + ## Consequences **Positive** @@ -48,7 +50,7 @@ INDEXING < DIAGNOSTICS < COMMAND < INTERACTIVE **Negative / costs** -- Commands still need a retry policy, because `INTERACTIVE` outranks them. The retry is one line at each call site and already proven in `findDefinitionAt`, but it is a rule every future command has to remember. +- Commands still need a retry policy, because `INTERACTIVE` outranks them and an edit's barrier preempts every priority (see the update under Decision). The retry is one line at each call site and already proven in `findDefinitionAt`, but it is a rule every future command has to remember. - Four tiers instead of three is more scheduler surface to reason about. - Background diagnostics now lose to any command, so a long search delays diagnostics for its duration. Acceptable: diagnostics are re-queued, never discarded. diff --git a/docs/adr/0015-one-pinned-ktfile-per-analysis.md b/docs/adr/0015-one-pinned-ktfile-per-analysis.md index ac52d16947..6acac6695e 100644 --- a/docs/adr/0015-one-pinned-ktfile-per-analysis.md +++ b/docs/adr/0015-one-pinned-ktfile-per-analysis.md @@ -73,7 +73,8 @@ declarations belong to the Analysis API. Marking the implementing class instead classifier is ever consulted. Gating the sink catches every such route at the one point they all arrive at. `analyzeMaybeDangling` has three production opt-ins: `PinnedKtFile.analyzing` and `analyzingVariant` (the sanctioned implementation) and `SourceFileIndexer.indexSourceFile` - the known exception, reached when -`refreshToCurrent` hands a freshly minted instance to `queueOnFileChangedAsync`, which carries the raw `KtFile` +`refreshToCurrent` hands the document's `KtFile` (freshly parsed, or since ADFA-6855 the same instance edited in +place) to `queueOnFileChangedAsync`, which carries the raw `KtFile` through `IndexCommand.IndexModifiedFile` and analyses it with no pin (pre-existing, tracked as a follow-up). What stays convention rather than compiler-enforced is the Analysis API's own `analyze` / `analyzeCopy`: both are @@ -95,6 +96,13 @@ whatever the current-file cache holds, which lags the buffer by however long the hand offsets from possibly-stale PSI into an edit. The behaviour is unchanged by this ADR's change and the fix is tracked separately; widening the hatch to a second caller has to weigh that, not just the analysis argument. +> **Update (2026-11-13, ADFA-6855):** "PSI-only, so no lock needed" no longer holds. An open document now keeps one +> `KtFile` that every refresh edits in place under `project.write` (`IncrementalKtDocument`), so an unpinned reader +> without `project.read` can see a half-applied edit - a torn tree, an exception, or a wrong import offset. The +> handler now computes its edits from the instance inside `project.read` and applies them to the editor after +> releasing it; the `UnpinnedKtFileAccess` KDoc states the rule for any future caller. The staleness gap above is +> unchanged. + ## Consequences **Positive** @@ -137,8 +145,10 @@ tracked separately; widening the hatch to a second caller has to weigh that, not the check undetected and is equally unsafe. - A narrow window remains between resolving an instance and installing its pin (documented on `withLiveKtFile`): a request arriving in that window sees no pin yet and can launch a refresh that completes inside the scope, - firing a FIR modification event under it. Instance identity still holds through every door - the pin is stamped - with the resolved instance's own version, so the bump is not lost, only deferred. Closing the window fully would + firing a FIR modification event under it. Since ADFA-6855 only a full parse with no document yet (the path's + first parse) can land there: the pin holds the document's edit read stamp, so in-place edits wait for the + release. Instance identity still holds through every door - the pin is stamped with the locked document's + applied version, so the bump is not lost, only deferred. Closing the window fully would mean publishing a pin before its file exists, making joiners wait on an unresolved entry in the one path every caller depends on; that deadlock risk was judged worse than the window. @@ -155,6 +165,11 @@ tracked separately; widening the hatch to a second caller has to weigh that, not regression fix. It would also still be a construction property a later refactor could quietly undo, rather than something the compiler holds; the team chose the type gate instead and did not schedule in-place reparse as a follow-up. + + > **Update (2026-11-13, ADFA-6855):** superseded - in-place reparse was later taken, for performance rather than + > as the identity fix, and it works in this environment (`IncrementalKtDocument`, via `BlockSupportImpl.reparse` + > and `DiffLog.doActualPsiChange`). It adds to the type gate rather than replacing it: the pin and the three + > markers still hold, and a failed in-place reparse still falls back to a fresh instance. - **A custom lint/detekt rule banning the raw accessors** - the build has no detekt; Spotless's ktlint integration only formats, it does not carry custom semantic rules, so there is no rule seat to put this in. diff --git a/docs/features/kotlin-find-usages.md b/docs/features/kotlin-find-usages.md index 67a23f9489..4b468578fa 100644 --- a/docs/features/kotlin-find-usages.md +++ b/docs/features/kotlin-find-usages.md @@ -142,7 +142,7 @@ There is **no result cap**. See R10 for why one is not needed. Granularity is per candidate file, and it is load-bearing: -- **One analysis session per candidate file.** A preemption by completion costs one file's work, which is retried once - `findDefinitionAt`'s pattern. The target-resolution phase is retried twice over, because losing *it* loses the whole search rather than one file (R12). One session for the whole search would let a single keystroke discard a whole-workspace scan. A file preempted *twice* is dropped like any other failed candidate (R12), not rethrown: keystroke-driven work winning the lock must not turn a search with plenty of hits into "no references". +- **One analysis session per candidate file.** A preemption - by completion, or by the barrier an edit to any open Kotlin document raises before applying it (ADFA-6855) - costs one file's work, which is retried once - `findDefinitionAt`'s pattern. The target-resolution phase is retried twice over, because losing *it* loses the whole search rather than one file (R12). One session for the whole search would let a single keystroke discard a whole-workspace scan. A file preempted *twice* is dropped like any other failed candidate (R12), not rethrown: keystroke-driven work or an edit winning the lock must not turn a search with plenty of hits into "no references". - **`project.read` per candidate file, never once for the search.** A whole-workspace search holding the read lock start to finish would block every `project.write`, which is what index refresh needs. - **The live-document await stays outside `project.read`.** The refresh it waits on needs `project.write`; awaiting it under the read lock deadlocks. Go-to-definition's R10 records the same constraint. - `params.cancelChecker` is honoured per prefiltered file, between candidate files **and** between references within a file. The prefilter checks it per file rather than once for the pass: a whole-workspace scan is seconds of I/O, and cancelling has to stop it rather than let it finish and discard the result. diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/actions/ExtractMethodAction.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/actions/ExtractMethodAction.kt index bb2f65644a..a34872971b 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/actions/ExtractMethodAction.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/actions/ExtractMethodAction.kt @@ -8,7 +8,6 @@ import com.itsaky.androidide.actions.requireEditor import com.itsaky.androidide.actions.requireFile import com.itsaky.androidide.idetooltips.TooltipTag import com.itsaky.androidide.lsp.kotlin.KotlinLanguageServer -import com.itsaky.androidide.lsp.kotlin.compiler.modules.ScheduledCancelChecker import com.itsaky.androidide.lsp.kotlin.refactor.KOTLIN_NAME_MESSAGES import com.itsaky.androidide.lsp.kotlin.refactor.candidateFor import com.itsaky.androidide.lsp.kotlin.refactor.toMethodCandidateViews @@ -78,7 +77,7 @@ class ExtractMethodAction : BaseKotlinCodeAction() { selectionEnd = maxOf(cursor.left, cursor.right), documentVersion = documentVersionOf(nioPath), // Ties the analysis to this action's coroutine: cancelling the action aborts the analysis. - cancelChecker = ScheduledCancelChecker(createJobCancelChecker()), + cancelChecker = createJobCancelChecker(), ) } diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/actions/ExtractVariableAction.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/actions/ExtractVariableAction.kt index 3fb178e44f..e8cb7f407f 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/actions/ExtractVariableAction.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/actions/ExtractVariableAction.kt @@ -7,7 +7,6 @@ import com.itsaky.androidide.actions.requireEditor import com.itsaky.androidide.actions.requireFile import com.itsaky.androidide.idetooltips.TooltipTag import com.itsaky.androidide.lsp.kotlin.KotlinLanguageServer -import com.itsaky.androidide.lsp.kotlin.compiler.modules.ScheduledCancelChecker import com.itsaky.androidide.lsp.kotlin.refactor.KOTLIN_NAME_MESSAGES import com.itsaky.androidide.lsp.kotlin.refactor.candidateAndScopeFor import com.itsaky.androidide.lsp.kotlin.refactor.toCandidateViews @@ -74,7 +73,7 @@ class ExtractVariableAction : BaseKotlinCodeAction() { selectionEnd = selectionEnd, documentVersion = documentVersionOf(nioPath), // Ties the analysis to this action's coroutine: cancelling the action aborts the analysis. - cancelChecker = ScheduledCancelChecker(createJobCancelChecker()), + cancelChecker = createJobCancelChecker(), ) } diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/actions/InlineVariableAction.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/actions/InlineVariableAction.kt index 15d20080fa..811b67f882 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/actions/InlineVariableAction.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/actions/InlineVariableAction.kt @@ -8,7 +8,6 @@ import com.itsaky.androidide.actions.requireEditor import com.itsaky.androidide.actions.requireFile import com.itsaky.androidide.idetooltips.TooltipTag import com.itsaky.androidide.lsp.kotlin.KotlinLanguageServer -import com.itsaky.androidide.lsp.kotlin.compiler.modules.ScheduledCancelChecker import com.itsaky.androidide.lsp.kotlin.refactor.ui.InlineVariableSheet import com.itsaky.androidide.lsp.kotlin.utils.refactor.InlineMode import com.itsaky.androidide.lsp.kotlin.utils.refactor.InlineRefusal @@ -77,7 +76,7 @@ class InlineVariableAction : BaseKotlinCodeAction() { offset = minOf(cursor.left, cursor.right), documentVersion = documentVersionOf(nioPath), // Ties the analysis to this action's coroutine: cancelling the action aborts the analysis. - cancelChecker = ScheduledCancelChecker(createJobCancelChecker()), + cancelChecker = createJobCancelChecker(), ) } diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/AbstractCompilationEnvironment.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/AbstractCompilationEnvironment.kt index fb7dd401c5..a549df5b17 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/AbstractCompilationEnvironment.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/AbstractCompilationEnvironment.kt @@ -2,6 +2,7 @@ package com.itsaky.androidide.lsp.kotlin.compiler import com.itsaky.androidide.lsp.kotlin.compiler.index.KtSymbolIndex import com.itsaky.androidide.lsp.kotlin.compiler.modules.AbstractKtModule +import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPreemptedException import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPriority import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisScheduler import com.itsaky.androidide.lsp.kotlin.compiler.modules.KtModule @@ -10,6 +11,7 @@ import com.itsaky.androidide.lsp.kotlin.compiler.modules.isSourceModule import com.itsaky.androidide.lsp.kotlin.compiler.services.JavaModuleAccessibilityChecker import com.itsaky.androidide.lsp.kotlin.compiler.services.JavaModuleAnnotationsProvider import com.itsaky.androidide.lsp.kotlin.compiler.services.KtLspService +import com.itsaky.androidide.lsp.kotlin.compiler.services.NoFormattingCodeStyleManager import com.itsaky.androidide.lsp.kotlin.compiler.services.ProjectStructureProvider import com.itsaky.androidide.lsp.kotlin.compiler.services.ResolutionScopeProvider import com.itsaky.androidide.lsp.kotlin.compiler.services.WriteAccessGuard @@ -73,8 +75,14 @@ import org.jetbrains.kotlin.com.intellij.openapi.editor.impl.DocumentWriteAccess import org.jetbrains.kotlin.com.intellij.openapi.roots.PackageIndex import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer import org.jetbrains.kotlin.com.intellij.openapi.vfs.VirtualFileManager +import org.jetbrains.kotlin.com.intellij.pom.PomModel +import org.jetbrains.kotlin.com.intellij.pom.core.impl.PomModelImpl +import org.jetbrains.kotlin.com.intellij.pom.tree.TreeAspect import org.jetbrains.kotlin.com.intellij.psi.ClassTypePointerFactory import org.jetbrains.kotlin.com.intellij.psi.PsiManager +import org.jetbrains.kotlin.com.intellij.psi.PsiTreeChangeAdapter +import org.jetbrains.kotlin.com.intellij.psi.PsiTreeChangeListener +import org.jetbrains.kotlin.com.intellij.psi.codeStyle.CodeStyleManager import org.jetbrains.kotlin.com.intellij.psi.impl.file.impl.JavaFileManager import org.jetbrains.kotlin.com.intellij.psi.impl.smartPointers.PsiClassReferenceTypePointerFactory import org.jetbrains.kotlin.com.intellij.psi.search.ProjectScope @@ -344,6 +352,16 @@ internal abstract class AbstractCompilationEnvironment( MetadataFinderFactory::class.java, CliMetadataFinderFactory(fileFinderFactory), ) + if (!extensionArea.hasExtensionPoint(PsiTreeChangeListener.EP.name)) { + CoreApplicationEnvironment.registerExtensionPoint( + extensionArea, + PsiTreeChangeListener.EP.name, + PsiTreeChangeAdapter::class.java, + ) + } + registerService(TreeAspect::class.java) + registerService(PomModel::class.java, PomModelImpl::class.java) + registerService(CodeStyleManager::class.java, NoFormattingCodeStyleManager(this)) } setupServices(libraryRoots) @@ -468,8 +486,14 @@ internal abstract class AbstractCompilationEnvironment( val deadline = System.nanoTime() + DISPOSE_WAIT.inWholeNanoseconds while (true) { val disposed = - AnalysisScheduler.withLock(AnalysisPriority.INTERACTIVE, ICancelChecker.NOOP, onPreempt = {}) { - project.tryWrite { Disposer.dispose(disposable) } + try { + AnalysisScheduler.withLock(AnalysisPriority.INTERACTIVE, ICancelChecker.NOOP, onPreempt = {}) { + project.tryWrite { Disposer.dispose(disposable) } + } + } catch (_: AnalysisPreemptedException) { + // An edit's barrier (any environment's: the scheduler is process-wide) turns every requester + // away; it is brief, so treat it as a miss rather than abandon disposal. + false } if (disposed) return diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IncrementalKtDocument.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IncrementalKtDocument.kt new file mode 100644 index 0000000000..d58b5b7ae9 --- /dev/null +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IncrementalKtDocument.kt @@ -0,0 +1,160 @@ +package com.itsaky.androidide.lsp.kotlin.compiler.index + +import androidx.annotation.VisibleForTesting +import org.jetbrains.kotlin.com.intellij.openapi.command.CommandProcessor +import org.jetbrains.kotlin.com.intellij.openapi.progress.EmptyProgressIndicator +import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtil +import org.jetbrains.kotlin.com.intellij.psi.impl.BlockSupportImpl +import org.jetbrains.kotlin.com.intellij.psi.impl.ChangedPsiRangeUtil +import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.FileElement +import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.com.intellij.testFramework.LightVirtualFile +import org.jetbrains.kotlin.psi.KtClassOrObject +import org.jetbrains.kotlin.psi.KtElement +import org.jetbrains.kotlin.psi.KtFile +import java.util.concurrent.CancellationException +import java.util.concurrent.locks.StampedLock + +/** + * The single [KtFile] of one open Kotlin document, kept across versions by applying each new text + * to it in place. + * + * @property ktFile The instance every version of the document is applied to. + */ +internal class IncrementalKtDocument( + val ktFile: KtFile, + appliedVersion: Int, +) { + /** The document version [ktFile]'s text reflects. */ + @Volatile + var appliedVersion: Int = appliedVersion + private set + + /** + * Pins hold a read stamp; an edit takes the write stamp with tryWriteLock only. + * + * A [StampedLock] rather than a ReentrantReadWriteLock because a pin is joined and released on + * different threads, and stamps are not bound to the thread that took them. + */ + val editLock = StampedLock() + + /** Replaces the in-place reparse in [apply]; set only by tests to force an outcome. */ + @VisibleForTesting + @Volatile + internal var reparseOverride: ((CharSequence) -> ReparseOutcome)? = null + + /** + * Advances [appliedVersion] to [version] if [text] is already [ktFile]'s text, and returns whether + * it did. + * + * Caller holds the [editLock] write stamp, which excludes every other edit, so this needs neither + * project.write nor the analysis barrier: a version bump that changes nothing preempts nothing. + */ + fun advanceIfUnchanged( + text: CharSequence, + version: Int, + ): Boolean { + if (!(ktFile.node as FileElement).textMatches(text)) return false + appliedVersion = version + return true + } + + /** + * Applies [text], the document at [version], to [ktFile] in place. + * + * Caller holds project.write and runs inside runWriteAction. On any outcome but + * [ReparseOutcome.Failed], [appliedVersion] becomes [version]; on [ReparseOutcome.Failed] the tree + * may be partly edited and the caller must stop using [ktFile]. + */ + fun apply( + text: CharSequence, + version: Int, + ): ReparseOutcome { + val outcome = + try { + reparseOverride?.invoke(text) ?: reparseInPlace(text) + } catch (e: CancellationException) { + throw e + } catch (e: Throwable) { + ReparseOutcome.Failed("in-place reparse threw ${e.javaClass.name}", e) + } + if (outcome !is ReparseOutcome.Failed) { + appliedVersion = version + } + return outcome + } + + private fun reparseInPlace(text: CharSequence): ReparseOutcome { + val fileNode = ktFile.node as FileElement + val range = ChangedPsiRangeUtil.getChangedPsiRange(ktFile, fileNode, text) ?: return ReparseOutcome.Unchanged + val viewProvider = ktFile.viewProvider + val result = + BlockSupportImpl.reparse(ktFile, fileNode, range, text, EmptyProgressIndicator(), viewProvider.contents) + val oldRoot = result.oldRoot + // The diff below moves the new children into oldRoot, so its old contents are only readable now. + val replacedHadClassOrObject = + oldRoot !is FileElement && PsiTreeUtil.findChildOfType(oldRoot.psi, KtClassOrObject::class.java) != null + // The contents go first, as in IntelliJ's own commit (document, then reparse): applying the PSI + // change checks the tree against the view provider's contents and reports a mismatch as an error + // carrying the whole file. doActualPsiChange does not update the contents itself. On a failure + // below the caller drops this KtFile, so contents ahead of the tree are never read. + (viewProvider.virtualFile as LightVirtualFile).setContent(null, text, false) + syncDocument(text) + // PomModelImpl refuses to change physical PSI outside a command. CoreCommandProcessor swallows + // whatever the runnable throws, so the text check below is the only reliable failure signal. + CommandProcessor.getInstance().executeCommand( + ktFile.project, + { result.log.doActualPsiChange(ktFile) }, + null, + null, + ) + if (!(ktFile.node as FileElement).textMatches(text)) { + return ReparseOutcome.Failed("tree text does not match the document after the reparse", null) + } + + if (oldRoot is FileElement) return ReparseOutcome.WholeFile + // A reparseable node outside the Kotlin PSI (KDoc) has no KtElement to report; the whole file is + // the conservative answer. + return (oldRoot.psi as? KtElement)?.let { ReparseOutcome.InBlock(it, replacedHadClassOrObject) } ?: ReparseOutcome.WholeFile + } + + /** + * The view provider caches a Document once anything asks for one (the PSI change itself does), and + * that Document, not the light file, is what the provider's contents and offset-to-line mapping read. + * Nothing syncs it from the PSI, so it is set here; when none is cached, the one created below + * already reads the new light-file content. + */ + private fun syncDocument(text: CharSequence) { + val document = ktFile.viewProvider.document ?: return + if (!StringUtil.equals(document.immutableCharSequence, text)) { + document.setText(text) + } + } +} + +/** What [IncrementalKtDocument.apply] did to the document's [KtFile]. */ +internal sealed interface ReparseOutcome { + /** The text already matched the tree; nothing changed. */ + data object Unchanged : ReparseOutcome + + /** + * Only [reparsed], a reparseable node still attached to the file, was replaced. + * + * @property reparsed The replaced node, now holding the new children. + * @property replacedHadClassOrObject Whether the children it held before the edit included a class + * or object declaration. + */ + data class InBlock( + val reparsed: KtElement, + val replacedHadClassOrObject: Boolean, + ) : ReparseOutcome + + /** The file element itself was reparsed, though the [KtFile] instance is kept. */ + data object WholeFile : ReparseOutcome + + /** The edit could not be applied in place; the caller falls back to a full parse. */ + data class Failed( + val reason: String, + val cause: Throwable?, + ) : ReparseOutcome +} diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorker.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorker.kt index 80e267aa49..c9211faae5 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorker.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IndexWorker.kt @@ -1,5 +1,6 @@ package com.itsaky.androidide.lsp.kotlin.compiler.index +import androidx.annotation.VisibleForTesting import com.itsaky.androidide.lsp.kotlin.compiler.CompilationEnvironment import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPreemptedException import com.itsaky.androidide.lsp.kotlin.compiler.modules.backingFilePath @@ -282,7 +283,13 @@ internal class IndexWorker( private fun beginIndexPhase() = Memprof.beginPhase("Index Kotlin sources", "source_index_complete") + /** Sees every command [submitCommand] receives, before it is queued; set only by tests. */ + @VisibleForTesting + @Volatile + internal var commandIntakeObserver: ((IndexCommand) -> Unit)? = null + suspend fun submitCommand(cmd: IndexCommand) { + commandIntakeObserver?.invoke(cmd) when (cmd) { is IndexCommand.ScanSourceFile, is IndexCommand.SourceScanningStarted, diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/KtSymbolIndex.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/KtSymbolIndex.kt index f2a738517a..a870fd4307 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/KtSymbolIndex.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/KtSymbolIndex.kt @@ -4,6 +4,7 @@ import androidx.annotation.VisibleForTesting import com.github.benmanes.caffeine.cache.Caffeine import com.itsaky.androidide.lsp.kotlin.compiler.CompilationKind import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPriority +import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisScheduler import com.itsaky.androidide.lsp.kotlin.compiler.modules.KtModule import com.itsaky.androidide.lsp.kotlin.compiler.modules.ScheduledCancelChecker import com.itsaky.androidide.lsp.kotlin.compiler.modules.UnpinnedAnalysis @@ -33,6 +34,8 @@ import org.checkerframework.checker.index.qual.NonNegative import org.jetbrains.kotlin.analysis.api.KaSession import org.jetbrains.kotlin.analysis.api.platform.modification.KaElementModificationType import org.jetbrains.kotlin.analysis.api.platform.modification.KaSourceModificationService +import org.jetbrains.kotlin.analysis.low.level.api.fir.LLFirInternals +import org.jetbrains.kotlin.analysis.low.level.api.fir.file.structure.LLFirDeclarationModificationService import org.jetbrains.kotlin.analysis.low.level.api.fir.util.originalKtFile import org.jetbrains.kotlin.com.intellij.openapi.application.ApplicationManager import org.jetbrains.kotlin.com.intellij.openapi.project.Project @@ -40,7 +43,9 @@ import org.jetbrains.kotlin.com.intellij.openapi.vfs.VirtualFile import org.jetbrains.kotlin.com.intellij.psi.PsiManager import org.jetbrains.kotlin.com.intellij.psi.impl.PsiManagerEx import org.jetbrains.kotlin.com.intellij.psi.impl.file.impl.FileManagerImpl +import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.psi.KtClassOrObject import org.jetbrains.kotlin.psi.KtElement import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtPsiFactory @@ -52,6 +57,7 @@ import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ExecutorService import java.util.concurrent.Executors import java.util.concurrent.TimeUnit +import java.util.concurrent.locks.StampedLock import kotlin.io.path.pathString /** @@ -86,7 +92,9 @@ internal class KtSymbolIndex( } private val workerQueue = WorkerQueue() - private val indexWorker = + + @VisibleForTesting + internal val indexWorker = IndexWorker( project = project, queue = workerQueue, @@ -138,6 +146,13 @@ internal class KtSymbolIndex( /** path -> last-launched version; read/written only inside that same `compute` section. */ private val currentVersions = ConcurrentHashMap() + /** path -> the one [KtFile] an open document keeps across versions, edited in place. */ + private val documents = ConcurrentHashMap() + + /** The in-place document held for [path], if it is open and has been parsed. */ + @VisibleForTesting + internal fun incrementalDocument(path: Path): IncrementalKtDocument? = documents[path] + /** * path -> the instance pinned for the duration of one or more open [LiveKtFile] scopes. * @@ -153,9 +168,15 @@ internal class KtSymbolIndex( */ private val pins = ConcurrentHashMap() + /** + * @property stamp The read stamp held on [editLock] for the pin's lifetime, so no in-place edit + * changes [file] under it; `0L` with no lock for a disk file. + */ private class Pin( val file: KtFile, val version: Int, + val editLock: StampedLock?, + val stamp: Long, ) { var count: Int = 0 @@ -276,7 +297,13 @@ internal class KtSymbolIndex( val version = doc.version val future = currentFiles.compute(path) { p, existing -> - if (existing != null && !existing.isCompletedExceptionally && currentVersions[p] == version) { + // The failure check runs last so a refresh that fails while the others are evaluated is relaunched. + val reusable = + existing != null && + currentVersions[p] == version && + !existing.isDeferredBelow(version) && + !existing.isCompletedExceptionally + if (reusable) { existing } else { currentVersions[p] = version @@ -297,18 +324,125 @@ internal class KtSymbolIndex( } /** - * Parses [path]'s live document into a fresh [KtFile], registers it as the in-memory file, and - * transitions the module's FIR session (invalidate + reindex) so later analysis sees the content. + * A refresh that found its document locked by a pin settles at the older applied version; it must + * not count as the refresh for the version it was launched for. A failed refresh has no value, so + * it does not count as deferred; the caller's own failure check relaunches it. + */ + private fun CompletableFuture.isDeferredBelow(version: Int): Boolean = + valueNowOrNull()?.let { it.version != version } ?: false + + /** + * Brings [path]'s [IncrementalKtDocument] to the live document and transitions the module's FIR + * session (invalidate + reindex) so later analysis sees the content. + * + * The text is applied to the document's one [KtFile] in place. A path with no document yet, or an + * edit that cannot be applied in place, takes a full parse into a fresh [KtFile] instead. A pinned + * document is not edited: the edit is owed to the pin's release and the applied version returned. * * The result is stamped with [version] (captured when the refresh was launched) even though the * content is read later, here. FileManager writes version-before-content unsynchronized, so the * stamp may lag the content but never lead it: callers never get older-than-requested content, and - * a lagging stamp only costs one redundant re-parse on the next request. + * a lagging stamp only costs one redundant refresh on the next request. */ private fun refreshToCurrent( path: Path, version: Int, old: KtFile?, + ): VersionedKtFile { + // A document whose file is not the one this refresh succeeds was left behind by a close racing a + // refresh; editing it in place would skip the in-memory registration the close undid. + val doc = + documents[path]?.takeIf { old != null && it.ktFile === old } + ?: return parseIntoNewDocument(path, version, old) + val stamp = doc.editLock.tryWriteLock() + if (stamp == 0L) { + // Never wait for the pin: it can be held across a whole analysis. Its release refreshes again. + pins[path]?.refreshOwed = true + return VersionedKtFile(doc.appliedVersion, doc.ktFile) + } + try { + val content = FileManager.getDocumentContents(path) + if (doc.advanceIfUnchanged(content, version)) { + return VersionedKtFile(version, doc.ktFile) + } + val outcome = applyInPlace(doc, content, version) + if (outcome is ReparseOutcome.Failed) { + logger.warn("in-place reparse of {} failed, parsing in full: {}", path, outcome.reason, outcome.cause) + return parseIntoNewDocument(path, version, doc.ktFile) + } + return VersionedKtFile(version, doc.ktFile) + } finally { + doc.editLock.unlockWrite(stamp) + } + } + + private fun applyInPlace( + doc: IncrementalKtDocument, + content: CharSequence, + version: Int, + ): ReparseOutcome = + AnalysisScheduler.preemptingAnalysis { + project.write { + val outcome = + ApplicationManager.getApplication().runWriteAction { + doc.apply(content, version).also { reportModification(doc.ktFile, it) } + } + if (outcome.needsReindex()) { + queueOnFileChangedAsync(doc.ktFile) + } + outcome + } + } + + /** Reports [outcome]'s change to FIR; caller runs inside runWriteAction. */ + @OptIn(LLFirInternals::class) + private fun reportModification( + ktFile: KtFile, + outcome: ReparseOutcome, + ) { + val modificationService = KaSourceModificationService.getInstance(project) + when (outcome) { + is ReparseOutcome.InBlock -> { + // `reparsed` is the old, still-attached node; the detached new root would be classified + // out-of-block against the wrong module. + modificationService.handleElementModification(outcome.reparsed, KaElementModificationType.Unknown) + // In-block changes are only queued; the platform's flush on writeActionFinished never fires + // because MockApplication.runWriteAction notifies no listeners. + LLFirDeclarationModificationService.getInstance(project).flushModifications() + } + + ReparseOutcome.WholeFile -> { + modificationService.handleElementModification(ktFile, KaElementModificationType.Unknown) + } + + ReparseOutcome.Unchanged, is ReparseOutcome.Failed -> {} + } + } + + /** SourceFileIndexer records local classes and objects, so only an in-block edit adding or removing one needs a reindex. */ + private fun ReparseOutcome.needsReindex(): Boolean = + when (this) { + is ReparseOutcome.InBlock -> { + replacedHadClassOrObject || PsiTreeUtil.findChildOfType(reparsed, KtClassOrObject::class.java) != null + } + + ReparseOutcome.WholeFile -> { + true + } + + ReparseOutcome.Unchanged, is ReparseOutcome.Failed -> { + false + } + } + + /** + * Parses [path]'s live document into a fresh [KtFile], registers it as the in-memory file, makes it + * the path's [IncrementalKtDocument], and reports [old] (or the new file) modified. + */ + private fun parseIntoNewDocument( + path: Path, + version: Int, + old: KtFile?, ): VersionedKtFile { val content = FileManager.getDocumentContents(path) val newKtFile = project.read { parser.createFile(path.pathString, content) } @@ -320,13 +454,21 @@ internal class KtSymbolIndex( .registerInMemoryFile(path.pathString, newKtFile.viewProvider.virtualFile) // project.write serializes the mutation against a concurrent `analyze` (read lock); runWriteAction // supplies the platform write access handleElementModification asserts, which our RW lock does not. - project.write { - ApplicationManager.getApplication().runWriteAction { - KaSourceModificationService - .getInstance(project) - .handleElementModification(old ?: newKtFile, KaElementModificationType.Unknown) + // The barrier preempts running analyses: completion waits on this refresh before it reaches the + // scheduler, so without it the keystroke waits out a whole indexing analysis. + AnalysisScheduler.preemptingAnalysis { + project.write { + ApplicationManager.getApplication().runWriteAction { + KaSourceModificationService + .getInstance(project) + .handleElementModification(old ?: newKtFile, KaElementModificationType.Unknown) + } + queueOnFileChangedAsync(newKtFile) } - queueOnFileChangedAsync(newKtFile) + } + // A close during the parse already ran invalidateCurrent; a document stored now would outlive it. + if (FileManager.isActive(path)) { + documents[path] = IncrementalKtDocument(newKtFile, version) } return VersionedKtFile(version, newKtFile) } @@ -348,6 +490,7 @@ internal class KtSymbolIndex( /** Drops the cached current file for [path] (e.g. on close). */ fun invalidateCurrent(path: Path) { + documents.remove(path) currentFiles.remove(path) currentVersions.remove(path) ProjectStructureProvider.getInstance(project).unregisterInMemoryFile(path.pathString) @@ -375,12 +518,15 @@ internal class KtSymbolIndex( * therefore check [LiveKtFile.isStale] and degrade rather than compute against frozen text. * * Known gap: the instance is resolved *before* the pin is installed, so a request arriving in that - * window sees no pin and can launch a refresh that completes inside this scope, firing - * `registerInMemoryFile` and a FIR modification event underneath it. Instance identity still holds - - * every door answers with the pinned instance for the whole scope - and the pin is stamped with the - * resolved instance's own version, so the bump is not lost. Closing the window entirely would mean - * publishing a pin before its file exists, making joiners wait on an unresolved entry inside the one - * path every caller depends on; that deadlock risk is worse than the window. + * window sees no pin and can launch a refresh. An in-place refresh cannot land inside this scope: + * the pin holds the document's edit read stamp, so the edit is deferred to the release; so is the + * full-parse fallback of a failed in-place reparse, which runs under the write stamp. Only a full + * parse with no document to stamp (the path's first parse) can complete inside the scope, firing + * `registerInMemoryFile` and a FIR modification event underneath it. Instance identity still holds - every door answers with the pinned instance for the whole + * scope - and the pin is stamped with the locked document's `appliedVersion`, so the bump is not + * lost. Closing the window entirely would mean publishing a pin before its file exists, making + * joiners wait on an unresolved entry inside the one path every caller depends on; that deadlock + * risk is worse than the window. */ fun withLiveKtFile( path: Path, @@ -394,7 +540,12 @@ internal class KtSymbolIndex( } } - /** Suspending [withLiveKtFile], for callers that must not block a dispatcher thread. */ + /** + * Suspending [withLiveKtFile], for callers that must not block a dispatcher thread on a refresh. + * + * It still blocks briefly once the refresh is done: installing the pin waits in the document's edit + * read lock while an in-place edit is being applied. + */ suspend fun withLiveKtFileAsync( path: Path, block: (LiveKtFile) -> R, @@ -421,7 +572,8 @@ internal class KtSymbolIndex( /** * The current instance for [path] with no pin, or `null` if none is cached. * - * Non-blocking and PSI-only. See [UnpinnedKtFileAccess] for why this is opt-in. + * Non-blocking and PSI-only. See [UnpinnedKtFileAccess] for why this is opt-in. The instance is + * edited in place by later refreshes, so read it only inside `project.read`. */ @UnpinnedKtFileAccess fun peekLiveKtFile(path: Path): KtFile? = getCurrentKtFileIfPresent(path) @@ -449,13 +601,24 @@ internal class KtSymbolIndex( path: Path, resolved: VersionedKtFile, ): Pin { + val locked = if (resolved.version == NO_DOCUMENT_VERSION) null else lockCurrentDocument(path) + // The document's file is pinned rather than the resolved one: an in-place edit or a fallback may + // have moved on since the resolve, and the stamp only guards the document's own file. + val candidate = + locked?.let { (doc, stamp) -> Pin(doc.ktFile, doc.appliedVersion, doc.editLock, stamp) } + ?: Pin(resolved.ktFile, resolved.version, null, 0L) + var joined = false val pin = pins.compute(path) { _, existing -> - // A concurrent acquirer may have won the race; join its pin and let this file go. Both - // resolved through the same single-flight future, so they are the same instance anyway. - existing?.also { it.count++ } - ?: Pin(resolved.ktFile, resolved.version).also { it.count = 1 } + // A concurrent acquirer may have won the race; join its pin and let this file go. + existing?.also { + it.count++ + joined = true + } ?: candidate.also { it.count = 1 } }!! + if (joined) { + candidate.unlockStamp() + } /* * The document can move on while the resolve is still parsing, so the pinned instance may already @@ -470,14 +633,36 @@ internal class KtSymbolIndex( return pin } + /** + * Read-locks [path]'s current document, or returns `null` if it has none (a disk file). + * + * The stamp is taken before the pin is published, so it may wait out an in-flight edit; that edit + * can replace the document with a full parse, hence the re-check after the lock. + */ + private fun lockCurrentDocument(path: Path): Pair? { + while (true) { + val doc = documents[path] ?: return null + val stamp = doc.editLock.readLock() + if (documents[path] === doc) return doc to stamp + doc.editLock.unlockRead(stamp) + } + } + + private fun Pin.unlockStamp() { + editLock?.unlockRead(stamp) + } + private fun releasePin(path: Path) { - var refreshOwed = false + var released: Pin? = null pins.compute(path) { _, pin -> if (pin == null) return@compute null if (--pin.count > 0) return@compute pin - refreshOwed = pin.refreshOwed + released = pin null } + val refreshOwed = released?.refreshOwed ?: false + // Unlocked before the owed refresh below is launched, so that refresh can take the write stamp. + released?.unlockStamp() /* * Applied on the way out rather than during the pin: the version bump that arrived while the path @@ -660,3 +845,7 @@ internal fun KtSymbolIndex.findSymbolBySimpleName( sourceIndex.findBySimpleName(name, limit, kinds) + libraryIndex.findBySimpleName(name, limit, kinds) ).let { if (limit <= 0) it else it.take(limit) } + +/** This future's value if it has completed normally, else `null`. */ +@VisibleForTesting +internal fun CompletableFuture.valueNowOrNull(): T? = runCatching { getNow(null) }.getOrNull() diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/LiveKtFile.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/LiveKtFile.kt index 5d468f9f72..173d9906d4 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/LiveKtFile.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/index/LiveKtFile.kt @@ -13,6 +13,9 @@ import java.nio.file.Path * An unpinned instance can be superseded while it is in use, which is what makes FIR report a file as * conflicting with itself (ADFA-4165, ADFA-5231). Opting in is only defensible for PSI-only work that * opens no analysis session; anything that analyses must use [KtSymbolIndex.withLiveKtFile]. + * + * An open document's instance is also edited in place by every refresh, under `project.write`, so + * PSI-only work must still read it inside `project.read`, or it can see a half-applied edit. */ @RequiresOptIn( level = RequiresOptIn.Level.ERROR, diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/modules/AnalysisScheduler.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/modules/AnalysisScheduler.kt index c4874af24d..d20172fb75 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/modules/AnalysisScheduler.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/modules/AnalysisScheduler.kt @@ -118,10 +118,11 @@ internal class ScheduledCancelChecker( /** * Runs [attempt] and, if it was preempted, runs it exactly once more. * - * The retry policy every [AnalysisPriority.COMMAND] call site needs. A command is preempted by - * keystroke-driven work ([AnalysisPriority.INTERACTIVE]), which - unlike a genuine cancellation - - * leaves the user's own request alive, so reporting the empty/failed result would be a lie: "no - * references" for a symbol that has plenty, or a silently skipped organize-imports. + * The retry policy every user-invoked call site needs. A command is preempted by keystroke-driven + * work ([AnalysisPriority.INTERACTIVE]), and any request, the INTERACTIVE refactoring planners + * included, by the barrier an edit raises. Unlike a genuine cancellation, a preemption leaves the + * user's own request alive, so reporting the empty/failed result would be a lie: "no references" for + * a symbol that has plenty, or a silently skipped organize-imports. * * Two details this centralises: * - **A fresh [ScheduledCancelChecker] per attempt.** [ScheduledCancelChecker.preempt] latches, so @@ -174,6 +175,9 @@ internal object AnalysisScheduler { private var holderPreempted = false private var holderPreempt: (() -> Unit)? = null + /** Number of [preemptingAnalysis] blocks currently running; non-zero turns every new requester away. */ + private var barriers = 0 + /** Number of threads currently waiting to acquire, per priority. */ private val waiting = IntArray(AnalysisPriority.entries.size) @@ -207,6 +211,10 @@ internal object AnalysisScheduler { // Bail out (rather than keep waiting) if this requester was cancelled while queued. cancelChecker.abortIfCancelled() + if (barriers > 0) { + throw AnalysisPreemptedException() + } + val hp = holderPriority if (holderThread != null && hp != null && !holderPreempted && ( @@ -276,6 +284,43 @@ internal object AnalysisScheduler { } } + /** + * Runs [block] with analysis held off, so [block] can take `project.write` without waiting for + * analyses to finish. + * + * Every analysis holds `project.read` for its whole run and takes this scheduler's slot while still + * holding it, so a write has to wait for all of them. While [block] runs: + * - the current holder, whatever its priority, is preempted (its `onPreempt` is invoked once); + * - every requester waiting in [acquire], and every new one, leaves with + * [AnalysisPreemptedException], releasing its read lock on the way out; + * - a reentrant [acquire] by the holder's own thread is unaffected, so the holder can unwind. + * + * Barriers nest and overlap: analysis resumes when the last one ends. + * + * The caller must not hold `project.read` or the analysis slot: [block] would then wait on itself. + */ + fun preemptingAnalysis(block: () -> R): R { + // The raise sits inside the try: a throwing onPreempt must not leave the count up, or every later + // analysis in the process is turned away. + try { + mutex.withLock { + barriers++ + // Waiters park on a timed await; wake them so they leave now rather than at the next poll. + available.signalAll() + if (holderThread != null && !holderPreempted) { + holderPreempted = true + holderPreempt?.invoke() + } + } + return block() + } finally { + mutex.withLock { + barriers-- + available.signalAll() + } + } + } + private fun higherPriorityWaiting(priority: AnalysisPriority): Boolean { for (i in priority.ordinal + 1 until waiting.size) { if (waiting[i] > 0) return true diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/NoFormattingCodeStyleManager.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/NoFormattingCodeStyleManager.kt new file mode 100644 index 0000000000..deb9dad592 --- /dev/null +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/services/NoFormattingCodeStyleManager.kt @@ -0,0 +1,114 @@ +package com.itsaky.androidide.lsp.kotlin.compiler.services + +import org.jetbrains.kotlin.com.intellij.formatting.FormattingMode +import org.jetbrains.kotlin.com.intellij.lang.ASTNode +import org.jetbrains.kotlin.com.intellij.openapi.editor.Document +import org.jetbrains.kotlin.com.intellij.openapi.fileTypes.FileType +import org.jetbrains.kotlin.com.intellij.openapi.project.Project +import org.jetbrains.kotlin.com.intellij.openapi.util.Computable +import org.jetbrains.kotlin.com.intellij.openapi.util.TextRange +import org.jetbrains.kotlin.com.intellij.psi.PsiElement +import org.jetbrains.kotlin.com.intellij.psi.PsiFile +import org.jetbrains.kotlin.com.intellij.psi.codeStyle.CodeStyleManager +import org.jetbrains.kotlin.com.intellij.psi.codeStyle.Indent +import org.jetbrains.kotlin.com.intellij.util.ThrowableRunnable + +/** + * A [CodeStyleManager] that exists only because `DiffLog.doActualPsiChange` looks one up to run its + * PSI change with the formatter disabled, and no standalone Analysis API jar ships an implementation. + * We never format through the PSI, so the "formatter disabled" actions just run their argument and + * every formatting entry point fails loudly. + */ +internal class NoFormattingCodeStyleManager( + private val project: Project, +) : CodeStyleManager() { + override fun getProject(): Project = project + + override fun performActionWithFormatterDisabled(r: Runnable) = r.run() + + override fun performActionWithFormatterDisabled(r: ThrowableRunnable) = r.run() + + override fun performActionWithFormatterDisabled(r: Computable): T = r.compute() + + override fun isSequentialProcessingAllowed(): Boolean = false + + override fun reformat(element: PsiElement): PsiElement = unsupported() + + override fun reformat( + element: PsiElement, + canChangeWhiteSpacesOnly: Boolean, + ): PsiElement = unsupported() + + override fun reformatRange( + element: PsiElement, + startOffset: Int, + endOffset: Int, + ): PsiElement = unsupported() + + override fun reformatRange( + element: PsiElement, + startOffset: Int, + endOffset: Int, + canChangeWhiteSpacesOnly: Boolean, + ): PsiElement = unsupported() + + override fun reformatText( + file: PsiFile, + startOffset: Int, + endOffset: Int, + ) = unsupported() + + override fun reformatText( + file: PsiFile, + ranges: Collection, + ) = unsupported() + + override fun adjustLineIndent( + file: PsiFile, + rangeToAdjust: TextRange, + ) = unsupported() + + override fun adjustLineIndent( + file: PsiFile, + offset: Int, + ): Int = unsupported() + + override fun adjustLineIndent( + document: Document, + offset: Int, + ): Int = unsupported() + + override fun isLineToBeIndented( + file: PsiFile, + offset: Int, + ): Boolean = unsupported() + + override fun getLineIndent( + file: PsiFile, + offset: Int, + ): String? = unsupported() + + override fun getLineIndent( + document: Document, + offset: Int, + ): String? = unsupported() + + override fun getIndent( + text: String, + fileType: FileType, + ): Indent = unsupported() + + override fun fillIndent( + indent: Indent, + fileType: FileType, + ): String = unsupported() + + override fun zeroIndent(): Indent = unsupported() + + override fun reformatNewlyAddedElement( + parent: ASTNode, + addedElement: ASTNode, + ) = unsupported() + + private fun unsupported(): Nothing = throw UnsupportedOperationException("formatting is not supported") +} diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/util/SLF4JLogger.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/util/SLF4JLogger.kt index dac66e7160..fb91af0391 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/util/SLF4JLogger.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/compiler/util/SLF4JLogger.kt @@ -3,14 +3,36 @@ package com.itsaky.androidide.lsp.kotlin.compiler.util import org.jetbrains.kotlin.com.intellij.openapi.diagnostic.Logger import org.slf4j.LoggerFactory -class SLF4JLogger(name: String) : Logger() { +class SLF4JLogger( + name: String, +) : Logger() { private val logger = LoggerFactory.getLogger(name) - override fun isDebugEnabled(): Boolean { - return logger.isDebugEnabled + companion object { + /** + * [message] with [details] summarized by count and length, never inlined. + * + * IntelliJ passes attachments as details, and they can be whole file texts: an "Inconsistent + * tree" error carries the file twice, which inlined wrote thousands of log lines per keystroke. + */ + fun errorMessage( + message: String?, + details: Array, + ): String { + val msg = message ?: "" + val attachments = details.filterNotNull() + if (attachments.isEmpty()) return msg + val lengths = attachments.joinToString { it.length.toString() } + return "$msg (${attachments.size} attachments omitted, lengths: $lengths)" + } } - override fun debug(message: String?, t: Throwable?) { + override fun isDebugEnabled(): Boolean = logger.isDebugEnabled + + override fun debug( + message: String?, + t: Throwable?, + ) { if (t != null) { logger.debug(message ?: "", t) } else { @@ -18,7 +40,10 @@ class SLF4JLogger(name: String) : Logger() { } } - override fun info(message: String?, t: Throwable?) { + override fun info( + message: String?, + t: Throwable?, + ) { if (t != null) { logger.info(message ?: "", t) } else { @@ -26,7 +51,10 @@ class SLF4JLogger(name: String) : Logger() { } } - override fun warn(message: String?, t: Throwable?) { + override fun warn( + message: String?, + t: Throwable?, + ) { if (t != null) { logger.warn(message ?: "", t) } else { @@ -34,20 +62,12 @@ class SLF4JLogger(name: String) : Logger() { } } - override fun error(message: String?, t: Throwable?, vararg details: String?) { - val msg = message ?: "" - val detailStr = if (details.isNotEmpty()) { - details.filterNotNull().joinToString(System.lineSeparator()) - } else { - "" - } - - val fullMessage = if (detailStr.isNotEmpty()) { - if (msg.isNotEmpty()) "$msg: $detailStr" else detailStr - } else { - msg - } - + override fun error( + message: String?, + t: Throwable?, + vararg details: String?, + ) { + val fullMessage = errorMessage(message, details) if (t != null) { logger.error(fullMessage, t) } else { diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/AdvancedKotlinEditHandler.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/AdvancedKotlinEditHandler.kt index 72903f627e..cb0223a174 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/AdvancedKotlinEditHandler.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/AdvancedKotlinEditHandler.kt @@ -1,8 +1,11 @@ package com.itsaky.androidide.lsp.kotlin.completion import com.itsaky.androidide.lsp.kotlin.compiler.index.UnpinnedKtFileAccess +import com.itsaky.androidide.lsp.kotlin.compiler.read import com.itsaky.androidide.lsp.kotlin.utils.AnalysisContext import com.itsaky.androidide.lsp.models.CompletionItem +import com.itsaky.androidide.lsp.models.TextEdit +import com.itsaky.androidide.lsp.util.RewriteHelper import io.github.rosemoe.sora.text.Content import io.github.rosemoe.sora.widget.CodeEditor import org.jetbrains.kotlin.psi.KtFile @@ -25,15 +28,23 @@ internal abstract class AdvancedKotlinEditHandler( index: Int, ) { // PSI-only, on the UI thread, after completion has already returned: there is no analysis to - // keep coherent, and pinning here would block the UI thread on a refresh. - val managedFile = analysisContext.env.ktSymbolIndex.peekLiveKtFile(analysisContext.file) - if (managedFile == null) { + // keep coherent, and pinning here would block the UI thread on a refresh. The read lock still + // matters: an in-place refresh mutates this same instance under project.write. The editor is + // changed only after the lock is released, so no listener it fires can run under it. + val env = analysisContext.env + val edits = + env.project.read { + env.ktSymbolIndex.peekLiveKtFile(analysisContext.file)?.let { managedFile -> + context(analysisContext) { computeEdits(managedFile, item) } + } + } + if (edits == null) { logger.error("Unable to perform edit. File not open.") return } - context(analysisContext) { - performEdits(managedFile, editor, item) + if (edits.isNotEmpty()) { + RewriteHelper.performEdits(edits, editor) } if (item.command != null) { @@ -41,10 +52,10 @@ internal abstract class AdvancedKotlinEditHandler( } } + /** The edits applying [item] makes to [ktFile]; runs under project.read, so it must not touch the editor. */ context(ctx: AnalysisContext) - abstract fun performEdits( + abstract fun computeEdits( ktFile: KtFile, - editor: CodeEditor, item: CompletionItem, - ) + ): List } diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinAutoImportEditHandler.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinAutoImportEditHandler.kt index 0b568fc04b..6a5764cbf0 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinAutoImportEditHandler.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/completion/KotlinAutoImportEditHandler.kt @@ -4,8 +4,7 @@ import com.itsaky.androidide.lsp.kotlin.utils.AnalysisContext import com.itsaky.androidide.lsp.kotlin.utils.insertImport import com.itsaky.androidide.lsp.models.ClassCompletionData import com.itsaky.androidide.lsp.models.CompletionItem -import com.itsaky.androidide.lsp.util.RewriteHelper -import io.github.rosemoe.sora.widget.CodeEditor +import com.itsaky.androidide.lsp.models.TextEdit import org.appdevforall.codeonthego.indexing.jvm.JvmSymbol import org.jetbrains.kotlin.psi.KtFile @@ -13,21 +12,16 @@ internal class KotlinAutoImportEditHandler( analysisContext: AnalysisContext, private val symbolToImport: JvmSymbol? = null, ) : AdvancedKotlinEditHandler(analysisContext) { - context(ctx: AnalysisContext) - override fun performEdits( + override fun computeEdits( ktFile: KtFile, - editor: CodeEditor, - item: CompletionItem - ) { + item: CompletionItem, + ): List { val fqnToImport = symbolToImport?.fqName ?: (item.data as? ClassCompletionData)?.className - ?: return + ?: return emptyList() - val edits = insertImport(ktFile, fqnToImport) - if (edits.isNotEmpty()) { - RewriteHelper.performEdits(edits, editor) - } + return insertImport(ktFile, fqnToImport) } -} \ No newline at end of file +} diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractMethodPlanner.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractMethodPlanner.kt index efed4a68bf..eda7c4b777 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractMethodPlanner.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractMethodPlanner.kt @@ -2,7 +2,8 @@ package com.itsaky.androidide.lsp.kotlin.utils.refactor import com.itsaky.androidide.lsp.kotlin.compiler.AbstractCompilationEnvironment import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPriority -import com.itsaky.androidide.lsp.kotlin.compiler.modules.ScheduledCancelChecker +import com.itsaky.androidide.lsp.kotlin.compiler.modules.retryingOnPreemption +import com.itsaky.androidide.progress.ICancelChecker import org.slf4j.LoggerFactory import java.nio.file.Path import kotlin.coroutines.cancellation.CancellationException @@ -26,61 +27,67 @@ internal fun buildExtractMethodPlan( selectionStart: Int, selectionEnd: Int, documentVersion: Int?, - cancelChecker: ScheduledCancelChecker, + cancelChecker: ICancelChecker, ): ExtractMethodPlan = runCatching { - env.ktSymbolIndex.withLiveKtFile(nioPath) { live -> - if (live.isStale) { - /* - * Joining another feature's scope hands over its text, which can be older than the buffer. - * The caller stamps `documentVersion` from the live buffer, so the apply-time version guard - * would compare an honest stamp against text one edit behind and pass - and offsets computed - * here would replace the wrong span. Refusing is the only safe answer. - */ - logger.debug("refusing extract-method plan for {}: pinned text is behind the buffer", nioPath) - return@withLiveKtFile ExtractMethodPlan.refused(ExtractionRefusal.CouldNotAnalyse) - } + /* + * Another path's edit raises the analysis barrier for every priority, which preempts this + * user-invoked plan; it is retried once, re-pinning the file, rather than failing the action. + */ + retryingOnPreemption(cancelChecker, "Extract-method plan for $nioPath") { checker -> + env.ktSymbolIndex.withLiveKtFile(nioPath) { live -> + if (live.isStale) { + /* + * Joining another feature's scope hands over its text, which can be older than the buffer. + * The caller stamps `documentVersion` from the live buffer, so the apply-time version guard + * would compare an honest stamp against text one edit behind and pass - and offsets computed + * here would replace the wrong span. Refusing is the only safe answer. + */ + logger.debug("refusing extract-method plan for {}: pinned text is behind the buffer", nioPath) + return@withLiveKtFile ExtractMethodPlan.refused(ExtractionRefusal.CouldNotAnalyse) + } - live.read { ktFile -> - val fileText = ktFile.text - val region = - resolveExtractionRegion(ktFile, selectionStart, selectionEnd) - ?: return@read ExtractMethodPlan.refused(ExtractionRefusal.NotASingleRegion, fileText, documentVersion) + live.read { ktFile -> + val fileText = ktFile.text + val region = + resolveExtractionRegion(ktFile, selectionStart, selectionEnd) + ?: return@read ExtractMethodPlan.refused(ExtractionRefusal.NotASingleRegion, fileText, documentVersion) - live.analyzing(AnalysisPriority.INTERACTIVE, cancelChecker) { - val results = - when (region) { - is ExtractionRegion.Expressions -> { - region.candidates.map { buildCandidate(listOf(it), isExpression = true, fileText = fileText) } - } + live.analyzing(AnalysisPriority.INTERACTIVE, checker) { + val results = + when (region) { + is ExtractionRegion.Expressions -> { + region.candidates.map { buildCandidate(listOf(it), isExpression = true, fileText = fileText) } + } - is ExtractionRegion.Statements -> { - listOf(buildCandidate(region.statements, isExpression = false, fileText = fileText)) + is ExtractionRegion.Statements -> { + listOf(buildCandidate(region.statements, isExpression = false, fileText = fileText)) + } } + + val candidates = results.filterIsInstance().map { it.candidate } + if (candidates.isEmpty()) { + /* + * The innermost region is the one the user pointed at, so its reason is the one to show. + * A region with no reason at all cannot happen; if it does, saying nothing useful beats + * blaming the selection. + */ + val refusal = + results.filterIsInstance().firstOrNull()?.refusal + ?: ExtractionRefusal.CouldNotAnalyse + return@analyzing ExtractMethodPlan.refused(refusal, fileText, documentVersion) } - val candidates = results.filterIsInstance().map { it.candidate } - if (candidates.isEmpty()) { - /* - * The innermost region is the one the user pointed at, so its reason is the one to show. - * A region with no reason at all cannot happen; if it does, saying nothing useful beats - * blaming the selection. - */ - val refusal = - results.filterIsInstance().firstOrNull()?.refusal - ?: ExtractionRefusal.CouldNotAnalyse - return@analyzing ExtractMethodPlan.refused(refusal, fileText, documentVersion) + ExtractMethodPlan( + fileText = fileText, + documentVersion = documentVersion, + candidates = candidates, + refusal = null, + ) } - - ExtractMethodPlan( - fileText = fileText, - documentVersion = documentVersion, - candidates = candidates, - refusal = null, - ) } - } - } ?: ExtractMethodPlan.refused(ExtractionRefusal.CouldNotAnalyse) + } ?: ExtractMethodPlan.refused(ExtractionRefusal.CouldNotAnalyse) + } }.getOrElse { error -> if (error is CancellationException) throw error logger.warn("Failed to build extract-method plan for {}", nioPath, error) diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractVariablePlanner.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractVariablePlanner.kt index fa315c28a0..3a85ad989c 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractVariablePlanner.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractVariablePlanner.kt @@ -2,7 +2,7 @@ package com.itsaky.androidide.lsp.kotlin.utils.refactor import com.itsaky.androidide.lsp.kotlin.compiler.AbstractCompilationEnvironment import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPriority -import com.itsaky.androidide.lsp.kotlin.compiler.modules.ScheduledCancelChecker +import com.itsaky.androidide.lsp.kotlin.compiler.modules.retryingOnPreemption import com.itsaky.androidide.lsp.kotlin.utils.renderName import com.itsaky.androidide.lsp.refactor.BlockPlacement import com.itsaky.androidide.lsp.refactor.TextSpan @@ -11,6 +11,7 @@ import com.itsaky.androidide.lsp.refactor.excludeUnsoundOccurrences import com.itsaky.androidide.lsp.refactor.hoistSkipsWrite import com.itsaky.androidide.lsp.refactor.hoistsOverLoopWrite import com.itsaky.androidide.lsp.refactor.servableOccurrences +import com.itsaky.androidide.progress.ICancelChecker import org.jetbrains.kotlin.analysis.api.KaExperimentalApi import org.jetbrains.kotlin.analysis.api.KaSession import org.jetbrains.kotlin.analysis.api.symbols.KaCallableSymbol @@ -42,37 +43,43 @@ internal fun buildExtractionPlan( selectionStart: Int, selectionEnd: Int, documentVersion: Int?, - cancelChecker: ScheduledCancelChecker, + cancelChecker: ICancelChecker, ): ExtractionPlan = runCatching { - env.ktSymbolIndex.withLiveKtFile(nioPath) { live -> - if (live.isStale) { - /* - * Joining another feature's scope hands over its text, which can be older than the buffer. - * The caller stamps `documentVersion` from the live buffer, so the apply-time version guard - * would compare an honest stamp against text one edit behind and pass - and offsets computed - * here would replace the wrong span. Refusing is the only safe answer. - */ - logger.debug("refusing extract-variable plan for {}: pinned text is behind the buffer", nioPath) - return@withLiveKtFile ExtractionPlan.empty() - } + /* + * Another path's edit raises the analysis barrier for every priority, which preempts this + * user-invoked plan; it is retried once, re-pinning the file, rather than failing the action. + */ + retryingOnPreemption(cancelChecker, "Extract-variable plan for $nioPath") { checker -> + env.ktSymbolIndex.withLiveKtFile(nioPath) { live -> + if (live.isStale) { + /* + * Joining another feature's scope hands over its text, which can be older than the buffer. + * The caller stamps `documentVersion` from the live buffer, so the apply-time version guard + * would compare an honest stamp against text one edit behind and pass - and offsets computed + * here would replace the wrong span. Refusing is the only safe answer. + */ + logger.debug("refusing extract-variable plan for {}: pinned text is behind the buffer", nioPath) + return@withLiveKtFile ExtractionPlan.empty() + } - live.read { ktFile -> - val syntax = candidateExpressionsAt(ktFile, selectionStart, selectionEnd) - if (syntax.expressions.isEmpty()) return@read ExtractionPlan.empty(ktFile.text, documentVersion) - - /* PsiFileImpl.getText() allocates a fresh String each call, so the plan pass reads it once and - * threads it down to every candidate and rung. */ - val fileText = ktFile.text - live.analyzing(AnalysisPriority.INTERACTIVE, cancelChecker) { - ExtractionPlan( - fileText = fileText, - documentVersion = documentVersion, - candidates = syntax.expressions.mapNotNull { candidateFor(it, fileText) }, - ) + live.read { ktFile -> + val syntax = candidateExpressionsAt(ktFile, selectionStart, selectionEnd) + if (syntax.expressions.isEmpty()) return@read ExtractionPlan.empty(ktFile.text, documentVersion) + + /* PsiFileImpl.getText() allocates a fresh String each call, so the plan pass reads it once and + * threads it down to every candidate and rung. */ + val fileText = ktFile.text + live.analyzing(AnalysisPriority.INTERACTIVE, checker) { + ExtractionPlan( + fileText = fileText, + documentVersion = documentVersion, + candidates = syntax.expressions.mapNotNull { candidateFor(it, fileText) }, + ) + } } - } - } ?: ExtractionPlan.empty() + } ?: ExtractionPlan.empty() + } }.getOrElse { error -> logger.warn("Failed to build extract-variable plan for {}", nioPath, error) ExtractionPlan.empty() diff --git a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/InlineVariablePlanner.kt b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/InlineVariablePlanner.kt index c6363619a4..637faf0235 100644 --- a/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/InlineVariablePlanner.kt +++ b/lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/InlineVariablePlanner.kt @@ -2,8 +2,9 @@ package com.itsaky.androidide.lsp.kotlin.utils.refactor import com.itsaky.androidide.lsp.kotlin.compiler.AbstractCompilationEnvironment import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPriority -import com.itsaky.androidide.lsp.kotlin.compiler.modules.ScheduledCancelChecker +import com.itsaky.androidide.lsp.kotlin.compiler.modules.retryingOnPreemption import com.itsaky.androidide.lsp.refactor.TextSpan +import com.itsaky.androidide.progress.ICancelChecker import org.jetbrains.kotlin.analysis.api.KaSession import org.jetbrains.kotlin.analysis.api.resolution.KaCallableMemberCall import org.jetbrains.kotlin.analysis.api.resolution.KaImplicitReceiverValue @@ -65,17 +66,23 @@ internal fun buildInlineVariablePlan( nioPath: Path, offset: Int, documentVersion: Int, - cancelChecker: ScheduledCancelChecker, + cancelChecker: ICancelChecker, ): InlineVariablePlan = runCatching { - env.ktSymbolIndex.withLiveKtFile(nioPath) { live -> - live.read { ktFile -> - val fileText = ktFile.text - live.analyzing(AnalysisPriority.INTERACTIVE, cancelChecker) { - planFor(ktFile, fileText, offset, documentVersion) + /* + * Another path's edit raises the analysis barrier for every priority, which preempts this + * user-invoked plan; it is retried once, re-pinning the file, rather than failing the action. + */ + retryingOnPreemption(cancelChecker, "Inline-variable plan for $nioPath") { checker -> + env.ktSymbolIndex.withLiveKtFile(nioPath) { live -> + live.read { ktFile -> + val fileText = ktFile.text + live.analyzing(AnalysisPriority.INTERACTIVE, checker) { + planFor(ktFile, fileText, offset, documentVersion) + } } - } - } ?: InlineVariablePlan.refused(InlineRefusal.CouldNotAnalyse) + } ?: InlineVariablePlan.refused(InlineRefusal.CouldNotAnalyse) + } }.getOrElse { error -> if (error is CancellationException) throw error logger.warn("Failed to build inline-variable plan for {}", nioPath, error) diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/CompilationEnvironmentCloseTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/CompilationEnvironmentCloseTest.kt index fb7575f0a8..516d719e85 100644 --- a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/CompilationEnvironmentCloseTest.kt +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/CompilationEnvironmentCloseTest.kt @@ -2,6 +2,7 @@ package com.itsaky.androidide.lsp.kotlin.compiler import com.google.common.truth.Truth.assertThat import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPriority +import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisScheduler import com.itsaky.androidide.lsp.kotlin.compiler.modules.ScheduledCancelChecker import com.itsaky.androidide.lsp.kotlin.compiler.modules.UnpinnedAnalysis import com.itsaky.androidide.lsp.kotlin.compiler.modules.analyzeMaybeDangling @@ -85,6 +86,33 @@ class CompilationEnvironmentCloseTest : KtLspTest() { assertThat(env.project.isDisposed).isTrue() } + @Test(timeout = 30_000) + fun `close disposes the project when an edit's barrier is up as it starts`() { + val raised = CountDownLatch(1) + val closeDone = CountDownLatch(1) + // With the fix, close cannot finish while the barrier is up, so the barrier stays up for the + // whole of close's first attempt and is lowered only by the bounded wait. + val barrier = + thread { + AnalysisScheduler.preemptingAnalysis { + raised.countDown() + closeDone.await(1, TimeUnit.SECONDS) + } + } + var closeError: Throwable? = null + try { + assertThat(raised.await(5, TimeUnit.SECONDS)).isTrue() + + closeError = runCatching { env.closeInWriteAction() }.exceptionOrNull() + } finally { + closeDone.countDown() + barrier.join() + } + + assertThat(closeError).isNull() + assertThat(env.project.isDisposed).isTrue() + } + // Starts the close, gives it time to dispose if it is going to, then lets the blocker finish. private fun closeWhileBlocked(release: CountDownLatch) { val closer = thread { env.closeInWriteAction() } diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/CurrentKtFileCacheTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/CurrentKtFileCacheTest.kt index 0b9e253042..be376a2d11 100644 --- a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/CurrentKtFileCacheTest.kt +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/CurrentKtFileCacheTest.kt @@ -104,7 +104,7 @@ internal class CurrentKtFileCacheTest : KtLspTest() { } @Test - fun `new version returns new instance reflecting new content`() { + fun `new version keeps the same instance with the new content`() { createSourceFile("B.kt", "fun b() {}") val path = sourcePath("B.kt") openDocument(path, "fun b() {}") @@ -113,7 +113,7 @@ internal class CurrentKtFileCacheTest : KtLspTest() { changeDocument(path, "fun b() {}\nfun c() {}", 2) val v2 = env.ktSymbolIndex.withLiveKtFile(path) { live -> - live.read { (it !== v1) to it.text } + live.read { (it === v1) to it.text } }!! assertNotNull(v1) diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/FutureValueNowOrNullTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/FutureValueNowOrNullTest.kt new file mode 100644 index 0000000000..3847c3d4fc --- /dev/null +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/FutureValueNowOrNullTest.kt @@ -0,0 +1,35 @@ +package com.itsaky.androidide.lsp.kotlin.compiler.index + +import com.google.common.truth.Truth.assertThat +import org.junit.Test +import java.util.concurrent.CompletableFuture + +/** + * Requirement: deciding whether a path's last refresh is reusable never throws, even when that refresh + * fails between the caller's own completion check and the read of its value. + */ +internal class FutureValueNowOrNullTest { + @Test + fun `a failed future has no value`() { + val failed = CompletableFuture().apply { completeExceptionally(IllegalStateException("parse failed")) } + + assertThat(failed.valueNowOrNull()).isNull() + } + + @Test + fun `a cancelled future has no value`() { + val cancelled = CompletableFuture().apply { cancel(false) } + + assertThat(cancelled.valueNowOrNull()).isNull() + } + + @Test + fun `a pending future has no value`() { + assertThat(CompletableFuture().valueNowOrNull()).isNull() + } + + @Test + fun `a completed future yields its value`() { + assertThat(CompletableFuture.completedFuture("v").valueNowOrNull()).isEqualTo("v") + } +} diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InBlockInvalidationTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InBlockInvalidationTest.kt new file mode 100644 index 0000000000..e867734144 --- /dev/null +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InBlockInvalidationTest.kt @@ -0,0 +1,222 @@ +package com.itsaky.androidide.lsp.kotlin.compiler.index + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.eventbus.events.editor.ChangeType +import com.itsaky.androidide.eventbus.events.editor.DocumentChangeEvent +import com.itsaky.androidide.eventbus.events.editor.DocumentCloseEvent +import com.itsaky.androidide.eventbus.events.editor.DocumentOpenEvent +import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPriority +import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest +import com.itsaky.androidide.models.Range +import com.itsaky.androidide.projects.FileManager +import kotlinx.coroutines.runBlocking +import org.jetbrains.kotlin.analysis.api.components.KaDiagnosticCheckerFilter +import org.jetbrains.kotlin.analysis.api.platform.analysisMessageBus +import org.jetbrains.kotlin.analysis.api.platform.modification.KotlinModificationEvent +import org.jetbrains.kotlin.analysis.api.platform.modification.KotlinModificationEventListener +import org.jetbrains.kotlin.analysis.api.platform.modification.KotlinModuleOutOfBlockModificationEvent +import org.jetbrains.kotlin.analysis.api.types.KaClassType +import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil +import org.jetbrains.kotlin.com.intellij.util.messages.MessageBusConnection +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.psi.KtProperty +import org.junit.After +import org.junit.Before +import org.junit.Test +import java.nio.file.Path +import java.util.Collections +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit + +/** + * Requirement: an edit confined to one reparseable block invalidates only that declaration, so FIR + * publishes no module out-of-block event for it, yet the next analysis sees the new body; edits that + * change a signature, an import or the top-level declarations still publish one. + */ +internal class InBlockInvalidationTest : KtLspTest() { + override val enableParserEventSystem = true + + private val openedPaths = mutableListOf() + private val outOfBlockEvents = Collections.synchronizedList(mutableListOf()) + private lateinit var connection: MessageBusConnection + + private val original = "package p\n\nfun f(a: Int): Int {\n val x = 1\n return a + x.hashCode()\n}" + + @Before + fun subscribe() { + connection = env.project.analysisMessageBus.connect() + connection.subscribe( + KotlinModificationEvent.TOPIC, + KotlinModificationEventListener { event -> + if (event is KotlinModuleOutOfBlockModificationEvent) outOfBlockEvents.add(event) + }, + ) + } + + @After + fun tearDown() { + connection.disconnect() + env.ktSymbolIndex.indexWorker.commandIntakeObserver = null + openedPaths.forEach { FileManager.onDocumentClose(DocumentCloseEvent(it)) } + openedPaths.clear() + } + + private fun open( + name: String, + content: String, + ): Path { + createSourceFile(name, content) + val path = env.sourceRoots.first().resolve(name) + FileManager.onDocumentOpen(DocumentOpenEvent(path, content, 1)) + openedPaths.add(path) + runBlocking { env.ktSymbolIndex.refreshCurrentKtFile(path) } + return path + } + + private fun edit( + path: Path, + content: String, + version: Int, + ) { + FileManager.onDocumentContentChange( + DocumentChangeEvent(path, content, content, version, ChangeType.NEW_TEXT, 0, Range.NONE), + ) + runBlocking { env.ktSymbolIndex.refreshCurrentKtFile(path) } + } + + /** The class id of the type of property [name], or null when it is not a class type. */ + private fun propertyType( + path: Path, + name: String, + ): String? = + env.ktSymbolIndex.withLiveKtFile(path) { live -> + live.analyzing(AnalysisPriority.DIAGNOSTICS, noopCancelChecker()) { ktFile -> + val property = PsiTreeUtil.findChildrenOfType(ktFile, KtProperty::class.java).single { it.name == name } + (property.returnType as? KaClassType)?.classId?.asString() + } + } + + private fun diagnosticNames(path: Path): List = + env.ktSymbolIndex.withLiveKtFile(path) { live -> + live.analyzing(AnalysisPriority.DIAGNOSTICS, noopCancelChecker()) { ktFile -> + ktFile.collectDiagnostics(KaDiagnosticCheckerFilter.EXTENDED_AND_COMMON_CHECKERS).map { it.factoryName } + } + }!! + + private fun pinnedText(path: Path): String? = env.ktSymbolIndex.withLiveKtFile(path) { live -> live.read { it.text } } + + @Test + fun `an in-body edit publishes no out-of-block event and analysis sees the new body`() { + val path = open("InBody.kt", original) + assertThat(propertyType(path, "x")).isEqualTo("kotlin/Int") + outOfBlockEvents.clear() + + edit(path, original.replace("val x = 1", "val x = \"s\""), 2) + + assertThat(outOfBlockEvents).isEmpty() + assertThat(propertyType(path, "x")).isEqualTo("kotlin/String") + } + + @Test + fun `an in-body edit that introduces an error is reported by the next diagnostics run`() { + val path = open("InBodyError.kt", original) + assertThat(diagnosticNames(path)).isEmpty() + + edit(path, original.replace("val x = 1", "val x: String = 1"), 2) + + assertThat(diagnosticNames(path)).isNotEmpty() + } + + @Test + fun `a signature edit publishes an out-of-block event for the module`() { + val path = open("Signature.kt", original) + propertyType(path, "x") + outOfBlockEvents.clear() + + edit(path, original.replace("a: Int", "a: Long"), 2) + + assertThat(outOfBlockEvents).isNotEmpty() + } + + @Test + fun `an import edit publishes an out-of-block event`() { + val path = open("Import.kt", original) + propertyType(path, "x") + outOfBlockEvents.clear() + + edit(path, original.replace("package p\n", "package p\nimport kotlin.math.abs\n"), 2) + + assertThat(outOfBlockEvents).isNotEmpty() + } + + @Test + fun `a top-level declaration edit publishes an out-of-block event`() { + val path = open("TopLevel.kt", original) + propertyType(path, "x") + outOfBlockEvents.clear() + + edit(path, original + "\nfun g() {}", 2) + + assertThat(outOfBlockEvents).isNotEmpty() + } + + @Test + fun `an edit inside a lambda in a property initializer is seen by analysis`() { + val lambda = "package p\n\nval p = run { 1 }\n" + val path = open("Lambda.kt", lambda) + assertThat(propertyType(path, "p")).isEqualTo("kotlin/Int") + + edit(path, lambda.replace("run { 1 }", "run { \"s\" }"), 2) + + assertThat(propertyType(path, "p")).isEqualTo("kotlin/String") + } + + @Test + fun `an edit inside a comment in a body is seen and does not throw`() { + val commented = original.replace(" val x = 1\n", " // note\n val x = 1\n") + val path = open("Comment.kt", commented) + assertThat(propertyType(path, "x")).isEqualTo("kotlin/Int") + val edited = commented.replace("// note", "// noted here") + + edit(path, edited, 2) + + assertThat(pinnedText(path)).isEqualTo(edited) + assertThat(propertyType(path, "x")).isEqualTo("kotlin/Int") + } + + @Test + fun `an in-body edit without a local class does not queue a reindex, and one adding a local class does`() { + val reindexed = LinkedBlockingQueue() + env.ktSymbolIndex.indexWorker.commandIntakeObserver = { cmd -> + if (cmd is IndexCommand.IndexModifiedFile) reindexed.add(cmd.ktFile) + } + val path = open("LocalClass.kt", original) + propertyType(path, "x") + val document = env.ktSymbolIndex.incrementalDocument(path)!!.ktFile + assertThat(reindexed.poll(5, TimeUnit.SECONDS)).isSameInstanceAs(document) + + edit(path, original.replace("val x = 1", "val x = 2"), 2) + edit(path, original.replace("val x = 1", "class L\n val x = 2"), 3) + + assertThat(reindexed.poll(5, TimeUnit.SECONDS)).isSameInstanceAs(document) + // The first edit's reindex, had it been queued, was launched before the second's. + assertThat(reindexed.poll(500, TimeUnit.MILLISECONDS)).isNull() + } + + @Test + fun `an in-body edit removing a block's only local class queues a reindex`() { + val reindexed = LinkedBlockingQueue() + env.ktSymbolIndex.indexWorker.commandIntakeObserver = { cmd -> + if (cmd is IndexCommand.IndexModifiedFile) reindexed.add(cmd.ktFile) + } + val withLocalClass = original.replace("val x = 1", "class L\n val x = 1") + val path = open("RemovedLocalClass.kt", withLocalClass) + propertyType(path, "x") + val document = env.ktSymbolIndex.incrementalDocument(path)!!.ktFile + assertThat(reindexed.poll(5, TimeUnit.SECONDS)).isSameInstanceAs(document) + + edit(path, original, 2) + + assertThat(reindexed.poll(5, TimeUnit.SECONDS)).isSameInstanceAs(document) + } +} diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InPlacePsiEditServicesTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InPlacePsiEditServicesTest.kt new file mode 100644 index 0000000000..d19892f913 --- /dev/null +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InPlacePsiEditServicesTest.kt @@ -0,0 +1,48 @@ +package com.itsaky.androidide.lsp.kotlin.compiler.index + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.lsp.kotlin.compiler.write +import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest +import org.jetbrains.kotlin.com.intellij.openapi.application.ApplicationManager +import org.jetbrains.kotlin.com.intellij.openapi.command.CommandProcessor +import org.jetbrains.kotlin.com.intellij.openapi.progress.EmptyProgressIndicator +import org.jetbrains.kotlin.com.intellij.psi.impl.BlockSupportImpl +import org.jetbrains.kotlin.com.intellij.psi.impl.ChangedPsiRangeUtil +import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.FileElement +import org.jetbrains.kotlin.psi.KtNamedFunction +import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType +import org.junit.Test + +class InPlacePsiEditServicesTest : KtLspTest() { + override val enableParserEventSystem = true + + @Test + fun `an in-place block reparse applies to the same KtFile`() { + val oldText = "fun f(a: Int): Int {\n val x = 1\n return a + x\n}" + val newText = oldText.replace("val x = 1", "val x = 2") + val file = env.ktSymbolIndex.parser.createFile("InPlace.kt", oldText) + val functionBefore = file.findDescendantOfType() + val project = env.project + + val oldRootType = + project.write { + ApplicationManager.getApplication().runWriteAction { + val fileNode = file.node as FileElement + val range = ChangedPsiRangeUtil.getChangedPsiRange(file, fileNode, newText)!! + val result = + BlockSupportImpl.reparse(file, fileNode, range, newText, EmptyProgressIndicator(), oldText) + CommandProcessor.getInstance().executeCommand( + project, + { result.log.doActualPsiChange(file) }, + null, + null, + ) + result.oldRoot.elementType.toString() + } + } + + assertThat((file.node as FileElement).textMatches(newText)).isTrue() + assertThat(file.findDescendantOfType()).isSameInstanceAs(functionBefore) + assertThat(oldRootType).isEqualTo("BLOCK") + } +} diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InPlaceRefreshTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InPlaceRefreshTest.kt new file mode 100644 index 0000000000..8d27da1a53 --- /dev/null +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/InPlaceRefreshTest.kt @@ -0,0 +1,195 @@ +package com.itsaky.androidide.lsp.kotlin.compiler.index + +import com.itsaky.androidide.eventbus.events.editor.ChangeType +import com.itsaky.androidide.eventbus.events.editor.DocumentChangeEvent +import com.itsaky.androidide.eventbus.events.editor.DocumentCloseEvent +import com.itsaky.androidide.eventbus.events.editor.DocumentOpenEvent +import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPriority +import com.itsaky.androidide.lsp.kotlin.compiler.write +import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest +import com.itsaky.androidide.models.Range +import com.itsaky.androidide.projects.FileManager +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.CoroutineStart +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.future.future +import kotlinx.coroutines.runBlocking +import org.jetbrains.kotlin.analysis.api.components.KaDiagnosticCheckerFilter +import org.jetbrains.kotlin.psi.KtFile +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNotSame +import org.junit.Assert.assertSame +import org.junit.Test +import java.nio.file.Path +import java.util.concurrent.TimeUnit.SECONDS + +/** Requirement: an open document keeps one [KtFile], edited in place, with pins deferring the edit. */ +internal class InPlaceRefreshTest : KtLspTest() { + override val enableParserEventSystem = true + + private val openedPaths = mutableListOf() + + @After + fun closeDocs() { + openedPaths.forEach { FileManager.onDocumentClose(DocumentCloseEvent(it)) } + openedPaths.clear() + } + + private fun sourcePath(relativePath: String): Path = env.sourceRoots.first().resolve(relativePath) + + private fun openDocument( + path: Path, + content: String, + ) { + FileManager.onDocumentOpen(DocumentOpenEvent(path, content, 1)) + openedPaths.add(path) + } + + private fun closeDocument(path: Path) { + FileManager.onDocumentClose(DocumentCloseEvent(path)) + openedPaths.remove(path) + } + + private fun changeDocument( + path: Path, + content: String, + version: Int, + ) { + FileManager.onDocumentContentChange( + DocumentChangeEvent(path, content, content, version, ChangeType.NEW_TEXT, 0, Range.NONE), + ) + } + + @OptIn(UnpinnedKtFileAccess::class) + private fun currentInstance(path: Path): KtFile? { + runBlocking { env.ktSymbolIndex.refreshCurrentKtFile(path) } + return env.ktSymbolIndex.peekLiveKtFile(path) + } + + private fun pinnedText(path: Path): String? = env.ktSymbolIndex.withLiveKtFile(path) { live -> live.read { it.text } } + + @Test + fun `a version bump keeps the same KtFile and its new text`() { + createSourceFile("A.kt", "fun a() {}") + val path = sourcePath("A.kt") + openDocument(path, "fun a() {}") + val v1 = currentInstance(path) + + changeDocument(path, "fun a() {\n println()\n}", 2) + val v2 = currentInstance(path) + + assertNotNull(v1) + assertSame(v1, v2) + assertEquals("fun a() {\n println()\n}", pinnedText(path)) + } + + @Test + fun `an edit while the path is pinned is deferred and applied when the pin is released`() { + createSourceFile("B.kt", "fun b() {}") + val path = sourcePath("B.kt") + openDocument(path, "fun b() {}") + currentInstance(path) + + val textInsidePin = + env.ktSymbolIndex.withLiveKtFile(path) { live -> + changeDocument(path, "fun b() {}\nfun c() {}", 2) + runBlocking { env.ktSymbolIndex.refreshCurrentKtFile(path) } + live.read { it.text } + } + + assertEquals("fun b() {}", textInsidePin) + assertEquals("fun b() {}\nfun c() {}", pinnedText(path)) + } + + /** + * A refresh can reach the document after a pin took its read stamp but before the pin is + * published; it must not edit under the stamp, and must not be mistaken for the new version later. + */ + @Test + fun `a refresh that finds the document locked is deferred and a later refresh applies it`() { + createSourceFile("C.kt", "fun c() {}") + val path = sourcePath("C.kt") + openDocument(path, "fun c() {}") + currentInstance(path) + val doc = env.ktSymbolIndex.incrementalDocument(path)!! + + val stamp = doc.editLock.readLock() + try { + changeDocument(path, "fun c() {}\nfun d() {}", 2) + runBlocking { env.ktSymbolIndex.refreshCurrentKtFile(path) } + assertEquals(1, doc.appliedVersion) + } finally { + doc.editLock.unlockRead(stamp) + } + + assertEquals("fun c() {}\nfun d() {}", pinnedText(path)) + assertEquals(2, doc.appliedVersion) + } + + @Test + fun `closing a document while an edit is deferred does not apply it`() { + createSourceFile("D.kt", "fun d() {}") + val path = sourcePath("D.kt") + openDocument(path, "fun d() {}") + currentInstance(path) + + env.ktSymbolIndex.withLiveKtFile(path) { + changeDocument(path, "fun d() {}\nfun e() {}", 2) + runBlocking { env.ktSymbolIndex.refreshCurrentKtFile(path) } + closeDocument(path) + env.ktSymbolIndex.invalidateCurrent(path) + } + + assertEquals(null, env.ktSymbolIndex.incrementalDocument(path)) + assertEquals("fun d() {}", pinnedText(path)) + } + + @Test + fun `a reparse failure falls back to a full parse with correct analysis`() { + createSourceFile("E.kt", "fun e(): Int = 1") + val path = sourcePath("E.kt") + openDocument(path, "fun e(): Int = 1") + currentInstance(path) + val failing = env.ktSymbolIndex.incrementalDocument(path)!! + failing.reparseOverride = { ReparseOutcome.Failed("forced by test", null) } + + changeDocument(path, "fun e(): Int = 1\nfun f(): Int = e()", 2) + val diagnosticMessages = + env.ktSymbolIndex.withLiveKtFile(path) { live -> + live.analyzing(AnalysisPriority.DIAGNOSTICS, noopCancelChecker()) { ktFile -> + ktFile + .collectDiagnostics(KaDiagnosticCheckerFilter.EXTENDED_AND_COMMON_CHECKERS) + .map { it.defaultMessage } + } + } + + assertNotSame(failing, env.ktSymbolIndex.incrementalDocument(path)) + assertEquals("fun e(): Int = 1\nfun f(): Int = e()", pinnedText(path)) + assertEquals(emptyList(), diagnosticMessages) + } + + @Test + fun `a full parse that finishes after close leaves no document behind`() { + createSourceFile("G.kt", "fun g() {}") + val path = sourcePath("G.kt") + openDocument(path, "fun g() {}") + + // The parse needs project.read, so holding the write lock keeps it in flight across the close. + val refresh = + env.project.write { + // Undispatched, so the refresh is launched on this thread before the close below. + val inFlight = + CoroutineScope(Dispatchers.IO).future(start = CoroutineStart.UNDISPATCHED) { + env.ktSymbolIndex.refreshCurrentKtFile(path) + } + closeDocument(path) + env.ktSymbolIndex.invalidateCurrent(path) + inFlight + } + refresh.get(10, SECONDS) + + assertEquals(null, env.ktSymbolIndex.incrementalDocument(path)) + } +} diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IncrementalKtDocumentTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IncrementalKtDocumentTest.kt new file mode 100644 index 0000000000..a483cd93af --- /dev/null +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/IncrementalKtDocumentTest.kt @@ -0,0 +1,154 @@ +package com.itsaky.androidide.lsp.kotlin.compiler.index + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.logging.provider.IdeLogRouter +import com.itsaky.androidide.lsp.kotlin.compiler.write +import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest +import org.jetbrains.kotlin.com.intellij.openapi.application.ApplicationManager +import org.jetbrains.kotlin.com.intellij.psi.PsiDocumentManager +import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.FileElement +import org.jetbrains.kotlin.psi.KtFile +import org.junit.Test +import org.slf4j.event.Level +import java.util.concurrent.CopyOnWriteArrayList + +/** Requirement: one [KtFile] per open document, with each new text applied to that same instance. */ +class IncrementalKtDocumentTest : KtLspTest() { + override val enableParserEventSystem = true + + private val oldText = "fun f(a: Int): Int {\n val x = 1\n return a + x\n}" + + private fun document(text: String = oldText): IncrementalKtDocument = + IncrementalKtDocument(env.ktSymbolIndex.parser.createFile("Doc.kt", text), appliedVersion = 1) + + private fun IncrementalKtDocument.applyUnderWrite( + text: String, + version: Int, + ): ReparseOutcome = + env.project.write { + ApplicationManager.getApplication().runWriteAction { apply(text, version) } + } + + private fun KtFile.treeMatches(text: String): Boolean = (node as FileElement).textMatches(text) + + @Test + fun `an in-body edit returns InBlock with the reparsed block attached to the same file`() { + val doc = document() + val newText = oldText.replace("val x = 1", "val x = 2") + + val outcome = doc.applyUnderWrite(newText, 2) + + assertThat(outcome).isInstanceOf(ReparseOutcome.InBlock::class.java) + assertThat((outcome as ReparseOutcome.InBlock).reparsed.containingKtFile).isSameInstanceAs(doc.ktFile) + assertThat(doc.ktFile.treeMatches(newText)).isTrue() + assertThat(doc.appliedVersion).isEqualTo(2) + } + + @Test + fun `an edit that unbalances braces returns WholeFile and the text matches`() { + val doc = document() + val newText = oldText.replace("val x = 1", "val x = {") + + val outcome = doc.applyUnderWrite(newText, 2) + + assertThat(outcome).isEqualTo(ReparseOutcome.WholeFile) + assertThat(doc.ktFile.treeMatches(newText)).isTrue() + } + + @Test + fun `replacing the whole buffer returns WholeFile on the same KtFile`() { + val doc = document() + val fileBefore = doc.ktFile + val newText = "class Other {\n val y = 2\n}\n" + + val outcome = doc.applyUnderWrite(newText, 2) + + assertThat(outcome).isEqualTo(ReparseOutcome.WholeFile) + assertThat(doc.ktFile).isSameInstanceAs(fileBefore) + assertThat(doc.ktFile.treeMatches(newText)).isTrue() + } + + @Test + fun `an unchanged text returns Unchanged`() { + val doc = document() + + val outcome = doc.applyUnderWrite(oldText, 2) + + assertThat(outcome).isEqualTo(ReparseOutcome.Unchanged) + } + + @Test + fun `the light file content follows the edit`() { + val doc = document() + val newText = oldText.replace("val x = 1", "val x = 2") + + doc.applyUnderWrite(newText, 2) + + val contents = doc.ktFile.viewProvider.contents + assertThat(contents.toString()).isEqualTo(newText) + } + + /** Diagnostic and edit ranges map offsets to lines through this document (see `toRange`). */ + @Test + fun `the PSI document follows every edit`() { + val doc = document() + val firstText = oldText.replace("val x = 1", "val x = 2") + val secondText = firstText.replace("return a + x", "return a +\n x") + + doc.applyUnderWrite(firstText, 2) + doc.applyUnderWrite(secondText, 3) + + val document = PsiDocumentManager.getInstance(env.project).getDocument(doc.ktFile)!! + assertThat(document.text).isEqualTo(secondText) + assertThat(document.lineCount).isEqualTo(5) + } + + /** + * IntelliJ checks the tree against the view provider's contents while the PSI change is applied, and + * reports a mismatch as an error carrying the file text, so the contents must already be the new text. + */ + @Test + fun `an in-place edit logs no IntelliJ error`() { + val doc = document() + + val errors = errorsLoggedDuring { doc.applyUnderWrite(oldText.replace("val x = 1", "val x = 12"), 2) } + + assertThat(errors).isEmpty() + } + + @Test + fun `an in-place edit logs no IntelliJ error when the file has a document`() { + val doc = document() + PsiDocumentManager.getInstance(env.project).getDocument(doc.ktFile)!! + + val errors = errorsLoggedDuring { doc.applyUnderWrite(oldText.replace("val x = 1", "val x = 12"), 2) } + + assertThat(errors).isEmpty() + } + + /** An uncommitted document would make IntelliJ reparse the file again on its next commit. */ + @Test + fun `an in-place edit leaves the file's document committed and in step with the tree`() { + val doc = document() + val documentManager = PsiDocumentManager.getInstance(env.project) + val document = documentManager.getDocument(doc.ktFile)!! + val newText = oldText.replace("val x = 1", "val x = 12") + + doc.applyUnderWrite(newText, 2) + + assertThat(document.text).isEqualTo(newText) + assertThat(documentManager.isCommitted(document)).isTrue() + } + + private fun errorsLoggedDuring(action: () -> Unit): List { + val errors = CopyOnWriteArrayList() + val sink = IdeLogRouter.ExternalSink { level, _, message, _ -> if (level == Level.ERROR) errors += message } + IdeLogRouter.addSink(sink) + try { + action() + } finally { + IdeLogRouter.removeSink(sink) + } + return errors + } +} diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/LiveKtFilePinTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/LiveKtFilePinTest.kt index 25a766672e..c3bc6e5b64 100644 --- a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/LiveKtFilePinTest.kt +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/LiveKtFilePinTest.kt @@ -5,6 +5,7 @@ import com.itsaky.androidide.eventbus.events.editor.ChangeType import com.itsaky.androidide.eventbus.events.editor.DocumentChangeEvent import com.itsaky.androidide.eventbus.events.editor.DocumentCloseEvent import com.itsaky.androidide.eventbus.events.editor.DocumentOpenEvent +import com.itsaky.androidide.lsp.kotlin.compiler.read import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest import com.itsaky.androidide.models.Range import com.itsaky.androidide.projects.FileManager @@ -59,9 +60,10 @@ internal class LiveKtFilePinTest : KtLspTest() { private fun bumpVersion( path: Path, version: Int, + text: String = content, ) { FileManager.onDocumentContentChange( - DocumentChangeEvent(path, content, content, version, ChangeType.NEW_TEXT, 0, Range.NONE), + DocumentChangeEvent(path, text, text, version, ChangeType.NEW_TEXT, 0, Range.NONE), ) } @@ -164,31 +166,34 @@ internal class LiveKtFilePinTest : KtLspTest() { fun `a refresh owed during a pin is applied after release`() { val path = openDocument() - val pinnedId = - env.ktSymbolIndex.withLiveKtFile(path) { live -> - bumpVersion(path, 2) - // Answered from the pin, which leaves the refresh for the new version owed. - runBlocking { env.ktSymbolIndex.refreshCurrentKtFile(path) } - val id = live.read { System.identityHashCode(it) } - val cached = env.ktSymbolIndex.peekLiveKtFile(path) - assertThat(System.identityHashCode(cached)).isEqualTo(id) - id - }!! + val bumped = "$content\n\nfun added(): Int = 0" + env.ktSymbolIndex.withLiveKtFile(path) { live -> + bumpVersion(path, 2, bumped) + // Answered from the pin, which leaves the refresh for the new version owed. + runBlocking { env.ktSymbolIndex.refreshCurrentKtFile(path) } + val id = live.read { System.identityHashCode(it) } + val cached = env.ktSymbolIndex.peekLiveKtFile(path) + assertThat(System.identityHashCode(cached)).isEqualTo(id) + } // Nothing below asks for the current file, so only the release's own deferred refresh can - // replace the cached instance. - assertThat(awaitInstanceChange(path, pinnedId)).isTrue() + // bring the live file to the new text. + assertThat(awaitLiveText(path, bumped)).isTrue() } + /** + * Polls through [KtSymbolIndex.peekLiveKtFile] because it never refreshes: acquiring a pin would + * launch the refresh itself and hide a release that forgot to. + */ @OptIn(UnpinnedKtFileAccess::class) - private fun awaitInstanceChange( + private fun awaitLiveText( path: Path, - staleId: Int, + expected: String, ): Boolean { val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(REFRESH_TIMEOUT_SECONDS) while (System.nanoTime() < deadline) { - val current = env.ktSymbolIndex.peekLiveKtFile(path) - if (current != null && System.identityHashCode(current) != staleId) return true + val text = env.project.read { env.ktSymbolIndex.peekLiveKtFile(path)?.text } + if (text == expected) return true Thread.sleep(POLL_INTERVAL_MILLIS) } return false diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/RefreshPreemptsAnalysisTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/RefreshPreemptsAnalysisTest.kt new file mode 100644 index 0000000000..b240d1c65c --- /dev/null +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/index/RefreshPreemptsAnalysisTest.kt @@ -0,0 +1,160 @@ +package com.itsaky.androidide.lsp.kotlin.compiler.index + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.eventbus.events.editor.ChangeType +import com.itsaky.androidide.eventbus.events.editor.DocumentChangeEvent +import com.itsaky.androidide.eventbus.events.editor.DocumentCloseEvent +import com.itsaky.androidide.eventbus.events.editor.DocumentOpenEvent +import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPreemptedException +import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPriority +import com.itsaky.androidide.lsp.kotlin.compiler.modules.ScheduledCancelChecker +import com.itsaky.androidide.lsp.kotlin.compiler.modules.withAnalysisLock +import com.itsaky.androidide.lsp.kotlin.compiler.read +import com.itsaky.androidide.lsp.kotlin.fixtures.KtLspTest +import com.itsaky.androidide.models.Range +import com.itsaky.androidide.progress.ICancelChecker +import com.itsaky.androidide.projects.FileManager +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withTimeout +import org.junit.After +import org.junit.Test +import java.nio.file.Path +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicReference +import kotlin.concurrent.thread + +/** + * Applying an edit needs `project.write`, which every analysis blocks by holding `project.read` for + * its whole run. Completion waits on that refresh before it ever reaches the scheduler, so without a + * barrier a long INDEXING analysis delays the keystroke's completion by its full duration. + */ +internal class RefreshPreemptsAnalysisTest : KtLspTest() { + override val enableParserEventSystem = true + + private val openedPaths = mutableListOf() + + @After + fun closeDocs() { + openedPaths.forEach { FileManager.onDocumentClose(DocumentCloseEvent(it)) } + openedPaths.clear() + } + + private val content = + """ + package p + + class Widget + """.trimIndent() + + private fun openDocument(): Path { + createSourceFile("Main.kt", content) + val path = env.sourceRoots.first().resolve("Main.kt") + FileManager.onDocumentOpen(DocumentOpenEvent(path, content, 1)) + openedPaths.add(path) + return path + } + + @Test(timeout = 30_000) + fun `a refresh does not wait for a running indexing analysis`() { + val path = openDocument() + assertRefreshPreemptsIndexing(path, content, 2) + } + + @Test(timeout = 30_000) + fun `an in-place edit does not wait for a running indexing analysis`() { + val path = openDocument() + runBlocking { env.ktSymbolIndex.refreshCurrentKtFile(path) } + val document = env.ktSymbolIndex.incrementalDocument(path)!! + + val edited = + """ + package p + + class Widget { + fun size(): Int = 1 + } + """.trimIndent() + assertRefreshPreemptsIndexing(path, edited, 2) + + assertThat(env.ktSymbolIndex.incrementalDocument(path)).isSameInstanceAs(document) + assertThat(document.appliedVersion).isEqualTo(2) + } + + @Test(timeout = 30_000) + fun `a version bump with unchanged content does not preempt a running analysis`() { + val path = openDocument() + runBlocking { env.ktSymbolIndex.refreshCurrentKtFile(path) } + val document = env.ktSymbolIndex.incrementalDocument(path)!! + + val started = CountDownLatch(1) + val analysisError = AtomicReference() + val cleanup = ICancelChecker.Default() + val checker = ScheduledCancelChecker(cleanup) + val analysis = runIndexingAnalysis(checker, started, analysisError) + try { + assertThat(started.await(5, SECONDS)).isTrue() + FileManager.onDocumentContentChange( + DocumentChangeEvent(path, content, content, 2, ChangeType.NEW_TEXT, 0, Range.NONE), + ) + + runBlocking { withTimeout(3_000) { env.ktSymbolIndex.refreshCurrentKtFile(path) } } + + analysis.join(500) + assertThat(analysisError.get()).isNull() + assertThat(analysis.isAlive).isTrue() + assertThat(document.appliedVersion).isEqualTo(2) + } finally { + cleanup.cancel() + analysis.join() + } + } + + private fun runIndexingAnalysis( + checker: ScheduledCancelChecker, + started: CountDownLatch, + analysisError: AtomicReference, + ): Thread = + thread { + try { + env.project.read { + withAnalysisLock(AnalysisPriority.INDEXING, checker) { + started.countDown() + while (true) { + checker.abortIfCancelled() + Thread.sleep(10) + } + } + } + } catch (e: Throwable) { + analysisError.set(e) + } + } + + private fun assertRefreshPreemptsIndexing( + path: Path, + newContent: String, + version: Int, + ) { + val started = CountDownLatch(1) + val analysisError = AtomicReference() + // Cancelled in finally so a regression (refresh timed out) still stops the analysis and frees the lock. + val cleanup = ICancelChecker.Default() + val checker = ScheduledCancelChecker(cleanup) + val analysis = runIndexingAnalysis(checker, started, analysisError) + try { + assertThat(started.await(5, SECONDS)).isTrue() + FileManager.onDocumentContentChange( + DocumentChangeEvent(path, newContent, newContent, version, ChangeType.NEW_TEXT, 0, Range.NONE), + ) + + runBlocking { withTimeout(3_000) { env.ktSymbolIndex.refreshCurrentKtFile(path) } } + + analysis.join(5_000) + assertThat(analysisError.get()).isInstanceOf(AnalysisPreemptedException::class.java) + } finally { + cleanup.cancel() + analysis.join() + } + } +} diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/modules/AnalysisPreemptionBarrierTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/modules/AnalysisPreemptionBarrierTest.kt new file mode 100644 index 0000000000..ed9259e9d4 --- /dev/null +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/modules/AnalysisPreemptionBarrierTest.kt @@ -0,0 +1,128 @@ +package com.itsaky.androidide.lsp.kotlin.compiler.modules + +import com.google.common.truth.Truth.assertThat +import com.itsaky.androidide.progress.ICancelChecker +import org.junit.Test +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicReference +import kotlin.concurrent.thread + +/** + * An edit's write must not wait behind a running or queued analysis: while + * [AnalysisScheduler.preemptingAnalysis] runs, the holder is preempted and every other requester + * leaves with [AnalysisPreemptedException], releasing the read lock the write needs. + */ +class AnalysisPreemptionBarrierTest { + private fun checker() = ScheduledCancelChecker(ICancelChecker.NOOP) + + @Test(timeout = 10_000) + fun `raising the barrier preempts the running holder`() { + val held = CountDownLatch(1) + val release = CountDownLatch(1) + val preempted = CountDownLatch(1) + val holder = + thread { + AnalysisScheduler.withLock(AnalysisPriority.INDEXING, checker(), onPreempt = { preempted.countDown() }) { + held.countDown() + release.await(5, SECONDS) + } + } + try { + assertThat(held.await(5, SECONDS)).isTrue() + + AnalysisScheduler.preemptingAnalysis { } + + assertThat(preempted.await(1, SECONDS)).isTrue() + } finally { + release.countDown() + holder.join() + } + } + + @Test(timeout = 10_000) + fun `a requester waiting for the lock leaves when the barrier goes up`() { + val held = CountDownLatch(1) + val release = CountDownLatch(1) + // The waiter preempts the INDEXING holder from inside acquire's loop, so this marks it queued. + val waiterQueued = CountDownLatch(1) + val waiterThrew = CountDownLatch(1) + val waiterError = AtomicReference() + val holder = + thread { + AnalysisScheduler.withLock(AnalysisPriority.INDEXING, checker(), onPreempt = { waiterQueued.countDown() }) { + held.countDown() + release.await(5, SECONDS) + } + } + var waiter: Thread? = null + try { + assertThat(held.await(5, SECONDS)).isTrue() + waiter = + thread { + try { + AnalysisScheduler.withLock(AnalysisPriority.DIAGNOSTICS, checker(), onPreempt = {}) { } + } catch (e: Throwable) { + waiterError.set(e) + waiterThrew.countDown() + } + } + assertThat(waiterQueued.await(5, SECONDS)).isTrue() + + val waiterLeft = AnalysisScheduler.preemptingAnalysis { waiterThrew.await(1, SECONDS) } + + assertThat(waiterLeft).isTrue() + assertThat(waiterError.get()).isInstanceOf(AnalysisPreemptedException::class.java) + } finally { + release.countDown() + holder.join() + waiter?.join() + } + } + + @Test(timeout = 10_000) + fun `requests after the barrier comes down proceed normally`() { + AnalysisScheduler.preemptingAnalysis { } + + val result = AnalysisScheduler.withLock(AnalysisPriority.INTERACTIVE, checker(), onPreempt = {}) { 42 } + + assertThat(result).isEqualTo(42) + } + + @Test(timeout = 10_000) + fun `the barrier is lowered when preempting the holder throws`() { + val held = CountDownLatch(1) + val release = CountDownLatch(1) + val holder = + thread { + AnalysisScheduler.withLock( + AnalysisPriority.INDEXING, + checker(), + onPreempt = { throw IllegalStateException("onPreempt failed") }, + ) { + held.countDown() + release.await(5, SECONDS) + } + } + try { + assertThat(held.await(5, SECONDS)).isTrue() + runCatching { AnalysisScheduler.preemptingAnalysis { } } + } finally { + release.countDown() + holder.join() + } + + val result = AnalysisScheduler.withLock(AnalysisPriority.INTERACTIVE, checker(), onPreempt = {}) { 42 } + + assertThat(result).isEqualTo(42) + } + + @Test(timeout = 10_000) + fun `the barrier is lowered when the block throws`() { + runCatching { AnalysisScheduler.preemptingAnalysis { throw IllegalStateException("block failed") } } + + val result = AnalysisScheduler.withLock(AnalysisPriority.INTERACTIVE, checker(), onPreempt = {}) { 42 } + + assertThat(result).isEqualTo(42) + } +} diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/util/SLF4JLoggerTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/util/SLF4JLoggerTest.kt index bd89089b76..5c4cba3683 100644 --- a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/util/SLF4JLoggerTest.kt +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/compiler/util/SLF4JLoggerTest.kt @@ -6,19 +6,32 @@ import org.jetbrains.kotlin.com.intellij.openapi.diagnostic.Logger import org.junit.Test class SLF4JLoggerTest : KtLspTest() { + @Test + fun `compiler logger factory routes to SLF4JLogger`() { + assertThat(Logger.getInstance("ADFA-4238")).isInstanceOf(SLF4JLogger::class.java) + } - @Test - fun `compiler logger factory routes to SLF4JLogger`() { - assertThat(Logger.getInstance("ADFA-4238")).isInstanceOf(SLF4JLogger::class.java) - } + @Test + fun `error does not throw - regression guard for ADFA-4238`() { + // DefaultLogger.error() rethrows as AssertionError, escalating a recoverable FIR cache + // inconsistency into a crash (Sentry APPDEVFORALL-13D). SLF4JLogger must log, not throw. + val logger = Logger.getInstance("ADFA-4238") + logger.error("Inconsistency in the cache. Someone without context put a null value in the cache") + logger.error("with throwable", RuntimeException("boom")) + logger.error("with details", RuntimeException(), "detail-1", "detail-2") + } - @Test - fun `error does not throw - regression guard for ADFA-4238`() { - // DefaultLogger.error() rethrows as AssertionError, escalating a recoverable FIR cache - // inconsistency into a crash (Sentry APPDEVFORALL-13D). SLF4JLogger must log, not throw. - val logger = Logger.getInstance("ADFA-4238") - logger.error("Inconsistency in the cache. Someone without context put a null value in the cache") - logger.error("with throwable", RuntimeException("boom")) - logger.error("with details", RuntimeException(), "detail-1", "detail-2") - } + @Test + fun `an error logs its attachments' sizes, not their contents`() { + // IntelliJ attaches whole file texts to errors such as "Inconsistent tree"; inlined, one error + // wrote thousands of log lines per keystroke. + val fileText = "class Foo\n".repeat(1_000) + + val message = SLF4JLogger.errorMessage("Inconsistent kotlin.FILE tree", arrayOf("/src/Foo.kt", fileText, null)) + + assertThat(message).startsWith("Inconsistent kotlin.FILE tree") + assertThat(message).doesNotContain("class Foo") + assertThat(message).contains("2 attachments") + assertThat(message).contains("${fileText.length}") + } } diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/fixtures/KtLspTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/fixtures/KtLspTest.kt index f341656042..acfc59b217 100644 --- a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/fixtures/KtLspTest.kt +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/fixtures/KtLspTest.kt @@ -1,6 +1,7 @@ package com.itsaky.androidide.lsp.kotlin.fixtures import com.itsaky.androidide.lsp.kotlin.compiler.index.toMetadata +import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPreemptedException import com.itsaky.androidide.lsp.kotlin.compiler.modules.AnalysisPriority import com.itsaky.androidide.lsp.kotlin.compiler.modules.ScheduledCancelChecker import com.itsaky.androidide.lsp.kotlin.compiler.modules.UnpinnedAnalysis @@ -83,6 +84,26 @@ abstract class KtLspTest { /** A fresh scheduler-aware checker with no upstream cancellation source, for call sites that require one. */ internal fun noopCancelChecker(): ScheduledCancelChecker = ScheduledCancelChecker(ICancelChecker.NOOP) + /** + * A checker whose first checkpoint reports a preemption and every later one passes, as when another + * path's edit raises the analysis barrier once while a request runs. + */ + internal fun preemptedOnceCancelChecker(): ICancelChecker = + object : ICancelChecker { + private var preempted = false + + override fun cancel() {} + + override fun isCancelled(): Boolean = false + + override fun abortIfCancelled() { + if (!preempted) { + preempted = true + throw AnalysisPreemptedException() + } + } + } + /** Resolves [call] to a function call and runs [action] inside the analyze block. */ protected fun analyzeMaybeDanglingForTest( call: KtCallElement, diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractMethodPlanEndToEndTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractMethodPlanEndToEndTest.kt index 9da1a5642b..a4fa46f6e5 100644 --- a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractMethodPlanEndToEndTest.kt +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractMethodPlanEndToEndTest.kt @@ -1478,4 +1478,22 @@ class ExtractMethodPlanEndToEndTest : KtLspTest() { val text = apply(content, buildExtractMethodRewrites(content, candidate, "emit")!!) assertTrue("the literal must not gain an indent level", text.contains("\nline one\n")) } + + @Test + fun `a plan preempted once by another edit is retried rather than dropped`() { + val content = + """ + package p + fun demo(a: Int, b: Int): Int { + return b * a + a + } + """.trimIndent() + createSourceFile("Main.kt", content) + val path = env.sourceRoots.first().resolve("Main.kt") + val offset = content.indexOf("b * a") + 1 + + val result = buildExtractMethodPlan(env, path, offset, offset, 1, preemptedOnceCancelChecker()) + + assertTrue(result.candidates.any { it.label == "b * a" }) + } } diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractVariablePlanEndToEndTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractVariablePlanEndToEndTest.kt index ce407201a5..7232d1cfb6 100644 --- a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractVariablePlanEndToEndTest.kt +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/ExtractVariablePlanEndToEndTest.kt @@ -1085,4 +1085,22 @@ class ExtractVariablePlanEndToEndTest : KtLspTest() { assertEquals(listOf("b", "b * c", "a + b * c"), result.candidates.map { it.label }) } + + @Test + fun `a plan preempted once by another edit is retried rather than reported empty`() { + val content = + """ + package p + fun demo(n: Int): Int { + return n * 2 + } + """.trimIndent() + createSourceFile("Main.kt", content) + val path = env.sourceRoots.first().resolve("Main.kt") + val start = content.indexOf("n * 2") + + val result = buildExtractionPlan(env, path, start, start + "n * 2".length, 1, preemptedOnceCancelChecker()) + + assertEquals(listOf("n * 2"), result.candidates.map { it.label }) + } } diff --git a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/InlineVariablePlanEndToEndTest.kt b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/InlineVariablePlanEndToEndTest.kt index daf92d367a..8a5dab6963 100644 --- a/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/InlineVariablePlanEndToEndTest.kt +++ b/lsp/kotlin/src/test/java/com/itsaky/androidide/lsp/kotlin/utils/refactor/InlineVariablePlanEndToEndTest.kt @@ -1075,4 +1075,22 @@ class InlineVariablePlanEndToEndTest : KtLspTest() { apply(content, buildInlineVariableRewrites(result, InlineMode.AllReferences)!!), ) } + + @Test + fun `a plan preempted once by another edit is retried rather than dropped`() { + val content = + """ + package p + fun demo(a: Int, b: Int): Int { + val total = a + b + return total * 2 + } + """.trimIndent() + createSourceFile("Main.kt", content) + val path = env.sourceRoots.first().resolve("Main.kt") + + val result = buildInlineVariablePlan(env, path, at(content, "total"), 1, preemptedOnceCancelChecker()) + + assertNull(result.refusal) + } }