-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: code assistant implementation * refactor: clean up
- Loading branch information
1 parent
d816485
commit 8d6ca73
Showing
32 changed files
with
944 additions
and
62 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
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
54 changes: 54 additions & 0 deletions
54
src/main/kotlin/ee/carlrobert/codegpt/CodeGPTLookupListener.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,54 @@ | ||
package ee.carlrobert.codegpt | ||
|
||
import com.intellij.codeInsight.lookup.Lookup | ||
import com.intellij.codeInsight.lookup.LookupEvent | ||
import com.intellij.codeInsight.lookup.LookupListener | ||
import com.intellij.codeInsight.lookup.LookupManagerListener | ||
import com.intellij.codeInsight.lookup.impl.LookupImpl | ||
import com.intellij.openapi.application.ApplicationManager | ||
import com.intellij.openapi.application.runReadAction | ||
import com.intellij.openapi.components.service | ||
import ee.carlrobert.codegpt.predictions.PredictionService | ||
import ee.carlrobert.codegpt.settings.GeneralSettings | ||
import ee.carlrobert.codegpt.settings.service.ServiceType | ||
import ee.carlrobert.codegpt.settings.service.codegpt.CodeGPTServiceSettings | ||
|
||
class CodeGPTLookupListener : LookupManagerListener { | ||
override fun activeLookupChanged(oldLookup: Lookup?, newLookup: Lookup?) { | ||
if (newLookup is LookupImpl) { | ||
newLookup.addLookupListener(object : LookupListener { | ||
|
||
var beforeApply: String = "" | ||
var cursorOffset: Int = 0 | ||
|
||
override fun beforeItemSelected(event: LookupEvent): Boolean { | ||
beforeApply = newLookup.editor.document.text | ||
cursorOffset = runReadAction { | ||
newLookup.editor.caretModel.offset | ||
} | ||
|
||
return true | ||
} | ||
|
||
override fun itemSelected(event: LookupEvent) { | ||
val editor = newLookup.editor | ||
|
||
if (GeneralSettings.getSelectedService() != ServiceType.CODEGPT | ||
|| !service<CodeGPTServiceSettings>().state.codeAssistantEnabled | ||
|| service<EncodingManager>().countTokens(editor.document.text) > 4098 | ||
) { | ||
return | ||
} | ||
|
||
ApplicationManager.getApplication().executeOnPooledThread { | ||
service<PredictionService>().displayLookupPrediction( | ||
editor, | ||
event, | ||
beforeApply | ||
) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/ee/carlrobert/codegpt/actions/CodeAssistantFeatureToggleActions.kt.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,35 @@ | ||
package ee.carlrobert.codegpt.actions | ||
|
||
import com.intellij.openapi.actionSystem.ActionUpdateThread | ||
import com.intellij.openapi.actionSystem.AnActionEvent | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.project.DumbAwareAction | ||
import ee.carlrobert.codegpt.settings.GeneralSettings | ||
import ee.carlrobert.codegpt.settings.service.ServiceType.CODEGPT | ||
import ee.carlrobert.codegpt.settings.service.codegpt.CodeGPTServiceSettings | ||
|
||
abstract class CodeAssistantFeatureToggleAction( | ||
private val enableFeatureAction: Boolean | ||
) : DumbAwareAction() { | ||
|
||
override fun actionPerformed(e: AnActionEvent) { | ||
val settings = service<CodeGPTServiceSettings>().state | ||
settings.codeAssistantEnabled = enableFeatureAction | ||
} | ||
|
||
override fun update(e: AnActionEvent) { | ||
val codeAssistantEnabled = service<CodeGPTServiceSettings>().state.codeAssistantEnabled | ||
|
||
e.presentation.isVisible = GeneralSettings.getSelectedService() == CODEGPT | ||
&& codeAssistantEnabled != enableFeatureAction | ||
e.presentation.isEnabled = GeneralSettings.getSelectedService() == CODEGPT | ||
} | ||
|
||
override fun getActionUpdateThread(): ActionUpdateThread { | ||
return ActionUpdateThread.BGT | ||
} | ||
} | ||
|
||
class EnableCodeAssistantAction : CodeAssistantFeatureToggleAction(true) | ||
|
||
class DisableCodeAssistantAction : CodeAssistantFeatureToggleAction(false) |
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
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
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/ee/carlrobert/codegpt/predictions/AcceptNextPredictionRevisionAction.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,32 @@ | ||
package ee.carlrobert.codegpt.predictions | ||
|
||
import com.intellij.codeInsight.hint.HintManagerImpl | ||
import com.intellij.openapi.actionSystem.DataContext | ||
import com.intellij.openapi.editor.Caret | ||
import com.intellij.openapi.editor.Editor | ||
import com.intellij.openapi.editor.actionSystem.EditorAction | ||
import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler | ||
import ee.carlrobert.codegpt.CodeGPTKeys | ||
|
||
class AcceptNextPredictionRevisionAction : EditorAction(Handler()), HintManagerImpl.ActionToIgnore { | ||
|
||
companion object { | ||
const val ID = "codegpt.acceptNextPrediction" | ||
} | ||
|
||
private class Handler : EditorWriteActionHandler() { | ||
|
||
override fun doExecute(editor: Editor, caret: Caret?, dataContext: DataContext?) { | ||
val diffViewer = editor.getUserData(CodeGPTKeys.EDITOR_PREDICTION_DIFF_VIEWER) | ||
if (diffViewer != null && diffViewer.isVisible()) { | ||
diffViewer.applyChanges() | ||
return | ||
} | ||
} | ||
|
||
override fun isEnabledForCaret(editor: Editor, caret: Caret, dataContext: DataContext): Boolean { | ||
val diffViewer = editor.getUserData(CodeGPTKeys.EDITOR_PREDICTION_DIFF_VIEWER) | ||
return diffViewer != null && diffViewer.isVisible() | ||
} | ||
} | ||
} |
Oops, something went wrong.