-
-
Notifications
You must be signed in to change notification settings - Fork 24
ADFA-3320: introduce KtFileManager to manage parsed KtFile instances #1143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
1b069cb
fix: add ability to re-write class name references in desugar plugin
itsaky-adfa a70fd66
feat: integrate Kotlin analysis API
itsaky-adfa 4ca1e97
Merge branch 'stage' into fix/ADFA-3366-include-analysis-api-as-depen…
itsaky-adfa 43286a6
fix: update kotlin-android to latest version
itsaky-adfa f1fe62f
fix: remove UnsafeImpl
itsaky-adfa 6e6d8b3
fix: update kotlin-android to latest version
itsaky-adfa b208658
fix: replace usages of Unsafe with UnsafeImpl
itsaky-adfa c844ad2
fix: make Kotlin LSP no-op
itsaky-adfa 58db2cb
feat: configure K2 standalone session when setting up LSP
itsaky-adfa bf7acd1
fix: JvmTarget resolution fails for input "21"
itsaky-adfa dc62a51
fix: do not early-init VirtualFileSystem
itsaky-adfa 4b1c8e4
fix: remove replaceClass desugar instruction for Unsafe
itsaky-adfa b14f6ee
fix: ensure boot class path is added as dependency to Android modules
itsaky-adfa 54ca7a9
feat: add diagnostic provider for Kotlin
itsaky-adfa 6e4d458
fix: remove unnecessary log statement
itsaky-adfa 5d841a3
fix: update to latest kotlin-android release
itsaky-adfa 4b7b0f2
fix: always re-initialize K2 session on setupWithProject
itsaky-adfa e2f137a
fix: diagnostics are always collected from the on-disk file
itsaky-adfa 09fc9ea
feat: add the ability to incrementally invalidate source roots on pro…
itsaky-adfa dd3d519
fix: dispatch build-related events from GradleBuildService
itsaky-adfa d6defa6
feat: introduct KtFileManager
itsaky-adfa 1586b8f
fix: remove unused onSourcesChanged func
itsaky-adfa 85807ca
fix: remove IncrementalModificationTracker
itsaky-adfa abecf1a
fix: remove unused Gradle build events
itsaky-adfa 68e316a
Merge branch 'stage' into feat/ADFA-3320-session-source-files-invalid…
itsaky-adfa 4988c26
Merge branch 'stage' into feat/ADFA-3320-session-source-files-invalid…
itsaky-adfa d54e63a
Merge branch 'feat/ADFA-3320-session-source-files-invalidation' into …
itsaky-adfa 6d858b3
Merge branch 'stage' into feat/ADFA-3320-KtFileManager
itsaky-adfa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/FileEventConsumer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.itsaky.androidide.lsp.kotlin | ||
|
|
||
| import java.nio.file.Path | ||
|
|
||
| interface FileEventConsumer { | ||
|
|
||
| fun onFileOpened(path: Path, content: String) | ||
| fun onFileClosed(path: Path) | ||
|
|
||
| fun onFileContentChanged(path: Path, content: String) | ||
| fun onFileSaved(path: Path) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
lsp/kotlin/src/main/java/com/itsaky/androidide/lsp/kotlin/KtFileManager.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| package com.itsaky.androidide.lsp.kotlin | ||
|
|
||
| import org.jetbrains.kotlin.analysis.api.KaSession | ||
| import org.jetbrains.kotlin.analysis.api.analyze | ||
| import org.jetbrains.kotlin.analysis.api.analyzeCopy | ||
| import org.jetbrains.kotlin.analysis.api.projectStructure.KaDanglingFileResolutionMode | ||
| import org.jetbrains.kotlin.com.intellij.openapi.editor.Document | ||
| import org.jetbrains.kotlin.com.intellij.openapi.vfs.StandardFileSystems | ||
| import org.jetbrains.kotlin.com.intellij.openapi.vfs.VirtualFileManager | ||
| import org.jetbrains.kotlin.com.intellij.psi.PsiDocumentManager | ||
| import org.jetbrains.kotlin.com.intellij.psi.PsiManager | ||
| import org.jetbrains.kotlin.psi.KtFile | ||
| import org.jetbrains.kotlin.psi.KtPsiFactory | ||
| import org.slf4j.LoggerFactory | ||
| import java.nio.file.Path | ||
| import java.util.concurrent.ConcurrentHashMap | ||
| import kotlin.io.path.name | ||
| import kotlin.io.path.pathString | ||
| import kotlin.time.Clock | ||
| import kotlin.time.Instant | ||
|
|
||
| /** | ||
| * Manages [KtFile] instances for all open files. | ||
| */ | ||
| class KtFileManager( | ||
| private val psiFactory: KtPsiFactory, | ||
| private val psiManager: PsiManager, | ||
| private val psiDocumentManager: PsiDocumentManager, | ||
| ) : FileEventConsumer, AutoCloseable { | ||
|
|
||
| companion object { | ||
| private val logger = LoggerFactory.getLogger(KtFileManager::class.java) | ||
| } | ||
|
|
||
| private val entries = ConcurrentHashMap<Path, ManagedFile>() | ||
|
|
||
| @ConsistentCopyVisibility | ||
| data class ManagedFile @Deprecated("Use ManagedFile.create instead") internal constructor( | ||
| val file: Path, | ||
| val diskKtFile: KtFile, | ||
| @Volatile var inMemoryKtFile: KtFile, | ||
| val document: Document, | ||
| @Volatile var lastModified: Instant, | ||
| @Volatile var isDirty: Boolean, | ||
| @Volatile var analyzeTimestamp: Instant, | ||
| ) { | ||
|
|
||
| /** | ||
| * Analyze this [ManagedFile] contents. | ||
| * | ||
| * @param action The analysis action. | ||
| */ | ||
| fun <R> analyze(action: KaSession.(file: KtFile) -> R): R { | ||
| if (diskKtFile === inMemoryKtFile) { | ||
| return analyze(useSiteElement = inMemoryKtFile) { action(inMemoryKtFile) } | ||
| } | ||
|
|
||
| return analyzeCopy( | ||
| useSiteElement = inMemoryKtFile, | ||
| resolutionMode = KaDanglingFileResolutionMode.PREFER_SELF | ||
| ) { | ||
| action(inMemoryKtFile) | ||
| } | ||
| } | ||
|
|
||
| fun createInMemoryFileWithContent(psiFactory: KtPsiFactory, content: String): KtFile { | ||
| val inMemoryFile = psiFactory.createFile(file.name, content) | ||
| inMemoryFile.originalFile = diskKtFile | ||
| return inMemoryFile | ||
| } | ||
|
|
||
| companion object { | ||
| @Suppress("DEPRECATION") | ||
| fun create( | ||
| file: Path, | ||
| ktFile: KtFile, | ||
| document: Document, | ||
| inMemoryKtFile: KtFile = ktFile, | ||
| lastModified: Instant = Clock.System.now(), | ||
| isDirty: Boolean = false, | ||
| analyzeTimestamp: Instant = Instant.DISTANT_PAST, | ||
| ) = | ||
| ManagedFile( | ||
| file = file, | ||
| diskKtFile = ktFile, | ||
| inMemoryKtFile = inMemoryKtFile, | ||
| document = document, | ||
| lastModified = lastModified, | ||
| isDirty = isDirty, | ||
| analyzeTimestamp = analyzeTimestamp, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| override fun onFileOpened(path: Path, content: String) { | ||
| logger.debug("onFileOpened: {}", path) | ||
|
|
||
| entries[path]?.let { existing -> | ||
| logger.info("File is already opened, updating content") | ||
| updateDocumentContent(existing, content) | ||
| return | ||
| } | ||
|
|
||
| val ktFile = resolveKtFile(path) | ||
|
|
||
| if (ktFile == null) { | ||
| logger.warn("Cannot resolve KtFile for: {}", path) | ||
| return | ||
| } | ||
|
|
||
| val document = getOrCreateDocument(ktFile) | ||
| if (document == null) { | ||
| logger.warn("Cannot obtain Document for: {}", path) | ||
| return | ||
| } | ||
|
|
||
| logger.info("Creating managed file entry") | ||
| val entry = ManagedFile.create( | ||
| file = path, | ||
| ktFile = ktFile, | ||
| document = document, | ||
| ) | ||
|
|
||
| entries[path] = entry | ||
|
|
||
| updateDocumentContent(entry, content) | ||
| logger.debug("File opened and managed: {}", path) | ||
| return | ||
| } | ||
|
|
||
| override fun onFileContentChanged(path: Path, content: String) { | ||
| logger.debug("onFileContentChanged: {}", path) | ||
| val entry = entries[path] ?: run { | ||
| logger.debug("Content changed for unmanaged file: {}. Ignoring.", path) | ||
| return | ||
| } | ||
|
|
||
| updateDocumentContent(entry, content) | ||
| } | ||
|
|
||
| override fun onFileSaved(path: Path) { | ||
| val entry = entries[path] ?: return | ||
| entry.isDirty = false | ||
|
|
||
| logger.debug("File saved: {}", path) | ||
| } | ||
|
|
||
| override fun onFileClosed(path: Path) { | ||
| entries.remove(path) ?: return | ||
| logger.debug("File closed: {}", path) | ||
| } | ||
|
|
||
| fun getOpenFile(path: Path): ManagedFile? = entries[path] | ||
|
|
||
| fun allOpenFiles(): Collection<ManagedFile> = | ||
| entries.values.toList() | ||
|
|
||
| fun clearAnalyzeTimestampOf(file: Path) { | ||
| val managed = getOpenFile(file) ?: return | ||
| managed.analyzeTimestamp = Instant.DISTANT_PAST | ||
| } | ||
|
|
||
| private fun resolveKtFile(path: Path): KtFile? { | ||
| val vfs = VirtualFileManager.getInstance() | ||
| .getFileSystem(StandardFileSystems.FILE_PROTOCOL) | ||
|
|
||
| val virtualFile = vfs.refreshAndFindFileByPath(path.pathString) | ||
|
itsaky-adfa marked this conversation as resolved.
|
||
| ?: return null | ||
|
|
||
| val psiFile = psiManager.findFile(virtualFile) | ||
|
|
||
| return psiFile as? KtFile | ||
| } | ||
|
|
||
| private fun getOrCreateDocument(ktFile: KtFile): Document? { | ||
| return psiDocumentManager.getDocument(ktFile) | ||
| } | ||
|
|
||
| private fun updateDocumentContent(entry: ManagedFile, content: String) { | ||
| logger.info("Updating doc content for {}", entry.file) | ||
|
|
||
| val normalized = content.replace("\r", "") | ||
| if (entry.inMemoryKtFile.text == normalized) return | ||
|
|
||
| entry.inMemoryKtFile = entry.createInMemoryFileWithContent(psiFactory, content) | ||
| entry.lastModified = Clock.System.now() | ||
| entry.isDirty = true | ||
| } | ||
|
|
||
| override fun close() { | ||
| entries.clear() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.