generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support generate http transcoding requests
- Loading branch information
Showing
23 changed files
with
553 additions
and
35 deletions.
There are no files selected for viewing
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
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
25 changes: 24 additions & 1 deletion
25
src/main/kotlin/io/kanro/idea/plugin/protobuf/aip/Extension.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 |
---|---|---|
@@ -1,6 +1,29 @@ | ||
package io.kanro.idea.plugin.protobuf.aip | ||
|
||
import com.intellij.psi.util.CachedValueProvider | ||
import com.intellij.psi.util.CachedValuesManager | ||
import com.intellij.psi.util.PsiModificationTracker | ||
import io.kanro.idea.plugin.protobuf.lang.psi.ProtobufMessageDefinition | ||
import io.kanro.idea.plugin.protobuf.lang.psi.ProtobufRpcDefinition | ||
import io.kanro.idea.plugin.protobuf.lang.psi.nullCachedValue | ||
import io.kanro.idea.plugin.protobuf.lang.psi.stringValue | ||
|
||
fun ProtobufRpcDefinition.aipMethod() { | ||
internal fun ProtobufRpcDefinition.transcodingBody(): String? { | ||
return CachedValuesManager.getCachedValue(this) { | ||
val option = | ||
options(AipOptions.httpOption).lastOrNull() | ||
?: return@getCachedValue nullCachedValue() | ||
|
||
val result = option.value(AipOptions.httpRuleBodyField)?.stringValue() ?: "" | ||
CachedValueProvider.Result.create(result, PsiModificationTracker.MODIFICATION_COUNT) | ||
} | ||
} | ||
|
||
internal fun ProtobufRpcDefinition.resolveInput(): ProtobufMessageDefinition? { | ||
return CachedValuesManager.getCachedValue(this) { | ||
val input = | ||
this.rpcIOList.firstOrNull()?.typeName?.reference?.resolve() as? ProtobufMessageDefinition | ||
?: return@getCachedValue nullCachedValue() | ||
return@getCachedValue CachedValueProvider.Result.create(input, PsiModificationTracker.MODIFICATION_COUNT) | ||
} | ||
} |
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
101 changes: 101 additions & 0 deletions
101
src/main/kotlin/io/kanro/idea/plugin/protobuf/grpc/gutter/AipRunRequestGutterProvider.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,101 @@ | ||
package io.kanro.idea.plugin.protobuf.grpc.gutter | ||
|
||
import com.google.api.pathtemplate.PathTemplate | ||
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo | ||
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider | ||
import com.intellij.codeInsight.template.impl.TemplateImpl | ||
import com.intellij.httpClient.actions.generation.HttpRequestUrlPathInfo | ||
import com.intellij.httpClient.actions.generation.HttpRequestUrlsGenerationRequest | ||
import com.intellij.httpClient.actions.generation.RequestUrlContextInfo | ||
import com.intellij.httpClient.executor.util.unwrap | ||
import com.intellij.httpClient.http.request.microservices.OpenInHttpClientLineMarkerBuilder | ||
import com.intellij.httpClient.http.request.microservices.RequestContextData | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.util.parentOfType | ||
import io.kanro.idea.plugin.protobuf.ProtobufIcons | ||
import io.kanro.idea.plugin.protobuf.aip.AipOptions | ||
import io.kanro.idea.plugin.protobuf.lang.psi.ProtobufFieldAssign | ||
import io.kanro.idea.plugin.protobuf.lang.psi.ProtobufRpcOption | ||
import io.kanro.idea.plugin.protobuf.lang.psi.firstLeaf | ||
import io.kanro.idea.plugin.protobuf.lang.psi.primitive.element.ProtobufRpcDefinition | ||
import io.kanro.idea.plugin.protobuf.lang.psi.stringValue | ||
import javax.swing.Icon | ||
|
||
class AipRunRequestGutterProvider : RelatedItemLineMarkerProvider() { | ||
override fun getIcon(): Icon { | ||
return ProtobufIcons.PROCEDURE | ||
} | ||
|
||
override fun getId(): String { | ||
return "AipRunRequestGutterProvider" | ||
} | ||
|
||
override fun getName(): String { | ||
return "Run gRpc via HTTP transcoding" | ||
} | ||
|
||
@Suppress("UnstableApiUsage") | ||
override fun collectNavigationMarkers( | ||
element: PsiElement, | ||
result: MutableCollection<in RelatedItemLineMarkerInfo<*>>, | ||
) { | ||
if (element !is ProtobufFieldAssign) return | ||
val option = element.parentOfType<ProtobufRpcOption>() ?: return | ||
val grpcDefinition = element.parentOfType<ProtobufRpcDefinition>() ?: return | ||
if (!option.isOption(AipOptions.httpOption)) return | ||
val fieldName = element.field()?.qualifiedName() ?: return | ||
if (fieldName !in AipOptions.httpRulesName) return | ||
val httpMethod = fieldName.lastComponent?.uppercase() ?: return | ||
val path = element.constant?.stringValue() ?: return | ||
|
||
val service = grpcDefinition.owner() ?: return | ||
val serviceName = service.qualifiedName() ?: return | ||
val methodName = grpcDefinition.name() ?: return | ||
val hasBody = httpMethod in listOf("POST", "PUT", "PATCH") && option.value(AipOptions.httpRuleBodyField) != null | ||
|
||
val request = | ||
HttpRequestUrlsGenerationRequest( | ||
listOfNotNull( | ||
HttpRequestUrlPathInfo.create( | ||
element.project, | ||
PathTemplate.create(path).withoutVars().toString(), | ||
listOf(httpMethod), | ||
).unwrap(false), | ||
), | ||
RequestUrlContextInfo.create( | ||
element.project, | ||
listOf("http://", "https://"), | ||
listOf("localhost:8080"), | ||
buildCustomRequestBodyTemplate(serviceName.toString(), methodName, hasBody), | ||
).unwrap(false) ?: return, | ||
) | ||
|
||
result += | ||
OpenInHttpClientLineMarkerBuilder.fromGenerationRequest(element.project, request) | ||
.createLineMarkerInfo(element.firstLeaf(), ProtobufIcons.PROCEDURE_HTTP) | ||
} | ||
|
||
private fun buildCustomRequestBodyTemplate( | ||
serviceName: String, | ||
methodName: String, | ||
hasBody: Boolean, | ||
): RequestContextData { | ||
return if (hasBody) { | ||
RequestContextData.CustomRequestBodyTemplate( | ||
TemplateImpl( | ||
"transcodingWithBody", | ||
"\ngrpc-method: $serviceName/$methodName\ncontent-type: application/json\n\n{\n}", | ||
"grpc", | ||
), | ||
) | ||
} else { | ||
RequestContextData.CustomRequestBodyTemplate( | ||
TemplateImpl( | ||
"transcodingWithoutBody", | ||
"\ngrpc-method: $serviceName/$methodName", | ||
"grpc", | ||
), | ||
) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.