-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d5e8aa
commit 2585918
Showing
2 changed files
with
136 additions
and
182 deletions.
There are no files selected for viewing
264 changes: 112 additions & 152 deletions
264
kotlin-eclipse-ui/src/org/jetbrains/kotlin/ui/launch/KotlinLaunchShortcut.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,186 +1,146 @@ | ||
/******************************************************************************* | ||
* Copyright 2000-2014 JetBrains s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*******************************************************************************/ | ||
package org.jetbrains.kotlin.ui.launch; | ||
* Copyright 2000-2014 JetBrains s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*******************************************************************************/ | ||
package org.jetbrains.kotlin.ui.launch | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.ArrayList | ||
import org.eclipse.core.resources.IContainer | ||
import org.eclipse.core.resources.IFile | ||
import org.eclipse.core.resources.IProject | ||
import org.eclipse.core.resources.IResource | ||
import org.eclipse.core.runtime.CoreException | ||
import org.eclipse.core.runtime.IAdaptable | ||
import org.eclipse.debug.core.DebugPlugin | ||
import org.eclipse.debug.core.ILaunchConfiguration | ||
import org.eclipse.debug.core.ILaunchConfigurationType | ||
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy | ||
import org.eclipse.debug.ui.DebugUITools | ||
import org.eclipse.debug.ui.ILaunchShortcut | ||
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants | ||
import org.eclipse.jface.viewers.ISelection | ||
import org.eclipse.jface.viewers.IStructuredSelection | ||
import org.eclipse.ui.IEditorPart | ||
import org.jetbrains.kotlin.core.builder.KotlinPsiManager | ||
import org.jetbrains.kotlin.core.log.KotlinLogger | ||
import org.jetbrains.kotlin.core.utils.ProjectUtils | ||
import org.jetbrains.kotlin.eclipse.ui.utils.EditorUtil | ||
import org.jetbrains.kotlin.fileClasses.getFileClassFqName | ||
import org.jetbrains.kotlin.fileClasses.NoResolveFileClassesProvider | ||
import org.jetbrains.kotlin.idea.KotlinFileType | ||
import org.jetbrains.kotlin.name.FqName | ||
import org.jetbrains.kotlin.psi.KtFile | ||
import org.jetbrains.kotlin.ui.editors.KotlinFileEditor | ||
|
||
import org.eclipse.core.resources.IContainer; | ||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.resources.IProject; | ||
import org.eclipse.core.resources.IResource; | ||
import org.eclipse.core.runtime.CoreException; | ||
import org.eclipse.core.runtime.IAdaptable; | ||
import org.eclipse.debug.core.DebugPlugin; | ||
import org.eclipse.debug.core.ILaunchConfiguration; | ||
import org.eclipse.debug.core.ILaunchConfigurationType; | ||
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; | ||
import org.eclipse.debug.ui.DebugUITools; | ||
import org.eclipse.debug.ui.ILaunchShortcut; | ||
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; | ||
import org.eclipse.jface.viewers.ISelection; | ||
import org.eclipse.jface.viewers.IStructuredSelection; | ||
import org.eclipse.ui.IEditorPart; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.jetbrains.kotlin.core.builder.KotlinPsiManager; | ||
import org.jetbrains.kotlin.core.log.KotlinLogger; | ||
import org.jetbrains.kotlin.core.utils.ProjectUtils; | ||
import org.jetbrains.kotlin.eclipse.ui.utils.EditorUtil; | ||
import org.jetbrains.kotlin.fileClasses.FileClasses; | ||
import org.jetbrains.kotlin.fileClasses.NoResolveFileClassesProvider; | ||
import org.jetbrains.kotlin.idea.KotlinFileType; | ||
import org.jetbrains.kotlin.name.FqName; | ||
import org.jetbrains.kotlin.psi.KtFile; | ||
import org.jetbrains.kotlin.ui.editors.KotlinFileEditor; | ||
|
||
public class KotlinLaunchShortcut implements ILaunchShortcut { | ||
@Override | ||
public void launch(ISelection selection, String mode) { | ||
if (!(selection instanceof IStructuredSelection)) { | ||
return; | ||
class KotlinLaunchShortcut : ILaunchShortcut { | ||
companion object { | ||
fun createConfiguration(file: IFile): ILaunchConfiguration { | ||
val configWC = getLaunchConfigurationType().newInstance(null, "Config - " + file.getName()) | ||
configWC.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, getFileClassName(file).asString()) | ||
configWC.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, file.getProject().getName()) | ||
|
||
return configWC.doSave() | ||
} | ||
|
||
IStructuredSelection structuredSelection = (IStructuredSelection) selection; | ||
|
||
List<IFile> files = new ArrayList<IFile>(); | ||
for (Object object : structuredSelection.toList()) { | ||
if (object instanceof IAdaptable) { | ||
IResource resource = ((IAdaptable) object).getAdapter(IResource.class); | ||
addFiles(files, resource); | ||
} | ||
fun getFileClassName(mainFile: IFile): FqName { | ||
val ktFile = KotlinPsiManager.INSTANCE.getParsedFile(mainFile) | ||
return NoResolveFileClassesProvider.getFileClassFqName(ktFile) | ||
} | ||
|
||
IFile fileWithMain = ProjectUtils.findFilesWithMain(files); | ||
|
||
if (fileWithMain == null) { | ||
launchProject(files.get(0).getProject(), mode); | ||
return; | ||
private fun getLaunchConfigurationType(): ILaunchConfigurationType { | ||
return DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION) | ||
} | ||
|
||
launchWithMainClass(fileWithMain, mode); | ||
} | ||
|
||
@Override | ||
public void launch(IEditorPart editor, String mode) { | ||
if (editor instanceof KotlinFileEditor) { | ||
IFile file = EditorUtil.getFile(editor); | ||
|
||
if (file == null) { | ||
KotlinLogger.logError("Failed to retrieve IFile from editor " + editor, null); | ||
return; | ||
} | ||
|
||
if (ProjectUtils.hasMain(file)) { | ||
launchWithMainClass(file, mode); | ||
return; | ||
override fun launch(selection: ISelection, mode: String) { | ||
if (selection !is IStructuredSelection) return | ||
|
||
val files = ArrayList<IFile>() | ||
for (element in selection.toList()) { | ||
if (element is IAdaptable) { | ||
val resource = element.getAdapter(IResource::class.java) | ||
addFiles(files, resource) | ||
} | ||
|
||
launchProject(file.getProject(), mode); | ||
} | ||
} | ||
|
||
private void launchProject(IProject project, String mode) { | ||
IFile fileWithMain = ProjectUtils.findFilesWithMain(KotlinPsiManager.INSTANCE.getFilesByProject(project)); | ||
|
||
val fileWithMain = ProjectUtils.findFilesWithMain(files) | ||
if (fileWithMain != null) { | ||
launchWithMainClass(fileWithMain, mode); | ||
launchWithMainClass(fileWithMain, mode) | ||
return | ||
} | ||
|
||
launchProject(files[0].getProject(), mode) | ||
} | ||
|
||
private void launchWithMainClass(IFile fileWithMain, String mode) { | ||
ILaunchConfiguration configuration = findLaunchConfiguration(getLaunchConfigurationType(), fileWithMain); | ||
override fun launch(editor: IEditorPart, mode: String) { | ||
if (editor !is KotlinFileEditor) return | ||
|
||
if (configuration == null) { | ||
configuration = createConfiguration(fileWithMain); | ||
} | ||
val file = EditorUtil.getFile(editor) | ||
if (file == null) { | ||
KotlinLogger.logError("Failed to retrieve IFile from editor " + editor, null) | ||
return | ||
} | ||
|
||
if (configuration == null) { | ||
return; | ||
} | ||
if (ProjectUtils.hasMain(file)) { | ||
launchWithMainClass(file, mode) | ||
return | ||
} | ||
|
||
DebugUITools.launch(configuration, mode); | ||
launchProject(file.getProject(), mode) | ||
} | ||
|
||
public static ILaunchConfiguration createConfiguration(IFile file) { | ||
ILaunchConfiguration configuration = null; | ||
ILaunchConfigurationWorkingCopy configWC = null; | ||
ILaunchConfigurationType configurationType = getLaunchConfigurationType(); | ||
|
||
try { | ||
configWC = configurationType.newInstance(null, "Config - " + file.getName()); | ||
configWC.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, getFileClassName(file).asString()); | ||
configWC.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, file.getProject().getName()); | ||
|
||
configuration = configWC.doSave(); | ||
} catch (CoreException e) { | ||
KotlinLogger.logAndThrow(e); | ||
private fun launchProject(project: IProject, mode: String) { | ||
val fileWithMain = ProjectUtils.findFilesWithMain(KotlinPsiManager.INSTANCE.getFilesByProject(project)) | ||
if (fileWithMain != null) { | ||
launchWithMainClass(fileWithMain, mode) | ||
} | ||
|
||
return configuration; | ||
} | ||
|
||
@Nullable | ||
private ILaunchConfiguration findLaunchConfiguration(ILaunchConfigurationType configurationType, IFile mainClass) { | ||
try { | ||
ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(configurationType); | ||
String mainClassName = getFileClassName(mainClass).asString(); | ||
for (ILaunchConfiguration config : configs) { | ||
if (config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String)null).equals(mainClassName) && | ||
config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String)null).equals(mainClass.getProject().getName())) { | ||
return config; | ||
} | ||
private fun launchWithMainClass(fileWithMain: IFile, mode: String) { | ||
val configuration = findLaunchConfiguration(getLaunchConfigurationType(), fileWithMain) ?: createConfiguration(fileWithMain) | ||
DebugUITools.launch(configuration, mode) | ||
} | ||
|
||
private fun findLaunchConfiguration(configurationType: ILaunchConfigurationType, mainClass: IFile): ILaunchConfiguration? { | ||
val configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(configurationType) | ||
val mainClassName = getFileClassName(mainClass).asString() | ||
for (config in configs) { | ||
if (config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, null as String?) == mainClassName && | ||
config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, null as String?) == mainClass.project.name) { | ||
return config | ||
} | ||
} catch (CoreException e) { | ||
KotlinLogger.logAndThrow(e); | ||
} | ||
|
||
return null; | ||
return null | ||
} | ||
|
||
public static FqName getFileClassName(@NotNull IFile mainFile) { | ||
KtFile ktFile = KotlinPsiManager.INSTANCE.getParsedFile(mainFile); | ||
return FileClasses.getFileClassFqName(NoResolveFileClassesProvider.INSTANCE, ktFile); | ||
} | ||
|
||
private void addFiles(List<IFile> files, IResource resource) { | ||
switch (resource.getType()) { | ||
case IResource.FILE: | ||
IFile file = (IFile) resource; | ||
if (resource.getFileExtension().equals(KotlinFileType.INSTANCE.getDefaultExtension())) { | ||
files.add(file); | ||
private fun addFiles(files: ArrayList<IFile>, resource: IResource) { | ||
when (resource.getType()) { | ||
IResource.FILE -> { | ||
val file = resource as IFile | ||
if (resource.getFileExtension() == KotlinFileType.INSTANCE.getDefaultExtension()) { | ||
files.add(file) | ||
} | ||
|
||
break; | ||
case IResource.FOLDER: | ||
case IResource.PROJECT: | ||
IContainer container = (IContainer) resource; | ||
try { | ||
for (IResource child : container.members()) { | ||
addFiles(files, child); | ||
} | ||
} catch (CoreException e) { | ||
KotlinLogger.logAndThrow(e); | ||
} | ||
|
||
IResource.FOLDER, IResource.PROJECT -> { | ||
val container = resource as IContainer | ||
for (child in container.members()) { | ||
addFiles(files, child) | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
private static ILaunchConfigurationType getLaunchConfigurationType() { | ||
return DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_JAVA_APPLICATION); | ||
} | ||
} |
54 changes: 24 additions & 30 deletions
54
kotlin-eclipse-ui/src/org/jetbrains/kotlin/ui/launch/KotlinLaunchableTester.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,28 @@ | ||
package org.jetbrains.kotlin.ui.launch; | ||
package org.jetbrains.kotlin.ui.launch | ||
|
||
import org.eclipse.core.expressions.PropertyTester; | ||
import org.eclipse.core.resources.IFile; | ||
import org.eclipse.core.runtime.IAdaptable; | ||
import org.eclipse.jdt.core.IJavaProject; | ||
import org.eclipse.jdt.core.JavaCore; | ||
import org.jetbrains.kotlin.core.builder.KotlinPsiManager; | ||
import org.jetbrains.kotlin.core.model.KotlinAnalysisFileCache; | ||
import org.jetbrains.kotlin.idea.MainFunctionDetector; | ||
import org.jetbrains.kotlin.psi.KtFile; | ||
import org.jetbrains.kotlin.resolve.BindingContext; | ||
import org.eclipse.core.expressions.PropertyTester | ||
import org.eclipse.core.resources.IFile | ||
import org.eclipse.core.runtime.IAdaptable | ||
import org.eclipse.jdt.core.IJavaProject | ||
import org.eclipse.jdt.core.JavaCore | ||
import org.jetbrains.kotlin.core.builder.KotlinPsiManager | ||
import org.jetbrains.kotlin.core.model.KotlinAnalysisFileCache | ||
import org.jetbrains.kotlin.idea.MainFunctionDetector | ||
import org.jetbrains.kotlin.psi.KtFile | ||
import org.jetbrains.kotlin.resolve.BindingContext | ||
|
||
public class KotlinLaunchableTester extends PropertyTester { | ||
@Override | ||
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { | ||
if (receiver instanceof IAdaptable) { | ||
IFile file = ((IAdaptable) receiver).getAdapter(IFile.class); | ||
if (file != null) { | ||
KtFile jetFile = KotlinPsiManager.getKotlinParsedFile(file); | ||
if (jetFile == null) { | ||
return false; | ||
} | ||
|
||
IJavaProject javaProject = JavaCore.create(file.getProject()); | ||
BindingContext bindingContext = KotlinAnalysisFileCache.INSTANCE.getAnalysisResult(jetFile, javaProject) | ||
.getAnalysisResult().getBindingContext(); | ||
return new MainFunctionDetector(bindingContext).hasMain(jetFile.getDeclarations()); | ||
} | ||
} | ||
class KotlinLaunchableTester : PropertyTester() { | ||
override fun test(receiver: Any?, property: String?, args: Array<Any>?, expectedValue: Any?): Boolean { | ||
if (receiver !is IAdaptable) return false | ||
|
||
return false; | ||
val file = receiver.getAdapter(IFile::class.java) | ||
if (file == null) return false | ||
|
||
val jetFile = KotlinPsiManager.getKotlinParsedFile(file) | ||
if (jetFile == null) return false | ||
|
||
val javaProject = JavaCore.create(file.getProject()) | ||
val bindingContext = KotlinAnalysisFileCache.getAnalysisResult(jetFile, javaProject).analysisResult.bindingContext | ||
return MainFunctionDetector(bindingContext).hasMain(jetFile.getDeclarations()) | ||
} | ||
} | ||
} |