mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
refactored & javadoc added
This commit is contained in:
@@ -131,7 +131,7 @@ public class RootsDetectionStep extends AbstractStepWithProgress<List<DetectedRo
|
||||
}
|
||||
myBuilder.setProjectRoots(roots);
|
||||
for (ProjectStructureDetector detector : roots.keySet()) {
|
||||
detector.setupProjectStructure(roots.get(detector), myBuilder);
|
||||
detector.setupProjectStructure(roots.get(detector), myBuilder.getProjectDescriptor(detector), myBuilder);
|
||||
}
|
||||
updateSelectedTypes();
|
||||
}
|
||||
|
||||
+4
-3
@@ -27,6 +27,7 @@ import com.intellij.psi.JavaTokenType;
|
||||
import com.intellij.psi.impl.source.tree.ElementType;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.tree.TokenSet;
|
||||
import com.intellij.util.NullableFunction;
|
||||
import com.intellij.util.StringBuilderSpinAllocator;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.text.CharArrayCharSequence;
|
||||
@@ -77,7 +78,7 @@ public class JavaSourceRootDetectionUtil {
|
||||
|
||||
|
||||
@Nullable
|
||||
public static Pair<File,String> suggestRootForJavaFile(File javaFile, File topmostPossibleRoot) {
|
||||
public static Pair<File,String> suggestRootForJavaFile(File javaFile, File topmostPossibleRoot, NullableFunction<CharSequence, String> packageNameFetcher) {
|
||||
if (!javaFile.isFile()) return null;
|
||||
|
||||
final CharSequence chars;
|
||||
@@ -88,7 +89,7 @@ public class JavaSourceRootDetectionUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
String packageName = getPackageStatement(chars);
|
||||
String packageName = packageNameFetcher.fun(chars);
|
||||
if (packageName != null) {
|
||||
File root = javaFile.getParentFile();
|
||||
int index = packageName.length();
|
||||
@@ -114,7 +115,7 @@ public class JavaSourceRootDetectionUtil {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getPackageStatement(CharSequence text) {
|
||||
public static String getPackageName(CharSequence text) {
|
||||
Lexer lexer = new JavaLexer(LanguageLevel.JDK_1_3);
|
||||
lexer.start(text);
|
||||
skipWhiteSpaceAndComments(lexer);
|
||||
|
||||
+7
-1
@@ -17,6 +17,7 @@ package com.intellij.ide.util.projectWizard.importSources;
|
||||
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.util.NullableFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
@@ -35,7 +36,7 @@ public abstract class JavaSourceRootDetector extends ProjectStructureDetector {
|
||||
if (child.isFile()) {
|
||||
String extension = FileUtil.getExtension(child.getName());
|
||||
if (extension.equals(fileExtension)) {
|
||||
Pair<File, String> root = JavaSourceRootDetectionUtil.suggestRootForJavaFile(child, base);
|
||||
Pair<File, String> root = JavaSourceRootDetectionUtil.suggestRootForJavaFile(child, base, getPackageNameFetcher());
|
||||
if (root != null) {
|
||||
result.add(new JavaModuleSourceRoot(root.getFirst(), root.getSecond(), getLanguageName()));
|
||||
return DirectoryProcessingResult.skipChildrenAndParentsUpTo(root.getFirst());
|
||||
@@ -49,7 +50,12 @@ public abstract class JavaSourceRootDetector extends ProjectStructureDetector {
|
||||
return DirectoryProcessingResult.PROCESS_CHILDREN;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected abstract String getLanguageName();
|
||||
|
||||
@NotNull
|
||||
protected abstract String getFileExtension();
|
||||
|
||||
@NotNull
|
||||
protected abstract NullableFunction<CharSequence, String> getPackageNameFetcher();
|
||||
}
|
||||
|
||||
+31
-1
@@ -28,20 +28,50 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Override this class to provide automatic detection of modules and libraries for 'Create from existing sources' mode of
|
||||
* the new project/module wizard.
|
||||
*
|
||||
* <p>
|
||||
* The implementation should be registered in your {@code plugin.xml}:
|
||||
* <pre>
|
||||
* <extensions defaultExtensionNs="com.intellij">
|
||||
* <projectStructureDetector implementation="qualified-class-name"/>
|
||||
* </extensions>
|
||||
* </pre>
|
||||
*
|
||||
* @author nik
|
||||
*/
|
||||
public abstract class ProjectStructureDetector {
|
||||
public static final ExtensionPointName<ProjectStructureDetector> EP_NAME = ExtensionPointName.create("com.intellij.projectStructureDetector");
|
||||
|
||||
/**
|
||||
* This methods is called recursively for all directories under the selected root directory.
|
||||
* @param dir current directory
|
||||
* @param children its children
|
||||
* @param base the root directory in which project files are detected. All detected roots must be located under this directory.
|
||||
* @param result list of detected roots
|
||||
* @return
|
||||
* <li>{@link DirectoryProcessingResult#PROCESS_CHILDREN} if children of {@code dir} need to be processed by this detector</li>
|
||||
* <li>{@link DirectoryProcessingResult#SKIP_CHILDREN} to skip all children of {@code dir} from processing</li>
|
||||
* <li><code>{@link DirectoryProcessingResult#skipChildrenAndParentsUpTo}(parent)</code> to skip all directories under {@code parent} from processing.
|
||||
* {@code parent} must be an ancestor of {@code dir} or {@code dir} itself
|
||||
* </li>
|
||||
*/
|
||||
@NotNull
|
||||
public abstract DirectoryProcessingResult detectRoots(@NotNull File dir, @NotNull File[] children, @NotNull File base,
|
||||
@NotNull List<DetectedProjectRoot> result);
|
||||
|
||||
/**
|
||||
* Return additional wizard steps which will be shown if some roots are detected by this detector
|
||||
*/
|
||||
public List<ModuleWizardStep> createWizardSteps(ProjectFromSourcesBuilder builder, ProjectDescriptor projectDescriptor, Icon stepIcon) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
public void setupProjectStructure(@NotNull Collection<DetectedProjectRoot> roots,
|
||||
/**
|
||||
* Setup modules and libraries for the selected roots
|
||||
*/
|
||||
public void setupProjectStructure(@NotNull Collection<DetectedProjectRoot> roots, @NotNull ProjectDescriptor projectDescriptor,
|
||||
@NotNull ProjectFromSourcesBuilder builder) {
|
||||
}
|
||||
|
||||
|
||||
+15
@@ -23,8 +23,11 @@ import com.intellij.ide.util.importProject.ModulesDetectionStep;
|
||||
import com.intellij.ide.util.importProject.ProjectDescriptor;
|
||||
import com.intellij.ide.util.projectWizard.ModuleWizardStep;
|
||||
import com.intellij.ide.util.projectWizard.ProjectWizardStepFactory;
|
||||
import com.intellij.ide.util.projectWizard.importSources.JavaSourceRootDetectionUtil;
|
||||
import com.intellij.ide.util.projectWizard.importSources.JavaSourceRootDetector;
|
||||
import com.intellij.ide.util.projectWizard.importSources.ProjectFromSourcesBuilder;
|
||||
import com.intellij.util.NullableFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.ArrayList;
|
||||
@@ -35,11 +38,13 @@ import java.util.List;
|
||||
*/
|
||||
public class JavaProjectStructureDetector extends JavaSourceRootDetector {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getLanguageName() {
|
||||
return "Java";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getFileExtension() {
|
||||
return JavaFileType.DEFAULT_EXTENSION;
|
||||
@@ -56,4 +61,14 @@ public class JavaProjectStructureDetector extends JavaSourceRootDetector {
|
||||
steps.add(ProjectWizardStepFactory.getInstance().createProjectJdkStep(builder.getContext()));
|
||||
return steps;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected NullableFunction<CharSequence, String> getPackageNameFetcher() {
|
||||
return new NullableFunction<CharSequence, String>() {
|
||||
@Override
|
||||
public String fun(CharSequence charSequence) {
|
||||
return JavaSourceRootDetectionUtil.getPackageName(charSequence);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -123,7 +123,7 @@ public class JavaVfsSourceRootDetectionUtil {
|
||||
|
||||
CharSequence chars = LoadTextUtil.loadText(javaFile);
|
||||
|
||||
String packageName = JavaSourceRootDetectionUtil.getPackageStatement(chars);
|
||||
String packageName = JavaSourceRootDetectionUtil.getPackageName(chars);
|
||||
if (packageName != null){
|
||||
VirtualFile root = javaFile.getParent();
|
||||
int index = packageName.length();
|
||||
|
||||
@@ -15,19 +15,35 @@
|
||||
*/
|
||||
package org.jetbrains.plugins.groovy;
|
||||
|
||||
import com.intellij.ide.util.projectWizard.importSources.JavaSourceRootDetectionUtil;
|
||||
import com.intellij.ide.util.projectWizard.importSources.JavaSourceRootDetector;
|
||||
import com.intellij.util.NullableFunction;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author nik
|
||||
*/
|
||||
public class GroovySourceRootDetector extends JavaSourceRootDetector {
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getLanguageName() {
|
||||
return "Groovy";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String getFileExtension() {
|
||||
return GroovyFileType.DEFAULT_EXTENSION;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected NullableFunction<CharSequence, String> getPackageNameFetcher() {
|
||||
return new NullableFunction<CharSequence, String>() {
|
||||
@Override
|
||||
public String fun(CharSequence charSequence) {
|
||||
//todo[nik] implement groovy-specific package name parsing?
|
||||
return JavaSourceRootDetectionUtil.getPackageName(charSequence);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user