Skip to content

Commit

Permalink
fix AccessDeniedException v2 (#65)
Browse files Browse the repository at this point in the history
* fix AccessDeniedException v2
http://github.com/smallcloudai/refact-intellij/issues/44

* rudiment setting

* fix multiple startups
  • Loading branch information
reymondzzzz authored Dec 7, 2023
1 parent 3379da9 commit efd582d
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 62 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies {


group = "com.smallcloud"
version = getVersionString("1.2.5")
version = getVersionString("1.2.7")

repositories {
mavenCentral()
Expand Down
15 changes: 10 additions & 5 deletions src/main/kotlin/com/smallcloud/refactai/Initializer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import com.smallcloud.refactai.account.LoginStateService
import com.smallcloud.refactai.account.login
import com.smallcloud.refactai.io.ConnectivityManager
import com.smallcloud.refactai.io.InferenceGlobalContext
import com.smallcloud.refactai.listeners.LastEditorGetterListener
import com.smallcloud.refactai.listeners.UninstallListener
import com.smallcloud.refactai.lsp.LSPProcessHolder
import com.smallcloud.refactai.notifications.notificationStartup
Expand All @@ -19,12 +18,16 @@ import com.smallcloud.refactai.settings.AppSettingsState
import com.smallcloud.refactai.settings.settingsStartup
import com.smallcloud.refactai.statistic.UsageStats
import com.smallcloud.refactai.struct.DeploymentMode
import java.util.concurrent.atomic.AtomicBoolean

class Initializer : ProjectActivity, Disposable {
override suspend fun execute(project: Project) {
initialize(project)
}
private fun initialize(project: Project) {
PrivacyService.instance.projectOpened(project)
val shouldInitialize = !(initialized.getAndSet(true) || ApplicationManager.getApplication().isUnitTestMode)
if (!shouldInitialize) {
return
}

Logger.getInstance("SMCInitializer").info("Bin prefix = ${Resources.binPrefix}")
ConnectivityManager.instance.startup()

Expand All @@ -46,7 +49,6 @@ class Initializer : ProjectActivity, Disposable {
settingsStartup()
notificationStartup()
UsageStats.instance
PrivacyService.instance.projectOpened(project)
PluginInstaller.addStateListener(UninstallListener())
UpdateChecker.instance
LSPProcessHolder.instance.startup()
Expand All @@ -55,4 +57,7 @@ class Initializer : ProjectActivity, Disposable {
override fun dispose() {
}

companion object {
private val initialized = AtomicBoolean(false)
}
}
11 changes: 0 additions & 11 deletions src/main/kotlin/com/smallcloud/refactai/PluginState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ interface ExtraInfoChangedNotifier {
fun tooltipMessageChanged(newMsg: String?) {}
fun inferenceMessageChanged(newMsg: String?) {}
fun loginMessageChanged(newMsg: String?) {}
fun pluginEnableChanged(newVal: Boolean) {}

companion object {
val TOPIC = Topic.create("Extra Info Changed Notifier", ExtraInfoChangedNotifier::class.java)
Expand All @@ -21,15 +20,6 @@ interface ExtraInfoChangedNotifier {
class PluginState : Disposable {
private val messageBus: MessageBus = ApplicationManager.getApplication().messageBus

var isEnabled: Boolean = false
set(value) {
if (value == field) return
field = value
messageBus
.syncPublisher(ExtraInfoChangedNotifier.TOPIC)
.pluginEnableChanged(field)
}

var tooltipMessage: String? = null
get() = AppSettingsState.instance.tooltipMessage
set(newMsg) {
Expand Down Expand Up @@ -67,7 +57,6 @@ class PluginState : Disposable {
val instance: PluginState
get() = ApplicationManager.getApplication().getService(PluginState::class.java)
fun startup(settings: AppSettingsState) {
instance.isEnabled = settings.pluginIsEnabled
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.smallcloud.refactai.lsp
import com.google.gson.Gson
import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationInfo
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.util.SystemInfo
Expand Down Expand Up @@ -105,7 +106,6 @@ class LSPProcessHolder: Disposable {
}
settingsChanged()


capsTask = schedulerCaps.scheduleWithFixedDelay({
capabilities = getCaps()
if (capabilities.cloudName.isNotEmpty()) {
Expand Down Expand Up @@ -212,7 +212,9 @@ class LSPProcessHolder: Disposable {
}

companion object {
private val BIN_PATH = Path(getTempDirectory(), "refact-lsp${getExeSuffix()}").toString()
private val BIN_PATH = Path(getTempDirectory(),
ApplicationInfo.getInstance().build.toString().replace(Regex("[^A-Za-z0-9 ]"), "_") +
"_refact_lsp${getExeSuffix()}").toString()
@JvmStatic
val instance: LSPProcessHolder
get() = ApplicationManager.getApplication().getService(LSPProcessHolder::class.java)
Expand Down
15 changes: 2 additions & 13 deletions src/main/kotlin/com/smallcloud/refactai/modes/ModeProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ class ModeProvider(
private var activeMode: Mode? = null,
private val pluginState: PluginState = PluginState.instance,
) : Disposable, InferenceGlobalContextChangedNotifier {
private val isEnabled: Boolean
get() = pluginState.isEnabled

@Transient
private val messageBus: MessageBus = ApplicationManager.getApplication().messageBus
Expand Down Expand Up @@ -123,37 +121,28 @@ class ModeProvider(
}

fun beforeDocumentChangeNonBulk(event: DocumentEvent?, editor: Editor) {
if (!isEnabled) return
if (event?.newFragment.toString() == DUMMY_IDENTIFIER) return
beforeTextChangeEventsQueue.add(DocumentEventExtra(event, editor, currentTimeMillis()))
}

fun onTextChange(event: DocumentEvent?, editor: Editor, force: Boolean) {
if (!isEnabled) return
if (event?.newFragment.toString() == DUMMY_IDENTIFIER) return
onTextChangeEventsQueue.add(DocumentEventExtra(event, editor, currentTimeMillis(), force))
}

fun onCaretChange(event: CaretEvent) {
if (!isEnabled) return
activeMode?.onCaretChange(event)
}

fun focusGained() {
if (!isEnabled) return
}
fun focusGained() {}

fun focusLost() {
if (!isEnabled) return
}
fun focusLost() {}

fun onTabPressed(editor: Editor, caret: Caret?, dataContext: DataContext) {
if (!isEnabled) return
activeMode?.onTabPressed(editor, caret, dataContext)
}

fun onEscPressed(editor: Editor, caret: Caret?, dataContext: DataContext) {
if (!isEnabled) return
activeMode?.onEscPressed(editor, caret, dataContext)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class HistoryComponent(private val state: State): JBPanel<HistoryComponent>(), N
mainPanel.isOpaque = false
mainPanel.border = JBUI.Borders.emptyLeft(8)
add(mainPanel)
var helloString = RefactAIBundle.message("aiToolbox.panes.chat.helloString", "\uD83D\uDC4B")
var helloString = RefactAIBundle.message("aiToolbox.panes.chat.helloString")
if (!AccountManager.isLoggedIn) {
helloString += " ${RefactAIBundle.message("aiToolbox.panes.chat.dontForgetLogin")}"
}
Expand Down
16 changes: 3 additions & 13 deletions src/main/kotlin/com/smallcloud/refactai/privacy/PrivacyService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.smallcloud.refactai.privacy
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.project.VetoableProjectManagerListener
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.smallcloud.refactai.settings.PrivacyState
Expand Down Expand Up @@ -73,14 +71,10 @@ data class PrivacyMember(
}


class PrivacyService : VetoableProjectManagerListener, Disposable {
class PrivacyService : Disposable {
private var privacyTree: MutableMap<String, PrivacyMember> = emptyMap<String, PrivacyMember>().toMutableMap()

init {
ProjectManager.getInstance().addProjectManagerListener(this)
}

override fun projectOpened(project: Project) {
fun projectOpened(project: Project) {
val path = project.basePath
val virtualFile = VirtualFileManager.getInstance().findFileByNioPath(Path.of(path))
val node = virtualFile?.let { getMember(it) }
Expand Down Expand Up @@ -231,11 +225,7 @@ class PrivacyService : VetoableProjectManagerListener, Disposable {
get() = ApplicationManager.getApplication().getService(PrivacyService::class.java)
}

override fun canClose(project: Project): Boolean {
return true
}

override fun dispose() {
ProjectManager.getInstance().removeProjectManagerListener(this)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class AppSettingsConfigurable : Configurable {
override fun isModified(): Boolean {
var modified =
(mySettingsComponent!!.tokenText.isNotEmpty() && (AccountManager.apiKey == null ||
mySettingsComponent!!.tokenText != AccountManager.apiKey))
mySettingsComponent!!.tokenText.trim() != AccountManager.apiKey))
modified = modified || (mySettingsComponent!!.tokenText.isEmpty() && AccountManager.apiKey != null)

modified = modified || (mySettingsComponent!!.modelText.isNotEmpty()
Expand Down Expand Up @@ -84,7 +84,7 @@ class AppSettingsConfigurable : Configurable {
}

override fun apply() {
AccountManager.apiKey = mySettingsComponent!!.tokenText.ifEmpty { null }
AccountManager.apiKey = mySettingsComponent!!.tokenText.trim().ifEmpty { null }
InferenceGlobalContext.model = mySettingsComponent!!.modelText.ifEmpty { null }
InferenceGlobalContext.inferenceUri =
makeRightUrl(mySettingsComponent!!.contrastUrlText.ifEmpty { null })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class AppSettingsState : PersistentStateComponent<AppSettingsState>, Disposable
var loginMessage: String? = null
var tooltipMessage: String? = null
var inferenceMessage: String? = null
var pluginIsEnabled: Boolean = true
var useAutoCompletion: Boolean = true
var useMultipleFilesCompletion: Boolean = false
var startupLoggedIn: Boolean = false
Expand Down Expand Up @@ -112,10 +111,6 @@ class AppSettingsState : PersistentStateComponent<AppSettingsState>, Disposable
override fun inferenceMessageChanged(newMsg: String?) {
instance.inferenceMessage = newMsg
}

override fun pluginEnableChanged(newVal: Boolean) {
instance.pluginIsEnabled = newVal
}
})
messageBus
.connect(PluginState.instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ class SMCStatusBarWidget(project: Project) : EditorBasedWidget(project), CustomS
override fun inferenceMessageChanged(newMsg: String?) {
update(newMsg)
}

override fun pluginEnableChanged(newVal: Boolean) {
update(null)
}
})
ApplicationManager.getApplication()
.messageBus
Expand Down Expand Up @@ -158,8 +154,6 @@ class SMCStatusBarWidget(project: Project) : EditorBasedWidget(project), CustomS
}

private fun getIcon(): Icon {
if (!PluginState.instance.isEnabled)
return AllIcons.Diff.GutterCheckBoxIndeterminate
if (!AccountManager.isLoggedIn && InferenceGlobalContext.isCloud) {
return LOGO_12x12
}
Expand Down Expand Up @@ -240,7 +234,7 @@ class SMCStatusBarWidget(project: Project) : EditorBasedWidget(project), CustomS
component!!.icon = getIcon()
component!!.toolTipText = newMsg ?: getTooltipText()
myStatusBar!!.updateWidget(ID())
val statusBar = WindowManager.getInstance().getStatusBar(myProject)
val statusBar = WindowManager.getInstance().getStatusBar(project)
statusBar?.component?.updateUI()
},
ModalityState.any()
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/bundles/RefactAI.properties
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ aiToolbox.panes.toolbox.placeholder=Don't forget to log in!
aiToolbox.panes.chat.alsoSend=Also send selected text
aiToolbox.panes.chat.addFileForContext=Attach {0}
aiToolbox.panes.chat.helloString=Hi! {0}\n\
This chat has more features, and it's more responsive than a free one you might find on the web.
aiToolbox.panes.chat.helloString=Welcome to Refact chat! How can I assist you today? Please type your question below.
aiToolbox.panes.chat.dontForgetLogin=Don't forget to log in!
aiToolbox.panes.chat.placeholderSelfhosted=No chats available

Expand Down

0 comments on commit efd582d

Please sign in to comment.