Skip to content

Commit

Permalink
[feat][#4] Add basic lint status widget to status bar.
Browse files Browse the repository at this point in the history
Show status icon and text.
  • Loading branch information
Blarc committed Jun 30, 2022
1 parent 76057dc commit 3aabfa1
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
### Fixed
- Added null check to localizedMessage in GitlabLintRunner to prevent NPE.

### Added
- Added basic lint status widget to status bar.

## [0.0.8]
### Changed
- Updated readme.
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

pluginGroup = com.github.blarc
pluginName = GitlabTemplateLint
pluginVersion = 0.0.8
pluginVersion = 0.0.9

# https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 212
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package com.github.blarc.gitlab.template.lint.plugin
import com.github.blarc.gitlab.template.lint.plugin.gitlab.GitLabFactory
import com.github.blarc.gitlab.template.lint.plugin.gitlab.Gitlab
import com.github.blarc.gitlab.template.lint.plugin.settings.AppSettingsState
import com.github.blarc.gitlab.template.lint.plugin.widgets.LintStatusEnum
import com.github.blarc.gitlab.template.lint.plugin.widgets.LintStatusWidget
import com.intellij.dvcs.DvcsUtil
import com.intellij.openapi.components.Service
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.WindowManager
import git4idea.GitUtil
import git4idea.repo.GitRepository
import okhttp3.HttpUrl
Expand All @@ -16,8 +19,12 @@ import java.util.concurrent.CompletableFuture
@Service
class GitlabLintRunner(private val project: Project) {

var status: LintStatusEnum = LintStatusEnum.WAITING

fun run(content: String, filePath: String): GitlabLintResponse? {

updateStatusWidget(LintStatusEnum.RUNNING)

val repositoryManager = GitUtil.getRepositoryManager(project)
val repository = DvcsUtil.guessCurrentRepositoryQuick(project, repositoryManager, filePath)

Expand Down Expand Up @@ -48,13 +55,20 @@ class GitlabLintRunner(private val project: Project) {
GitlabLintUtils.createNotification(project, e.localizedMessage?: "Unknown exception!")
}

if (result?.valid == true) updateStatusWidget(LintStatusEnum.VALID) else updateStatusWidget(LintStatusEnum.INVALID)
return result
}

updateStatusWidget(LintStatusEnum.INVALID)
GitlabLintUtils.createNotification(project, "File is not part of a Gitlab repository.")
return null
}

private fun updateStatusWidget(lintingStatus: LintStatusEnum) {
status = lintingStatus
WindowManager.getInstance().getStatusBar(project)?.updateWidget(LintStatusWidget.ID)
}

private fun getProjectId(remoteUrl: String, gitlab: Gitlab) : CompletableFuture<Long> {
val remotesMap = AppSettingsState.instance.remotesMap
if (remotesMap.containsKey(remoteUrl)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.github.blarc.gitlab.template.lint.plugin.widgets

enum class LintStatusEnum(val text: String, val tooltip: String) {
INVALID("Invalid", "Gitlab CI yaml is invalid!"),
VALID("Valid", "Gitlab CI yaml is valid."),
RUNNING("Running", "Gitlab CI linter is running."),
WAITING("Waiting", "Gitlab CI linter is waiting.")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.blarc.gitlab.template.lint.plugin.widgets

import com.github.blarc.gitlab.template.lint.plugin.GitlabLintRunner
import com.intellij.icons.AllIcons
import com.intellij.openapi.components.service
import com.intellij.openapi.ui.popup.ListPopup
import com.intellij.openapi.wm.StatusBar
import com.intellij.openapi.wm.StatusBarWidget
import com.intellij.util.Consumer
import org.jetbrains.annotations.Nullable
import java.awt.event.MouseEvent
import javax.swing.Icon

class LintStatusPresentation(private val statusBar: StatusBar) : StatusBarWidget.MultipleTextValuesPresentation {
override fun getTooltipText(): String {
val status = statusBar.project?.service<GitlabLintRunner>()?.status ?: return LintStatusEnum.WAITING.tooltip
return status.tooltip
}

override fun getClickConsumer(): Consumer<MouseEvent>? {
return null
}

@Nullable("Null means the widget is unable to show the popup.")
override fun getPopupStep(): ListPopup? {
return null
}

override fun getSelectedValue(): String {
val status = statusBar.project?.service<GitlabLintRunner>()?.status ?: return LintStatusEnum.WAITING.text
return status.text
}


override fun getIcon(): Icon {
return when (statusBar.project?.service<GitlabLintRunner>()?.status) {
LintStatusEnum.INVALID -> AllIcons.General.ExclMark
LintStatusEnum.VALID -> AllIcons.General.InspectionsOK
LintStatusEnum.RUNNING -> AllIcons.General.InlineRefreshHover
else -> {
AllIcons.General.InspectionsPause
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.blarc.gitlab.template.lint.plugin.widgets

import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.StatusBar
import com.intellij.openapi.wm.StatusBarWidget
import com.intellij.openapi.wm.WindowManager

class LintStatusWidget(project: Project) : StatusBarWidget {

private var statusBar = WindowManager.getInstance().getStatusBar(project)

companion object {
const val ID = "LintStatus"
}

override fun ID(): String = ID

override fun install(statusBar: StatusBar) {
statusBar.updateWidget(ID)
}

override fun getPresentation(): StatusBarWidget.WidgetPresentation {
return LintStatusPresentation(statusBar)
}

override fun dispose() {
println("Hello")
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github.blarc.gitlab.template.lint.plugin.widgets

import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.StatusBar
import com.intellij.openapi.wm.StatusBarWidget
import com.intellij.openapi.wm.StatusBarWidgetFactory

class LintStatusWidgetFactory : StatusBarWidgetFactory {

override fun getId(): String = LintStatusWidget.ID

override fun getDisplayName(): String = "Lint Status"

override fun isAvailable(project: Project): Boolean = true

override fun createWidget(project: Project): StatusBarWidget = LintStatusWidget(project)

override fun disposeWidget(widget: StatusBarWidget) {}

override fun canBeEnabledOn(statusBar: StatusBar): Boolean = true
}
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,11 @@
id="GitlabLint"
/>

<statusBarWidgetFactory
id="lintStatusWidget"
implementation="com.github.blarc.gitlab.template.lint.plugin.widgets.LintStatusWidgetFactory"
order="first"
/>

</extensions>
</idea-plugin>

0 comments on commit 3aabfa1

Please sign in to comment.