Skip to content

Commit

Permalink
DEV2-4253 refine workspace folders resolution (#688)
Browse files Browse the repository at this point in the history
* DEV2-4253 refine workspace folders resolution

* add another debug log

* log
  • Loading branch information
yonip23 authored Nov 16, 2023
1 parent d06a855 commit 4194fca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class WorkspaceFoldersHandler(gson: Gson) : ChatMessageHandler<Unit, WorkspaceFo
private val workspaceService = ServiceManager.getService(WorkspaceListenerService::class.java)

override fun handle(payload: Unit?, project: Project): WorkspaceFoldersPayload? {
val rootPaths = workspaceService.getWorkspaceRootPaths() ?: return null
val rootPaths = workspaceService.getWorkspaceRootPaths(project) ?: return null
return WorkspaceFoldersPayload(rootPaths)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.tabnineCommon.lifecycle

import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.util.concurrency.AppExecutorUtil
Expand All @@ -11,6 +12,9 @@ import java.net.URL
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean

const val UPDATE_WORKSPACE_INITIAL_DELAY = 5L
const val UPDATE_WORKSPACE_INTERVAL = 30L

@Service
class WorkspaceListenerService {
private val binaryRequestFacade = DependencyContainer.instanceOfBinaryRequestFacade()
Expand All @@ -21,26 +25,36 @@ class WorkspaceListenerService {
if (started.getAndSet(true)) return

scheduler.scheduleWithFixedDelay(
{
val rootPaths = getWorkspaceRootPaths() ?: return@scheduleWithFixedDelay
binaryRequestFacade.executeRequest(Workspace(rootPaths))
},
5, 30, TimeUnit.SECONDS
{ updateWorkspaceRootPaths() },
UPDATE_WORKSPACE_INITIAL_DELAY,
UPDATE_WORKSPACE_INTERVAL,
TimeUnit.SECONDS
)
}

fun getWorkspaceRootPaths(): List<String>? {
val project = ProjectManager.getInstance().openProjects.firstOrNull() ?: return null
private fun updateWorkspaceRootPaths() {
val rootPaths = ProjectManager.getInstance()
.openProjects
.map { getWorkspaceRootPaths(it) ?: emptyList() }
.reduceOrNull { acc, cur -> acc.plus(cur) }

if (rootPaths.isNullOrEmpty()) return
Logger.getInstance(javaClass).info("All root paths collected: $rootPaths")

binaryRequestFacade.executeRequest(Workspace(rootPaths))
}

fun getWorkspaceRootPaths(project: Project): List<String>? {
if (project.isDisposed) {
Logger.getInstance(javaClass).warn("Project ${project.name} is disposed, skipping workspace update")
Logger.getInstance(javaClass).warn("Project ${project.name} is disposed, skipping root paths resolution")
return null
}

val rootPaths = mutableListOf<String>()
for (contentRootUrl in ProjectRootManager.getInstance(project).contentRootUrls) {
val url = URL(contentRootUrl)
if (url.protocol != "file") {
Logger.getInstance(javaClass).debug("Skipping workspace update for project ${project.name}, unsupported protocol ${url.protocol}")
Logger.getInstance(javaClass).debug("$url in project ${project.name} has unsupported protocol (${url.protocol})")
continue
}
rootPaths.add(url.path)
Expand Down

0 comments on commit 4194fca

Please sign in to comment.