generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TableGen] Implement resolution of
include
statements (#52)
- Loading branch information
Showing
27 changed files
with
680 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains 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
88 changes: 88 additions & 0 deletions
88
...main/kotlin/com/github/zero9178/mlirods/clion/CMakeTableGenCompilationCommandsProvider.kt
This file contains 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,88 @@ | ||
package com.github.zero9178.mlirods.clion | ||
|
||
import com.github.zero9178.mlirods.model.TableGenCompilationCommandsProvider | ||
import com.github.zero9178.mlirods.model.CompilationCommandsState | ||
import com.github.zero9178.mlirods.model.IncludePaths | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.diagnostic.logger | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.vfs.VirtualFileManager | ||
import com.intellij.openapi.vfs.newvfs.BulkFileListener | ||
import com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent | ||
import com.intellij.openapi.vfs.newvfs.events.VFileDeleteEvent | ||
import com.intellij.openapi.vfs.newvfs.events.VFileEvent | ||
import com.intellij.openapi.vfs.newvfs.events.VFileMoveEvent | ||
import com.intellij.util.messages.impl.subscribeAsFlow | ||
import kotlinx.coroutines.currentCoroutineContext | ||
import kotlinx.coroutines.ensureActive | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.combine | ||
import kotlinx.coroutines.flow.filterNotNull | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.transform | ||
import org.yaml.snakeyaml.LoaderOptions | ||
import org.yaml.snakeyaml.Yaml | ||
import org.yaml.snakeyaml.constructor.Constructor | ||
import java.io.FileNotFoundException | ||
import kotlin.io.path.Path | ||
|
||
// Note: Needs to be public due to limitations in snakeyaml. | ||
data class FileInfoDto( | ||
var filepath: String = "", | ||
var includes: String = "", | ||
) | ||
|
||
private val LOG = logger<CMakeTableGenCompilationCommandsProvider>() | ||
|
||
class CMakeTableGenCompilationCommandsProvider : TableGenCompilationCommandsProvider { | ||
|
||
override fun getCompilationCommandsFlow(project: Project): Flow<CompilationCommandsState> { | ||
// React to VCS changes that might create, move or delete one of the files returned by the flow. | ||
val vfsChangeFlow = project.messageBus.subscribeAsFlow(VirtualFileManager.VFS_CHANGES) { | ||
send(Unit) | ||
object : BulkFileListener { | ||
override fun after(events: List<VFileEvent>) { | ||
if (events.any { | ||
it is VFileCreateEvent || it is VFileMoveEvent || it is VFileDeleteEvent | ||
}) trySend(Unit) | ||
} | ||
} | ||
} | ||
|
||
return project.service<CMakeActiveProfileService>().profileFlow.filterNotNull().map { | ||
it.generationDir.resolve("tablegen_compile_commands.yml") | ||
}.transform { file -> | ||
try { | ||
val result = file.inputStream().use { inputStream -> | ||
Yaml(Constructor(FileInfoDto::class.java, LoaderOptions())).loadAll(inputStream) | ||
.filterIsInstance<FileInfoDto>() | ||
} | ||
emit(result) | ||
} catch (_: FileNotFoundException) { | ||
// Swallow completely. | ||
} catch (e: Throwable) { | ||
// Rethrow cancellations. | ||
currentCoroutineContext().ensureActive() | ||
// Otherwise just warn. | ||
LOG.warn(e) | ||
} | ||
}.combine(vfsChangeFlow) { dtos, _ -> | ||
val instance = VirtualFileManager.getInstance() | ||
val map = dtos.flatMap { | ||
val virtualFile = instance.findFileByNioPath(Path(it.filepath)) | ||
if (virtualFile == null) { | ||
LOG.warn("failed to find virtual file for ${it.filepath}") | ||
return@flatMap emptyList() | ||
} | ||
|
||
listOf( | ||
virtualFile to IncludePaths( | ||
it.includes.split( | ||
';' | ||
).map { it -> Path(it) }) | ||
) | ||
}.associate { it } | ||
CompilationCommandsState(map) | ||
} | ||
} | ||
} |
This file contains 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
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/github/zero9178/mlirods/index/TableGenIncludingIndex.kt
This file contains 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,17 @@ | ||
package com.github.zero9178.mlirods.index | ||
|
||
import com.github.zero9178.mlirods.language.generated.psi.TableGenIncludeDirective | ||
import com.intellij.psi.stubs.StringStubIndexExtension | ||
import com.intellij.psi.stubs.StubIndexKey | ||
|
||
val INCLUDED_INDEX = StubIndexKey.createIndexKey<String, TableGenIncludeDirective>("INCLUDED_INDEX") | ||
|
||
/** | ||
* Maps a filename (not path!) to every include directive that ends with that file name. | ||
*/ | ||
private class TableGenIncludingIndex : StringStubIndexExtension<TableGenIncludeDirective>() { | ||
|
||
override fun getKey(): StubIndexKey<String, TableGenIncludeDirective> { | ||
return INCLUDED_INDEX | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains 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
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/github/zero9178/mlirods/language/psi/TableGenIncludeManipulator.kt
This file contains 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,19 @@ | ||
package com.github.zero9178.mlirods.language.psi | ||
|
||
import com.github.zero9178.mlirods.language.generated.psi.TableGenIncludeDirective | ||
import com.intellij.openapi.util.TextRange | ||
import com.intellij.psi.AbstractElementManipulator | ||
|
||
class TableGenIncludeManipulator : AbstractElementManipulator<TableGenIncludeDirective>() { | ||
override fun handleContentChange( | ||
element: TableGenIncludeDirective, | ||
range: TextRange, | ||
newContent: String? | ||
): TableGenIncludeDirective? { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun getRangeInElement(element: TableGenIncludeDirective): TextRange { | ||
return element.string?.textRangeInParent ?: TextRange.EMPTY_RANGE | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/com/github/zero9178/mlirods/language/psi/TableGenIncludeReference.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.github.zero9178.mlirods.language.psi | ||
|
||
import com.github.zero9178.mlirods.language.generated.psi.TableGenIncludeDirective | ||
import com.github.zero9178.mlirods.model.TableGenIncludeGraph | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.vfs.VirtualFileManager | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiManager | ||
import com.intellij.psi.PsiReferenceBase | ||
|
||
class TableGenIncludeReference(element: TableGenIncludeDirective) : | ||
PsiReferenceBase<TableGenIncludeDirective>(element) { | ||
override fun resolve(): PsiElement? { | ||
|
||
val file = element.containingFile | ||
val project = file.project | ||
|
||
val vf = file.virtualFile ?: return null | ||
val includes = project.service<TableGenIncludeGraph>().getIncludePaths(vf) | ||
|
||
for (include in includes) { | ||
val file = VirtualFileManager.getInstance().findFileByNioPath(include.resolve(element.includeSuffix)) | ||
?: continue | ||
return PsiManager.getInstance(project).findFile(file) ?: continue | ||
} | ||
return null | ||
} | ||
} |
Oops, something went wrong.