added a simple little CompilerPlugin interface and a loosely coupled loader of KDoc if its configured (via -docOutput) and its on the classpath then it can be used at the same stage as a compile & refactored KDoc to work with the compiler. Added a "docStdlib" goal to try out the kdoc on stdlib if the kdoc plugin has been built in kdoc/target/*.jar

This commit is contained in:
James Strachan
2012-02-22 13:28:08 +00:00
parent 10b2bd089c
commit e05d299874
9 changed files with 231 additions and 38 deletions
@@ -17,38 +17,30 @@
*/
package org.jetbrains.jet.cli;
import com.google.common.base.Predicates;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.compiler.CompilerPlugin;
import org.jetbrains.jet.compiler.JetCoreEnvironment;
import org.jetbrains.jet.compiler.JetFileProcessor;
import org.jetbrains.jet.lang.Configuration;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
import org.jetbrains.jet.lang.resolve.java.JavaBridgeConfiguration;
import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.jet.resolve.DescriptorRenderer;
import java.io.File;
import java.lang.reflect.Constructor;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* A simple facade to auto-detect the KDoc processor if its available on the classpath
*/
public class KDocProcessor implements JetFileProcessor {
public class KDocProcessor implements CompilerPlugin {
private final String outputDir;
@@ -56,11 +48,50 @@ public class KDocProcessor implements JetFileProcessor {
this.outputDir = outputDir;
}
public CompilerPlugin createCompilerPlugin() {
// lets see if we can see the KDoc class
String name = "org.jetbrains.kotlin.doc.KDoc";
Class<?> aClass = null;
try {
aClass = loadClass(name);
} catch (ClassNotFoundException e) {
System.out.println("Could not find class: " + name);
return null;
}
if (aClass != null) {
try {
File dir = new File(outputDir);
Constructor<?> constructor = aClass.getConstructor(File.class);
if (constructor != null) {
return (CompilerPlugin) constructor.newInstance(dir);
}
} catch (Exception e) {
System.out.println("Failed to create Processor: " + e);
}
}
return null;
}
public static Class<?> loadClass(String name) throws ClassNotFoundException {
try {
return Class.forName(name);
} catch (ClassNotFoundException e) {
try {
return Thread.currentThread().getContextClassLoader().loadClass(name);
} catch (ClassNotFoundException e1) {
return KDocProcessor.class.getClassLoader().loadClass(name);
}
}
}
@Override
public void processFiles(List<JetFile> sources) {
public void processFiles(BindingContext context, List<JetFile> sources) {
/*
// JetFile's are PSI (Program Source Interface) classes, i.e. they contain the concrete syntax trees of files
if (sources.isEmpty()) return;
// Let's perform the semantic analysis
Project project = sources.get(0).getProject();
Configuration javaBridgeConfiguration = JavaBridgeConfiguration.createJavaBridgeConfiguration(project, new BindingTraceContext(), Configuration.EMPTY);
@@ -82,6 +113,7 @@ public class KDocProcessor implements JetFileProcessor {
}
// TODO fire up the KDoc processor here...
*/
}
private void processDescriptors(Collection<DeclarationDescriptor> allDescriptors, BindingContext context) {
@@ -125,6 +157,6 @@ public class KDocProcessor implements JetFileProcessor {
});
List<JetFile> files = Lists.newArrayList();
files.add(JetPsiFactory.createFile(jetCoreEnvironment.getProject(), "package a;/**sdfsdf*/class A {fun foo() {} /**doc*/ fun bar() {}}"));
new KDocProcessor("").processFiles(files);
new KDocProcessor("").processFiles(null, files);
}
}
@@ -21,6 +21,7 @@ import com.sampullara.cli.Argument;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.compiler.CompileEnvironment;
import org.jetbrains.jet.compiler.CompileEnvironmentException;
import org.jetbrains.jet.compiler.CompilerPlugin;
import org.jetbrains.jet.compiler.FileNameTransformer;
import java.io.ByteArrayOutputStream;
@@ -127,9 +128,13 @@ public class KotlinCompiler {
environment.setErrorStream(errStream);
}
// if (arguments.docOutputDir != null) {
// environment.getMyEnvironment().getFileProcessors().add(new KDocProcessor(arguments.docOutputDir));
// }
if (arguments.docOutputDir != null) {
KDocProcessor factory = new KDocProcessor(arguments.docOutputDir);
CompilerPlugin processor = factory.createCompilerPlugin();
if (processor != null) {
environment.getMyEnvironment().getCompilerPlugins().add(processor);
}
}
if (arguments.stdlib != null) {
environment.setStdlib(arguments.stdlib);
@@ -204,10 +204,10 @@ public class CompileSession {
generationState.compileCorrectFiles(myBindingContext, mySourceFiles);
ClassFileFactory answer = generationState.getFactory();
List<JetFileProcessor> fileProcessors = myEnvironment.getFileProcessors();
List<CompilerPlugin> fileProcessors = myEnvironment.getCompilerPlugins();
if (fileProcessors != null) {
for (JetFileProcessor processor : fileProcessors) {
processor.processFiles(getSourceFileNamespaces());
for (CompilerPlugin processor : fileProcessors) {
processor.processFiles(myBindingContext, getSourceFileNamespaces());
}
}
return answer;
@@ -18,11 +18,14 @@
package org.jetbrains.jet.compiler;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.BindingContext;
import java.util.List;
/**
* A simple interface for compiler plugins to run after the compiler has finished such as for things like
* generating documentation or code generation etc
*/
public interface JetFileProcessor {
void processFiles(List<JetFile> sources);
public interface CompilerPlugin {
void processFiles(BindingContext context, List<JetFile> sources);
}
@@ -33,7 +33,7 @@ import java.util.List;
* @author yole
*/
public class JetCoreEnvironment extends JavaCoreEnvironment {
private List<JetFileProcessor> fileProcessors = new ArrayList<JetFileProcessor>();
private List<CompilerPlugin> compilerPlugins = new ArrayList<CompilerPlugin>();
public JetCoreEnvironment(Disposable parentDisposable) {
super(parentDisposable);
@@ -61,11 +61,11 @@ public class JetCoreEnvironment extends JavaCoreEnvironment {
return myApplication;
}
public List<JetFileProcessor> getFileProcessors() {
return fileProcessors;
public List<CompilerPlugin> getCompilerPlugins() {
return compilerPlugins;
}
public void setFileProcessors(List<JetFileProcessor> fileProcessors) {
this.fileProcessors = fileProcessors;
public void setCompilerPlugins(List<CompilerPlugin> compilerPlugins) {
this.compilerPlugins = compilerPlugins;
}
}