Introduce js header library concept
This commit is contained in:
@@ -222,7 +222,8 @@
|
|||||||
<framework.type implementation="org.jetbrains.jet.plugin.framework.JavaFrameworkType"/>
|
<framework.type implementation="org.jetbrains.jet.plugin.framework.JavaFrameworkType"/>
|
||||||
<framework.type implementation="org.jetbrains.jet.plugin.framework.JSFrameworkType"/>
|
<framework.type implementation="org.jetbrains.jet.plugin.framework.JSFrameworkType"/>
|
||||||
<library.presentationProvider implementation="org.jetbrains.jet.plugin.framework.JavaRuntimePresentationProvider"/>
|
<library.presentationProvider implementation="org.jetbrains.jet.plugin.framework.JavaRuntimePresentationProvider"/>
|
||||||
<library.presentationProvider implementation="org.jetbrains.jet.plugin.framework.JSHeadersPresentationProvider"/>
|
<library.presentationProvider implementation="org.jetbrains.jet.plugin.framework.JSLibraryStdPresentationProvider"/>
|
||||||
|
<library.presentationProvider implementation="org.jetbrains.jet.plugin.framework.JsHeaderLibraryPresentationProvider"/>
|
||||||
|
|
||||||
<java.elementFinder implementation="org.jetbrains.jet.asJava.JavaElementFinder"/>
|
<java.elementFinder implementation="org.jetbrains.jet.asJava.JavaElementFinder"/>
|
||||||
<java.shortNamesCache implementation="org.jetbrains.jet.plugin.caches.JetShortNamesCache"/>
|
<java.shortNamesCache implementation="org.jetbrains.jet.plugin.caches.JetShortNamesCache"/>
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2013 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.jet.plugin.framework;
|
||||||
|
|
||||||
|
import com.intellij.openapi.roots.OrderRootType;
|
||||||
|
import com.intellij.openapi.roots.libraries.*;
|
||||||
|
import com.intellij.openapi.vfs.VirtualFile;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public abstract class CachingLibraryPresentationProvider<T extends LibraryProperties> extends LibraryPresentationProvider<T> {
|
||||||
|
protected CachingLibraryPresentationProvider(@NotNull LibraryKind kind) {
|
||||||
|
super(kind);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDetected(@NotNull Library library) {
|
||||||
|
return isDetected(Arrays.asList(library.getFiles(OrderRootType.CLASSES)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDetected(@NotNull List<VirtualFile> classesRoots) {
|
||||||
|
return cachedDetect(classesRoots, getKind());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean cachedDetect(@NotNull List<VirtualFile> classesRoots, @NotNull final LibraryKind libraryKind) {
|
||||||
|
return !LibraryDetectionManager.getInstance().processProperties(
|
||||||
|
classesRoots,
|
||||||
|
new LibraryDetectionManager.LibraryPropertiesProcessor() {
|
||||||
|
@Override
|
||||||
|
public <P extends LibraryProperties> boolean processProperties(@NotNull LibraryKind processedKind, @NotNull P properties) {
|
||||||
|
return !libraryKind.equals(processedKind);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -45,7 +45,7 @@ public class JSFrameworkSupportProvider extends FrameworkSupportInModuleProvider
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
public CustomLibraryDescription createLibraryDescription() {
|
public CustomLibraryDescription createLibraryDescription() {
|
||||||
return new JSLibraryDescription();
|
return new JSLibraryStdDescription();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|||||||
+4
-2
@@ -35,9 +35,11 @@ import org.jetbrains.jet.utils.PathUtil;
|
|||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.*;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class JSLibraryDescription extends CustomLibraryDescription {
|
public class JSLibraryStdDescription extends CustomLibraryDescription {
|
||||||
public static final LibraryKind KOTLIN_JAVASCRIPT_KIND = LibraryKind.create("kotlin-js-stdlib");
|
public static final LibraryKind KOTLIN_JAVASCRIPT_KIND = LibraryKind.create("kotlin-js-stdlib");
|
||||||
public static final String LIBRARY_NAME = "KotlinJavaScript";
|
public static final String LIBRARY_NAME = "KotlinJavaScript";
|
||||||
|
|
||||||
+6
-5
@@ -28,13 +28,13 @@ import org.jetbrains.jet.utils.PathUtil;
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class JSHeadersPresentationProvider extends LibraryPresentationProvider<LibraryVersionProperties> {
|
public class JSLibraryStdPresentationProvider extends CachingLibraryPresentationProvider<LibraryVersionProperties> {
|
||||||
public static JSHeadersPresentationProvider getInstance() {
|
public static JSLibraryStdPresentationProvider getInstance() {
|
||||||
return LibraryPresentationProvider.EP_NAME.findExtension(JSHeadersPresentationProvider.class);
|
return LibraryPresentationProvider.EP_NAME.findExtension(JSLibraryStdPresentationProvider.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected JSHeadersPresentationProvider() {
|
protected JSLibraryStdPresentationProvider() {
|
||||||
super(JSLibraryDescription.KOTLIN_JAVASCRIPT_KIND);
|
super(JSLibraryStdDescription.KOTLIN_JAVASCRIPT_KIND);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@@ -53,6 +53,7 @@ public class JSHeadersPresentationProvider extends LibraryPresentationProvider<L
|
|||||||
|
|
||||||
for (VirtualFile root : classesRoots) {
|
for (VirtualFile root : classesRoots) {
|
||||||
if (root.getName().equals(PathUtil.JS_LIB_JAR_NAME)) {
|
if (root.getName().equals(PathUtil.JS_LIB_JAR_NAME)) {
|
||||||
|
assert JsHeaderLibraryPresentationProvider.getInstance().detect(classesRoots) != null : "StdLib should also be detected as headers library";
|
||||||
return new LibraryVersionProperties(KotlinRuntimeLibraryUtil.getLibraryVersion(root));
|
return new LibraryVersionProperties(KotlinRuntimeLibraryUtil.getLibraryVersion(root));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,7 +66,7 @@ public class JavaFrameworkSupportProvider extends FrameworkSupportInModuleProvid
|
|||||||
@NotNull ModifiableModelsProvider modifiableModelsProvider) {
|
@NotNull ModifiableModelsProvider modifiableModelsProvider) {
|
||||||
FrameworksCompatibilityUtils.suggestRemoveIncompatibleFramework(
|
FrameworksCompatibilityUtils.suggestRemoveIncompatibleFramework(
|
||||||
rootModel,
|
rootModel,
|
||||||
new JSLibraryDescription(),
|
new JSLibraryStdDescription(),
|
||||||
JSFrameworkType.getInstance());
|
JSFrameworkType.getInstance());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import org.jetbrains.jet.utils.PathUtil;
|
|||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class JavaRuntimePresentationProvider extends LibraryPresentationProvider<LibraryVersionProperties> {
|
public class JavaRuntimePresentationProvider extends CachingLibraryPresentationProvider<LibraryVersionProperties> {
|
||||||
public static JavaRuntimePresentationProvider getInstance() {
|
public static JavaRuntimePresentationProvider getInstance() {
|
||||||
return LibraryPresentationProvider.EP_NAME.findExtension(JavaRuntimePresentationProvider.class);
|
return LibraryPresentationProvider.EP_NAME.findExtension(JavaRuntimePresentationProvider.class);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2013 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.jet.plugin.framework;
|
||||||
|
|
||||||
|
import com.intellij.framework.library.LibraryVersionProperties;
|
||||||
|
import com.intellij.openapi.roots.libraries.LibraryKind;
|
||||||
|
import com.intellij.openapi.roots.libraries.LibraryPresentationProvider;
|
||||||
|
import com.intellij.openapi.vfs.VfsUtil;
|
||||||
|
import com.intellij.openapi.vfs.VirtualFile;
|
||||||
|
import com.intellij.util.CommonProcessors;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.jet.plugin.JetFileType;
|
||||||
|
import org.jetbrains.jet.plugin.JetIcons;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class JsHeaderLibraryPresentationProvider extends CachingLibraryPresentationProvider<LibraryVersionProperties> {
|
||||||
|
public static final LibraryKind KOTLIN_JAVASCRIPT_HEADER_KIND = LibraryKind.create("kotlin-js-header");
|
||||||
|
|
||||||
|
public static JsHeaderLibraryPresentationProvider getInstance() {
|
||||||
|
return LibraryPresentationProvider.EP_NAME.findExtension(JsHeaderLibraryPresentationProvider.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected JsHeaderLibraryPresentationProvider() {
|
||||||
|
super(KOTLIN_JAVASCRIPT_HEADER_KIND);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public Icon getIcon() {
|
||||||
|
return JetIcons.SMALL_LOGO_13;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public LibraryVersionProperties detect(@NotNull List<VirtualFile> classesRoots) {
|
||||||
|
if (JavaRuntimePresentationProvider.getInstance().detect(classesRoots) != null) {
|
||||||
|
// Prevent clashing with java runtime
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (VirtualFile file : classesRoots) {
|
||||||
|
CommonProcessors.FindFirstProcessor<VirtualFile> findKTProcessor = new CommonProcessors.FindFirstProcessor<VirtualFile>() {
|
||||||
|
@Override
|
||||||
|
protected boolean accept(VirtualFile file) {
|
||||||
|
String extension = file.getExtension();
|
||||||
|
return extension != null && extension.equals(JetFileType.INSTANCE.getDefaultExtension());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
VfsUtil.processFilesRecursively(file, findKTProcessor);
|
||||||
|
|
||||||
|
if (findKTProcessor.isFound()) {
|
||||||
|
return new LibraryVersionProperties(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.jet.plugin.framework;
|
package org.jetbrains.jet.plugin.framework;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.Sets;
|
||||||
import com.intellij.openapi.application.ApplicationManager;
|
import com.intellij.openapi.application.ApplicationManager;
|
||||||
import com.intellij.openapi.module.Module;
|
import com.intellij.openapi.module.Module;
|
||||||
import com.intellij.openapi.module.ModuleManager;
|
import com.intellij.openapi.module.ModuleManager;
|
||||||
@@ -26,25 +27,25 @@ import com.intellij.openapi.roots.ModuleRootManager;
|
|||||||
import com.intellij.openapi.roots.OrderRootType;
|
import com.intellij.openapi.roots.OrderRootType;
|
||||||
import com.intellij.openapi.roots.ProjectRootModificationTracker;
|
import com.intellij.openapi.roots.ProjectRootModificationTracker;
|
||||||
import com.intellij.openapi.roots.libraries.Library;
|
import com.intellij.openapi.roots.libraries.Library;
|
||||||
import com.intellij.openapi.util.Computable;
|
|
||||||
import com.intellij.openapi.util.Key;
|
import com.intellij.openapi.util.Key;
|
||||||
import com.intellij.openapi.util.Pair;
|
import com.intellij.openapi.util.Pair;
|
||||||
|
import com.intellij.openapi.util.Ref;
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
import com.intellij.openapi.vfs.VirtualFile;
|
||||||
import com.intellij.psi.search.GlobalSearchScope;
|
import com.intellij.psi.search.GlobalSearchScope;
|
||||||
import com.intellij.psi.util.CachedValue;
|
import com.intellij.psi.util.CachedValue;
|
||||||
import com.intellij.psi.util.CachedValueProvider;
|
import com.intellij.psi.util.CachedValueProvider;
|
||||||
import com.intellij.psi.util.CachedValuesManager;
|
import com.intellij.psi.util.CachedValuesManager;
|
||||||
|
import com.intellij.util.PathUtil;
|
||||||
import com.intellij.util.Processor;
|
import com.intellij.util.Processor;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.psi.JetFile;
|
import org.jetbrains.jet.lang.psi.JetFile;
|
||||||
import org.jetbrains.jet.plugin.versions.KotlinRuntimeLibraryUtil;
|
import org.jetbrains.jet.plugin.versions.KotlinRuntimeLibraryUtil;
|
||||||
import org.jetbrains.jet.utils.PathUtil;
|
|
||||||
import org.jetbrains.k2js.config.EcmaVersion;
|
import org.jetbrains.k2js.config.EcmaVersion;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class KotlinFrameworkDetector {
|
public class KotlinFrameworkDetector {
|
||||||
private static final Key<CachedValue<Boolean>> IS_KOTLIN_JS_MODULE = Key.create("IS_KOTLIN_JS_MODULE");
|
private static final Key<CachedValue<Boolean>> IS_KOTLIN_JS_MODULE = Key.create("IS_KOTLIN_JS_MODULE");
|
||||||
@@ -83,8 +84,7 @@ public class KotlinFrameworkDetector {
|
|||||||
result = CachedValuesManager.getManager(module.getProject()).createCachedValue(new CachedValueProvider<Boolean>() {
|
result = CachedValuesManager.getManager(module.getProject()).createCachedValue(new CachedValueProvider<Boolean>() {
|
||||||
@Override
|
@Override
|
||||||
public Result<Boolean> compute() {
|
public Result<Boolean> compute() {
|
||||||
return Result.create(getStandardJavaScriptLibrary(module) != null,
|
return Result.create(getJSStandardLibrary(module) != null, ProjectRootModificationTracker.getInstance(module.getProject()));
|
||||||
ProjectRootModificationTracker.getInstance(module.getProject()));
|
|
||||||
}
|
}
|
||||||
}, false);
|
}, false);
|
||||||
|
|
||||||
@@ -94,47 +94,6 @@ public class KotlinFrameworkDetector {
|
|||||||
return result.getValue();
|
return result.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private static Collection<Library> getJavaScriptHeadersLibraries(final Module module) {
|
|
||||||
final Collection<Library> headersLibraries = Lists.newArrayList();
|
|
||||||
|
|
||||||
ApplicationManager.getApplication().runReadAction(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
ModuleRootManager.getInstance(module).orderEntries().librariesOnly().forEachLibrary(new Processor<Library>() {
|
|
||||||
@Override
|
|
||||||
public boolean process(Library library) {
|
|
||||||
if (JSHeadersPresentationProvider.getInstance().detect(
|
|
||||||
Arrays.asList(library.getFiles(OrderRootType.CLASSES))) != null) {
|
|
||||||
headersLibraries.add(library);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return headersLibraries;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private static Library getStandardJavaScriptLibrary(final Module module) {
|
|
||||||
return ApplicationManager.getApplication().runReadAction(new Computable<Library>() {
|
|
||||||
@Override
|
|
||||||
public Library compute() {
|
|
||||||
for (Library library : getJavaScriptHeadersLibraries(module)) {
|
|
||||||
for (VirtualFile file : library.getFiles(OrderRootType.SOURCES)) {
|
|
||||||
if (file.getName().equals(PathUtil.JS_LIB_JAR_NAME)) {
|
|
||||||
return library;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static Pair<List<String>, String> getLibLocationAndTargetForProject(@NotNull Project project) {
|
public static Pair<List<String>, String> getLibLocationAndTargetForProject(@NotNull Project project) {
|
||||||
Module[] modules = ModuleManager.getInstance(project).getModules();
|
Module[] modules = ModuleManager.getInstance(project).getModules();
|
||||||
@@ -147,20 +106,52 @@ public class KotlinFrameworkDetector {
|
|||||||
return Pair.empty();
|
return Pair.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Pair<List<String>, String> getLibLocationAndTargetForProject(Module module) {
|
public static Pair<List<String>, String> getLibLocationAndTargetForProject(final Module module) {
|
||||||
Library library = getStandardJavaScriptLibrary(module);
|
final Set<String> pathsToJSLib = Sets.newHashSet();
|
||||||
|
|
||||||
if (library != null) {
|
ApplicationManager.getApplication().runReadAction(new Runnable() {
|
||||||
List<String> pathsToJSLib = Lists.newArrayList();
|
@Override
|
||||||
VirtualFile[] files = library.getRootProvider().getFiles(OrderRootType.SOURCES);
|
public void run() {
|
||||||
for (VirtualFile file : files) {
|
ModuleRootManager.getInstance(module).orderEntries().librariesOnly().forEachLibrary(new Processor<Library>() {
|
||||||
pathsToJSLib.add(com.intellij.util.PathUtil.getLocalPath(file));
|
@Override
|
||||||
|
public boolean process(Library library) {
|
||||||
|
if (JsHeaderLibraryPresentationProvider.getInstance().isDetected(library)) {
|
||||||
|
for (VirtualFile file : library.getRootProvider().getFiles(OrderRootType.SOURCES)) {
|
||||||
|
pathsToJSLib.add(PathUtil.getLocalPath(file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return Pair.create(pathsToJSLib, EcmaVersion.defaultVersion().toString());
|
return Pair.<List<String>, String>create(Lists.newArrayList(pathsToJSLib), EcmaVersion.defaultVersion().toString());
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
throw new IllegalStateException("Should be called for JS module only. JS library is expected to be found");
|
|
||||||
}
|
@Nullable
|
||||||
|
private static Library getJSStandardLibrary(final Module module) {
|
||||||
|
final Ref<Library> jsLibrary = Ref.create();
|
||||||
|
|
||||||
|
ApplicationManager.getApplication().runReadAction(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
ModuleRootManager.getInstance(module).orderEntries().librariesOnly().forEachLibrary(new Processor<Library>() {
|
||||||
|
@Override
|
||||||
|
public boolean process(Library library) {
|
||||||
|
if (JSLibraryStdPresentationProvider.getInstance().detect(Arrays.asList(library.getFiles(OrderRootType.CLASSES))) != null) {
|
||||||
|
jsLibrary.set(library);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return jsLibrary.get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user