Skip to content

Commit 3902b06

Browse files
authored
fix gjs formatting (#302)
1 parent 9b024fa commit 3902b06

File tree

3 files changed

+392
-34
lines changed

3 files changed

+392
-34
lines changed

src/main/kotlin/com/emberjs/gts/GtsSupport.kt

Lines changed: 206 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import com.intellij.lang.javascript.config.JSImportResolveContext
2626
import com.intellij.lang.javascript.dialects.ECMA6ParserDefinition
2727
import com.intellij.lang.javascript.dialects.TypeScriptParserDefinition
2828
import com.intellij.lang.javascript.editing.JavascriptCommenter
29+
import com.intellij.lang.javascript.formatter.JSBlockContext
2930
import com.intellij.lang.javascript.formatter.JavascriptFormattingModelBuilder
31+
import com.intellij.lang.javascript.formatter.blocks.JSBlock
3032
import com.intellij.lang.javascript.highlighting.JSHighlighter
3133
import com.intellij.lang.javascript.index.IndexedFileTypeProvider
3234
import com.intellij.lang.javascript.modules.JSImportCandidateDescriptor
@@ -81,6 +83,7 @@ import com.intellij.psi.search.ProjectScope
8183
import com.intellij.psi.templateLanguages.*
8284
import com.intellij.psi.tree.IElementType
8385
import com.intellij.psi.tree.IFileElementType
86+
import com.intellij.psi.tree.TokenSet
8487
import com.intellij.psi.util.PsiTreeUtil
8588
import com.intellij.psi.xml.XmlTokenType
8689
import com.intellij.refactoring.suggested.endOffset
@@ -459,8 +462,8 @@ class GjsFileType : LanguageFileType(GjsLanguage.INSTANCE) {
459462
class GtsFileViewProviderFactory: FileViewProviderFactory {
460463
override fun createFileViewProvider(file: VirtualFile, language: Language?, manager: PsiManager, eventSystemEnabled: Boolean): FileViewProvider {
461464
return (language as? GtsLanguage)
462-
?.let { GtsFileViewProvider(manager, file, eventSystemEnabled, it) }
463-
?: GtsFileViewProvider(manager, file, eventSystemEnabled)
465+
?.let { GtsFileViewProvider(manager, file, eventSystemEnabled, it) }
466+
?: GtsFileViewProvider(manager, file, eventSystemEnabled)
464467
}
465468

466469
}
@@ -615,8 +618,8 @@ class GtsImportResolver(project: Project,
615618

616619
private fun processGtsPackage(processor: Processor<in VirtualFile>) {
617620
TypeScriptImportsResolverProvider.getDefaultProvider(project, resolveContext)
618-
.resolveFileModule("gts", contextFile)
619-
?.let { processor.process(it) }
621+
.resolveFileModule("gts", contextFile)
622+
?.let { processor.process(it) }
620623
}
621624

622625
override fun getPriority(): Int = TypeScriptFileImportsResolver.JS_DEFAULT_PRIORITY
@@ -651,10 +654,10 @@ class GtsComponentCandidatesProvider(val placeInfo: JSImportPlaceInfo) : JSImpor
651654

652655
private val candidates: Map<String, List<Info>> by lazy {
653656
val list = FilenameIndex.getAllFilesByExt(project, "gts",
654-
createProjectImportsScope(placeInfo, getStructureModuleRoot(placeInfo)))
655-
.map { getExports(it) }
656-
.flatten()
657-
.toMutableList()
657+
createProjectImportsScope(placeInfo, getStructureModuleRoot(placeInfo)))
658+
.map { getExports(it) }
659+
.flatten()
660+
.toMutableList()
658661

659662
val scopes = EmberUtils.getScopesForFile(myPlaceInfo.file)
660663

@@ -663,18 +666,18 @@ class GtsComponentCandidatesProvider(val placeInfo: JSImportPlaceInfo) : JSImpor
663666

664667
// Collect all components from the index
665668
EmberNameIndex.getFilteredProjectKeys(scope) { it.type == "component" }
666-
.toCollection(emberNames)
669+
.toCollection(emberNames)
667670

668671
// Collect all component templates from the index
669672
EmberNameIndex.getFilteredProjectKeys(scope) { it.isComponentTemplate }
670-
.filter { !emberNames.contains(it) }
671-
.toCollection(emberNames)
673+
.filter { !emberNames.contains(it) }
674+
.toCollection(emberNames)
672675

673676
EmberNameIndex.getFilteredProjectKeys(scope) { it.type == "helper" }
674-
.toCollection(emberNames)
677+
.toCollection(emberNames)
675678

676679
EmberNameIndex.getFilteredProjectKeys(scope) { it.type == "modifier" }
677-
.toCollection(emberNames)
680+
.toCollection(emberNames)
678681

679682
emberNames.removeIf { it.virtualFile?.let { !EmberUtils.isInScope(it, scopes) } ?: false }
680683

@@ -795,8 +798,161 @@ val NoWrap by lazy {
795798
}
796799

797800

801+
class SimpleAstNode: ASTNode {
802+
override fun getElementType(): IElementType {
803+
return GjsFileElementType.INSTANCE
804+
}
805+
806+
override fun getText(): String {
807+
TODO("Not yet implemented")
808+
}
809+
810+
override fun getChars(): CharSequence {
811+
TODO("Not yet implemented")
812+
}
813+
814+
override fun textContains(p0: Char): Boolean {
815+
TODO("Not yet implemented")
816+
}
817+
818+
override fun getStartOffset(): Int {
819+
TODO("Not yet implemented")
820+
}
821+
822+
override fun getTextLength(): Int {
823+
return 0
824+
}
825+
826+
override fun getTextRange(): TextRange? {
827+
TODO("Not yet implemented")
828+
}
829+
830+
override fun getTreeParent(): ASTNode? {
831+
return null
832+
}
833+
834+
override fun getFirstChildNode(): ASTNode? {
835+
return null
836+
}
837+
838+
override fun getLastChildNode(): ASTNode? {
839+
return null
840+
}
841+
842+
override fun getTreeNext(): ASTNode? {
843+
TODO("Not yet implemented")
844+
}
845+
846+
override fun getTreePrev(): ASTNode? {
847+
TODO("Not yet implemented")
848+
}
849+
850+
override fun getChildren(p0: TokenSet?): Array<out ASTNode?> {
851+
TODO("Not yet implemented")
852+
}
853+
854+
override fun addChild(p0: ASTNode) {
855+
TODO("Not yet implemented")
856+
}
857+
858+
override fun addChild(p0: ASTNode, p1: ASTNode?) {
859+
TODO("Not yet implemented")
860+
}
861+
862+
override fun addLeaf(
863+
p0: IElementType,
864+
p1: CharSequence,
865+
p2: ASTNode?
866+
) {
867+
TODO("Not yet implemented")
868+
}
869+
870+
override fun removeChild(p0: ASTNode) {
871+
TODO("Not yet implemented")
872+
}
873+
874+
override fun removeRange(p0: ASTNode, p1: ASTNode?) {
875+
TODO("Not yet implemented")
876+
}
877+
878+
override fun replaceChild(p0: ASTNode, p1: ASTNode) {
879+
TODO("Not yet implemented")
880+
}
881+
882+
override fun replaceAllChildrenToChildrenOf(p0: ASTNode) {
883+
TODO("Not yet implemented")
884+
}
885+
886+
override fun addChildren(
887+
p0: ASTNode,
888+
p1: ASTNode?,
889+
p2: ASTNode?
890+
) {
891+
TODO("Not yet implemented")
892+
}
893+
894+
override fun clone(): Any {
895+
TODO("Not yet implemented")
896+
}
897+
898+
override fun copyElement(): ASTNode? {
899+
TODO("Not yet implemented")
900+
}
901+
902+
override fun findLeafElementAt(p0: Int): ASTNode? {
903+
TODO("Not yet implemented")
904+
}
905+
906+
override fun <T : Any?> getCopyableUserData(p0: Key<T?>): T? {
907+
TODO("Not yet implemented")
908+
}
909+
910+
override fun <T : Any?> putCopyableUserData(p0: Key<T?>, p1: T?) {
911+
TODO("Not yet implemented")
912+
}
913+
914+
override fun findChildByType(p0: IElementType): ASTNode? {
915+
TODO("Not yet implemented")
916+
}
917+
918+
override fun findChildByType(
919+
p0: IElementType,
920+
p1: ASTNode?
921+
): ASTNode? {
922+
TODO("Not yet implemented")
923+
}
924+
925+
override fun findChildByType(p0: TokenSet): ASTNode? {
926+
TODO("Not yet implemented")
927+
}
928+
929+
override fun findChildByType(
930+
p0: TokenSet,
931+
p1: ASTNode?
932+
): ASTNode? {
933+
TODO("Not yet implemented")
934+
}
935+
936+
override fun getPsi(): PsiElement? {
937+
TODO("Not yet implemented")
938+
}
939+
940+
override fun <T : PsiElement?> getPsi(p0: Class<T?>): T? {
941+
TODO("Not yet implemented")
942+
}
943+
944+
override fun <T : Any?> getUserData(p0: Key<T?>): T? {
945+
TODO("Not yet implemented")
946+
}
947+
948+
override fun <T : Any?> putUserData(p0: Key<T?>, p1: T?) {
949+
TODO("Not yet implemented")
950+
}
951+
952+
}
953+
798954
// wrapper to patch JsBlocks to include outer language block into JSAssignmentExpression and JSVarStatement
799-
open class JsBlockWrapper(val block: Block, val parent: JsBlockWrapper?, var hbsBlock: Block? = null): Block by block {
955+
open class JsBlockWrapper(val block: Block, val parent: JsBlockWrapper?, var hbsBlock: Block? = null): JSBlock((block as? ASTBlock)?.node ?: SimpleAstNode(), null, null, null, CodeStyleSettings.getDefaults()) {
800956

801957
private var cachedBlocks: MutableList<JsBlockWrapper>? = null
802958
val astnode =(block as? ASTBlock)?.node
@@ -805,6 +961,34 @@ open class JsBlockWrapper(val block: Block, val parent: JsBlockWrapper?, var hbs
805961
this.subBlocks
806962
}
807963

964+
override fun getAlignment(): Alignment? {
965+
return block.alignment
966+
}
967+
968+
override fun getChildAttributes(newChildIndex: Int): ChildAttributes {
969+
return block.getChildAttributes(newChildIndex)
970+
}
971+
972+
override fun getIndent(): Indent? {
973+
return block.indent
974+
}
975+
976+
override fun getNode(): ASTNode {
977+
return astnode ?: SimpleAstNode()
978+
}
979+
980+
override fun getSpacing(child1: Block?, child2: Block): Spacing? {
981+
return block.getSpacing(child1, child2)
982+
}
983+
984+
override fun isLeaf(): Boolean {
985+
return block.isLeaf
986+
}
987+
988+
override fun isIncomplete(): Boolean {
989+
return block.isIncomplete
990+
}
991+
808992
override fun getWrap(): Wrap? {
809993
if (parent?.subBlocks?.lastOrNull()?.block is RootBlockWrapper) {
810994
return NoWrap
@@ -879,8 +1063,8 @@ open class JsBlockWrapper(val block: Block, val parent: JsBlockWrapper?, var hbs
8791063
}
8801064

8811065
class JSAstBlockWrapper(block: ASTBlock, parent: JsBlockWrapper?, hbsBlock: Block?): JsBlockWrapper(block, parent, hbsBlock), ASTBlock {
882-
override fun getNode(): ASTNode? {
883-
return super.astnode
1066+
override fun getNode(): ASTNode {
1067+
return super.astnode ?: this.myNode
8841068
}
8851069
}
8861070

@@ -1061,9 +1245,9 @@ class GtsFormattingModelBuilder : AbstractXmlTemplateFormattingModelBuilder() {
10611245
val psiFile = element.containingFile
10621246
val documentModel = FormattingDocumentModelImpl.createOn(psiFile)
10631247
val model = XmlFormattingModel(
1064-
psiFile,
1065-
block,
1066-
documentModel)
1248+
psiFile,
1249+
block,
1250+
documentModel)
10671251
return DocumentBasedFormattingModel(model.rootBlock, element.project, formattingContext.codeStyleSettings, psiFile.fileType, psiFile)
10681252
}
10691253
return jsModel
@@ -1096,9 +1280,9 @@ class GtsFormattingModelBuilder : AbstractXmlTemplateFormattingModelBuilder() {
10961280
val documentModel = FormattingDocumentModelImpl.createOn(block.node!!.psi.containingFile)
10971281
val rootBlock = RootBlockWrapper(block, HtmlPolicy(settings, documentModel))
10981282
val model = XmlFormattingModel(
1099-
block.node!!.psi.containingFile,
1100-
rootBlock,
1101-
documentModel)
1283+
block.node!!.psi.containingFile,
1284+
rootBlock,
1285+
documentModel)
11021286
return DocumentBasedFormattingModel(model.rootBlock, element.project, settings, file.fileType, file).rootBlock
11031287
}
11041288
return createModel(FormattingContext.create(node.psi, settings)).rootBlock

0 commit comments

Comments
 (0)