Skip to content

Commit

Permalink
DEV2-4218 add support for multi-repo workspace (#685)
Browse files Browse the repository at this point in the history
* DEV2-4218 add update workspace service

* remove workspace folders from init response

* remove workspace folders from init response

* add "workspace_folders" command
  • Loading branch information
yonip23 authored Nov 15, 2023
1 parent f8b6c16 commit d06a855
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.tabnineCommon.binary.requests.fileLifecycle

import com.google.gson.annotations.SerializedName
import com.tabnineCommon.binary.BinaryRequest
import com.tabnineCommon.binary.requests.EmptyResponse

data class Workspace(
@SerializedName("root_paths") private val rootPaths: List<String>
) : BinaryRequest<EmptyResponse> {
override fun response(): Class<EmptyResponse> {
return EmptyResponse::class.java
}

override fun serialize(): Any {
return mapOf("Workspace" to this)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.tabnineCommon.chat

import InitHandler
import InsertAtCursorHandler
import WorkspaceFoldersHandler
import com.google.gson.JsonElement
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.Project
Expand Down Expand Up @@ -39,7 +40,8 @@ object ChatMessagesRouter {
"get_settings" to GetChatSettingsHandler(gson),
"update_settings" to UpdateChatSettingsHandler(gson),
"get_server_url" to GetServerUrlHandler(gson),
"navigate_to_location" to NavigateToLocationHandler(gson)
"navigate_to_location" to NavigateToLocationHandler(gson),
"workspace_folders" to WorkspaceFoldersHandler(gson)
)

fun handleRawMessage(rawRequest: String, project: Project): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import com.google.gson.Gson
import com.google.gson.JsonElement
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.project.Project
import com.tabnineCommon.chat.commandHandlers.ChatMessageHandler
import com.tabnineCommon.lifecycle.WorkspaceListenerService

data class WorkspaceFoldersPayload(private val rootPaths: List<String>)

class WorkspaceFoldersHandler(gson: Gson) : ChatMessageHandler<Unit, WorkspaceFoldersPayload>(gson) {
private val workspaceService = ServiceManager.getService(WorkspaceListenerService::class.java)

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

override fun deserializeRequest(data: JsonElement?) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import com.intellij.openapi.components.ServiceManager

fun initializeLifecycleEndpoints() {
ServiceManager.getService(BinaryStateService::class.java).startUpdateLoop()
ServiceManager.getService(WorkspaceListenerService::class.java).start()
TabnineFileEditorListener.registerListener()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.tabnineCommon.lifecycle

import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.util.concurrency.AppExecutorUtil
import com.tabnineCommon.binary.requests.fileLifecycle.Workspace
import com.tabnineCommon.general.DependencyContainer
import java.net.URL
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean

@Service
class WorkspaceListenerService {
private val binaryRequestFacade = DependencyContainer.instanceOfBinaryRequestFacade()
private val scheduler = AppExecutorUtil.getAppScheduledExecutorService()
private val started = AtomicBoolean(false)

fun start() {
if (started.getAndSet(true)) return

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

fun getWorkspaceRootPaths(): List<String>? {
val project = ProjectManager.getInstance().openProjects.firstOrNull() ?: return null
if (project.isDisposed) {
Logger.getInstance(javaClass).warn("Project ${project.name} is disposed, skipping workspace update")
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}")
continue
}
rootPaths.add(url.path)
}

Logger.getInstance(javaClass).debug("Root paths for project ${project.name} found: $rootPaths")
return rootPaths
}
}

0 comments on commit d06a855

Please sign in to comment.