11/* ******************************************************************************
2- * Copyright 2000-2015 JetBrains s.r.o.
3- *
4- * Licensed under the Apache License, Version 2.0 (the "License");
5- * you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at
7- *
8- * http://www.apache.org/licenses/LICENSE-2.0
9- *
10- * Unless required by applicable law or agreed to in writing, software
11- * distributed under the License is distributed on an "AS IS" BASIS,
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- * See the License for the specific language governing permissions and
14- * limitations under the License.
15- *
16- *******************************************************************************/
17- package org.jetbrains.kotlin.ui.builder ;
2+ * Copyright 2000-2015 JetBrains s.r.o.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ *
16+ *******************************************************************************/
17+ package org.jetbrains.kotlin.ui.builder
1818
19- import java.util.Collection;
20- import java.util.List;
21- import java.util.Map;
22- import java.util.Set;
19+ import org.eclipse.core.resources.IFile
20+ import org.eclipse.core.resources.IProject
21+ import org.eclipse.core.resources.IResource
22+ import org.eclipse.core.resources.IResourceDelta
23+ import org.eclipse.core.resources.IResourceDeltaVisitor
24+ import org.eclipse.core.resources.IncrementalProjectBuilder
25+ import org.eclipse.core.runtime.CoreException
26+ import org.eclipse.core.runtime.IProgressMonitor
27+ import org.eclipse.debug.core.model.LaunchConfigurationDelegate
28+ import org.eclipse.jdt.core.IJavaProject
29+ import org.eclipse.jdt.core.JavaCore
30+ import org.jetbrains.kotlin.analyzer.AnalysisResult
31+ import org.jetbrains.kotlin.core.asJava.KotlinLightClassGeneration
32+ import org.jetbrains.kotlin.core.builder.KotlinPsiManager
33+ import org.jetbrains.kotlin.core.compiler.KotlinCompiler.KotlinCompilerResult
34+ import org.jetbrains.kotlin.core.compiler.KotlinCompilerUtils
35+ import org.jetbrains.kotlin.core.model.KotlinAnalysisProjectCache
36+ import org.jetbrains.kotlin.eclipse.ui.utils.EditorUtil
37+ import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics
38+ import org.jetbrains.kotlin.ui.editors.annotations.AnnotationManager
39+ import org.jetbrains.kotlin.ui.editors.annotations.DiagnosticAnnotation
40+ import org.jetbrains.kotlin.ui.editors.annotations.DiagnosticAnnotationUtil
41+ import com.google.common.collect.Sets
2342
24- import org.eclipse.core.resources.IFile;
25- import org.eclipse.core.resources.IProject;
26- import org.eclipse.core.resources.IResource;
27- import org.eclipse.core.resources.IResourceDelta;
28- import org.eclipse.core.resources.IResourceDeltaVisitor;
29- import org.eclipse.core.resources.IncrementalProjectBuilder;
30- import org.eclipse.core.runtime.CoreException;
31- import org.eclipse.core.runtime.IProgressMonitor;
32- import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
33- import org.eclipse.jdt.core.IJavaProject;
34- import org.eclipse.jdt.core.JavaCore;
35- import org.jetbrains.annotations.NotNull;
36- import org.jetbrains.kotlin.analyzer.AnalysisResult;
37- import org.jetbrains.kotlin.core.asJava.KotlinLightClassGeneration;
38- import org.jetbrains.kotlin.core.builder.KotlinPsiManager;
39- import org.jetbrains.kotlin.core.compiler.KotlinCompiler.KotlinCompilerResult;
40- import org.jetbrains.kotlin.core.compiler.KotlinCompilerUtils;
41- import org.jetbrains.kotlin.core.model.KotlinAnalysisProjectCache;
42- import org.jetbrains.kotlin.eclipse.ui.utils.EditorUtil;
43- import org.jetbrains.kotlin.resolve.diagnostics.Diagnostics;
44- import org.jetbrains.kotlin.ui.editors.annotations.AnnotationManager;
45- import org.jetbrains.kotlin.ui.editors.annotations.DiagnosticAnnotation;
46- import org.jetbrains.kotlin.ui.editors.annotations.DiagnosticAnnotationUtil;
47-
48- import com.google.common.collect.Sets;
49-
50- public class KotlinBuilder extends IncrementalProjectBuilder {
51-
52- @Override
53- protected IProject [] build(int kind, Map <String , String > args, IProgressMonitor monitor) throws CoreException {
54- IJavaProject javaProject = JavaCore .create(getProject());
43+ class KotlinBuilder : IncrementalProjectBuilder () {
44+ override fun build (kind : Int , args : Map <String , String >? , monitor : IProgressMonitor ? ): Array <IProject >? {
45+ val project = getProject()
46+ val javaProject = JavaCore .create(project)
5547 if (isBuildingForLaunch()) {
56- compileKotlinFiles(javaProject);
57- return null ;
48+ compileKotlinFiles(javaProject)
49+ return null
5850 }
5951
60- final Set <IFile > affectedFiles = Sets .newHashSet();
61- if (kind == FULL_BUILD ) {
62- affectedFiles.addAll(KotlinPsiManager .INSTANCE .getFilesByProject(getProject()));
52+ val affectedFiles = if (kind == FULL_BUILD ) {
53+ KotlinPsiManager .INSTANCE .getFilesByProject(project)
6354 } else {
64- IResourceDelta delta = getDelta(getProject());
65- if (delta != null ) {
66- affectedFiles.addAll(getAffectedFiles(delta, javaProject));
67- }
55+ val delta = getDelta(project)
56+ if (delta != null ) getAffectedFiles(delta, javaProject) else emptySet()
6857 }
6958
70- commitFiles(affectedFiles);
59+ commitFiles(affectedFiles)
7160
72- if (! affectedFiles.isEmpty ()) {
73- KotlinLightClassGeneration .updateLightClasses(javaProject, affectedFiles);
61+ if (affectedFiles.isNotEmpty ()) {
62+ KotlinLightClassGeneration .updateLightClasses(javaProject, affectedFiles)
7463 }
7564
76- AnalysisResult analysisResult = KotlinAnalysisProjectCache .INSTANCE . getAnalysisResult(javaProject);
77- updateLineMarkers(analysisResult.getBindingContext().getDiagnostics());
65+ val analysisResult = KotlinAnalysisProjectCache .getAnalysisResult(javaProject)
66+ updateLineMarkers(analysisResult.bindingContext.diagnostics)
7867
79- return null ;
68+ return null
8069 }
8170
82- private void commitFiles(@NotNull Collection <IFile > files ) {
83- for (IFile file : files) {
71+ private fun commitFiles (files : Collection <IFile >) {
72+ for (file in files) {
8473 if (file.exists()) {
85- KotlinPsiManager .getKotlinFileIfExist(file, EditorUtil .getDocument(file).get());
74+ KotlinPsiManager .getKotlinFileIfExist(file, EditorUtil .getDocument(file).get())
8675 }
8776 }
8877 }
8978
90- private Set <IFile > getAffectedFiles(@NotNull IResourceDelta delta, @NotNull final IJavaProject javaProject) throws CoreException {
91- final Set <IFile > affectedFiles = Sets .newHashSet();
92- delta.accept(new IResourceDeltaVisitor () {
93- @Override
94- public boolean visit(IResourceDelta delta) throws CoreException {
95- if (delta.getKind() != IResourceDelta .NO_CHANGE ) {
96- IResource resource = delta.getResource();
97- if (KotlinPsiManager .INSTANCE .isKotlinSourceFile(resource, javaProject)) {
98- affectedFiles.add((IFile ) resource);
99- return false ;
100- }
101-
102- if (! (resource instanceof IFile )) {
103- return true ;
104- }
79+ private fun getAffectedFiles (resourceDelta : IResourceDelta , javaProject : IJavaProject ): Set <IFile > {
80+ val affectedFiles = hashSetOf<IFile >()
81+ resourceDelta.accept { delta ->
82+ if (delta.getKind() != IResourceDelta .NO_CHANGE ) {
83+ val resource = delta.getResource()
84+ if (KotlinPsiManager .INSTANCE .isKotlinSourceFile(resource, javaProject)) {
85+ affectedFiles.add(resource as IFile )
86+ return @accept false
10587 }
10688
107- return false ;
89+ if (resource !is IFile ) return @accept true
10890 }
109- });
91+
92+ false
93+ }
11094
111- return affectedFiles;
95+ return affectedFiles
11296 }
11397
114- private boolean isBuildingForLaunch() {
115- String launchDelegateFQName = LaunchConfigurationDelegate .class .getCanonicalName();
116- for (StackTraceElement stackTraceElement : Thread .currentThread().getStackTrace()) {
117- if (launchDelegateFQName.equals(stackTraceElement.getClassName())) {
118- return true ;
119- }
120- }
121-
122- return false ;
98+ private fun isBuildingForLaunch ():Boolean {
99+ val launchDelegateFQName = LaunchConfigurationDelegate ::class .java.getCanonicalName()
100+ return Thread .currentThread().getStackTrace().find { it.className == launchDelegateFQName } != null
123101 }
124102
125- private void compileKotlinFiles(@NotNull IJavaProject javaProject) throws CoreException {
126- KotlinCompilerResult compilerResult = KotlinCompilerUtils .compileWholeProject(javaProject);
103+ private fun compileKotlinFiles (javaProject : IJavaProject ) {
104+ val compilerResult = KotlinCompilerUtils .compileWholeProject(javaProject)
127105 if (! compilerResult.compiledCorrectly()) {
128- KotlinCompilerUtils .handleCompilerOutput(compilerResult.getCompilerOutput());
106+ KotlinCompilerUtils .handleCompilerOutput(compilerResult.getCompilerOutput())
129107 }
130108 }
131109
132- private void updateLineMarkers(@NotNull Diagnostics diagnostics) throws CoreException {
133- addMarkersToProject(DiagnosticAnnotationUtil .INSTANCE .handleDiagnostics(diagnostics), getProject());
110+ private fun updateLineMarkers (diagnostics : Diagnostics ) {
111+ addMarkersToProject(DiagnosticAnnotationUtil .INSTANCE .handleDiagnostics(diagnostics), getProject())
134112 }
135113
136- private void addMarkersToProject(Map <IFile , List <DiagnosticAnnotation >> annotations, IProject project) throws CoreException {
137- AnnotationManager .INSTANCE .clearAllMarkersFromProject(project);
138-
139- for (IFile file : KotlinPsiManager .INSTANCE .getFilesByProject(getProject())) {
140- DiagnosticAnnotationUtil .INSTANCE .addParsingDiagnosticAnnotations(file, annotations);
114+ private fun addMarkersToProject (annotations : Map <IFile , List <DiagnosticAnnotation >>, project : IProject ) {
115+ AnnotationManager .clearAllMarkersFromProject(project)
116+ for (file in KotlinPsiManager .INSTANCE .getFilesByProject(getProject())) {
117+ DiagnosticAnnotationUtil .INSTANCE .addParsingDiagnosticAnnotations(file, annotations)
141118 }
142119
143- for (Map . Entry < IFile , List < DiagnosticAnnotation >> entry : annotations.entrySet() ) {
144- for (DiagnosticAnnotation annotation : entry.getValue() ) {
145- AnnotationManager .INSTANCE . addProblemMarker(annotation, entry.getKey());
120+ for ((file, diagnosticAnnotations) in annotations) {
121+ for (annotation in diagnosticAnnotations ) {
122+ AnnotationManager .addProblemMarker(annotation, file)
146123 }
147124 }
148125 }
149- }
126+ }
0 commit comments