Skip to content

Commit a652a5e

Browse files
committed
Reformat code with ktfmt plugin (default Meta style)
1 parent 9e65d79 commit a652a5e

File tree

5 files changed

+130
-132
lines changed

5 files changed

+130
-132
lines changed

ktfmt_idea_plugin/src/main/kotlin/com/facebook/ktfmt/intellij/InitialConfigurationStartupActivity.kt

+29-30
Original file line numberDiff line numberDiff line change
@@ -24,39 +24,38 @@ import com.intellij.openapi.project.Project
2424
import com.intellij.openapi.startup.StartupActivity.Background
2525

2626
class InitialConfigurationStartupActivity : Background {
27-
override fun runActivity(project: Project) {
28-
val settings = KtfmtSettings.getInstance(project)
27+
override fun runActivity(project: Project) {
28+
val settings = KtfmtSettings.getInstance(project)
2929

30-
if (settings.isUninitialized) {
31-
settings.isEnabled = false
32-
displayNewUserNotification(project, settings)
33-
}
30+
if (settings.isUninitialized) {
31+
settings.isEnabled = false
32+
displayNewUserNotification(project, settings)
3433
}
34+
}
3535

36-
private fun displayNewUserNotification(project: Project, settings: KtfmtSettings) {
37-
val notification =
38-
Notification(
39-
NotificationGroupManager.getInstance()
40-
.getNotificationGroup(NOTIFICATION_TITLE)
41-
.displayId,
42-
NOTIFICATION_TITLE,
43-
"The ktfmt plugin is disabled by default.",
44-
INFORMATION,
45-
)
36+
private fun displayNewUserNotification(project: Project, settings: KtfmtSettings) {
37+
val notification =
38+
Notification(
39+
NotificationGroupManager.getInstance()
40+
.getNotificationGroup(NOTIFICATION_TITLE)
41+
.displayId,
42+
NOTIFICATION_TITLE,
43+
"The ktfmt plugin is disabled by default.",
44+
INFORMATION,
45+
)
4646

47-
notification
48-
.addAction(
49-
object : AnAction("Enable for This Project") {
50-
override fun actionPerformed(e: AnActionEvent) {
51-
settings.isEnabled = true
52-
notification.expire()
53-
}
54-
}
55-
)
56-
.notify(project)
57-
}
47+
notification
48+
.addAction(
49+
object : AnAction("Enable for This Project") {
50+
override fun actionPerformed(e: AnActionEvent) {
51+
settings.isEnabled = true
52+
notification.expire()
53+
}
54+
})
55+
.notify(project)
56+
}
5857

59-
companion object {
60-
private const val NOTIFICATION_TITLE = "Enable ktfmt"
61-
}
58+
companion object {
59+
private const val NOTIFICATION_TITLE = "Enable ktfmt"
60+
}
6261
}

ktfmt_idea_plugin/src/main/kotlin/com/facebook/ktfmt/intellij/KtfmtConfigurable.kt

+21-21
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@ class KtfmtConfigurable(project: Project) :
1818
_id = "com.facebook.ktfmt_idea_plugin.settings",
1919
helpTopic = "ktfmt",
2020
) {
21-
private val settings = KtfmtSettings.getInstance(project)
21+
private val settings = KtfmtSettings.getInstance(project)
2222

23-
override fun createPanel(): DialogPanel = panel {
24-
lateinit var enabledCheckbox: JCheckBox
25-
row {
26-
enabledCheckbox =
27-
checkBox("Enable ktfmt")
28-
.bindSelected(
29-
getter = { settings.isEnabled },
30-
setter = { settings.setEnabled(if (it) Enabled else Disabled) },
31-
)
32-
.component
33-
}
23+
override fun createPanel(): DialogPanel = panel {
24+
lateinit var enabledCheckbox: JCheckBox
25+
row {
26+
enabledCheckbox =
27+
checkBox("Enable ktfmt")
28+
.bindSelected(
29+
getter = { settings.isEnabled },
30+
setter = { settings.setEnabled(if (it) Enabled else Disabled) },
31+
)
32+
.component
33+
}
3434

35-
row {
36-
comboBox(UiFormatterStyle.entries.toList())
37-
.label("Code style:")
38-
.bindItem(
39-
getter = { settings.uiFormatterStyle },
40-
setter = { settings.uiFormatterStyle = it ?: UiFormatterStyle.Meta },
41-
)
42-
.enabledIf(enabledCheckbox.selected)
43-
}
35+
row {
36+
comboBox(UiFormatterStyle.entries.toList())
37+
.label("Code style:")
38+
.bindItem(
39+
getter = { settings.uiFormatterStyle },
40+
setter = { settings.uiFormatterStyle = it ?: UiFormatterStyle.Meta },
41+
)
42+
.enabledIf(enabledCheckbox.selected)
4443
}
44+
}
4545
}

ktfmt_idea_plugin/src/main/kotlin/com/facebook/ktfmt/intellij/KtfmtFormattingService.kt

+29-29
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,40 @@ private const val PARSING_ERROR_TITLE: String = PARSING_ERROR_NOTIFICATION_GROUP
2828

2929
/** Uses `ktfmt` to reformat code. */
3030
class KtfmtFormattingService : AsyncDocumentFormattingService() {
31-
override fun createFormattingTask(request: AsyncFormattingRequest): FormattingTask {
32-
val project = request.context.project
33-
val style = KtfmtSettings.getInstance(project).uiFormatterStyle
34-
return KtfmtFormattingTask(request, style)
35-
}
31+
override fun createFormattingTask(request: AsyncFormattingRequest): FormattingTask {
32+
val project = request.context.project
33+
val style = KtfmtSettings.getInstance(project).uiFormatterStyle
34+
return KtfmtFormattingTask(request, style)
35+
}
3636

37-
override fun getNotificationGroupId(): String = PARSING_ERROR_NOTIFICATION_GROUP
37+
override fun getNotificationGroupId(): String = PARSING_ERROR_NOTIFICATION_GROUP
3838

39-
override fun getName(): String = "ktfmt"
39+
override fun getName(): String = "ktfmt"
4040

41-
override fun getFeatures(): Set<Feature> = emptySet()
41+
override fun getFeatures(): Set<Feature> = emptySet()
4242

43-
override fun canFormat(file: PsiFile): Boolean =
44-
KotlinFileType.INSTANCE.name == file.fileType.name &&
45-
KtfmtSettings.getInstance(file.project).isEnabled
43+
override fun canFormat(file: PsiFile): Boolean =
44+
KotlinFileType.INSTANCE.name == file.fileType.name &&
45+
KtfmtSettings.getInstance(file.project).isEnabled
4646

47-
private class KtfmtFormattingTask(
48-
private val request: AsyncFormattingRequest,
49-
private val style: UiFormatterStyle,
50-
) : FormattingTask {
51-
override fun run() {
52-
try {
53-
val formattedText = format(style.formattingOptions, request.documentText)
54-
request.onTextReady(formattedText)
55-
} catch (e: FormatterException) {
56-
request.onError(
57-
PARSING_ERROR_TITLE,
58-
"ktfmt failed. Does ${request.context.containingFile.name} have syntax errors?",
59-
)
60-
}
61-
}
47+
private class KtfmtFormattingTask(
48+
private val request: AsyncFormattingRequest,
49+
private val style: UiFormatterStyle,
50+
) : FormattingTask {
51+
override fun run() {
52+
try {
53+
val formattedText = format(style.formattingOptions, request.documentText)
54+
request.onTextReady(formattedText)
55+
} catch (e: FormatterException) {
56+
request.onError(
57+
PARSING_ERROR_TITLE,
58+
"ktfmt failed. Does ${request.context.containingFile.name} have syntax errors?",
59+
)
60+
}
61+
}
6262

63-
override fun isRunUnderProgress(): Boolean = true
63+
override fun isRunUnderProgress(): Boolean = true
6464

65-
override fun cancel(): Boolean = false
66-
}
65+
override fun cancel(): Boolean = false
66+
}
6767
}

ktfmt_idea_plugin/src/main/kotlin/com/facebook/ktfmt/intellij/KtfmtSettings.kt

+47-48
Original file line numberDiff line numberDiff line change
@@ -29,65 +29,64 @@ import com.intellij.openapi.project.Project
2929
@Service(PROJECT)
3030
@State(name = "KtfmtSettings", storages = [Storage("ktfmt.xml")])
3131
internal class KtfmtSettings : PersistentStateComponent<KtfmtSettings.State> {
32-
private var state = State()
32+
private var state = State()
3333

34-
override fun getState(): State = state
34+
override fun getState(): State = state
3535

36-
override fun loadState(state: State) {
37-
this.state = state
38-
}
39-
40-
var isEnabled: Boolean
41-
get() = state.enabled == Enabled
42-
set(enabled) {
43-
setEnabled(if (enabled) Enabled else Disabled)
44-
}
36+
override fun loadState(state: State) {
37+
this.state = state
38+
}
4539

46-
fun setEnabled(enabled: EnabledState) {
47-
state.enabled = enabled
40+
var isEnabled: Boolean
41+
get() = state.enabled == Enabled
42+
set(enabled) {
43+
setEnabled(if (enabled) Enabled else Disabled)
4844
}
4945

50-
val isUninitialized: Boolean
51-
get() = state.enabled == Unknown
46+
fun setEnabled(enabled: EnabledState) {
47+
state.enabled = enabled
48+
}
5249

53-
var uiFormatterStyle: UiFormatterStyle
54-
get() = state.uiFormatterStyle
55-
set(uiFormatterStyle) {
56-
state.uiFormatterStyle = uiFormatterStyle
57-
}
50+
val isUninitialized: Boolean
51+
get() = state.enabled == Unknown
5852

59-
internal enum class EnabledState {
60-
Unknown,
61-
Enabled,
62-
Disabled,
53+
var uiFormatterStyle: UiFormatterStyle
54+
get() = state.uiFormatterStyle
55+
set(uiFormatterStyle) {
56+
state.uiFormatterStyle = uiFormatterStyle
6357
}
6458

65-
internal class State {
66-
@JvmField var enabled: EnabledState = Unknown
67-
var uiFormatterStyle: UiFormatterStyle = Meta
59+
internal enum class EnabledState {
60+
Unknown,
61+
Enabled,
62+
Disabled,
63+
}
6864

69-
// enabled used to be a boolean so we use bean property methods for backwards
70-
// compatibility
71-
fun setEnabled(enabledStr: String?) {
72-
enabled =
73-
when {
74-
enabledStr == null -> Unknown
75-
enabledStr.toBoolean() -> Enabled
76-
else -> Disabled
77-
}
78-
}
65+
internal class State {
66+
@JvmField var enabled: EnabledState = Unknown
67+
var uiFormatterStyle: UiFormatterStyle = Meta
7968

80-
fun getEnabled(): String? =
81-
when (enabled) {
82-
Enabled -> "true"
83-
Disabled -> "false"
84-
else -> null
85-
}
69+
// enabled used to be a boolean so we use bean property methods for backwards
70+
// compatibility
71+
fun setEnabled(enabledStr: String?) {
72+
enabled =
73+
when {
74+
enabledStr == null -> Unknown
75+
enabledStr.toBoolean() -> Enabled
76+
else -> Disabled
77+
}
8678
}
8779

88-
companion object {
89-
@JvmStatic
90-
fun getInstance(project: Project): KtfmtSettings =
91-
project.getService(KtfmtSettings::class.java)
92-
}
80+
fun getEnabled(): String? =
81+
when (enabled) {
82+
Enabled -> "true"
83+
Disabled -> "false"
84+
else -> null
85+
}
86+
}
87+
88+
companion object {
89+
@JvmStatic
90+
fun getInstance(project: Project): KtfmtSettings = project.getService(KtfmtSettings::class.java)
91+
}
9392
}

ktfmt_idea_plugin/src/main/kotlin/com/facebook/ktfmt/intellij/UiFormatterStyle.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ internal enum class UiFormatterStyle(
2525
private val description: String,
2626
val formattingOptions: FormattingOptions,
2727
) {
28-
Meta("Meta (default)", META_FORMAT),
29-
Google("Google (internal)", GOOGLE_FORMAT),
30-
KotlinLang("Kotlinlang", KOTLINLANG_FORMAT);
28+
Meta("Meta (default)", META_FORMAT),
29+
Google("Google (internal)", GOOGLE_FORMAT),
30+
KotlinLang("Kotlinlang", KOTLINLANG_FORMAT);
3131

32-
override fun toString(): String = description
32+
override fun toString(): String = description
3333
}

0 commit comments

Comments
 (0)