Skip to content

Commit

Permalink
Cleanup PSI names
Browse files Browse the repository at this point in the history
  • Loading branch information
sblundy committed Nov 16, 2020
1 parent bee2a6d commit 4d2f580
Show file tree
Hide file tree
Showing 21 changed files with 54 additions and 49 deletions.
5 changes: 3 additions & 2 deletions src/gen/com/github/sblundy/elvish/lang/ElvishParser.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/gen/com/github/sblundy/elvish/psi/ElvishFnCommand.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/gen/com/github/sblundy/elvish/psi/ElvishParameter.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/main/grammars/Elvish.bnf
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Lambda ::= LambdaArguments? OPEN_BRACE Chunk CLOSE_BRACE {
extends="com.github.sblundy.elvish.psi.ElvishLambdaBase"
}
LambdaArguments ::=OPEN_BRACKET Space? ((parameter | MapPair) Space?)* CLOSE_BRACKET
parameter ::= AT_SYMBOL? Compound {
parameter ::= AT_SYMBOL? VariableName {
extends="com.github.sblundy.elvish.psi.ElvishParameterBase"
implements="com.github.sblundy.elvish.psi.ElvishVariableDeclaration"
}
Expand Down Expand Up @@ -170,6 +170,7 @@ FinallyBlock ::= KEYWORD_FINALLY Space OPEN_BRACE Chunk CLOSE_BRACE
FnCommand ::= KEYWORD_FN Space VariableName Space LambdaArguments? OPEN_BRACE Chunk CLOSE_BRACE {
extends="com.github.sblundy.elvish.psi.FnCommandBase"
implements="com.github.sblundy.elvish.psi.ElvishFunctionDeclaration"
methods=[commandName="VariableName"]
}
UseCommand ::= KEYWORD_USE Space (RelativeModuleSpec | LibModuleSpec) (Space ModuleAlias)?
LibModuleSpec ::= (VARIABLE_CHAR+'.')* ModulePathSegment* (VariableName COLON)* VariableName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ElvishStructureViewFactory : PsiStructureViewFactory {

private class ElvishStructureViewFunction(fn: ElvishFnCommand): PsiTreeElementBase<ElvishFnCommand>(fn) {
override fun getChildrenBase(): MutableCollection<StructureViewTreeElement> = mutableListOf()
override fun getPresentableText(): String? = element?.variableName?.text
override fun getPresentableText(): String? = element?.getCommandName()?.text
override fun getIcon(open: Boolean): Icon = AllIcons.Nodes.Method
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ElvishUsagesProvider : FindUsagesProvider {
return when (element) {
is ElvishVariable -> element.text
is ElvishParameter -> element.text
is ElvishFnCommand -> element.variableName.text
is ElvishFnCommand -> element.getCommandName().text
else -> ""
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal class ElvishCommandReference(element: ElvishCommandExpressionBase, rang
val variants = mutableListOf<Any>()
scope.processFnCommands {
when (it) {
is ElvishFnCommand -> variants.add(LookupElementBuilder.create(it, it.variableName.text).withIcon(AllIcons.Nodes.Function))
is ElvishFnCommand -> variants.add(LookupElementBuilder.create(it, it.getCommandName().text).withIcon(AllIcons.Nodes.Function))
is ElvishPsiBuiltinCommand -> variants.add(LookupElementBuilder.create(it, it.name).withIcon(ElvishIcons.BUILTIN_FUNCTION))
else -> log.error("function type not supported: ${it.javaClass.canonicalName}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ import com.intellij.psi.PsiElement
*/
interface ElvishFunctionDeclaration : PsiElement {
fun nameMatches(ref: ReferenceWithNamespacePsiElement): Boolean
fun getCommandName(): ElvishVariableName
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@ import com.intellij.psi.PsiNameIdentifierOwner
import com.intellij.util.IncorrectOperationException
import javax.swing.Icon

abstract class ElvishParameterBase(node: ASTNode) : ASTWrapperPsiElement(node), PsiNameIdentifierOwner {
override fun getNameIdentifier(): PsiElement? = compound
abstract class ElvishParameterBase(node: ASTNode) : ASTWrapperPsiElement(node), PsiNameIdentifierOwner, ElvishParameter {
override fun getNameIdentifier(): PsiElement? = getVariableName()

override fun getName(): String? = compound.text
override fun getName(): String? = getVariableName().text

@Throws(IncorrectOperationException::class)
override fun setName(name: String): PsiElement {
//TODO is actually a variable name?
val ne = newNameElement(name, project)
compound.replace(ne)
getVariableName().replace(ne)
return this
}

fun nameMatches(ref: ReferenceWithNamespacePsiElement): Boolean = !ref.hasNamespace && compound.textMatches(ref.targetElement)

abstract val compound: ElvishCompound
override fun nameMatches(ref: ReferenceWithNamespacePsiElement): Boolean = !ref.hasNamespace && getVariableName().textMatches(ref.targetElement)

override fun getIcon(flags: Int): Icon? = AllIcons.Nodes.Parameter

override fun getPresentation(): ItemPresentation? = ElvishBasicItemPresentation(compound.text, AllIcons.Nodes.Parameter)
override fun getPresentation(): ItemPresentation? = ElvishBasicItemPresentation(getVariableName().text, AllIcons.Nodes.Parameter)
}
11 changes: 11 additions & 0 deletions src/main/kotlin/com/github/sblundy/elvish/psi/ElvishPsiBuiltin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,35 @@ sealed class ElvishPsiBuiltin(private val manager: PsiManager) : FakePsiElement(

class ElvishPsiBuiltinCommand(override val builtin: String, manager: PsiManager) : ElvishPsiBuiltin(manager),
ElvishFunctionDeclaration {
private val commandName = FakeVariableName(builtin, manager)
override fun nameMatches(ref: ReferenceWithNamespacePsiElement) = !ref.hasNamespace && ref.targetElement.textMatches(builtin)
override fun getCommandName(): ElvishVariableName = commandName

override val isDoNotUse: Boolean = builtin.startsWith('-') && builtin != "-"
override fun getIcon(open: Boolean): Icon? = ElvishIcons.BUILTIN_FUNCTION
override fun getPresentation(): ItemPresentation? = ElvishBasicItemPresentation(builtin, ElvishIcons.BUILTIN_FUNCTION)
}

class ElvishPsiBuiltinVariable(override val builtin: String, manager: PsiManager) : ElvishPsiBuiltin(manager),
ElvishVariableDeclaration {
private val variableName = FakeVariableName(builtin, manager)
override fun nameMatches(ref: ReferenceWithNamespacePsiElement) = !ref.hasNamespace && ref.targetElement.textMatches(builtin)
override fun getVariableName(): ElvishVariableName = variableName
override val isDoNotUse: Boolean = builtin.startsWith('-')
override fun getIcon(open: Boolean): Icon? = ElvishIcons.BUILTIN_VARIABLE
override fun getPresentation(): ItemPresentation? = ElvishBasicItemPresentation(builtin, ElvishIcons.BUILTIN_VARIABLE)
}

class ElvishPsiBuiltinValue(override val builtin: String, manager: PsiManager) : ElvishPsiBuiltin(manager),
ElvishVariableDeclaration {
private val variableName = FakeVariableName(builtin, manager)
override fun nameMatches(ref: ReferenceWithNamespacePsiElement) = !ref.hasNamespace && ref.targetElement.textMatches(builtin)
override fun getVariableName(): ElvishVariableName = variableName
override val isDoNotUse: Boolean = builtin.startsWith('-')
override fun getIcon(open: Boolean): Icon? = ElvishIcons.BUILTIN_VALUE
override fun getPresentation(): ItemPresentation? = ElvishBasicItemPresentation(builtin, ElvishIcons.BUILTIN_VALUE)
}

private class FakeVariableName(override val builtin: String, manager: PsiManager): ElvishPsiBuiltin(manager), ElvishVariableName {
override val isDoNotUse: Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ object ElvishPsiUtils {
fun newNameElement(name: String, myProject: Project): ElvishVariableName {
val file = createDummyFile("$name = 1", myProject)
val chunk = file.firstChild as ElvishChunk
return chunk.assignmentList[0].variableList[0].variableName
return chunk.assignmentList[0].variableList[0].getVariableName()
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ import com.intellij.psi.PsiElement
*/
interface ElvishVariableDeclaration : PsiElement {
fun nameMatches(ref:ReferenceWithNamespacePsiElement): Boolean
fun getVariableName(): ElvishVariableName
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ internal class ElvishVariableReference(element: ElvishVariableRefBase, rangeInEl
}

private fun ElvishFnCommand.toLookupElement(): LookupElement {
return LookupElementBuilder.create(this, variableName.text + "~").withIcon(AllIcons.Nodes.Function)
return LookupElementBuilder.create(this, getCommandName().text + "~").withIcon(AllIcons.Nodes.Function)
}

private fun ElvishPsiBuiltinCommand.toLookupElement(): LookupElement {
return LookupElementBuilder.create(this, "$name~").withIcon(ElvishIcons.BUILTIN_FUNCTION)
}

private fun ElvishParameter.toLookupElement(): LookupElement {
return LookupElementBuilder.create(this, compound.text).withIcon(AllIcons.Nodes.Parameter)
return LookupElementBuilder.create(this, getVariableName().text).withIcon(AllIcons.Nodes.Parameter)
}

private fun ElvishVariable.toLookupElement(): LookupElement {
val namespacePrefix = namespaceName?.let { namespaceName -> namespaceName.variableNameList.map { it.text }}?: emptyList()
val fullname = (namespacePrefix+variableName.text).joinToString(":")
val fullname = (namespacePrefix+getVariableName().text).joinToString(":")
return LookupElementBuilder.create(this, fullname)
.withPresentableText(fullname).withIcon(AllIcons.Nodes.Variable)
}
Expand Down
14 changes: 6 additions & 8 deletions src/main/kotlin/com/github/sblundy/elvish/psi/FnCommandBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,26 @@ import javax.swing.Icon

abstract class FnCommandBase(node: ASTNode) : ElvishLambdaBase(node), ElvishFunctionDeclaration, PsiNameIdentifierOwner {
override fun nameMatches(ref: ReferenceWithNamespacePsiElement): Boolean =
!ref.hasNamespace && variableName.textMatches(ref.targetElement)
!ref.hasNamespace && getCommandName().textMatches(ref.targetElement)

abstract val variableName: ElvishVariableName

override fun getNameIdentifier(): PsiElement? = variableName
override fun getNameIdentifier(): PsiElement? = getCommandName()

override fun getName(): String? {
return variableName.text
return getCommandName().text
}

@Throws(IncorrectOperationException::class)
override fun setName(name: String): PsiElement {
val ne = ElvishPsiUtils.newNameElement(name, project)
variableName.replace(ne)
getCommandName().replace(ne)
return this
}

override fun getTextOffset(): Int {
return variableName.textOffset
return getCommandName().textOffset
}

override fun getIcon(flags: Int): Icon? = AllIcons.Nodes.Function

override fun getPresentation(): ItemPresentation? = ElvishBasicItemPresentation(variableName.text, AllIcons.Nodes.Function)
override fun getPresentation(): ItemPresentation? = ElvishBasicItemPresentation(getCommandName().text, AllIcons.Nodes.Function)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ Elvish File
ElvishLambdaArgumentsImpl(LAMBDA_ARGUMENTS)
PsiElement(ElvishTokenType.[)('[')
ElvishParameterImpl(PARAMETER)
ElvishCompoundImpl(COMPOUND)
ElvishBarewordImpl(BAREWORD)
PsiElement(ElvishTokenType.VARIABLE_CHAR)('y')
ElvishVariableNameImpl(VARIABLE_NAME)
PsiElement(ElvishTokenType.VARIABLE_CHAR)('y')
PsiElement(ElvishTokenType.])(']')
PsiElement(ElvishTokenType.{)('{')
ElvishChunkImpl(CHUNK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ Elvish File
ElvishLambdaArgumentsImpl(LAMBDA_ARGUMENTS)
PsiElement(ElvishTokenType.[)('[')
ElvishParameterImpl(PARAMETER)
ElvishCompoundImpl(COMPOUND)
ElvishBarewordImpl(BAREWORD)
PsiElement(ElvishTokenType.VARIABLE_CHAR)('y')
ElvishVariableNameImpl(VARIABLE_NAME)
PsiElement(ElvishTokenType.VARIABLE_CHAR)('y')
PsiElement(ElvishTokenType.])(']')
PsiElement(ElvishTokenType.{)('{')
ElvishChunkImpl(CHUNK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ Elvish File
ElvishLambdaArgumentsImpl(LAMBDA_ARGUMENTS)
PsiElement(ElvishTokenType.[)('[')
ElvishParameterImpl(PARAMETER)
ElvishCompoundImpl(COMPOUND)
ElvishBarewordImpl(BAREWORD)
PsiElement(ElvishTokenType.VARIABLE_CHAR)('y')
ElvishVariableNameImpl(VARIABLE_NAME)
PsiElement(ElvishTokenType.VARIABLE_CHAR)('y')
PsiElement(ElvishTokenType.INLINE_WHITESPACE)(' ')
ElvishParameterImpl(PARAMETER)
PsiElement(ElvishTokenType.@)('@')
ElvishCompoundImpl(COMPOUND)
ElvishBarewordImpl(BAREWORD)
PsiElement(ElvishTokenType.VARIABLE_CHAR)('rest')
ElvishVariableNameImpl(VARIABLE_NAME)
PsiElement(ElvishTokenType.VARIABLE_CHAR)('rest')
PsiElement(ElvishTokenType.])(']')
PsiElement(ElvishTokenType.{)('{')
ElvishChunkImpl(CHUNK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ Elvish File
ElvishLambdaArgumentsImpl(LAMBDA_ARGUMENTS)
PsiElement(ElvishTokenType.[)('[')
ElvishParameterImpl(PARAMETER)
ElvishCompoundImpl(COMPOUND)
ElvishBarewordImpl(BAREWORD)
PsiElement(ElvishTokenType.VARIABLE_CHAR)('i')
ElvishVariableNameImpl(VARIABLE_NAME)
PsiElement(ElvishTokenType.VARIABLE_CHAR)('i')
PsiElement(ElvishTokenType.])(']')
PsiElement(ElvishTokenType.{)('{')
ElvishChunkImpl(CHUNK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ Elvish File
PsiElement(ElvishTokenType.[)('[')
ElvishParameterImpl(PARAMETER)
PsiElement(ElvishTokenType.@)('@')
ElvishCompoundImpl(COMPOUND)
ElvishBarewordImpl(BAREWORD)
PsiElement(ElvishTokenType.VARIABLE_CHAR)('y')
ElvishVariableNameImpl(VARIABLE_NAME)
PsiElement(ElvishTokenType.VARIABLE_CHAR)('y')
PsiElement(ElvishTokenType.])(']')
PsiElement(ElvishTokenType.{)('{')
ElvishChunkImpl(CHUNK)
Expand Down

0 comments on commit 4d2f580

Please sign in to comment.