Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ember project detection for v2 addons #106

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 43 additions & 28 deletions src/main/kotlin/com/emberjs/cli/EmberCliFrameworkDetector.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
package com.emberjs.cli


import com.emberjs.glint.GlintLspSupportProvider
import com.emberjs.glint.GlintTypeScriptService
import com.emberjs.glint.getGlintDescriptor
import com.emberjs.utils.emberRoot
import com.emberjs.utils.isEmber
Expand All @@ -29,14 +25,20 @@ import com.intellij.util.ProcessingContext
import com.intellij.util.indexing.FileContent
import com.intellij.webcore.libraries.ScriptingLibraryModel

val detectedFrameworks = HashMap<Project, List<EmberCliFrameworkDetector.EmberFrameworkDescription>>()

class EmberCliFrameworkDetector : FrameworkDetector("Ember", 2) {
/** Use package json keys to detect ember */

override fun getFileType(): FileType = JsonFileType.INSTANCE

override fun createSuitableFilePattern(): ElementPattern<FileContent> {
return FileContentPattern.fileContent()
.with(object : PatternCondition<FileContent>("emberKey") {
override fun accepts(fileContent: FileContent, context: ProcessingContext): Boolean {
if (fileContent.file.path.contains("node_modules")) {
return false
}
if (fileContent.fileName != "package.json") {
return false
}
Expand Down Expand Up @@ -84,19 +86,24 @@ class EmberCliFrameworkDetector : FrameworkDetector("Ember", 2) {

override fun detect(newFiles: MutableCollection<out VirtualFile>, context: FrameworkDetectionContext): MutableList<out DetectedFrameworkDescription> {
newFiles.removeIf { !it.path.endsWith("package.json") || it.parent != it.emberRoot || !it.parent.isEmber }
val rootDir = newFiles.firstOrNull()?.parent

if (rootDir != null && context.project != null && !isConfigured(newFiles, context.project)) {
return mutableListOf(EmberFrameworkDescription(rootDir, newFiles, context.project!!))
} else if (rootDir != null) {
context.project?.let {
ApplicationManager.getApplication().invokeLaterOnWriteThread {
getGlintDescriptor(it).ensureStarted()

val frameworkDescriptions = mutableListOf<EmberFrameworkDescription>()

newFiles.forEach {
val rootDir = it.parent
if (rootDir != null && context.project != null && !isConfigured(newFiles, context.project)) {
frameworkDescriptions.add(EmberFrameworkDescription(rootDir, newFiles, context.project!!))
} else if (rootDir != null) {
context.project?.let {
ApplicationManager.getApplication().invokeLaterOnWriteThread {
getGlintDescriptor(it).ensureStarted()
}
}
frameworkDescriptions.add(EmberFrameworkDescription(rootDir, newFiles, context.project!!))
}
return mutableListOf(EmberFrameworkDescription(rootDir, newFiles, context.project!!))
}
return mutableListOf()
detectedFrameworks[context.project!!] = frameworkDescriptions
return frameworkDescriptions
}

public fun isConfigured(files: Collection<VirtualFile>, project: Project?): Boolean {
Expand All @@ -118,23 +125,31 @@ class EmberCliFrameworkDetector : FrameworkDetector("Ember", 2) {
override fun hashCode() = files.hashCode()

override fun canSetupFramework(allDetectedFrameworks: MutableCollection<out DetectedFrameworkDescription>): Boolean {
return !detector.isConfigured(files, project)
return !this.isConfigured()
}

fun isConfigured(): Boolean {
return detector.isConfigured(files, project)
}

override fun setupFramework(modifiableModelsProvider: ModifiableModelsProvider, modulesProvider: ModulesProvider) {
modulesProvider.modules
.filter { ModuleRootManager.getInstance(it).contentRoots.contains(root) }
.forEach { module ->
val model = modifiableModelsProvider.getModuleModifiableModel(module)
val entry = MarkRootActionBase.findContentEntry(model, root)
if (entry != null) {
EmberCliProjectConfigurator.setupEmber(model.project, entry, root)
getGlintDescriptor(model.project).ensureStarted()
modifiableModelsProvider.commitModuleModifiableModel(model)
} else {
modifiableModelsProvider.disposeModuleModifiableModel(model)
}
}
ModuleUtilCore.findModuleForFile(root, project)?.let { module ->
val model = modifiableModelsProvider.getModuleModifiableModel(module)
val entry = MarkRootActionBase.findContentEntry(model, root)
if (entry != null) {
EmberCliProjectConfigurator.setupEmber(model.project, entry, root)
getGlintDescriptor(model.project).ensureStarted()
modifiableModelsProvider.commitModuleModifiableModel(model)
} else {
modifiableModelsProvider.disposeModuleModifiableModel(model)
}
}
}
}

companion object {
fun hasEnabledEmberFramework(project: Project): Boolean {
return detectedFrameworks.getOrDefault(project, emptyList()).any { it.isConfigured() }
}
}
}
5 changes: 3 additions & 2 deletions src/main/kotlin/com/emberjs/glint/GlintLanguageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.dmarcotte.handlebars.file.HbFileType
import com.dmarcotte.handlebars.psi.HbPsiFile
import com.emberjs.gts.GtsFileType
import com.emberjs.hbs.HbReference
import com.emberjs.utils.EmberUtils
import com.emberjs.utils.emberRoot
import com.emberjs.utils.originalVirtualFile
import com.intellij.codeInsight.completion.CompletionParameters
Expand Down Expand Up @@ -61,7 +62,7 @@ class GlintLanguageServiceProvider(val project: Project) : JSLanguageServiceProv
override fun getService(file: VirtualFile) = allServices.firstOrNull()

override fun getAllServices() =
if (project.guessProjectDir()?.emberRoot != null) listOf(GlintTypeScriptService.getInstance(project)) else emptyList()
if (EmberUtils.isEnabledEmberProject(project)) listOf(GlintTypeScriptService.getInstance(project)) else emptyList()
}


Expand All @@ -85,7 +86,7 @@ class GlintTypeScriptService(project: Project) : BaseLspTypeScriptService(projec
}

fun getDescriptor(): GlintLspServerDescriptor? {
return if (project.guessProjectDir()?.emberRoot != null) getGlintDescriptor(project) else null
return if (EmberUtils.isEnabledEmberProject(project)) getGlintDescriptor(project) else null
}

override val name = "Glint TypeScript LSP"
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/com/emberjs/glint/GlintStatusBarWidget.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.emberjs.glint

import com.emberjs.utils.EmberUtils
import com.emberjs.utils.emberRoot
import com.intellij.lang.javascript.JavaScriptBundle
import com.intellij.lang.typescript.compiler.languageService.TypeScriptMessageBus
Expand Down Expand Up @@ -137,7 +138,7 @@ open class GlintRestartServiceAction : AnAction("Restart Glint", "Restarts the g
if (project != null) {
val hasStarted = GlintLanguageServiceProvider(project).allServices.firstOrNull()?.isServiceCreated() == true
val presentation = e.presentation
if (project.guessProjectDir()?.emberRoot != null) {
if (EmberUtils.isEnabledEmberProject(project)) {
presentation.isVisible = true
}
setEnableAndVisible(presentation, hasStarted)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/emberjs/utils/EmberUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class EmberUtils {
return project.guessProjectDir()?.isEmber ?: false
}
fun isEnabledEmberProject(project: Project): Boolean {
return FrameworkDetectionManager.getInstance(project).detectedFrameworks.find { it.detector is EmberCliFrameworkDetector && (it.detector as EmberCliFrameworkDetector).isConfigured(it.relatedFiles, project) } != null
return EmberCliFrameworkDetector.hasEnabledEmberFramework(project)
}

fun resolveModifier(file: PsiElement?): Array<JSFunction?> {
Expand Down
Loading