Skip to content

Commit b1bfdb4

Browse files
committed
Sets default code styles for old and new projects
1 parent 5e15a5d commit b1bfdb4

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

kotlin-eclipse-core/preferences.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ compilerPlugins/jpa/jarPath=$KOTLIN_HOME/lib/noarg-compiler-plugin.jar
1111
compilerPlugins/jpa/args=org.jetbrains.kotlin.noarg:preset=jpa
1212
compilerPlugins/sam-with-receiver/active=false
1313
compilerPlugins/sam-with-receiver/jarPath=$KOTLIN_HOME/lib/sam-with-receiver-compiler-plugin.jar
14-
codeStyle/codeStyleId=KOTLIN_OFFICIAL
14+
codeStyle/codeStyleId=KOTLIN_OLD_DEFAULTS

kotlin-eclipse-core/src/org/jetbrains/kotlin/core/model/KotlinNature.kt

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@
1616
*******************************************************************************/
1717
package org.jetbrains.kotlin.core.model
1818

19-
import org.eclipse.core.resources.ICommand
2019
import org.eclipse.core.resources.IProject
21-
import org.eclipse.core.resources.IProjectDescription
2220
import org.eclipse.core.resources.IProjectNature
23-
import org.eclipse.core.runtime.CoreException
21+
import org.eclipse.core.resources.ProjectScope
2422
import kotlin.properties.Delegates
2523
import org.jetbrains.kotlin.core.builder.KotlinPsiManager
26-
import org.eclipse.core.resources.IResourceDelta
2724
import java.util.LinkedList
2825
import org.eclipse.core.runtime.jobs.Job
2926
import org.eclipse.core.runtime.IProgressMonitor
@@ -32,8 +29,9 @@ import org.eclipse.jdt.core.JavaCore
3229
import java.util.Collections
3330
import org.eclipse.core.runtime.Status
3431
import org.eclipse.core.resources.ResourcesPlugin
32+
import org.jetbrains.kotlin.core.preferences.KotlinCodeStyleProperties
3533

