From 578f8d466162bd49cde38999e1e9f3726959eb1d Mon Sep 17 00:00:00 2001 From: me Date: Fri, 8 Aug 2025 15:49:35 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=AF=AD=E6=B3=95=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 4 +- src/main/grammar/main.bnf | 146 +++++++----------- .../slint/lang/psi/SlintPsiKeyword.java | 37 ----- .../slint/brace/SlintPairedBraceMatcher.kt | 15 +- .../completion/SlintCompletionContributor.kt | 3 +- .../AbstractSlintCompletionProvider.kt | 2 +- .../provider/AtChildrenCompletionProvider.kt | 3 +- .../completion/provider/BasicTypeProvider.kt | 2 +- .../ComponentElementKeywordProvider.kt | 2 +- .../provider/ComponentNameProvider.kt | 2 +- .../provider/ExportElementProvider.kt | 2 +- .../provider/InheritsCompletionProvider.kt | 2 +- .../provider/PropertyBindingProvider.kt | 2 +- .../provider/PropertyDeclarationProvider.kt | 65 ++++++++ .../provider/SubComponentNameProvider.kt | 2 +- .../completion/provider/TopElementProvider.kt | 2 +- .../zhouxi/slint/formatter/FormattingBlock.kt | 6 +- .../formatter/SlintFormatterModelBuilder.kt | 4 - .../highlight/KeywordHighlightAnnotator.kt | 10 +- .../zhouxi/slint/lang/SlintElementFactory.kt | 1 - .../slint/lang/psi/builder/SlintPsiBuilder.kt | 29 ---- .../lang/psi/impl/SlintPsiNamedElementImpl.kt | 1 - .../stubs/stub/impl/SlintPropertyStubImpl.kt | 2 - .../stubs/types/SlintComponentElementType.kt | 5 +- .../stubs/types/SlintPropertyElementType.kt | 16 +- .../slint/lang/psi/utils/SlintTokens.kt | 6 - .../me/zhouxi/slint/preview/PreviewAction.kt | 3 +- .../provider/PropertyReferenceProvider.kt | 4 +- 28 files changed, 156 insertions(+), 222 deletions(-) delete mode 100644 src/main/java/me/zhouxi/slint/lang/psi/SlintPsiKeyword.java create mode 100644 src/main/kotlin/me/zhouxi/slint/completion/provider/PropertyDeclarationProvider.kt delete mode 100644 src/main/kotlin/me/zhouxi/slint/lang/psi/builder/SlintPsiBuilder.kt diff --git a/build.gradle.kts b/build.gradle.kts index 54a9303..2880b67 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -36,9 +36,9 @@ repositories { } intellij { - version.set("IU-2024.1") + version.set("IU-241-EAP-SNAPSHOT") sandboxDir.set("idea-sandbox") - plugins.set(listOf("java")) + plugins.set(listOf("java","Git4Idea")) } dependencies { } diff --git a/src/main/grammar/main.bnf b/src/main/grammar/main.bnf index fe82228..58c6097 100644 --- a/src/main/grammar/main.bnf +++ b/src/main/grammar/main.bnf @@ -12,9 +12,6 @@ psiPackage="me.zhouxi.slint.lang.psi" psiImplPackage="me.zhouxi.slint.lang.psi.impl" extends(".*Expression")=Expression - implements(".*Keyword")=["me.zhouxi.slint.lang.psi.SlintPsiKeyword"] - extends(".*Keyword")="me.zhouxi.slint.lang.psi.SlintPsiKeyword.Impl" - elementTypeFactory(".*Keyword")="me.zhouxi.slint.lang.psi.SlintPsiKeyword.type" tokens=[ Comma = "," FatArrow = "=>" @@ -61,7 +58,7 @@ StringLiteral = 'regexp:(^"[^"\r\n]*")' LineComment = 'regexp:^//[^\r\n]*' BlockComment = 'regexp:/\*[\s\S]*?\*/' - + ] } // @@ -70,20 +67,20 @@ private recoverTopElement ::= !('component' | 'struct' | 'enum' | 'global'| 'exp private DocumentElement ::= Import | Struct | Enum | GlobalSingleton | Component | Export { recoverWhile=recoverTopElement } -GlobalSingleton ::= ExportKeyword? GlobalKeyword Identifier '{' ComponentElement* '}' { +GlobalSingleton ::= 'export'? 'global' Identifier '{' ComponentElement* '}' { pin=2 implements=["me.zhouxi.slint.lang.psi.SlintPsiNamedElement"] mixin="me.zhouxi.slint.lang.psi.impl.SlintPsiNamedElementImpl" } //import 定义 -Import ::= ImportKeyword (ImportElement|ModuleRef)';'{ +Import ::= 'import' (ImportElement|ModuleRef)';'{ pin=1 elementTypeFactory="me.zhouxi.slint.lang.psi.stubs.types.SlintStubTypes.slintImport" stubClass="me.zhouxi.slint.lang.psi.stubs.stub.SlintImportStub" extends="me.zhouxi.slint.lang.psi.impl.SlintStubBasedPsiElementImpl" } -ImportElement ::= '{' ImportSpecifier (',' ImportSpecifier)* '}' FromKeyword ModuleRef{ +ImportElement ::= '{' ImportSpecifier (',' ImportSpecifier)* '}' 'from' ModuleRef{ pin=1 } @@ -96,28 +93,28 @@ ImportSpecifier ::= ComponentRef ImportAlias?{ } private AliasNameRecover::=!(','|'}'|';') -ImportAlias ::= AsKeyword Identifier { +ImportAlias ::= 'as' Identifier { pin=1 recoverWhile=AliasNameRecover implements=["me.zhouxi.slint.lang.psi.SlintPsiNamedElement"] extends="me.zhouxi.slint.lang.psi.impl.SlintPsiNamedElementImpl" } //Struct 定义 -Struct ::= ExportKeyword? StructKeyword Identifier (':=')? StructBody { +Struct ::= 'export'? 'struct' Identifier (':=')? StructBody { pin=2 } private StructBody ::= '{' FieldDeclarations? '}'{ pin=1 } //EnumDeclaration -Enum ::= ExportKeyword? EnumKeyword Identifier '{' (Identifier (','Identifier)*','? )? '}'{ +Enum ::= 'export'? 'enum' Identifier '{' [Identifier (','Identifier)*','? ] '}'{ pin=2 implements=["me.zhouxi.slint.lang.psi.SlintPsiNamedElement"] extends="me.zhouxi.slint.lang.psi.impl.SlintPsiNamedElementImpl" } //--------------ExportsList ------------------------------------- //ExportsList -Export ::= ExportKeyword (ExportType | ExportModule) { +Export ::= 'export' (ExportType | ExportModule) { pin=1 } @@ -129,15 +126,15 @@ ExportSpecifier::= ComponentRef ExportAlias?{ implements=["me.zhouxi.slint.lang.psi.SlintPsiNamedElement"] extends="me.zhouxi.slint.lang.psi.impl.SlintPsiNamedElementImpl" } -ExportAlias ::= AsKeyword Identifier{ +ExportAlias ::= 'as' Identifier{ pin=1 } -ExportModule ::= '*' FromKeyword ModuleRef ';'{ +ExportModule ::= '*' 'from' ModuleRef ';'{ pin=1 } -Component ::= ExportKeyword? ComponentKeyword Identifier InheritDeclaration? '{' ComponentElement* '}' { - pin=2 +Component ::= ComponentModifier '{' ComponentElement* '}' { + pin=1 implements=[ "me.zhouxi.slint.lang.psi.SlintPsiNamedElement" ] @@ -145,18 +142,21 @@ Component ::= ExportKeyword? ComponentKeyword Identifier InheritDeclaration? '{' stubClass="me.zhouxi.slint.lang.psi.stubs.stub.SlintComponentStub" extends="me.zhouxi.slint.lang.psi.impl.SlintStubBasedPsiNamedElementImpl" } + +private ComponentModifier::= 'export'? 'component' Identifier InheritDeclaration?{ + pin=2 + recoverWhile=recoverInherit +} //组件定义 -//private LegacyComponent ::= (ComponentName|NamedIdentifier ':=' ) ComponentBody -InheritDeclaration ::= InheritsKeyword ComponentRef { +InheritDeclaration ::= 'inherits' ComponentRef { pin=1 recoverWhile=recoverInherit } private recoverInherit::=!('{') //组件元素定义 -private ComponentElement ::=ChildrenPlaceholder| Property | Callback +private ComponentElement ::= ChildrenPlaceholder| PropertyDeclaration | CallbackDeclaration | Function | PropertyAnimation | CallbackConnection | Transitions - | PropertyChanged - | States | TwoWayBinding | ConditionalElement + | PropertyChanged | States | TwoWayBinding | ConditionalElement | RepetitionElement | SubComponent | PropertyBinding { recoverWhile=recoverWhileForComponentBody } @@ -167,8 +167,8 @@ ChildrenPlaceholder ::= '@' 'children'{ } //--------------------------------PropertyDeclaration Start---------------------------------------------------- // 属性定义 in property name: value / in property name <=> value -Property ::= PropertyModifier? PropertyKeyword ('<' Type '>')? Identifier (PropertyValue|PropertyTwoWayBindingValue|';'){ - pin=2 +PropertyDeclaration ::= PropertyModifier? 'property' ('<' Type '>')? Identifier PropertyDeclarationValue{ + pin=4 implements=[ "me.zhouxi.slint.lang.psi.SlintPsiNamedElement" ] @@ -176,33 +176,34 @@ Property ::= PropertyModifier? PropertyKeyword ('<' Type '>')? Identifier (Prope stubClass="me.zhouxi.slint.lang.psi.stubs.stub.SlintPropertyStub" extends="me.zhouxi.slint.lang.psi.impl.SlintStubBasedPsiNamedElementImpl" } -PropertyModifier ::= InKeyword|OutKeyword|InOutKeyword|PrivateKeyword -private PropertyValue::= ':' BindingStatement { - pin=1 -} -private PropertyTwoWayBindingValue ::= '<=>' QualifiedPropertyRef ';' { - pin=1 -} +//修饰符 +PropertyModifier ::= 'in'|'out'|'in-out'|'private' + +//属性定义的ValueBinding +PropertyDeclarationValue::=(':' BindingExpressionStatement)| ('<=>' Expression ';') | ';' + +//绑定类型表达式 +BindingExpressionStatement::= '{' '}' | Expression ';' | '{' Expression '}' | ObjectCreationExpression ';' + //--------------------------------PropertyChanged Start---------------------------------------------------- -PropertyChanged ::= ChangedKeyword LocalVariable '=>' CodeBlock{ +PropertyChanged ::= 'changed' LocalVariable '=>' CodeBlock{ pin=1 } //--------------------------------CallbackDeclaration Start---------------------------------------------------- // 回调定义 pure callback abc()->int; callback abc; callback(..);callback()->type; -Callback ::= PureKeyword? CallbackKeyword Identifier CallbackBinding? ';'{ +CallbackDeclaration ::= 'pure'? 'callback' Identifier CallbackArgument? CallbackBinding? ';'{ pin=2 implements=["me.zhouxi.slint.lang.psi.SlintPsiNamedElement"] extends="me.zhouxi.slint.lang.psi.impl.SlintPsiNamedElementImpl" } + //回调参数定义 CallbackBinding::= CallbackArgument | ('<=>'QualifiedPropertyRef) //入参定义 -CallbackArgument ::= '(' (Type (','Type)* ','? )? ')' ReturnType?{ - pin=1 -} -private ReturnType ::= '->' (QualifiedTypeRef|ArrayType){ +CallbackArgument ::= '(' (Type (','Type)* ','? )? ')' ('->' Type)?{ pin=1 } + //--------------------------------TwoWayBindingDeclaration Start---------------------------------------------------- //组件双向绑定 TwoWayBinding ::= PropertyRef '<=>' QualifiedPropertyRef ';' { @@ -211,12 +212,12 @@ TwoWayBinding ::= PropertyRef '<=>' QualifiedPropertyRef ';' { //--------------------------------FunctionDeclaration Start---------------------------------------------------- //函数定义 protected? pure? function f() -Function ::= FunctionModifiers? FunctionKeyword Identifier FunctionArguments ReturnType? CodeBlock{ - pin=2 +Function ::= FunctionModifiers? 'function' Identifier FunctionArguments ReturnType? CodeBlock{ + pin=3 implements=["me.zhouxi.slint.lang.psi.SlintPsiNamedElement"] extends="me.zhouxi.slint.lang.psi.impl.SlintPsiNamedElementImpl" } -private FunctionModifiers ::= ((PublicKeyword | ProtectedKeyword) PureKeyword?) | (PureKeyword (PublicKeyword | ProtectedKeyword)?) +private FunctionModifiers ::= (('public' | 'protected') 'pure'?) | ('pure' ('public' | 'protected')?) private FunctionArguments ::= '(' FieldDeclarations* ')'{ pin=1 @@ -230,10 +231,10 @@ CallbackConnection ::= FunctionRef CallbackConnectionArguments? '=>' CodeBlock ' private CallbackConnectionArguments ::= '(' (LocalVariable (',' LocalVariable)* ','?)? ')' //--------------------------------ConditionalElementDeclaration Start--------------------------------------------------- -ConditionalElement ::= IfKeyword Expression ':' SubComponent{ +ConditionalElement ::= 'if' Expression ':' SubComponent{ pin=1 } -RepetitionElement ::= ForKeyword RepetitionVariable InKeyword Expression ':' SubComponent { +RepetitionElement ::= 'for' RepetitionVariable 'in' Expression ':' SubComponent { pin=1 } private RepetitionVariable ::= LocalVariable RepetitionIndex?{ @@ -252,17 +253,17 @@ SubComponent ::= (Identifier ':=')? ComponentRef '{' ComponentElement* '}'{ //--------------------------------TransitionsDeclaration Start--------------------------------------------------- //过渡绑定 -Transitions ::= TransitionsKeyword '[' Transition* ']'{ +Transitions ::= 'transitions' '[' Transition* ']'{ pin=1 } // -Transition ::= (InKeyword|OutKeyword) LocalVariable ':' '{' PropertyAnimation* '}'{ +Transition ::= ('in'|'out') LocalVariable ':' '{' PropertyAnimation* '}'{ pin=1 recoverWhile=recoverForRBracket } //--------------------------------TransitionsDeclaration End--------------------------------------------------- // in | out name : { } -States ::= StatesKeyword '[' State* ']'{ +States ::= 'states' '[' State* ']'{ pin=1 } // identifier [when] : { ... } @@ -273,17 +274,20 @@ State ::= LocalVariable StateCondition? ':' '{' StateItem* '}' { recoverWhile=recoverForRBracket } private recoverForRBracket::=!(']'|'}'|';'|Identifier) -StateCondition ::= WhenKeyword Expression { +StateCondition ::= 'when' Expression { pin=1 } //状态可以由transition propertyBinding 和 animationBinding组成 private StateItem ::= QualifiedNamePropertyBinding | StateTransition -StateTransition ::= (InKeyword|OutKeyword) ':' '{' PropertyAnimation* '}'{ +StateTransition ::= ('in'|'out') ':' '{' PropertyAnimation* '}'{ pin=1 } //------------------------------------------------------------------------------------------ //类型定义 -Type ::= QualifiedTypeRef | UnnamedType | ArrayType +Type ::= TypeRef | UnnamedType | ArrayType +TypeRef::=QualifiedName{ + extends=Referred +} ArrayType ::= '[' Type ']' UnnamedType ::= '{' FieldDeclarations* '}' private FieldDeclarations ::= FieldDeclaration (',' FieldDeclaration)* ','? @@ -303,23 +307,23 @@ ExpressionStatement ::= Expression (';'+ | &'}') { //private recoverWhileStatement::=!(GenericIdentifier|';'|'}') -ReturnStatement ::= ReturnKeyword (Expression)?{ +ReturnStatement ::= 'return' (Expression)?{ pin=1 } private IfElseStatement ::= IfStatement (ElseIfStatement)* ElseStatement?{ pin=1 } -IfStatement ::= IfKeyword Expression CodeBlock { +IfStatement ::= 'if' Expression CodeBlock { pin=1 } -ElseIfStatement ::= ElseKeyword IfKeyword Expression CodeBlock{ +ElseIfStatement ::= 'else' 'if' Expression CodeBlock{ pin=2 } -ElseStatement ::= ElseKeyword CodeBlock { +ElseStatement ::= 'else' CodeBlock { pin=1 } //动画定义 -PropertyAnimation ::= AnimateKeyword ('*'| (QualifiedPropertyRef (',' QualifiedPropertyRef)*)) '{' QualifiedNamePropertyBinding*'}'{ +PropertyAnimation ::= 'animate' ('*'| (QualifiedPropertyRef (',' QualifiedPropertyRef)*)) '{' QualifiedNamePropertyBinding*'}'{ pin=1 } //组件属性绑定 name: xxx ; name : {}; name : {} @@ -480,37 +484,7 @@ private ObjectPropertyBinding ::= Identifier ':' Expression{ recoverWhile=recover } private recover::=!(';'|'}'|',') -//-------------------------------For Keyword highlighting------------------------------------ -ComponentKeyword ::= 'component' -StructKeyword ::='struct' -EnumKeyword::='enum' -GlobalKeyword::='global' -ExportKeyword::='export' -ImportKeyword::='import' -AsKeyword::='as' -FromKeyword::='from' -InheritsKeyword::='inherits' -PropertyKeyword::='property' -CallbackKeyword::='callback' -StatesKeyword::='states' -TransitionsKeyword::='transitions' -PureKeyword::='pure' -FunctionKeyword::='function' -PublicKeyword::='public' -ProtectedKeyword::='protected' -ForKeyword::='for' -IfKeyword::='if' -ChangedKeyword::='changed' -InKeyword::='in' -WhenKeyword::='when' -ElseKeyword::='else' -AnimateKeyword::='animate' -OutKeyword::='out' -InOutKeyword::='in-out' -PrivateKeyword::='private' -ReturnKeyword::='return' -//---------NamedIdentifier ,简化PsiTree----------------------------------- -//noinspection BnfUnusedRule 用于标记命名节点对应的identifier + LocalVariable ::= Identifier{ extends="me.zhouxi.slint.lang.psi.impl.SlintPsiNamedElementImpl" implements=["me.zhouxi.slint.lang.psi.SlintPsiNamedElement"] @@ -528,13 +502,9 @@ QualifiedPropertyRef ::= PropertyRef ('.' PropertyRef)*{ PropertyRef ::= Identifier{ extends=Referred } -QualifiedTypeRef::= TypeRef ('.' TypeRef)*{ - extends=TypeRef -} -TypeRef::=Identifier{ - extends=Referred -} +QualifiedName::=Identifier('.' Identifier)* + FunctionRef::=Identifier{ extends=Referred } diff --git a/src/main/java/me/zhouxi/slint/lang/psi/SlintPsiKeyword.java b/src/main/java/me/zhouxi/slint/lang/psi/SlintPsiKeyword.java deleted file mode 100644 index 6aeb7bd..0000000 --- a/src/main/java/me/zhouxi/slint/lang/psi/SlintPsiKeyword.java +++ /dev/null @@ -1,37 +0,0 @@ -package me.zhouxi.slint.lang.psi; - -import com.intellij.lang.ASTNode; -import me.zhouxi.slint.lang.SlintElementType; -import me.zhouxi.slint.lang.psi.impl.SlintPsiElementImpl; -import org.jetbrains.annotations.NotNull; - -public interface SlintPsiKeyword extends SlintPsiElement { - - - abstract class Impl extends SlintPsiElementImpl implements SlintPsiKeyword { - - public Impl(@NotNull ASTNode node) { - super(node); - } - - @Override - public String toString() { - return "SlintKeyword:" + getText(); - } - - } - - static ElementType type(String debugName) { - return ElementType.TYPE; - } - - - class ElementType extends SlintElementType { - - public static final ElementType TYPE = new ElementType("SlintKeyword"); - - private ElementType(@NotNull String debugName) { - super(debugName); - } - } -} diff --git a/src/main/kotlin/me/zhouxi/slint/brace/SlintPairedBraceMatcher.kt b/src/main/kotlin/me/zhouxi/slint/brace/SlintPairedBraceMatcher.kt index 870485b..d86dc88 100644 --- a/src/main/kotlin/me/zhouxi/slint/brace/SlintPairedBraceMatcher.kt +++ b/src/main/kotlin/me/zhouxi/slint/brace/SlintPairedBraceMatcher.kt @@ -19,11 +19,12 @@ class SlintPairedBraceMatcher : PairedBraceMatcher { return 0 } + companion object { + val Pair: Array = arrayOf( + BracePair(SlintTypes.LBrace, SlintTypes.RBrace, true), + BracePair(SlintTypes.LParent, SlintTypes.RParent, true), + BracePair(SlintTypes.LBracket, SlintTypes.RBracket, true), + BracePair(SlintTypes.LAngle, SlintTypes.RAngle, true) + ) + } } - -val Pair: Array = arrayOf( - BracePair(SlintTypes.LBrace, SlintTypes.RBrace, true), - BracePair(SlintTypes.LParent, SlintTypes.RParent, true), - BracePair(SlintTypes.LBracket, SlintTypes.RBracket, true), - BracePair(SlintTypes.LAngle, SlintTypes.RAngle, true) -) \ No newline at end of file diff --git a/src/main/kotlin/me/zhouxi/slint/completion/SlintCompletionContributor.kt b/src/main/kotlin/me/zhouxi/slint/completion/SlintCompletionContributor.kt index bb02d58..d4a01c0 100644 --- a/src/main/kotlin/me/zhouxi/slint/completion/SlintCompletionContributor.kt +++ b/src/main/kotlin/me/zhouxi/slint/completion/SlintCompletionContributor.kt @@ -18,10 +18,11 @@ class SlintCompletionContributor : CompletionContributor() { extend(InheritsCompletionProvider) extend(ExportElementProvider) extend(SubComponentNameProvider) + extend(PropertyDeclarationProvider) } private fun extend( - provider: AbstractSlintCompletionProvider, + provider: AbstractSlintCompletionProvider, type: CompletionType = CompletionType.BASIC, ) { extend(type, provider.pattern(), provider) diff --git a/src/main/kotlin/me/zhouxi/slint/completion/provider/AbstractSlintCompletionProvider.kt b/src/main/kotlin/me/zhouxi/slint/completion/provider/AbstractSlintCompletionProvider.kt index 5228f02..a5bf570 100644 --- a/src/main/kotlin/me/zhouxi/slint/completion/provider/AbstractSlintCompletionProvider.kt +++ b/src/main/kotlin/me/zhouxi/slint/completion/provider/AbstractSlintCompletionProvider.kt @@ -8,7 +8,7 @@ import com.intellij.psi.PsiElement /** * @author zhouxi 2024/5/29 */ -abstract class AbstractSlintCompletionProvider : CompletionProvider() { +abstract class AbstractSlintCompletionProvider : CompletionProvider() { abstract fun pattern(): ElementPattern } diff --git a/src/main/kotlin/me/zhouxi/slint/completion/provider/AtChildrenCompletionProvider.kt b/src/main/kotlin/me/zhouxi/slint/completion/provider/AtChildrenCompletionProvider.kt index 8b1ef7c..01e1abd 100644 --- a/src/main/kotlin/me/zhouxi/slint/completion/provider/AtChildrenCompletionProvider.kt +++ b/src/main/kotlin/me/zhouxi/slint/completion/provider/AtChildrenCompletionProvider.kt @@ -9,10 +9,11 @@ import com.intellij.patterns.PlatformPatterns.psiElement import com.intellij.psi.PsiElement import com.intellij.util.ProcessingContext import me.zhouxi.slint.lang.psi.SlintTypes.Component + /** * @author zhouxi 2024/5/29 */ -object AtChildrenCompletionProvider : AbstractSlintCompletionProvider() { +object AtChildrenCompletionProvider : AbstractSlintCompletionProvider() { override fun pattern(): ElementPattern { return psiElement() .afterLeaf(psiElement().withText("@")) diff --git a/src/main/kotlin/me/zhouxi/slint/completion/provider/BasicTypeProvider.kt b/src/main/kotlin/me/zhouxi/slint/completion/provider/BasicTypeProvider.kt index 966926d..026bb41 100644 --- a/src/main/kotlin/me/zhouxi/slint/completion/provider/BasicTypeProvider.kt +++ b/src/main/kotlin/me/zhouxi/slint/completion/provider/BasicTypeProvider.kt @@ -10,7 +10,7 @@ import com.intellij.util.ProcessingContext import me.zhouxi.slint.lang.psi.SlintPsiUtils.InternalTypes import me.zhouxi.slint.lang.psi.SlintTypes.Type -object BasicTypeProvider : AbstractSlintCompletionProvider() { +object BasicTypeProvider : AbstractSlintCompletionProvider() { override fun addCompletions( parameters: CompletionParameters, context: ProcessingContext, diff --git a/src/main/kotlin/me/zhouxi/slint/completion/provider/ComponentElementKeywordProvider.kt b/src/main/kotlin/me/zhouxi/slint/completion/provider/ComponentElementKeywordProvider.kt index e39e17a..219cab7 100644 --- a/src/main/kotlin/me/zhouxi/slint/completion/provider/ComponentElementKeywordProvider.kt +++ b/src/main/kotlin/me/zhouxi/slint/completion/provider/ComponentElementKeywordProvider.kt @@ -9,7 +9,7 @@ import com.intellij.psi.PsiElement import com.intellij.util.ProcessingContext import me.zhouxi.slint.lang.psi.SlintTypes.Component -object ComponentElementKeywordProvider : AbstractSlintCompletionProvider() { +object ComponentElementKeywordProvider : AbstractSlintCompletionProvider() { override fun addCompletions( parameters: CompletionParameters, context: ProcessingContext, diff --git a/src/main/kotlin/me/zhouxi/slint/completion/provider/ComponentNameProvider.kt b/src/main/kotlin/me/zhouxi/slint/completion/provider/ComponentNameProvider.kt index 593684b..d2603a9 100644 --- a/src/main/kotlin/me/zhouxi/slint/completion/provider/ComponentNameProvider.kt +++ b/src/main/kotlin/me/zhouxi/slint/completion/provider/ComponentNameProvider.kt @@ -14,7 +14,7 @@ import me.zhouxi.slint.lang.psi.SlintTypes.InheritDeclaration import me.zhouxi.slint.lang.psi.extension.relativePathOf import me.zhouxi.slint.lang.psi.stubs.index.searchComponent -object ComponentNameProvider : AbstractSlintCompletionProvider() { +object ComponentNameProvider : AbstractSlintCompletionProvider() { override fun addCompletions( parameters: CompletionParameters, context: ProcessingContext, diff --git a/src/main/kotlin/me/zhouxi/slint/completion/provider/ExportElementProvider.kt b/src/main/kotlin/me/zhouxi/slint/completion/provider/ExportElementProvider.kt index 370deb6..c89fcc7 100644 --- a/src/main/kotlin/me/zhouxi/slint/completion/provider/ExportElementProvider.kt +++ b/src/main/kotlin/me/zhouxi/slint/completion/provider/ExportElementProvider.kt @@ -20,7 +20,7 @@ import me.zhouxi.slint.lang.psi.stubs.types.SlintFileElementType /** * @author zhouxi 2024/6/6 */ -object ExportElementProvider : AbstractSlintCompletionProvider() { +object ExportElementProvider : AbstractSlintCompletionProvider() { override fun pattern(): ElementPattern { diff --git a/src/main/kotlin/me/zhouxi/slint/completion/provider/InheritsCompletionProvider.kt b/src/main/kotlin/me/zhouxi/slint/completion/provider/InheritsCompletionProvider.kt index d686380..9c8b8f6 100644 --- a/src/main/kotlin/me/zhouxi/slint/completion/provider/InheritsCompletionProvider.kt +++ b/src/main/kotlin/me/zhouxi/slint/completion/provider/InheritsCompletionProvider.kt @@ -12,7 +12,7 @@ import me.zhouxi.slint.lang.psi.SlintTypes /** * @author zhouxi 2024/5/29 */ -object InheritsCompletionProvider : AbstractSlintCompletionProvider() { +object InheritsCompletionProvider : AbstractSlintCompletionProvider() { override fun pattern(): ElementPattern { return PlatformPatterns.psiElement() .afterLeaf(PlatformPatterns.psiElement().withParent(PlatformPatterns.psiElement(SlintTypes.IDENTIFIER))) diff --git a/src/main/kotlin/me/zhouxi/slint/completion/provider/PropertyBindingProvider.kt b/src/main/kotlin/me/zhouxi/slint/completion/provider/PropertyBindingProvider.kt index 1cfb6b6..aa188de 100644 --- a/src/main/kotlin/me/zhouxi/slint/completion/provider/PropertyBindingProvider.kt +++ b/src/main/kotlin/me/zhouxi/slint/completion/provider/PropertyBindingProvider.kt @@ -19,7 +19,7 @@ import me.zhouxi.slint.lang.psi.extension.inheritsProperties import me.zhouxi.slint.lang.psi.extension.resolve import me.zhouxi.slint.lang.psi.extension.toLookupElement -object PropertyBindingProvider : AbstractSlintCompletionProvider() { +object PropertyBindingProvider : AbstractSlintCompletionProvider() { override fun addCompletions( parameters: CompletionParameters, context: ProcessingContext, diff --git a/src/main/kotlin/me/zhouxi/slint/completion/provider/PropertyDeclarationProvider.kt b/src/main/kotlin/me/zhouxi/slint/completion/provider/PropertyDeclarationProvider.kt new file mode 100644 index 0000000..a9b24db --- /dev/null +++ b/src/main/kotlin/me/zhouxi/slint/completion/provider/PropertyDeclarationProvider.kt @@ -0,0 +1,65 @@ +package me.zhouxi.slint.completion.provider + +import com.intellij.codeInsight.completion.CompletionParameters +import com.intellij.codeInsight.completion.CompletionResultSet +import com.intellij.codeInsight.completion.InsertHandler +import com.intellij.codeInsight.completion.InsertionContext +import com.intellij.codeInsight.lookup.LookupElement +import com.intellij.codeInsight.lookup.LookupElementBuilder +import com.intellij.codeInsight.lookup.LookupElementBuilder.* +import com.intellij.codeInsight.template.Template +import com.intellij.codeInsight.template.TemplateEditingAdapter +import com.intellij.codeInsight.template.TemplateManager +import com.intellij.codeInsight.template.impl.ConstantNode +import com.intellij.patterns.ElementPattern +import com.intellij.patterns.PlatformPatterns.psiElement +import com.intellij.patterns.StandardPatterns.or +import com.intellij.psi.PsiElement +import com.intellij.util.ProcessingContext +import me.zhouxi.slint.lang.psi.SlintTypes.Component +import me.zhouxi.slint.lang.psi.SlintTypes.SubComponent + +/** + * @author zhouxi 2024/6/6 + */ +object PropertyDeclarationProvider : AbstractSlintCompletionProvider() { + + override fun pattern(): ElementPattern { + return psiElement().withParent(or(psiElement(Component), psiElement(SubComponent))) + } + + override fun addCompletions( + parameters: CompletionParameters, + context: ProcessingContext, + result: CompletionResultSet + ) { + result.addAllElements(lookup) + } + + val lookup = arrayOf( + create("property"), + create("in property"), + create("out property"), + create("in-out property") + ).flatMap { arrayListOf(it.withInsertHandler(InternalInsertHandler())) } + .map { it.withBoldness(true) } + + + class InternalInsertHandler : InsertHandler { + override fun handleInsert(context: InsertionContext, item: LookupElement) { + val manager = TemplateManager.getInstance(context.project) + val template = manager.createTemplate("", "", "<\$Type$> \$Name$;").apply { + isToReformat = true + addVariable("Type", ConstantNode(""), true) + addVariable("Name", ConstantNode(""), true) + } + manager.startTemplate(context.editor, template, object : TemplateEditingAdapter() { + override fun templateFinished(template: Template, brokenOff: Boolean) { + context.editor.caretModel.moveToOffset(context.tailOffset - 2); + } + }) + } + + } + +} diff --git a/src/main/kotlin/me/zhouxi/slint/completion/provider/SubComponentNameProvider.kt b/src/main/kotlin/me/zhouxi/slint/completion/provider/SubComponentNameProvider.kt index 3331489..7737749 100644 --- a/src/main/kotlin/me/zhouxi/slint/completion/provider/SubComponentNameProvider.kt +++ b/src/main/kotlin/me/zhouxi/slint/completion/provider/SubComponentNameProvider.kt @@ -30,7 +30,7 @@ import me.zhouxi.slint.lang.psi.extension.relativePathOf import me.zhouxi.slint.lang.psi.stubs.index.searchComponent object SubComponentNameProvider - : AbstractSlintCompletionProvider() { + : AbstractSlintCompletionProvider() { override fun addCompletions( parameters: CompletionParameters, context: ProcessingContext, diff --git a/src/main/kotlin/me/zhouxi/slint/completion/provider/TopElementProvider.kt b/src/main/kotlin/me/zhouxi/slint/completion/provider/TopElementProvider.kt index a3340d8..86d1e36 100644 --- a/src/main/kotlin/me/zhouxi/slint/completion/provider/TopElementProvider.kt +++ b/src/main/kotlin/me/zhouxi/slint/completion/provider/TopElementProvider.kt @@ -17,7 +17,7 @@ import com.intellij.util.ProcessingContext import me.zhouxi.slint.lang.psi.SlintTypes.Component import me.zhouxi.slint.lang.psi.stubs.types.SlintFileElementType -object TopElementProvider : AbstractSlintCompletionProvider() { +object TopElementProvider : AbstractSlintCompletionProvider() { override fun addCompletions( parameters: CompletionParameters, context: ProcessingContext, diff --git a/src/main/kotlin/me/zhouxi/slint/formatter/FormattingBlock.kt b/src/main/kotlin/me/zhouxi/slint/formatter/FormattingBlock.kt index 4fe16ff..9219d98 100644 --- a/src/main/kotlin/me/zhouxi/slint/formatter/FormattingBlock.kt +++ b/src/main/kotlin/me/zhouxi/slint/formatter/FormattingBlock.kt @@ -5,13 +5,10 @@ import com.intellij.lang.ASTNode import com.intellij.psi.TokenType import com.intellij.psi.formatter.common.AbstractBlock import com.intellij.psi.tree.TokenSet -import me.zhouxi.slint.lang.psi.SlintPsiKeyword -import me.zhouxi.slint.lang.psi.SlintTypes import me.zhouxi.slint.lang.psi.SlintTypes.* import me.zhouxi.slint.lang.psi.stubs.types.SlintFileElementType import me.zhouxi.slint.lang.psi.utils.braces import me.zhouxi.slint.lang.psi.utils.expressions -import me.zhouxi.slint.lang.psi.utils.keywords /** * @author zhouxi 2024/5/28 @@ -85,8 +82,7 @@ class FormattingBlock( FunctionRef, TypeRef, LocalVariable, - PropertyModifier, - SlintPsiKeyword.ElementType.TYPE + PropertyModifier ) private val syntheticParentTokens = TokenSet.create( diff --git a/src/main/kotlin/me/zhouxi/slint/formatter/SlintFormatterModelBuilder.kt b/src/main/kotlin/me/zhouxi/slint/formatter/SlintFormatterModelBuilder.kt index 0aef9b3..19b6825 100644 --- a/src/main/kotlin/me/zhouxi/slint/formatter/SlintFormatterModelBuilder.kt +++ b/src/main/kotlin/me/zhouxi/slint/formatter/SlintFormatterModelBuilder.kt @@ -7,10 +7,8 @@ import com.intellij.psi.PsiFile import com.intellij.psi.codeStyle.CodeStyleSettings import com.intellij.psi.tree.TokenSet import me.zhouxi.slint.lang.SlintLanguage -import me.zhouxi.slint.lang.psi.SlintTypes import me.zhouxi.slint.lang.psi.SlintTypes.* import me.zhouxi.slint.lang.psi.stubs.types.SlintFileElementType -import me.zhouxi.slint.lang.psi.utils.keywords /** * @author zhouxi 2024/5/7 @@ -54,8 +52,6 @@ class SlintFormatterModelBuilder : FormattingModelBuilder { .spaces(1) .before(CodeBlock) .spacing(1, 1, 0, true, 2) - .around(keywords) - .spaces(1) .after(RAngle) .spaces(1) .afterInside(TokenSet.ANY, SlintFileElementType) diff --git a/src/main/kotlin/me/zhouxi/slint/highlight/KeywordHighlightAnnotator.kt b/src/main/kotlin/me/zhouxi/slint/highlight/KeywordHighlightAnnotator.kt index a60e551..10119e2 100644 --- a/src/main/kotlin/me/zhouxi/slint/highlight/KeywordHighlightAnnotator.kt +++ b/src/main/kotlin/me/zhouxi/slint/highlight/KeywordHighlightAnnotator.kt @@ -6,7 +6,6 @@ import com.intellij.lang.annotation.HighlightSeverity import com.intellij.openapi.editor.DefaultLanguageHighlighterColors import com.intellij.psi.PsiElement import me.zhouxi.slint.lang.psi.* -import me.zhouxi.slint.lang.psi.SlintPsiKeyword class KeywordHighlightAnnotator : Annotator { override fun annotate(element: PsiElement, holder: AnnotationHolder) { @@ -17,13 +16,6 @@ class KeywordHighlightAnnotator : Annotator { .create() return } - if (element is SlintPsiKeyword) { - holder.newSilentAnnotation(HighlightSeverity.INFORMATION) - .range(element) - .textAttributes(Definitions._KeyWord) - .create() - return - } if (element is SlintTypeRef && SlintPsiUtils.isInternalType(element)) { holder.newSilentAnnotation(HighlightSeverity.INFORMATION) .range(element) @@ -40,7 +32,7 @@ class KeywordHighlightAnnotator : Annotator { } if (element is SlintProperty) { holder.newSilentAnnotation(HighlightSeverity.INFORMATION) - .range(element.identifier!!) + .range(element.identifier) .textAttributes(DefaultLanguageHighlighterColors.INSTANCE_FIELD) .create() return diff --git a/src/main/kotlin/me/zhouxi/slint/lang/SlintElementFactory.kt b/src/main/kotlin/me/zhouxi/slint/lang/SlintElementFactory.kt index 5cbc6d0..462e031 100644 --- a/src/main/kotlin/me/zhouxi/slint/lang/SlintElementFactory.kt +++ b/src/main/kotlin/me/zhouxi/slint/lang/SlintElementFactory.kt @@ -4,7 +4,6 @@ import com.intellij.openapi.project.Project import com.intellij.psi.PsiElement import com.intellij.psi.PsiFileFactory import com.intellij.psi.util.PsiTreeUtil -import com.intellij.psi.util.nextLeafs import me.zhouxi.slint.lang.psi.SlintComponent import me.zhouxi.slint.lang.psi.SlintFile import me.zhouxi.slint.lang.psi.SlintImportSpecifier diff --git a/src/main/kotlin/me/zhouxi/slint/lang/psi/builder/SlintPsiBuilder.kt b/src/main/kotlin/me/zhouxi/slint/lang/psi/builder/SlintPsiBuilder.kt deleted file mode 100644 index 7e74a9c..0000000 --- a/src/main/kotlin/me/zhouxi/slint/lang/psi/builder/SlintPsiBuilder.kt +++ /dev/null @@ -1,29 +0,0 @@ -package me.zhouxi.slint.lang.psi.builder - -import com.intellij.lang.PsiBuilder -import com.intellij.lang.impl.DelegateMarker -import com.intellij.lang.impl.PsiBuilderAdapter -import com.intellij.psi.tree.IElementType - -/** - * @author zhouxi 2024/5/29 - */ -class SlintPsiBuilder(delegate: PsiBuilder) : PsiBuilderAdapter(delegate) { - override fun mark(): PsiBuilder.Marker { - return SlintMarker(super.mark()) - } - - /** - * @author zhouxi 2024/5/29 - */ - class SlintMarker(delegate: PsiBuilder.Marker) : DelegateMarker(delegate) { - override fun precede(): PsiBuilder.Marker { - return SlintMarker(super.precede()) - } - - override fun done(type: IElementType) { - println(type) - super.done(type) - } - } -} diff --git a/src/main/kotlin/me/zhouxi/slint/lang/psi/impl/SlintPsiNamedElementImpl.kt b/src/main/kotlin/me/zhouxi/slint/lang/psi/impl/SlintPsiNamedElementImpl.kt index c6bd9df..9e6fc89 100644 --- a/src/main/kotlin/me/zhouxi/slint/lang/psi/impl/SlintPsiNamedElementImpl.kt +++ b/src/main/kotlin/me/zhouxi/slint/lang/psi/impl/SlintPsiNamedElementImpl.kt @@ -5,7 +5,6 @@ import com.intellij.openapi.util.NlsSafe import com.intellij.psi.PsiElement import com.intellij.util.IncorrectOperationException import me.zhouxi.slint.lang.createIdentifier -import me.zhouxi.slint.lang.psi.SlintNamed import me.zhouxi.slint.lang.psi.SlintPsiNamedElement import me.zhouxi.slint.lang.psi.SlintTypes diff --git a/src/main/kotlin/me/zhouxi/slint/lang/psi/stubs/stub/impl/SlintPropertyStubImpl.kt b/src/main/kotlin/me/zhouxi/slint/lang/psi/stubs/stub/impl/SlintPropertyStubImpl.kt index 3a7ce18..99a8808 100644 --- a/src/main/kotlin/me/zhouxi/slint/lang/psi/stubs/stub/impl/SlintPropertyStubImpl.kt +++ b/src/main/kotlin/me/zhouxi/slint/lang/psi/stubs/stub/impl/SlintPropertyStubImpl.kt @@ -1,7 +1,5 @@ package me.zhouxi.slint.lang.psi.stubs.stub.impl -import com.intellij.psi.stubs.IStubElementType -import com.intellij.psi.stubs.StubBase import com.intellij.psi.stubs.StubElement import com.intellij.util.BitUtil import me.zhouxi.slint.lang.psi.SlintProperty diff --git a/src/main/kotlin/me/zhouxi/slint/lang/psi/stubs/types/SlintComponentElementType.kt b/src/main/kotlin/me/zhouxi/slint/lang/psi/stubs/types/SlintComponentElementType.kt index e7cdff9..2229e16 100644 --- a/src/main/kotlin/me/zhouxi/slint/lang/psi/stubs/types/SlintComponentElementType.kt +++ b/src/main/kotlin/me/zhouxi/slint/lang/psi/stubs/types/SlintComponentElementType.kt @@ -26,7 +26,7 @@ object SlintComponentElementType : override fun createStub(psi: SlintComponent, parentStub: StubElement): SlintComponentStub { return SlintComponentStubImpl( parentStub, - psi.exportKeyword != null, + false, psi.identifier!!.text ) } @@ -49,14 +49,13 @@ object SlintComponentElementType : } override fun createStub(tree: LighterAST, node: LighterASTNode, parentStub: StubElement<*>): SlintComponentStub { - val exported = tree.getChildren(node).any { it.tokenType == ExportKeyword } val token = tree.getChildren(node).find { it.tokenType == IDENTIFIER } ?.let { val text = it as LighterASTTokenNode tree.charTable.intern(text.text) } - return SlintComponentStubImpl(parentStub, exported, token.toString()) + return SlintComponentStubImpl(parentStub, false, token.toString()) } override fun indexStub(stub: SlintComponentStub, sink: IndexSink) { diff --git a/src/main/kotlin/me/zhouxi/slint/lang/psi/stubs/types/SlintPropertyElementType.kt b/src/main/kotlin/me/zhouxi/slint/lang/psi/stubs/types/SlintPropertyElementType.kt index 3bbe1d1..24abdfb 100644 --- a/src/main/kotlin/me/zhouxi/slint/lang/psi/stubs/types/SlintPropertyElementType.kt +++ b/src/main/kotlin/me/zhouxi/slint/lang/psi/stubs/types/SlintPropertyElementType.kt @@ -19,14 +19,7 @@ object SlintPropertyElementType : ILightStubElementType("SlintProperty", SlintLanguage.INSTANCE) { override fun createStub(tree: LighterAST, node: LighterASTNode, parentStub: StubElement<*>): SlintPropertyStub { val children = tree.getChildren(node) - val modifierNode = children.firstOrNull { it.tokenType == PropertyModifier }?.let { - when (tree.getChildren(it)[0].tokenType) { - InKeyword -> 1 - OutKeyword -> 2 - InOutKeyword -> 3 - else -> 0 - } - } ?: 0 + val modifierNode = 0 val name = children.first { it.tokenType == IDENTIFIER } as LighterASTTokenNode return SlintPropertyStubImpl( parentStub, @@ -47,11 +40,8 @@ object SlintPropertyElementType : psi: SlintProperty, parentStub: StubElement ): SlintPropertyStub { - val modifier = psi.propertyModifier - val isIn = modifier?.inKeyword != null || modifier?.inOutKeyword != null - val isOut = modifier?.outKeyword != null || modifier?.inOutKeyword != null - val name = psi.identifier!!.text - val flag = SlintPropertyStubImpl.pack(isIn, isOut) + val name = psi.identifier.text + val flag: Short = 0 return SlintPropertyStubImpl(parentStub, flag, name) } diff --git a/src/main/kotlin/me/zhouxi/slint/lang/psi/utils/SlintTokens.kt b/src/main/kotlin/me/zhouxi/slint/lang/psi/utils/SlintTokens.kt index ba803c4..9830b2f 100644 --- a/src/main/kotlin/me/zhouxi/slint/lang/psi/utils/SlintTokens.kt +++ b/src/main/kotlin/me/zhouxi/slint/lang/psi/utils/SlintTokens.kt @@ -4,12 +4,6 @@ import com.intellij.psi.tree.IElementType import com.intellij.psi.tree.TokenSet import me.zhouxi.slint.lang.psi.SlintTypes - -val keywords = TokenSet.create(*SlintTypes::class.java.declaredFields - .filter { it.name.endsWith("Keyword") } - .map { it.get(null) as IElementType }.toTypedArray() -) - val braces = TokenSet.create( SlintTypes.LBrace, SlintTypes.LParent, diff --git a/src/main/kotlin/me/zhouxi/slint/preview/PreviewAction.kt b/src/main/kotlin/me/zhouxi/slint/preview/PreviewAction.kt index 3d77906..aeda9b3 100644 --- a/src/main/kotlin/me/zhouxi/slint/preview/PreviewAction.kt +++ b/src/main/kotlin/me/zhouxi/slint/preview/PreviewAction.kt @@ -1,12 +1,11 @@ package me.zhouxi.slint.preview -import com.intellij.execution.RunManager import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnActionEvent class PreviewAction : AnAction() { override fun actionPerformed(e: AnActionEvent) { - val manager = RunManager.getInstance(e.project!!) +// val manager = RunManager.getInstance(e.project!!) // e.si // manager.createConfiguration() diff --git a/src/main/kotlin/me/zhouxi/slint/reference/provider/PropertyReferenceProvider.kt b/src/main/kotlin/me/zhouxi/slint/reference/provider/PropertyReferenceProvider.kt index 83323cf..4553250 100644 --- a/src/main/kotlin/me/zhouxi/slint/reference/provider/PropertyReferenceProvider.kt +++ b/src/main/kotlin/me/zhouxi/slint/reference/provider/PropertyReferenceProvider.kt @@ -21,10 +21,10 @@ object PropertyReferenceProvider : PsiReferenceProvider() { val subComponent = element.parentOfType() if (subComponent != null) { val component = subComponent.resolve() ?: return null - return component.inheritsProperties().find { it.identifier?.textMatches(element) == true } + return component.inheritsProperties().find { it.identifier.textMatches(element) } } val component = element.parentOfType() ?: return null - return component.inheritsProperties().find { it.identifier?.textMatches(element) == true } + return component.inheritsProperties().find { it.identifier.textMatches(element) } } } }