Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #32 from koxudaxi/imporove_handling_sdk
Browse files Browse the repository at this point in the history
improve handling sdk
  • Loading branch information
koxudaxi committed May 18, 2020
2 parents b51f4c3 + 6ed46e1 commit 013377c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
3 changes: 2 additions & 1 deletion resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<h2>version 0.0.7</h2>
<p>Features</p>
<ul>
<li>Improve handling sdk [#32] </li>
<li>Support installing extras [#31] </li>
<li>Update jvm version [#26] </li>
</ul>
Expand Down Expand Up @@ -95,7 +96,7 @@

<resource-bundle/>
<actions>
<action id="poetryInstallExtras" class="com.koxudaxi.poetry.PoetryInstallExtras" text="Poetry install extras"
<action id="poetryInstallExtras" class="com.koxudaxi.poetry.PoetryInstallExtras" text="Install Packages"
description="Poetry install extras packages">
<add-to-group group-id="$LRU" anchor="first"/>
</action>
Expand Down
6 changes: 4 additions & 2 deletions src/com/koxudaxi/poetry/PoetryPackageManagerListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import com.jetbrains.python.sdk.associatedModule

class PoetryPackageManagerListener : PyPackageManager.Listener {
override fun packagesRefreshed(sdk: Sdk) {
val module = sdk.associatedModule ?: return
if (!isPoetry(module.project, sdk)) return
ApplicationManager.getApplication().invokeLater {
ApplicationManager.getApplication().executeOnPooledThread {
if (sdk.associatedModule?.pyProjectToml == null) return@executeOnPooledThread
PyPoetryPackageManager.getInstance(sdk).refreshAndGetPackages(true, notify = false)
if (module.pyProjectToml == null) return@executeOnPooledThread
PyPoetryPackageManager.getInstance(sdk).refreshAndGetPackages(alwaysRefresh = true, notify = false)
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/com/koxudaxi/poetry/PyPoetryPackageManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.module.Module
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.roots.OrderRootType
import com.intellij.openapi.util.Key
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.openapi.vfs.VirtualFile
import com.jetbrains.python.packaging.*
Expand Down Expand Up @@ -35,9 +36,6 @@ class PyPoetryPackageManager(val sdk: Sdk) : PyPackageManager() {
}

override fun installManagement() {}
override fun refreshAndGetPackages(alwaysRefresh: Boolean): List<PyPackage> {
return refreshAndGetPackages(alwaysRefresh, true)
}

override fun hasManagement() = true

Expand Down Expand Up @@ -89,14 +87,18 @@ class PyPoetryPackageManager(val sdk: Sdk) : PyPackageManager() {

fun getRequirements() = requirements

override fun refreshAndGetPackages(alwaysRefresh: Boolean): List<PyPackage> {
return refreshAndGetPackages(alwaysRefresh, true)
}

fun refreshAndGetPackages(alwaysRefresh: Boolean, notify: Boolean): List<PyPackage> {
if (alwaysRefresh || packages == null) {
packages = null
val output = try {
runPoetry(sdk, "install", "--dry-run")
} catch (e: ExecutionException) {
packages = emptyList()
throw e
return packages ?: emptyList()
}
val allPackage = parsePoetryInstallDryRun(output)
packages = allPackage.first
Expand Down
12 changes: 11 additions & 1 deletion src/com/koxudaxi/poetry/poetry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,16 @@ class PyProjectTomlWatcher : EditorFactoryListener {
try {
val document = event.document
val module = document.virtualFile?.getModule(project) ?: return
// TODO: Should we remove listener when a sdk is changed to non-poetry sdk?
// if (!isPoetry(module.project)) {
// with(document) {
// putUserData(notificationActive, null)
// val listener = getUserData(changeListenerKey) ?: return
// removeDocumentListener(listener)
// putUserData(changeListenerKey, null)
// return
// }
// }
if (FileDocumentManager.getInstance().isDocumentUnsaved(document)) {
notifyPyProjectTomlChanged(module)
}
Expand Down Expand Up @@ -498,7 +508,7 @@ fun runPoetryInBackground(module: Module, args: List<String>, description: Strin
PythonSdkUtil.getSitePackagesDirectory(sdk)?.refresh(true, true)
sdk.associatedModule?.baseDir?.refresh(true, false)
if (isPoetry(project)) {
PyPoetryPackageManager.getInstance(sdk).refreshAndGetPackages(true, notify = true)
PyPoetryPackageManager.getInstance(sdk).refreshAndGetPackages(true)
}
}
}
Expand Down

0 comments on commit 013377c

Please sign in to comment.