Skip to content

Commit

Permalink
Reformat code with ktfmt plugin (default Meta style)
Browse files Browse the repository at this point in the history
  • Loading branch information
rock3r committed Aug 8, 2024
1 parent 9e65d79 commit a652a5e
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,38 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.startup.StartupActivity.Background

class InitialConfigurationStartupActivity : Background {
override fun runActivity(project: Project) {
val settings = KtfmtSettings.getInstance(project)
override fun runActivity(project: Project) {
val settings = KtfmtSettings.getInstance(project)

if (settings.isUninitialized) {
settings.isEnabled = false
displayNewUserNotification(project, settings)
}
if (settings.isUninitialized) {
settings.isEnabled = false
displayNewUserNotification(project, settings)
}
}

private fun displayNewUserNotification(project: Project, settings: KtfmtSettings) {
val notification =
Notification(
NotificationGroupManager.getInstance()
.getNotificationGroup(NOTIFICATION_TITLE)
.displayId,
NOTIFICATION_TITLE,
"The ktfmt plugin is disabled by default.",
INFORMATION,
)
private fun displayNewUserNotification(project: Project, settings: KtfmtSettings) {
val notification =
Notification(
NotificationGroupManager.getInstance()
.getNotificationGroup(NOTIFICATION_TITLE)
.displayId,
NOTIFICATION_TITLE,
"The ktfmt plugin is disabled by default.",
INFORMATION,
)

notification
.addAction(
object : AnAction("Enable for This Project") {
override fun actionPerformed(e: AnActionEvent) {
settings.isEnabled = true
notification.expire()
}
}
)
.notify(project)
}
notification
.addAction(
object : AnAction("Enable for This Project") {
override fun actionPerformed(e: AnActionEvent) {
settings.isEnabled = true
notification.expire()
}
})
.notify(project)
}

companion object {
private const val NOTIFICATION_TITLE = "Enable ktfmt"
}
companion object {
private const val NOTIFICATION_TITLE = "Enable ktfmt"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ class KtfmtConfigurable(project: Project) :
_id = "com.facebook.ktfmt_idea_plugin.settings",
helpTopic = "ktfmt",
) {
private val settings = KtfmtSettings.getInstance(project)
private val settings = KtfmtSettings.getInstance(project)

override fun createPanel(): DialogPanel = panel {
lateinit var enabledCheckbox: JCheckBox
row {
enabledCheckbox =
checkBox("Enable ktfmt")
.bindSelected(
getter = { settings.isEnabled },
setter = { settings.setEnabled(if (it) Enabled else Disabled) },
)
.component
}
override fun createPanel(): DialogPanel = panel {
lateinit var enabledCheckbox: JCheckBox
row {
enabledCheckbox =
checkBox("Enable ktfmt")
.bindSelected(
getter = { settings.isEnabled },
setter = { settings.setEnabled(if (it) Enabled else Disabled) },
)
.component
}

row {
comboBox(UiFormatterStyle.entries.toList())
.label("Code style:")
.bindItem(
getter = { settings.uiFormatterStyle },
setter = { settings.uiFormatterStyle = it ?: UiFormatterStyle.Meta },
)
.enabledIf(enabledCheckbox.selected)
}
row {
comboBox(UiFormatterStyle.entries.toList())
.label("Code style:")
.bindItem(
getter = { settings.uiFormatterStyle },
setter = { settings.uiFormatterStyle = it ?: UiFormatterStyle.Meta },
)
.enabledIf(enabledCheckbox.selected)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,40 @@ private const val PARSING_ERROR_TITLE: String = PARSING_ERROR_NOTIFICATION_GROUP

/** Uses `ktfmt` to reformat code. */
class KtfmtFormattingService : AsyncDocumentFormattingService() {
override fun createFormattingTask(request: AsyncFormattingRequest): FormattingTask {
val project = request.context.project
val style = KtfmtSettings.getInstance(project).uiFormatterStyle
return KtfmtFormattingTask(request, style)
}
override fun createFormattingTask(request: AsyncFormattingRequest): FormattingTask {
val project = request.context.project
val style = KtfmtSettings.getInstance(project).uiFormatterStyle
return KtfmtFormattingTask(request, style)
}

override fun getNotificationGroupId(): String = PARSING_ERROR_NOTIFICATION_GROUP
override fun getNotificationGroupId(): String = PARSING_ERROR_NOTIFICATION_GROUP

override fun getName(): String = "ktfmt"
override fun getName(): String = "ktfmt"

override fun getFeatures(): Set<Feature> = emptySet()
override fun getFeatures(): Set<Feature> = emptySet()

override fun canFormat(file: PsiFile): Boolean =
KotlinFileType.INSTANCE.name == file.fileType.name &&
KtfmtSettings.getInstance(file.project).isEnabled
override fun canFormat(file: PsiFile): Boolean =
KotlinFileType.INSTANCE.name == file.fileType.name &&
KtfmtSettings.getInstance(file.project).isEnabled

private class KtfmtFormattingTask(
private val request: AsyncFormattingRequest,
private val style: UiFormatterStyle,
) : FormattingTask {
override fun run() {
try {
val formattedText = format(style.formattingOptions, request.documentText)
request.onTextReady(formattedText)
} catch (e: FormatterException) {
request.onError(
PARSING_ERROR_TITLE,
"ktfmt failed. Does ${request.context.containingFile.name} have syntax errors?",
)
}
}
private class KtfmtFormattingTask(
private val request: AsyncFormattingRequest,
private val style: UiFormatterStyle,
) : FormattingTask {
override fun run() {
try {
val formattedText = format(style.formattingOptions, request.documentText)
request.onTextReady(formattedText)
} catch (e: FormatterException) {
request.onError(
PARSING_ERROR_TITLE,
"ktfmt failed. Does ${request.context.containingFile.name} have syntax errors?",
)
}
}

override fun isRunUnderProgress(): Boolean = true
override fun isRunUnderProgress(): Boolean = true

override fun cancel(): Boolean = false
}
override fun cancel(): Boolean = false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,65 +29,64 @@ import com.intellij.openapi.project.Project
@Service(PROJECT)
@State(name = "KtfmtSettings", storages = [Storage("ktfmt.xml")])
internal class KtfmtSettings : PersistentStateComponent<KtfmtSettings.State> {
private var state = State()
private var state = State()

override fun getState(): State = state
override fun getState(): State = state

override fun loadState(state: State) {
this.state = state
}

var isEnabled: Boolean
get() = state.enabled == Enabled
set(enabled) {
setEnabled(if (enabled) Enabled else Disabled)
}
override fun loadState(state: State) {
this.state = state
}

fun setEnabled(enabled: EnabledState) {
state.enabled = enabled
var isEnabled: Boolean
get() = state.enabled == Enabled
set(enabled) {
setEnabled(if (enabled) Enabled else Disabled)
}

val isUninitialized: Boolean
get() = state.enabled == Unknown
fun setEnabled(enabled: EnabledState) {
state.enabled = enabled
}

var uiFormatterStyle: UiFormatterStyle
get() = state.uiFormatterStyle
set(uiFormatterStyle) {
state.uiFormatterStyle = uiFormatterStyle
}
val isUninitialized: Boolean
get() = state.enabled == Unknown

internal enum class EnabledState {
Unknown,
Enabled,
Disabled,
var uiFormatterStyle: UiFormatterStyle
get() = state.uiFormatterStyle
set(uiFormatterStyle) {
state.uiFormatterStyle = uiFormatterStyle
}

internal class State {
@JvmField var enabled: EnabledState = Unknown
var uiFormatterStyle: UiFormatterStyle = Meta
internal enum class EnabledState {
Unknown,
Enabled,
Disabled,
}

// enabled used to be a boolean so we use bean property methods for backwards
// compatibility
fun setEnabled(enabledStr: String?) {
enabled =
when {
enabledStr == null -> Unknown
enabledStr.toBoolean() -> Enabled
else -> Disabled
}
}
internal class State {
@JvmField var enabled: EnabledState = Unknown
var uiFormatterStyle: UiFormatterStyle = Meta

fun getEnabled(): String? =
when (enabled) {
Enabled -> "true"
Disabled -> "false"
else -> null
}
// enabled used to be a boolean so we use bean property methods for backwards
// compatibility
fun setEnabled(enabledStr: String?) {
enabled =
when {
enabledStr == null -> Unknown
enabledStr.toBoolean() -> Enabled
else -> Disabled
}
}

companion object {
@JvmStatic
fun getInstance(project: Project): KtfmtSettings =
project.getService(KtfmtSettings::class.java)
}
fun getEnabled(): String? =
when (enabled) {
Enabled -> "true"
Disabled -> "false"
else -> null
}
}

companion object {
@JvmStatic
fun getInstance(project: Project): KtfmtSettings = project.getService(KtfmtSettings::class.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ internal enum class UiFormatterStyle(
private val description: String,
val formattingOptions: FormattingOptions,
) {
Meta("Meta (default)", META_FORMAT),
Google("Google (internal)", GOOGLE_FORMAT),
KotlinLang("Kotlinlang", KOTLINLANG_FORMAT);
Meta("Meta (default)", META_FORMAT),
Google("Google (internal)", GOOGLE_FORMAT),
KotlinLang("Kotlinlang", KOTLINLANG_FORMAT);

override fun toString(): String = description
override fun toString(): String = description
}

0 comments on commit a652a5e

Please sign in to comment.