-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds generated protocol bindings boilerplate ## Test plan - Bindings are as of know unused. No additional testing required - Verified that build still works
- Loading branch information
Showing
202 changed files
with
4,519 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env bash | ||
set -eu | ||
|
||
echo "=====================================================" | ||
echo "= Copying protocol files from CODY_DIR =" | ||
echo "=====================================================" | ||
|
||
# if CODY_DIR is not set then we assume it's relative to the gitroot (look that up) | ||
# and then ../cody/ (which will need to be converted into an absolute path) | ||
if [ -z "${CODY_DIR:-}" ]; then | ||
CODY_DIR="$(git rev-parse --show-toplevel)/../cody/" | ||
echo "CODY_DIR is not set so using ${CODY_DIR}" | ||
fi | ||
CODY_DIR="$CODY_DIR" ./gradlew copyProtocol -PforceProtocolCopy=true |
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
57 changes: 57 additions & 0 deletions
57
src/main/kotlin/com/sourcegraph/cody/agent/EnumTypeAdapterFactory.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,57 @@ | ||
package com.sourcegraph.cody.agent | ||
|
||
import com.google.gson.* | ||
import com.google.gson.annotations.SerializedName | ||
import com.google.gson.reflect.TypeToken | ||
import com.google.gson.stream.JsonReader | ||
import com.google.gson.stream.JsonWriter | ||
import java.io.IOException | ||
import java.lang.reflect.Field | ||
|
||
class EnumTypeAdapterFactory : TypeAdapterFactory { | ||
override fun <T> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? { | ||
val rawType = type.rawType as? Class<*> ?: return null | ||
if (!rawType.isEnum) { | ||
return null | ||
} | ||
@Suppress("UNCHECKED_CAST") | ||
return EnumTypeAdapter(rawType as Class<out Enum<*>>) as TypeAdapter<T> | ||
} | ||
} | ||
|
||
class EnumTypeAdapter<T : Enum<T>>(private val classOfT: Class<T>) : TypeAdapter<T>() { | ||
private val nameToConstant: Map<String, T> = HashMap() | ||
private val constantToName: Map<T, String> = HashMap() | ||
|
||
init { | ||
for (constant in classOfT.enumConstants) { | ||
val name = getSerializedName(constant) ?: constant.name | ||
(nameToConstant as HashMap)[name.lowercase()] = constant | ||
(constantToName as HashMap)[constant] = name | ||
} | ||
} | ||
|
||
private fun getSerializedName(enumConstant: T): String? { | ||
return try { | ||
val field: Field = classOfT.getField(enumConstant.name) | ||
field.getAnnotation(SerializedName::class.java)?.value | ||
} catch (e: NoSuchFieldException) { | ||
null | ||
} | ||
} | ||
|
||
override fun write(out: JsonWriter, value: T?) { | ||
if (value == null) { | ||
out.nullValue() | ||
} else { | ||
out.value(constantToName[value]) | ||
} | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun read(`in`: JsonReader): T? { | ||
val value = `in`.nextString() | ||
return nameToConstant[value.lowercase()] | ||
?: throw JsonParseException("Unknown enum value: $value") | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/sourcegraph/cody/agent/protocol/package-info.java
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,5 @@ | ||
/** | ||
* @deprecated Replaced by {@link package com.sourcegraph.cody.agent.generated_protocol} and {@link package com.sourcegraph.cody.agent.protocol_extensions} | ||
*/ | ||
@Deprecated | ||
package com.sourcegraph.cody.agent.protocol; |
40 changes: 40 additions & 0 deletions
40
src/main/kotlin/com/sourcegraph/cody/agent/protocol_extensions/ClientCapabilities.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,40 @@ | ||
package com.sourcegraph.cody.agent.protocol_extensions | ||
|
||
import com.sourcegraph.cody.agent.protocol_generated.ClientCapabilities | ||
import com.sourcegraph.cody.agent.protocol_generated.ClientCapabilities.ChatEnum | ||
import com.sourcegraph.cody.agent.protocol_generated.ClientCapabilities.CodeLensesEnum | ||
import com.sourcegraph.cody.agent.protocol_generated.ClientCapabilities.CompletionsEnum | ||
import com.sourcegraph.cody.agent.protocol_generated.ClientCapabilities.EditEnum | ||
import com.sourcegraph.cody.agent.protocol_generated.ClientCapabilities.EditWorkspaceEnum | ||
import com.sourcegraph.cody.agent.protocol_generated.ClientCapabilities.GitEnum | ||
import com.sourcegraph.cody.agent.protocol_generated.ClientCapabilities.IgnoreEnum | ||
import com.sourcegraph.cody.agent.protocol_generated.ClientCapabilities.ProgressBarsEnum | ||
import com.sourcegraph.cody.agent.protocol_generated.ClientCapabilities.ShowDocumentEnum | ||
import com.sourcegraph.cody.agent.protocol_generated.ClientCapabilities.UntitledDocumentsEnum | ||
|
||
object ClientCapabilitiesFactory { | ||
fun build( | ||
completions: String? = null, | ||
chat: String? = null, | ||
git: String? = null, | ||
progressBars: String? = null, | ||
edit: String? = null, | ||
editWorkspace: String? = null, | ||
codeLenses: String? = null, | ||
showDocument: String? = null, | ||
ignore: String? = null, | ||
untitledDocuments: String? = null | ||
): ClientCapabilities { | ||
return ClientCapabilities( | ||
completions = completions?.toEnumIgnoreCase<CompletionsEnum>(), | ||
chat = chat?.toEnumIgnoreCase<ChatEnum>(), | ||
git = git?.toEnumIgnoreCase<GitEnum>(), | ||
progressBars = progressBars?.toEnumIgnoreCase<ProgressBarsEnum>(), | ||
edit = edit?.toEnumIgnoreCase<EditEnum>(), | ||
editWorkspace = editWorkspace?.toEnumIgnoreCase<EditWorkspaceEnum>(), | ||
codeLenses = codeLenses?.toEnumIgnoreCase<CodeLensesEnum>(), | ||
showDocument = showDocument?.toEnumIgnoreCase<ShowDocumentEnum>(), | ||
ignore = ignore?.toEnumIgnoreCase<IgnoreEnum>(), | ||
untitledDocuments = untitledDocuments?.toEnumIgnoreCase<UntitledDocumentsEnum>()) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/com/sourcegraph/cody/agent/protocol_extensions/ClientInfo.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 com.sourcegraph.cody.agent.protocol_extensions | ||
|
||
import com.sourcegraph.cody.agent.protocol_generated.ClientCapabilities | ||
import com.sourcegraph.cody.agent.protocol_generated.ClientInfo | ||
import com.sourcegraph.cody.agent.protocol_generated.ExtensionConfiguration | ||
|
||
object ClientInfoFactory { | ||
fun build( | ||
version: String, | ||
ideVersion: String, | ||
workspaceRootUri: String, | ||
extensionConfiguration: ExtensionConfiguration?, | ||
capabilities: ClientCapabilities? | ||
): ClientInfo { | ||
return ClientInfo( | ||
name = "JetBrains", | ||
version = version, | ||
ideVersion = ideVersion, | ||
workspaceRootUri = workspaceRootUri, | ||
extensionConfiguration = extensionConfiguration, | ||
capabilities = capabilities, | ||
) | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/kotlin/com/sourcegraph/cody/agent/protocol_extensions/Position.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,45 @@ | ||
package com.sourcegraph.cody.agent.protocol_extensions | ||
|
||
// because line / column should probably be a | ||
// long | ||
import com.intellij.openapi.editor.Document | ||
import com.intellij.openapi.editor.LogicalPosition | ||
import com.sourcegraph.cody.agent.protocol_generated.Position | ||
import kotlin.math.max | ||
import kotlin.math.min | ||
|
||
fun Position(line: Int, character: Int): Position { | ||
return Position(line.toLong(), character.toLong()) | ||
} | ||
|
||
object PositionFactory { | ||
fun fromOffset(document: Document, offset: Int): Position { | ||
val line = document.getLineNumber(offset) | ||
val lineStartOffset = document.getLineStartOffset(line) | ||
return Position(line.toLong(), (offset - lineStartOffset).toLong()) | ||
} | ||
} | ||
|
||
fun Position.isStartOrEndOfDocumentMarker(document: Document): Boolean { | ||
return line < 0 || line > document.lineCount | ||
} | ||
|
||
fun Position.getRealLine(document: Document): Int { | ||
return min(max(0, document.lineCount - 1), line.toInt()) | ||
} | ||
|
||
fun Position.getRealColumn(document: Document): Int { | ||
val realLine = getRealLine(document) | ||
val lineLength = document.getLineEndOffset(realLine) - document.getLineStartOffset(realLine) | ||
return min(lineLength, character.toInt()) | ||
} | ||
|
||
fun Position.toLogicalPosition(document: Document): LogicalPosition { | ||
return LogicalPosition(getRealLine(document), getRealColumn(document)) | ||
} | ||
|
||
/** Return zero-based offset of this position in the document. */ | ||
fun Position.toOffset(document: Document): Int { | ||
val lineStartOffset = document.getLineStartOffset(getRealLine(document)) | ||
return lineStartOffset + getRealColumn(document) | ||
} |
Oops, something went wrong.