Added M68kIncludeFileProvider, but dunno what it's actually for.

This commit is contained in:
2021-08-06 19:12:54 +02:00
parent ddf78ec210
commit 8d7977927f
24 changed files with 199 additions and 35 deletions
@@ -10,7 +10,7 @@ class M68kFileElementType private constructor() : ILightStubFileElementType<PsiF
@JvmField
val INSTANCE = M68kFileElementType()
const val STUB_VERSION = 5
const val STUB_VERSION = 6
const val STUB_EXTERNAL_ID_PREFIX = "MC68000."
const val EXTERNAL_ID = STUB_EXTERNAL_ID_PREFIX + "FILE"
}
@@ -72,4 +72,13 @@ object LexerUtil {
lexer.yybegin(_M68kLexer.MACROLINE)
return TokenType.WHITE_SPACE
}
fun unquoteString(string: String) = string.run {
when {
startsWith('"') -> removeSurrounding("\"")
startsWith('\'') -> removeSurrounding("'")
startsWith('<') -> removeSurrounding("<", ">")
else -> this
}
}
}
@@ -173,7 +173,9 @@ AsmOp ::= MNEMONIC OperandSize? {
methods = [getMnemonic getOpSize]
}
PreprocessorDirective ::= Label? (DATA_DIRECTIVE | OTHER_DIRECTIVE)
PreprocessorKeyword ::= (DATA_DIRECTIVE | OTHER_DIRECTIVE)
PreprocessorDirective ::= Label? PreprocessorKeyword
PreprocessorOperands?
MacroPlainLine ::= MACRO_LINE
@@ -2,21 +2,13 @@ package de.platon42.intellij.plugins.m68k.psi
import com.intellij.extapi.psi.ASTWrapperPsiElement
import com.intellij.lang.ASTNode
import de.platon42.intellij.plugins.m68k.lexer.LexerUtil
abstract class M68kLiteralExprMixin(node: ASTNode) : ASTWrapperPsiElement(node), M68kLiteralExpr {
override fun getValue(): Any? {
val childNode = firstChild.node
when (childNode.elementType) {
M68kTypes.STRINGLIT -> {
return text.run {
when {
startsWith('"') -> removeSurrounding("\"")
startsWith('\'') -> removeSurrounding("'")
startsWith('<') -> removeSurrounding(">")
else -> this
}
}
}
M68kTypes.STRINGLIT -> LexerUtil.unquoteString(childNode.text)
M68kTypes.DECIMAL -> {
try {
return childNode.text.toInt()
@@ -46,6 +38,6 @@ abstract class M68kLiteralExprMixin(node: ASTNode) : ASTWrapperPsiElement(node),
}
}
}
return text
return childNode.text
}
}
@@ -0,0 +1,59 @@
package de.platon42.intellij.plugins.m68k.scanner
import com.intellij.openapi.fileTypes.FileType
import com.intellij.openapi.fileTypes.FileTypeRegistry
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.impl.include.FileIncludeInfo
import com.intellij.psi.impl.include.FileIncludeProvider
import com.intellij.psi.impl.source.tree.LightTreeUtil
import com.intellij.util.Consumer
import com.intellij.util.PathUtilRt
import com.intellij.util.indexing.FileContent
import com.intellij.util.indexing.PsiDependentFileContent
import de.platon42.intellij.plugins.m68k.M68kFileType.Companion.INSTANCE
import de.platon42.intellij.plugins.m68k.lexer.LexerUtil
import de.platon42.intellij.plugins.m68k.psi.M68kTypes
class M68kIncludeFileProvider : FileIncludeProvider() {
override fun getId(): String {
return "mc68000_include"
}
override fun acceptFile(file: VirtualFile): Boolean {
return FileTypeRegistry.getInstance().isFileOfType(file, INSTANCE)
}
override fun registerFileTypesUsedForIndexing(fileTypeSink: Consumer<in FileType?>) {
fileTypeSink.consume(INSTANCE)
}
override fun getIncludeInfos(content: FileContent): Array<FileIncludeInfo> {
if (content.contentAsText.indexOfAny(listOf("include", "incbin"), ignoreCase = true) < 0) return emptyArray()
val tree = (content as PsiDependentFileContent).lighterAST
val statements = LightTreeUtil.getChildrenOfType(tree, tree.root, M68kTypes.STATEMENT).asSequence()
.mapNotNull { LightTreeUtil.firstChildOfType(tree, it, M68kTypes.PREPROCESSOR_DIRECTIVE) }
.mapNotNull {
val keywordNode = LightTreeUtil.firstChildOfType(tree, it, M68kTypes.PREPROCESSOR_KEYWORD) ?: return@mapNotNull null
val keyword = LightTreeUtil.toFilteredString(tree, keywordNode, null)
if (keyword.equals("include", true) || keyword.equals("incbin", true)) {
keyword to it
} else {
null
}
}
.mapNotNull {
val pathNode = LightTreeUtil.firstChildOfType(tree, it.second, M68kTypes.LITERAL_EXPR) ?: return@mapNotNull null
val path = LexerUtil.unquoteString(LightTreeUtil.toFilteredString(tree, pathNode, null)).replace("\\", "/")
if (it.first.equals("include", true)) {
FileIncludeInfo(path)
} else {
FileIncludeInfo(PathUtilRt.getFileName(path), path, 0, true)
}
}
.toList()
return statements.toTypedArray()
}
}