36-
public class KotlinNature: IProjectNature {
34+
class KotlinNature: IProjectNature {
3735
companion object {
3836
val KOTLIN_NATURE: String = "org.jetbrains.kotlin.core.kotlinNature"
3937
@JvmField val KOTLIN_BUILDER: String = "org.jetbrains.kotlin.ui.kotlinBuilder"
@@ -47,64 +45,70 @@ public class KotlinNature: IProjectNature {
4745
fun hasKotlinBuilder(project: IProject) : Boolean {
4846
if (!project.isAccessible) return false
4947

50-
return project.getDescription().getBuildSpec().any {
51-
KOTLIN_BUILDER == it.getBuilderName()
48+
return project.description.buildSpec.any {
49+
KOTLIN_BUILDER == it.builderName
5250
}
5351
}
5452

5553
@JvmStatic
5654
fun addNature(project:IProject) {
5755
if (!hasKotlinNature(project)) {
58-
val description = project.getDescription()
59-
60-
val newNatureIds = description.getNatureIds().toMutableList()
61-
newNatureIds.add(KotlinNature.KOTLIN_NATURE)
62-
63-
description.setNatureIds(newNatureIds.toTypedArray())
56+
val description = project.description
57+
description.natureIds += KOTLIN_NATURE
6458
project.setDescription(description, null)
6559
}
6660
}
6761
}
68-
69-
public var eclipseProject: IProject by Delegates.notNull()
70-
71-
override public fun configure() {
62+
63+
var eclipseProject: IProject by Delegates.notNull()
64+
65+
override fun configure() {
7266
addKotlinBuilder(eclipseProject)
67+
setPreferredCodeStyle(eclipseProject)
7368
}
74-
75-
override public fun deconfigure() {
69+
70+
override fun deconfigure() {
7671
removeKotlinBuilder(eclipseProject)
7772
KotlinPsiManager.removeProjectFromManager(eclipseProject)
7873
KotlinAnalysisFileCache.resetCache()
7974
KotlinAnalysisProjectCache.resetCache(eclipseProject)
8075
}
81-
82-
override public fun setProject(project: IProject) {
76+
77+
override fun setProject(project: IProject) {
8378
eclipseProject = project
8479
}
85-
86-
override public fun getProject(): IProject = eclipseProject
80+
81+
override fun getProject(): IProject = eclipseProject
8782

8883
private fun addKotlinBuilder(project: IProject) {
8984
if (!hasKotlinBuilder(project)) {
90-
val description = project.getDescription()
85+
val description = project.description
9186

92-
val kotlinBuilderCommand = description.newCommand().apply { setBuilderName(KOTLIN_BUILDER) }
87+
val kotlinBuilderCommand = description.newCommand().apply { builderName = KOTLIN_BUILDER }
9388

94-
val newBuildCommands = description.getBuildSpec().toCollection(LinkedList())
89+
val newBuildCommands = description.buildSpec.toCollection(LinkedList())
9590
newBuildCommands.addFirst(kotlinBuilderCommand)
96-
97-
description.setBuildSpec(newBuildCommands.toTypedArray())
91+
92+
description.buildSpec = newBuildCommands.toTypedArray()
9893
project.setDescription(description, null)
9994
}
10095
}
96+
97+
private fun setPreferredCodeStyle(eclipseProject: IProject) {
98+
KotlinCodeStyleProperties(ProjectScope(eclipseProject)).apply {
99+
codeStyleId = "KOTLIN_OFFICIAL"
100+
globalsOverridden = true
101+
saveChanges()
102+
}
103+
104+
}
101105

102106
private fun removeKotlinBuilder(project: IProject) {
103107
if (hasKotlinBuilder(project)) {
104-
val description = project.getDescription()
105-
val newBuildCommands = description.getBuildSpec().filter { it.getBuilderName() != KotlinNature.KOTLIN_BUILDER }
106-
107-
description.setBuildSpec(newBuildCommands.toTypedArray())
108+
val description = project.description
109+
val newBuildCommands = description.buildSpec.filter { it.builderName != KotlinNature.KOTLIN_BUILDER }
110+
111+
description.buildSpec = newBuildCommands.toTypedArray()
108112
project.setDescription(description, null)
109113
}
110114
}
@@ -114,23 +118,23 @@ public class KotlinNature: IProjectNature {
114118
fun setKotlinBuilderBeforeJavaBuilder(project: IProject) {
115119
val job = object : Job("Swap Kotlin builder with Java Builder") {
116120
override fun run(monitor: IProgressMonitor?): IStatus? {
117-
val description = project.getDescription()
121+
val description = project.description
118122

119-
val builders = description.getBuildSpec().toCollection(LinkedList())
120-
val kotlinBuilderIndex = builders.indexOfFirst { it.getBuilderName() == KotlinNature.KOTLIN_BUILDER }
121-
val javaBuilderIndex = builders.indexOfFirst { it.getBuilderName() == JavaCore.BUILDER_ID }
123+
val builders = description.buildSpec.toCollection(LinkedList())
124+
val kotlinBuilderIndex = builders.indexOfFirst { it.builderName == KotlinNature.KOTLIN_BUILDER }
125+
val javaBuilderIndex = builders.indexOfFirst { it.builderName == JavaCore.BUILDER_ID }
122126

123127
if (kotlinBuilderIndex >= 0 && javaBuilderIndex >= 0 && javaBuilderIndex < kotlinBuilderIndex) {
124128
Collections.swap(builders, kotlinBuilderIndex, javaBuilderIndex)
125-
126-
description.setBuildSpec(builders.toTypedArray())
129+
130+
description.buildSpec = builders.toTypedArray()
127131
project.setDescription(description, monitor)
128132
}
129133

130134
return Status.OK_STATUS
131135
}
132136
}
133-
134-
job.setRule(ResourcesPlugin.getWorkspace().getRoot())
137+
138+
job.rule = ResourcesPlugin.getWorkspace().root
135139
job.schedule()
136140
}

0 commit comments

Comments
 (0)