Introduce js header library concept

This commit is contained in:
Nikolay Krasko
2013-03-07 13:09:35 +04:00
parent 7c0be23416
commit 5fe4e2f4ae
9 changed files with 190 additions and 69 deletions
+2 -1
View File
@@ -222,7 +222,8 @@
<framework.type implementation="org.jetbrains.jet.plugin.framework.JavaFrameworkType"/>
<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.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.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
@Override
public CustomLibraryDescription createLibraryDescription() {
return new JSLibraryDescription();
return new JSLibraryStdDescription();
}
@Nullable
@@ -35,9 +35,11 @@ import org.jetbrains.jet.utils.PathUtil;
import javax.swing.*;
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 String LIBRARY_NAME = "KotlinJavaScript";
@@ -28,13 +28,13 @@ import org.jetbrains.jet.utils.PathUtil;
import javax.swing.*;
import java.util.List;
public class JSHeadersPresentationProvider extends LibraryPresentationProvider<LibraryVersionProperties> {
public static JSHeadersPresentationProvider getInstance() {
return LibraryPresentationProvider.EP_NAME.findExtension(JSHeadersPresentationProvider.class);
public class JSLibraryStdPresentationProvider extends CachingLibraryPresentationProvider<LibraryVersionProperties> {
public static JSLibraryStdPresentationProvider getInstance() {
return LibraryPresentationProvider.EP_NAME.findExtension(JSLibraryStdPresentationProvider.class);
}
protected JSHeadersPresentationProvider() {
super(JSLibraryDescription.KOTLIN_JAVASCRIPT_KIND);
protected JSLibraryStdPresentationProvider() {
super(JSLibraryStdDescription.KOTLIN_JAVASCRIPT_KIND);
}
@Nullable
@@ -53,6 +53,7 @@ public class JSHeadersPresentationProvider extends LibraryPresentationProvider<L
for (VirtualFile root : classesRoots) {
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));
}
}
@@ -66,7 +66,7 @@ public class JavaFrameworkSupportProvider extends FrameworkSupportInModuleProvid
@NotNull ModifiableModelsProvider modifiableModelsProvider) {
FrameworksCompatibilityUtils.suggestRemoveIncompatibleFramework(
rootModel,
new JSLibraryDescription(),
new JSLibraryStdDescription(),
JSFrameworkType.getInstance());
}
};
@@ -28,7 +28,7 @@ import org.jetbrains.jet.utils.PathUtil;
import javax.swing.*;
import java.util.List;
public class JavaRuntimePresentationProvider extends LibraryPresentationProvider<LibraryVersionProperties> {
public class JavaRuntimePresentationProvider extends CachingLibraryPresentationProvider<LibraryVersionProperties> {
public static JavaRuntimePresentationProvider getInstance() {
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;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
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.ProjectRootModificationTracker;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.CachedValue;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.util.PathUtil;
import com.intellij.util.Processor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.plugin.versions.KotlinRuntimeLibraryUtil;
import org.jetbrains.jet.utils.PathUtil;
import org.jetbrains.k2js.config.EcmaVersion;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
public class KotlinFrameworkDetector {
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>() {
@Override
public Result<Boolean> compute() {
return Result.create(getStandardJavaScriptLibrary(module) != null,
ProjectRootModificationTracker.getInstance(module.getProject()));
return Result.create(getJSStandardLibrary(module) != null, ProjectRootModificationTracker.getInstance(module.getProject()));
}
}, false);
@@ -94,47 +94,6 @@ public class KotlinFrameworkDetector {
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
public static Pair<List<String>, String> getLibLocationAndTargetForProject(@NotNull Project project) {
Module[] modules = ModuleManager.getInstance(project).getModules();
@@ -147,20 +106,52 @@ public class KotlinFrameworkDetector {
return Pair.empty();
}
public static Pair<List<String>, String> getLibLocationAndTargetForProject(Module module) {
Library library = getStandardJavaScriptLibrary(module);
public static Pair<List<String>, String> getLibLocationAndTargetForProject(final Module module) {
final Set<String> pathsToJSLib = Sets.newHashSet();
if (library != null) {
List<String> pathsToJSLib = Lists.newArrayList();
VirtualFile[] files = library.getRootProvider().getFiles(OrderRootType.SOURCES);
for (VirtualFile file : files) {
pathsToJSLib.add(com.intellij.util.PathUtil.getLocalPath(file));
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 (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());
}
else {
throw new IllegalStateException("Should be called for JS module only. JS library is expected to be found");
}
return Pair.<List<String>, String>create(Lists.newArrayList(pathsToJSLib), EcmaVersion.defaultVersion().toString());
}
@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();
}
}