@@ -83,17 +83,28 @@ import java.util.function.Predicate
8383import javax.swing.Icon
8484
8585val TS : JSLanguageDialect = JavaScriptSupportLoader .TYPESCRIPT
86+ val JS : Language = JavaScriptSupportLoader .JAVASCRIPT .language
8687
87- class GtsLanguage : Language (TS , " Gts" ) {
88+
89+
90+ open class GtsLanguage (val lang : JSLanguageDialect = TS , id : String =" Gts" ) : Language(lang, id) {
8891
8992 companion object {
9093 val INSTANCE = GtsLanguage ()
9194 }
9295}
9396
94- class GtsFile (viewProvider : FileViewProvider ? ) : JSFileImpl(viewProvider!!, GtsLanguage .INSTANCE ) {
97+ class GjsLanguage (): GtsLanguage(JavascriptLanguage .INSTANCE , " Gjs" ) {
98+ companion object {
99+ val INSTANCE = GjsLanguage ()
100+ }
101+ }
102+
103+
104+ class GtsFile (viewProvider : FileViewProvider ? , val isJS : Boolean =false )
105+ : JSFileImpl (viewProvider!! , isJS.ifTrue { GjsLanguage .INSTANCE } ? : GtsLanguage .INSTANCE ) {
95106 override fun getFileType (): FileType {
96- return GtsFileType .INSTANCE
107+ return isJS.ifTrue { GjsFileType . INSTANCE } ? : GtsFileType .INSTANCE
97108 }
98109
99110 override fun toString (): String {
@@ -200,12 +211,36 @@ class GtsElementTypes {
200211 return node.firstChildNode ? : PsiWhiteSpaceImpl (" " )
201212 }
202213 }
214+ val GJS_FILE_NODE_TYPE = object : IFileElementType (" GJS" , GjsLanguage .INSTANCE ) {
215+ override fun equals (other : Any? ): Boolean {
216+ if (other == JavaScriptFileType .INSTANCE ) {
217+ return true
218+ }
219+ return super .equals(other)
220+ }
221+
222+ override fun hashCode (): Int {
223+ return JavaScriptFileType .INSTANCE .hashCode()
224+ }
225+
226+ override fun doParseContents (chameleon : ASTNode , psi : PsiElement ): ASTNode {
227+ val project = psi.project
228+ val languageForParser = getLanguageForParser(psi)
229+ val builder = PsiBuilderFactory .getInstance().createBuilder(project, chameleon, null , languageForParser, chameleon.chars)
230+ val parser = GjsParserDefinition ().createParser(project)
231+ val node = parser.parse(this , builder)
232+ return node.firstChildNode ? : PsiWhiteSpaceImpl (" " )
233+ }
234+ }
203235 }
204236}
205237
206- class GtsParserDefinition : TypeScriptParserDefinition () {
238+ open class GtsParserDefinition ( val isJS : Boolean = false ) : TypeScriptParserDefinition() {
207239
208240 override fun getFileNodeType (): IFileElementType {
241+ if (isJS) {
242+ return GtsElementTypes .GJS_FILE_NODE_TYPE
243+ }
209244 return GtsElementTypes .GTS_FILE_NODE_TYPE
210245 }
211246
@@ -228,11 +263,15 @@ class GtsParserDefinition : TypeScriptParserDefinition() {
228263 }
229264
230265 override fun createFile (viewProvider : FileViewProvider ): JSFile {
231- return GtsFile (viewProvider)
266+ if (viewProvider.baseLanguage.id == GjsLanguage .INSTANCE .id) {
267+ return GtsFile (viewProvider, true )
268+ }
269+ return GtsFile (viewProvider, isJS)
232270 }
233-
234271}
235272
273+ class GjsParserDefinition : GtsParserDefinition (true )
274+
236275
237276internal object GtsIcons {
238277 val icon: Icon = IconLoader .getIcon(" /com/emberjs/icons/glimmer.svg" , GtsIcons ::class .java)
@@ -336,14 +375,50 @@ class GtsFileType : LanguageFileType(GtsLanguage.INSTANCE) {
336375 }
337376}
338377
378+ class GjsFileType : LanguageFileType (GjsLanguage .INSTANCE ) {
379+
380+ override fun equals (other : Any? ): Boolean {
381+ if (other == JavaScriptFileType .INSTANCE ) {
382+ return true
383+ }
384+ return super .equals(other)
385+ }
386+
387+ companion object {
388+ val INSTANCE = GjsFileType ()
389+ }
390+
391+ override fun getName (): String {
392+ return " Gjs File"
393+ }
394+
395+ override fun getDescription (): String {
396+ return " Gjs file type"
397+ }
398+
399+ override fun getDefaultExtension (): String {
400+ return " gjs"
401+ }
402+
403+ override fun getIcon (): Icon {
404+ return GtsIcons .icon
405+ }
406+
407+ override fun hashCode (): Int {
408+ return javaClass.hashCode()
409+ }
410+ }
411+
339412class GtsFileViewProviderFactory : FileViewProviderFactory {
340413 override fun createFileViewProvider (file : VirtualFile , language : Language ? , manager : PsiManager , eventSystemEnabled : Boolean ): FileViewProvider {
341- return GtsFileViewProvider (manager, file, eventSystemEnabled)
414+ return (language as ? GtsLanguage )
415+ ?.let { GtsFileViewProvider (manager, file, eventSystemEnabled, it) }
416+ ? : GtsFileViewProvider (manager, file, eventSystemEnabled)
342417 }
343418
344419}
345420
346- class GtsFileViewProvider (manager : PsiManager , virtualFile : VirtualFile , eventSystemEnabled : Boolean ) : MultiplePsiFilesPerDocumentFileViewProvider(manager, virtualFile, eventSystemEnabled), TemplateLanguageFileViewProvider {
421+ class GtsFileViewProvider (manager : PsiManager , virtualFile : VirtualFile , eventSystemEnabled : Boolean , private val baseLang : GtsLanguage = GtsLanguage . INSTANCE ) : MultiplePsiFilesPerDocumentFileViewProvider(manager, virtualFile, eventSystemEnabled), TemplateLanguageFileViewProvider {
347422
348423
349424 override fun findElementAt (offset : Int ): PsiElement ? {
@@ -355,10 +430,13 @@ class GtsFileViewProvider(manager: PsiManager, virtualFile: VirtualFile, eventSy
355430 }
356431
357432 override fun getBaseLanguage (): Language {
358- return GtsLanguage . INSTANCE
433+ return baseLang
359434 }
360435
361436 override fun getLanguages (): MutableSet <Language > {
437+ if (baseLang == GjsLanguage .INSTANCE ) {
438+ return mutableSetOf (HTMLLanguage .INSTANCE , HbLanguage .INSTANCE , JavascriptLanguage .INSTANCE , GjsLanguage .INSTANCE )
439+ }
362440 return mutableSetOf (HTMLLanguage .INSTANCE , HbLanguage .INSTANCE , TS , GtsLanguage .INSTANCE )
363441 }
364442
@@ -367,18 +445,26 @@ class GtsFileViewProvider(manager: PsiManager, virtualFile: VirtualFile, eventSy
367445 }
368446
369447 override fun cloneInner (virtualFile : VirtualFile ): MultiplePsiFilesPerDocumentFileViewProvider {
370- return GtsFileViewProvider (this .manager, virtualFile, false )
448+ return GtsFileViewProvider (this .manager, virtualFile, false , baseLang )
371449 }
372450
373451 override fun createFile (lang : Language ): PsiFile ? {
374452 if (lang.id == GtsLanguage .INSTANCE .id) {
375453 return GtsParserDefinition ().createFile(this )
376454 }
455+ if (lang.id == GjsLanguage .INSTANCE .id) {
456+ return GjsParserDefinition ().createFile(this )
457+ }
377458 if (lang.id == TS .id) {
378459 val f = TypeScriptParserDefinition ().createFile(this )
379460 (f as PsiFileImpl ).contentElementType = GtsFileElementType .INSTANCE
380461 return f
381462 }
463+ if (lang.id == JavascriptLanguage .INSTANCE .id) {
464+ val f = JavascriptParserDefinition ().createFile(this )
465+ (f as PsiFileImpl ).contentElementType = GtsFileElementType .INSTANCE
466+ return f
467+ }
382468 if (lang.id == HTMLLanguage .INSTANCE .id) {
383469 val f = HTMLParserDefinition ().createFile(this )
384470 (f as PsiFileImpl ).contentElementType = GtsElementTypes .HTML_CONTENT_ELEMENT_TYPE
@@ -839,9 +925,9 @@ class GtsFormattingModelBuilder : AbstractXmlTemplateFormattingModelBuilder() {
839925
840926 var element = formattingContext.psiElement.containingFile.findElementAt(formattingContext.formattingRange.startOffset) ? : formattingContext.psiElement
841927 if (formattingContext.psiElement is PsiFile && formattingContext.formattingRange.startOffset == 0 ) {
842- element = formattingContext.containingFile.viewProvider.getPsi(TS )
928+ element = formattingContext.containingFile.viewProvider.getPsi(TS ) ? : formattingContext.containingFile.viewProvider.getPsi( JS )
843929 }
844- val tsFile = formattingContext.containingFile.viewProvider.getPsi(TS )
930+ val tsFile = formattingContext.containingFile.viewProvider.getPsi(TS ) ? : formattingContext.containingFile.viewProvider.getPsi( JS )
845931 val m = jsModelBuilder.createModel(formattingContext.withPsiElement(tsFile))
846932 val jsModel = JavascriptFormattingModelBuilder .createJSFormattingModel(tsFile, formattingContext.codeStyleSettings, JSAstBlockWrapper (m.rootBlock as ASTBlock , null , null ))
847933 if (element.language is JSLanguageDialect ) {
0 commit comments