-
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.
Fix project build with platformVersion >= 2024
- Loading branch information
Showing
10 changed files
with
195 additions
and
39 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions
34
...otlin/com/intellij/codeInsight/inline/completion/suggestion/InlineCompletionSuggestion.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,34 @@ | ||
package com.intellij.codeInsight.inline.completion.suggestion | ||
|
||
import com.intellij.codeInsight.inline.completion.elements.InlineCompletionElement | ||
import com.intellij.openapi.util.UserDataHolderBase | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.FlowCollector | ||
|
||
interface InlineCompletionSuggestion { | ||
|
||
object Empty : InlineCompletionSuggestion {} | ||
} | ||
|
||
interface InlineCompletionSingleSuggestion : InlineCompletionSuggestion { | ||
|
||
companion object { | ||
|
||
/** @see [InlineCompletionVariant.build] */ | ||
fun build( | ||
data: UserDataHolderBase = UserDataHolderBase(), | ||
buildElements: | ||
suspend FlowCollector<InlineCompletionElement>.(data: UserDataHolderBase) -> Unit | ||
): InlineCompletionSingleSuggestion { | ||
return object : InlineCompletionSingleSuggestion {} | ||
} | ||
|
||
/** @see InlineCompletionVariant.build */ | ||
fun build( | ||
data: UserDataHolderBase = UserDataHolderBase(), | ||
elements: Flow<InlineCompletionElement> | ||
): InlineCompletionSingleSuggestion { | ||
return object : InlineCompletionSingleSuggestion {} | ||
} | ||
} | ||
} |
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
147 changes: 147 additions & 0 deletions
147
src/intellij2024/kotlin/com/sourcegraph/cody/autocomplete/CodyInlineCompletionProvider.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,147 @@ | ||
package com.sourcegraph.cody.autocomplete | ||
|
||
import com.intellij.codeInsight.inline.completion.* | ||
import com.intellij.codeInsight.inline.completion.elements.InlineCompletionGrayTextElement | ||
import com.intellij.codeInsight.inline.completion.suggestion.InlineCompletionSingleSuggestion | ||
import com.intellij.codeInsight.inline.completion.suggestion.InlineCompletionSuggestion | ||
import com.intellij.openapi.application.ApplicationInfo | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.application.ReadAction | ||
import com.intellij.openapi.client.ClientSessionsManager | ||
import com.intellij.openapi.command.WriteCommandAction | ||
import com.intellij.openapi.diagnostic.Logger | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.editor.event.DocumentEvent | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.util.TextRange | ||
import com.intellij.util.concurrency.annotations.RequiresReadLock | ||
import com.sourcegraph.cody.agent.CodyAgentService | ||
import com.sourcegraph.cody.agent.protocol.AutocompleteResult | ||
import com.sourcegraph.cody.agent.protocol.CompletionItemParams | ||
import com.sourcegraph.cody.statusbar.CodyStatusService.Companion.resetApplication | ||
import com.sourcegraph.cody.vscode.CancellationToken | ||
import com.sourcegraph.cody.vscode.InlineCompletionTriggerKind | ||
import com.sourcegraph.cody.vscode.IntelliJTextDocument | ||
import com.sourcegraph.config.ConfigUtil | ||
import com.sourcegraph.utils.CodyEditorUtil.getTextRange | ||
import com.sourcegraph.utils.CodyEditorUtil.isImplicitAutocompleteEnabledForEditor | ||
import com.sourcegraph.utils.CodyFormatter | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.TimeUnit | ||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
class CodyInlineCompletionProvider : InlineCompletionProvider { | ||
private val logger = Logger.getInstance(CodyInlineCompletionProvider::class.java) | ||
private val currentJob = AtomicReference(CancellationToken()) | ||
override val id = InlineCompletionProviderID("Cody") | ||
|
||
override suspend fun getSuggestion(request: InlineCompletionRequest): InlineCompletionSuggestion { | ||
ApplicationManager.getApplication().assertIsNonDispatchThread() | ||
val editor = request.editor | ||
val project = editor.project ?: return InlineCompletionSuggestion.Empty | ||
if (!isImplicitAutocompleteEnabledForEditor(editor)) { | ||
return InlineCompletionSuggestion.Empty | ||
} | ||
val lookupString: String? = null // todo: can we use this provider for lookups? | ||
|
||
cancelCurrentJob(project) | ||
val cancellationToken = CancellationToken() | ||
currentJob.set(cancellationToken) | ||
|
||
val triggerKind = | ||
if (request.event is InlineCompletionEvent.DirectCall) { | ||
InlineCompletionTriggerKind.INVOKE | ||
} else { | ||
InlineCompletionTriggerKind.AUTOMATIC | ||
} | ||
|
||
val completions = | ||
fetchCompletions(project, editor, triggerKind, cancellationToken, lookupString) | ||
.completeOnTimeout(null, 1, TimeUnit.SECONDS) | ||
.get() ?: return InlineCompletionSuggestion.Empty | ||
|
||
return InlineCompletionSingleSuggestion.build { | ||
completions.items | ||
.firstNotNullOfOrNull { | ||
WriteCommandAction.runWriteCommandAction<InlineCompletionGrayTextElement?>( | ||
editor.project) { | ||
val range = getTextRange(editor.document, it.range) | ||
val originalText = editor.document.getText(range) | ||
|
||
val formattedCompletionText: String = | ||
if (System.getProperty("cody.autocomplete.enableFormatting") == "false") { | ||
it.insertText | ||
} else { | ||
CodyFormatter.formatStringBasedOnDocument( | ||
it.insertText, project, editor.document, range, request.endOffset) | ||
} | ||
|
||
val completionText = formattedCompletionText.removeSuffix(originalText) | ||
if (completionText.trim().isBlank()) { | ||
null | ||
} else { | ||
|
||
CodyAgentService.withAgent(project) { agent -> | ||
agent.server.completionSuggested(CompletionItemParams(it.id)) | ||
} | ||
|
||
InlineCompletionGrayTextElement(completionText) | ||
} | ||
} | ||
} | ||
?.let { emit(it) } | ||
} | ||
} | ||
|
||
@RequiresReadLock | ||
private fun fetchCompletions( | ||
project: Project, | ||
editor: Editor, | ||
triggerKind: InlineCompletionTriggerKind, | ||
cancellationToken: CancellationToken, | ||
lookupString: String?, | ||
): CompletableFuture<AutocompleteResult?> { | ||
val textDocument = IntelliJTextDocument(editor, project) | ||
val offset = ReadAction.compute<Int, Throwable> { editor.caretModel.offset } | ||
val lineNumber = editor.document.getLineNumber(offset) | ||
val caretPositionInLine = offset - editor.document.getLineStartOffset(lineNumber) | ||
val originalText = editor.document.getText(TextRange(offset - caretPositionInLine, offset)) | ||
|
||
val result = CompletableFuture<AutocompleteResult?>() | ||
Utils.triggerAutocompleteAsync( | ||
project, | ||
editor, | ||
offset, | ||
textDocument, | ||
triggerKind, | ||
cancellationToken, | ||
lookupString, | ||
originalText, | ||
logger) { autocompleteResult -> | ||
result.complete(autocompleteResult) | ||
} | ||
return result | ||
} | ||
|
||
private fun cancelCurrentJob(project: Project?) { | ||
currentJob.get().abort() | ||
project?.let { resetApplication(it) } | ||
} | ||
|
||
override fun isEnabled(event: InlineCompletionEvent): Boolean { | ||
return isEnabled() | ||
} | ||
|
||
fun isEnabled(event: DocumentEvent): Boolean { | ||
return isEnabled() | ||
} | ||
|
||
private fun isEnabled(): Boolean { | ||
val ideVersion = ApplicationInfo.getInstance().build.baselineVersion | ||
val isRemoteDev = ClientSessionsManager.getAppSession()?.isRemote ?: false | ||
return ideVersion >= 233 && | ||
isRemoteDev && | ||
ConfigUtil.isCodyEnabled() && | ||
ConfigUtil.isCodyAutocompleteEnabled() | ||
} | ||
} |
34 changes: 0 additions & 34 deletions
34
src/main/kotlin/com/intellij/codeInsight/inline/completion/InlineCompletionSuggestion.kt
This file was deleted.
Oops, something went wrong.