generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/DigmaProtocolApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.digma.intellij.plugin.protocol | ||
|
||
import com.intellij.openapi.components.Service | ||
import com.intellij.openapi.diagnostic.Logger | ||
import com.intellij.openapi.project.Project | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import org.digma.intellij.plugin.common.DisposableAdaptor | ||
import org.digma.intellij.plugin.errorreporting.ErrorReporter | ||
import org.digma.intellij.plugin.log.Log | ||
|
||
const val ACTION_ASSETS = "assets" | ||
|
||
@Service(Service.Level.PROJECT) | ||
class DigmaProtocolApi(val cs: CoroutineScope) : DisposableAdaptor { | ||
|
||
private val logger: Logger = Logger.getInstance(this::class.java) | ||
|
||
fun performAction(project: Project, action: String, waitForJcef: Boolean) { | ||
try { | ||
|
||
Log.log(logger::trace, "perform action {}, thread {}", action, Thread.currentThread().name) | ||
|
||
cs.launch { | ||
if (waitForJcef) { | ||
delay(5000) | ||
} | ||
project.messageBus.syncPublisher(ProtocolCommandEvent.PROTOCOL_COMMAND_TOPIC).protocolCommand(action) | ||
} | ||
|
||
} catch (e: Throwable) { | ||
ErrorReporter.getInstance().reportError("DigmaProtocolApi.performAction", e) | ||
} | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/DigmaProtocolCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package org.digma.intellij.plugin.protocol | ||
|
||
import com.intellij.ide.RecentProjectListActionProvider | ||
import com.intellij.ide.RecentProjectsManager | ||
import com.intellij.ide.RecentProjectsManagerBase | ||
import com.intellij.ide.ReopenProjectAction | ||
import com.intellij.ide.impl.OpenProjectTask | ||
import com.intellij.navigation.PROJECT_NAME_KEY | ||
import com.intellij.navigation.ProtocolOpenProjectResult | ||
import com.intellij.navigation.openProject | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.application.JBProtocolCommand | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.diagnostic.Logger | ||
import com.intellij.util.text.nullize | ||
import org.digma.intellij.plugin.common.findActiveProject | ||
import org.digma.intellij.plugin.errorreporting.ErrorReporter | ||
import org.digma.intellij.plugin.log.Log | ||
import org.digma.intellij.plugin.ui.ToolWindowShower | ||
import java.nio.file.Path | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.Future | ||
|
||
const val DIGMA_COMMAND = "digma" | ||
const val DIGMA_PLUGIN_TARGET = "plugin" | ||
const val ACTION_NAME_KEY = "action" | ||
|
||
//jetbrains://idea/digma/plugin?action=assets | ||
//jetbrains://idea/digma/plugin?project=spring-petclinic&action=assets | ||
|
||
class DigmaProtocolCommand : JBProtocolCommand(DIGMA_COMMAND) { | ||
private val logger: Logger = Logger.getInstance(this::class.java) | ||
|
||
// override fun perform(target: String?, parameters: Map<String, String>, fragment: String?): Future<String?> { | ||
override suspend fun execute(target: String?, parameters: Map<String, String>, fragment: String?): String? { | ||
try { | ||
return executeImpl(target,parameters,fragment) | ||
}catch (e:Throwable){ | ||
ErrorReporter.getInstance().reportError("DigmaProtocolCommand.execute",e) | ||
return "Error $e" | ||
} | ||
} | ||
|
||
private suspend fun executeImpl(target: String?, parameters: Map<String, String>, fragment: String?): String? { | ||
|
||
if (target != DIGMA_PLUGIN_TARGET) { | ||
// return CompletableFuture.completedFuture("DigmaProtocolCommand Supports Only Plugin Target") | ||
return "DigmaProtocolCommand Supports Only Plugin Target" | ||
} | ||
|
||
Log.log( | ||
logger::trace, | ||
"execute called with target={},fragment={},parameters={}, thread={}", | ||
target, | ||
fragment.toString(), | ||
parameters.toUrlQueryString(), | ||
Thread.currentThread().name | ||
) | ||
|
||
|
||
var project = if (parameters.containsKey(PROJECT_NAME_KEY)) { | ||
when (val openProjectResult = openProject(parameters)) { | ||
is ProtocolOpenProjectResult.Success -> openProjectResult.project | ||
// is ProtocolOpenProjectResult.Error -> return CompletableFuture.completedFuture(openProjectResult.message) | ||
is ProtocolOpenProjectResult.Error -> return openProjectResult.message | ||
} | ||
} else { | ||
findActiveProject() | ||
} | ||
|
||
if (project == null) { | ||
val recentProjectPath = | ||
RecentProjectListActionProvider.getInstance().getActions().asSequence().filterIsInstance(ReopenProjectAction::class.java) | ||
.firstOrNull()?.projectPath ?: RecentProjectsManager.getInstance().lastProjectCreationLocation | ||
if (recentProjectPath != null) { | ||
project = RecentProjectsManagerBase.getInstanceEx().openProject(Path.of(recentProjectPath), OpenProjectTask()) | ||
} | ||
} | ||
|
||
|
||
if (project != null) { | ||
|
||
Log.log(logger::trace, "got project {}", project.name) | ||
|
||
val action = parameters[ACTION_NAME_KEY]?.nullize(nullizeSpaces = true) | ||
// ?: return CompletableFuture.completedFuture("DigmaProtocolCommand no action in request") | ||
?: return "DigmaProtocolCommand no action in request" | ||
|
||
|
||
var waitForJcef = false | ||
if (!ToolWindowShower.getInstance(project).isToolWindowVisible) { | ||
waitForJcef = true | ||
ApplicationManager.getApplication().invokeAndWait { | ||
Log.log(logger::trace, "showing tool window") | ||
ToolWindowShower.getInstance(project).showToolWindow() | ||
Log.log(logger::trace, "tool window shown") | ||
} | ||
} | ||
|
||
Log.log(logger::trace, "executing action {}", action) | ||
project.service<DigmaProtocolApi>().performAction(project, action, waitForJcef) | ||
Log.log(logger::trace, "after execute action {}", action) | ||
// return CompletableFuture.completedFuture(null) | ||
return null | ||
} | ||
|
||
// return CompletableFuture.completedFuture("DigmaProtocolCommand can not find project") | ||
return "DigmaProtocolCommand can not find project" | ||
|
||
} | ||
|
||
// override suspend fun execute(target: String?, parameters: Map<String, String>, fragment: String?): String? { | ||
// return super.execute(target, parameters, fragment) | ||
// } | ||
} |
24 changes: 24 additions & 0 deletions
24
ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/ProtocolCommandEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.digma.intellij.plugin.protocol | ||
|
||
import com.intellij.util.messages.Topic | ||
|
||
|
||
/** | ||
* this is an application event that should fire when we change the api client, | ||
* usually when user changes the api url in settings. | ||
*/ | ||
fun interface ProtocolCommandEvent { | ||
|
||
|
||
companion object { | ||
@JvmStatic | ||
@Topic.ProjectLevel | ||
val PROTOCOL_COMMAND_TOPIC: Topic<ProtocolCommandEvent> = Topic.create( | ||
"PROTOCOL COMMAND", | ||
ProtocolCommandEvent::class.java | ||
) | ||
} | ||
|
||
fun protocolCommand(action: String) | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
ide-common/src/main/kotlin/org/digma/intellij/plugin/protocol/protocol-utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.digma.intellij.plugin.protocol | ||
|
||
import java.net.URLEncoder | ||
|
||
|
||
fun Map<String, String>.toUrlQueryString() = | ||
this.map {(k,v) -> "${URLEncoder.encode(k, "UTF-8")}=${URLEncoder.encode(v, "UTF-8")}" } | ||
.joinToString("&") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters