@@ -6,6 +6,7 @@ package util
66import  java .util .concurrent .atomic .AtomicLong 
77
88import  scala .jdk .CollectionConverters .* 
9+ import  scala .util .matching .Regex 
910
1011import  bitlap .sbt .analyzer .model .* 
1112import  bitlap .sbt .analyzer .parsing .* 
@@ -25,17 +26,14 @@ import com.intellij.openapi.util.text.StringUtil
2526
2627object  DependencyUtils  {
2728
28-   final  val  DefaultConfiguration  =  toDAScope(" default" 
29+   final  val  DEFAULT_CONFIGURATION  =  toDAScope(" default" 
2930
30-   private  final  val  rootId      =  new  AtomicLong (0 )
31-   private  final  val  artifactId  =  new  AtomicLong (0 )
31+   private  final  val  rootId        =  new  AtomicLong (0 )
32+   private  final  val  artifactId    =  new  AtomicLong (0 )
33+   private  val  SBT_ARTIFACT_REGEX  =  " (.*):(.*):(.*)" 
3234
33-   private  val  SBT_ARTIFACT_REGEX       =  " (.*):(.*):(.*)" 
34-   private  val  SCALA_ARTIFACT_REGEX     =  " (.*)_(.*)" 
35-   private  val  NATIVE_ARTIFACT_PATTERN  =  " _native\\ d(\\ .\\ d+)?_\\ d(\\ .\\ d+)?" 
36-   private  val  SJS_ARTIFACT_PATTERN     =  " _sjs\\ d(\\ .\\ d+)?_\\ d(\\ .\\ d+)?" 
37-   private  val  NATIVE_ARTIFACT_REGEX    =  " (.*)(_native\\ d(\\ .\\ d+)?)_(.*)" 
38-   private  val  SJS_ARTIFACT_REGEX       =  " (.*)(_sjs\\ d(\\ .\\ d+)?)_(.*)" 
35+   val  SCALA_VERSION_PATTERN :  Regex  = 
36+     """ ^([a-zA-Z0-9-]+?)(?:_(?:sjs\d+(?:\.\d+)?|native\d*(?:\.\d+)?|2\.1[123]|3))+$""" 
3937
4038  private  final  case  class  PlatformModule (
4139    module : String ,
@@ -50,10 +48,10 @@ object DependencyUtils {
5048   *  and applying filtering logic, which is critical for dependency graph/tree operations as this node serves as the 
5149   *  root of the structure. 
5250   */  
53-   def  isSelfNode (dn : DependencyNode , context : ModuleContext ):  Boolean  =  {
51+   def  isSelfNode (dn : DependencyNode , context : AnalyzerContext ):  Boolean  =  {
5452    dn.getDisplayName match 
5553      case  SBT_ARTIFACT_REGEX (group, artifact, _) => 
56-         context.organization ==  group &&  isSelfArtifact (artifact, context)
54+         context.organization ==  group &&  isSubModule (artifact, context)
5755      case  _ =>  false 
5856  }
5957
@@ -96,7 +94,7 @@ object DependencyUtils {
9694  def  appendChildrenAndFixProjectNodes [N  <:  DependencyNode ](
9795    parentNode : N ,
9896    nodes : Seq [DependencyNode ],
99-     context : ModuleContext 
97+     context : AnalyzerContext 
10098  ):  Unit  =  {
10199    parentNode.getDependencies.addAll(nodes.asJava)
102100    val  moduleDependencies  =  nodes.filter(d =>  isProjectModule(d, context))
@@ -112,8 +110,8 @@ object DependencyUtils {
112110      val  group       =  artifact.map(_.group).getOrElse(Constants .EMPTY_STRING )
113111      //  Use artifact to determine whether there are modules in the dependency.
114112      if  (
115-         context.ideaModuleIdSbtModuleNames .values
116-           .exists(d =>  group ==  context.organization &&  toPlatformModule (artifactId).module  ==  d)
113+         context.moduleIdArtifactIdsCache .values
114+           .exists(d =>  group ==  context.organization &&  getPlatformModule (artifactId) ==  d)
117115      ) {
118116        appendChildrenAndFixProjectNodes(
119117          node,
@@ -124,51 +122,40 @@ object DependencyUtils {
124122    }
125123  }
126124
127-   private  def  isSelfArtifact ( artifact : String , context : ModuleContext ):  Boolean  =  {
128-     //  processing  cross-platform, module name is not artifact!
125+   private  def  isSubModule ( maybeModule : String , context : AnalyzerContext ):  Boolean  =  {
126+     //  Handles the  cross-platform, module name is not equals to  artifact!
129127    val  currentModuleName  = 
130-       context.ideaModuleIdSbtModuleNames .getOrElse(
128+       context.moduleIdArtifactIdsCache .getOrElse(
131129        context.currentModuleId,
132-         context.ideaModuleIdSbtModuleNames .getOrElse(
130+         context.moduleIdArtifactIdsCache .getOrElse(
133131          Constants .SINGLE_SBT_MODULE ,
134-           context.ideaModuleIdSbtModuleNames .getOrElse(Constants .ROOT_SBT_MODULE , context.currentModuleId)
132+           context.moduleIdArtifactIdsCache .getOrElse(Constants .ROOT_SBT_MODULE , context.currentModuleId)
135133        )
136134      )
137135
138136    //  NOTE: we don't determine the Scala version number.
139-     if  (context.isScalaNative) {
140-       val  module  =  artifact.replaceAll(NATIVE_ARTIFACT_PATTERN , Constants .EMPTY_STRING )
141-       currentModuleName.equalsIgnoreCase(module)
142-     } else  if  (context.isScalaJs) {
143-       val  module  =  artifact.replaceAll(SJS_ARTIFACT_PATTERN , Constants .EMPTY_STRING )
144-       currentModuleName.equalsIgnoreCase(module)
145-     } else  {
146-       artifact match 
147-         case  SCALA_ARTIFACT_REGEX (module, _) => 
148-           currentModuleName.equalsIgnoreCase(module)
149-         //  it is a java project
150-         case  _ =>  artifact.equalsIgnoreCase(currentModuleName)
137+     maybeModule match  {
138+       case  SCALA_VERSION_PATTERN (module) =>  module.equalsIgnoreCase(currentModuleName)
139+       case  _                             =>  maybeModule.equalsIgnoreCase(currentModuleName)
151140    }
152141  }
153142
154-   private  def  toPlatformModule (artifact : String ):  PlatformModule  =  {
143+   private  def  getPlatformModule (artifact : String ):  String  =  {
155144    artifact match 
156-       case  SJS_ARTIFACT_REGEX (module, _, _, scalaVer)    =>  PlatformModule (module, scalaVer)
157-       case  NATIVE_ARTIFACT_REGEX (module, _, _, scalaVer) =>  PlatformModule (module, scalaVer)
158-       case  SCALA_ARTIFACT_REGEX (module, scalaVer)        =>  PlatformModule (module, scalaVer)
159-       case  _                                             =>  PlatformModule (artifact, Constants .EMPTY_STRING )
145+       case  SCALA_VERSION_PATTERN (module) =>  module
146+       case  _                             =>  artifact
160147  }
161148
162-   private  def  toProjectDependencyNode (dn : DependencyNode , context : ModuleContext ):  Option [DependencyNode ] =  {
149+   private  def  toProjectDependencyNode (dn : DependencyNode , context : AnalyzerContext ):  Option [DependencyNode ] =  {
163150    val  artifactInfo  =  getArtifactInfoFromDisplayName(dn.getDisplayName).orNull
164151    if  (artifactInfo ==  null ) return  None 
165-     val  sbtModuleName   =  toPlatformModule (artifactInfo.artifact).module 
166-     val  ideaModuleName  =  context.ideaModuleIdSbtModuleNames .find(_._2 ==  sbtModuleName).map(_._1)
152+     val  sbtModuleName   =  getPlatformModule (artifactInfo.artifact)
153+     val  ideaModuleName  =  context.moduleIdArtifactIdsCache .find(_._2 ==  sbtModuleName).map(_._1)
167154
168155    //  Processing cross-platform, module name is not artifact
169156    //  This is a project node, we need a module not an artifact to get project path!
170157
171-     val  fixedCustomName  =  context.ideaModuleNamePaths .map { case  (name, path) => 
158+     val  fixedCustomName  =  context.moduleNamePathsCache .map { case  (name, path) => 
172159      if  (name.exists(_ ==  ' ' ))
173160        name.toLowerCase.replace(' ' , '-' ) ->  path
174161      else 
@@ -177,9 +164,9 @@ object DependencyUtils {
177164
178165    val  projectPath  = 
179166      ideaModuleName
180-         .flatMap(m =>  context.ideaModuleNamePaths .get(m))
167+         .flatMap(m =>  context.moduleNamePathsCache .get(m))
181168        .getOrElse(
182-           context.ideaModuleNamePaths 
169+           context.moduleNamePathsCache 
183170            .getOrElse(sbtModuleName, fixedCustomName.getOrElse(sbtModuleName.toLowerCase, Constants .EMPTY_STRING ))
184171        )
185172
@@ -201,14 +188,14 @@ object DependencyUtils {
201188    Some (p)
202189  }
203190
204-   private  def  isProjectModule (dn : DependencyNode , context : ModuleContext ):  Boolean  =  {
191+   private  def  isProjectModule (dn : DependencyNode , context : AnalyzerContext ):  Boolean  =  {
205192    //  module dependency
206193    val  artifactInfo  =  getArtifactInfoFromDisplayName(dn.getDisplayName).orNull
207194    if  (artifactInfo ==  null ) return  false 
208195    if  (artifactInfo.group !=  context.organization) return  false 
209196    //  Use artifacts to determine if there are dependent modules
210197    val  matchModule  = 
211-       context.ideaModuleIdSbtModuleNames .values.filter(m =>  m ==  toPlatformModule (artifactInfo.artifact).module )
198+       context.moduleIdArtifactIdsCache .values.filter(m =>  m ==  getPlatformModule (artifactInfo.artifact))
212199
213200    matchModule.nonEmpty
214201
0 commit comments