Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEV2-3340 include text in workspace context #609

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.tabnineCommon.chat.commandHandlers.context

import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.Key

object BasicContextCache {
private val basicContextKey = Key.create<BasicContext>("com.tabnine.BasicContextCacheKey")
fun save(editor: Editor, entry: BasicContext) = editor.putUserData(basicContextKey, entry)

fun get(editor: Editor) = editor.getUserData(basicContextKey)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import com.tabnineCommon.general.DependencyContainer
import java.io.File

data class BasicContext(
private val fileUri: String? = null,
private val language: String? = null,
private var metadata: JsonObject? = null
val fileUri: String? = null,
val language: String? = null,
var metadata: JsonObject? = null
) {
constructor(metadata: JsonObject?) : this() {
this.metadata = metadata
Expand All @@ -23,20 +23,23 @@ data class BasicContext(
class GetBasicContextHandler(gson: Gson) : ChatMessageHandler<Unit, BasicContext>(gson) {
private val binaryRequestFacade = DependencyContainer.instanceOfBinaryRequestFacade()

override fun handle(payload: Unit?, project: Project): BasicContext? {
override fun handle(payload: Unit?, project: Project): BasicContext {
val editor = getEditorFromProject(project) ?: return noEditorResponse(project)

val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(editor.document)
val fileUri = psiFile?.virtualFile?.path
val language = psiFile?.language?.id

var metadata = if (fileUri != null) binaryRequestFacade.executeRequest(FileMetadataRequest(fileUri)) else null

var metadata = fileUri?.let {
binaryRequestFacade.executeRequest(FileMetadataRequest(it))
}
Comment on lines +31 to +33
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same logic - used ?.let instead of if/else

if (metadata?.has("error") == true) {
metadata = null
}
val language = metadata?.get("language")?.asString ?: psiFile?.language?.id

val basicContext = BasicContext(fileUri, language, metadata)
BasicContextCache.save(editor, basicContext)

return BasicContext(fileUri, language, metadata)
return basicContext
}

override fun deserializeRequest(data: JsonElement?) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.tabnineCommon.chat.commandHandlers.context.workspace

import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.tabnineCommon.chat.commandHandlers.context.BasicContextCache
import com.tabnineCommon.chat.commandHandlers.utils.StringCaseConverter
import com.tabnineCommon.chat.commandHandlers.utils.SymbolsResolver
import com.tabnineCommon.chat.commandHandlers.utils.submitReadAction
Expand All @@ -11,6 +13,12 @@ private const val MAX_RESULTS_PER_SYMBOL = 5

class FindSymbolsCommandExecutor : CommandsExecutor {
override fun execute(arg: String, editor: Editor, project: Project): List<String> {
val basicContext = BasicContextCache.get(editor)
if (basicContext == null || basicContext.language.isNullOrBlank()) {
Logger.getInstance(javaClass).warn("Could not obtain basic context, skipping findSymbols command execution")
return emptyList()
}

val camelCaseArg = StringCaseConverter.toCamelCase(arg)
val snakeCaseArg = StringCaseConverter.toSnakeCase(arg)

Expand All @@ -31,8 +39,9 @@ class FindSymbolsCommandExecutor : CommandsExecutor {

CompletableFuture.allOf(*tasks.toTypedArray()).get()

return tasks.map { it.get() }.flatten()
.map { "${it.name} - ${it.relativePath}" }
.toList()
return tasks.asSequence().map { it.get() }.flatten()
.filter { !it.text.isNullOrBlank() }
.take(2)
.map { "file: ${it.relativePath}\n```${basicContext.language.toLowerCase()}\n${it.text}\n```" }.toList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ data class WorkspaceContext(

tasks.mapNotNull { it.get() }.forEach { executionResult ->
when (executionResult.command) {
Command.FindSymbols -> symbols.addAll(executionResult.result)
// we reverse the list to get the most relevant symbols at the bottom - closer
// to the end of the prompt eventually.
Command.FindSymbols -> symbols.addAll(executionResult.result.reversed())
}
}

Expand Down
7 changes: 7 additions & 0 deletions Tabnine/src/test/java/com/tabnine/unitTests/GsonSerDeTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class GsonDeserializeTests {
assert(testGson.test == "test" && testGson.number == 1)
}

@Test
fun shouldIgnoreExtraFields() {
val json = "{\"test\":\"test\",\"number\":1,\"extra\":\"extra\"}"
val testGson = ourSingletonGson.fromJson(json, Simple::class.java)
assert(testGson.test == "test" && testGson.number == 1)
}

@Test
fun shouldDeserializeDoubleCorrectly() {
val json = "{\"double\":1.1}"
Expand Down