mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Groovy Shell: support classes
This commit is contained in:
+54
-4
@@ -27,7 +27,9 @@ import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.debugger.fragments.GroovyCodeFragment;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable;
|
||||
import org.jetbrains.plugins.groovy.lang.resolve.processors.ClassHint;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@@ -36,15 +38,20 @@ import java.util.Map;
|
||||
*/
|
||||
public class GroovyShellCodeFragment extends GroovyCodeFragment {
|
||||
private final Map<String, PsiVariable> myVariables = ContainerUtil.newHashMap();
|
||||
|
||||
public GroovyShellCodeFragment(Project project, CharSequence text) {
|
||||
super(project, text);
|
||||
}
|
||||
private final Map<String, GrTypeDefinition> myTypeDefinitions = ContainerUtil.newHashMap();
|
||||
|
||||
public GroovyShellCodeFragment(Project project, LightVirtualFile virtualFile) {
|
||||
super(project, virtualFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GroovyCodeFragment clone() {
|
||||
GroovyShellCodeFragment clone = (GroovyShellCodeFragment)super.clone();
|
||||
clone.myVariables.putAll(myVariables);
|
||||
clone.myTypeDefinitions.putAll(myTypeDefinitions);
|
||||
return clone;
|
||||
}
|
||||
|
||||
|
||||
public void addVariable(String name, GrExpression expr) {
|
||||
myVariables.put(name, new GrLightVariable(getManager(), name, expr.getType(), this));
|
||||
@@ -67,10 +74,21 @@ public class GroovyShellCodeFragment extends GroovyCodeFragment {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!processTypeDefinitions(processor, state)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean processVariables(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state) {
|
||||
ClassHint classHint = processor.getHint(ClassHint.KEY);
|
||||
if (classHint != null &&
|
||||
!classHint.shouldProcess(ClassHint.ResolveKind.METHOD) &&
|
||||
!classHint.shouldProcess(ClassHint.ResolveKind.PROPERTY)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
NameHint nameHint = processor.getHint(NameHint.KEY);
|
||||
String name = nameHint != null ? nameHint.getName(state) : null;
|
||||
|
||||
@@ -92,4 +110,36 @@ public class GroovyShellCodeFragment extends GroovyCodeFragment {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean processTypeDefinitions(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state) {
|
||||
ClassHint classHint = processor.getHint(ClassHint.KEY);
|
||||
if (classHint != null && !classHint.shouldProcess(ClassHint.ResolveKind.CLASS)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
NameHint nameHint = processor.getHint(NameHint.KEY);
|
||||
String name = nameHint != null ? nameHint.getName(state) : null;
|
||||
|
||||
if (name != null) {
|
||||
final GrTypeDefinition definition = myTypeDefinitions.get(name);
|
||||
if (definition != null) {
|
||||
if (processor.execute(definition, state)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
for (GrTypeDefinition definition : myTypeDefinitions.values()) {
|
||||
if (!processor.execute(definition, state)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void addTypeDefinition(GrTypeDefinition typeDefinition) {
|
||||
myTypeDefinitions.put(typeDefinition.getName(), typeDefinition);
|
||||
}
|
||||
}
|
||||
|
||||
+15
-2
@@ -26,12 +26,14 @@ import com.intellij.testFramework.LightVirtualFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.plugins.groovy.GroovyFileType;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement;
|
||||
@@ -53,7 +55,7 @@ public class GroovyShellConsoleImpl extends LanguageConsoleImpl {
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected String addToHistoryInner(TextRange textRange, EditorEx editor, boolean erase, boolean preserveMarkup) {
|
||||
protected String addToHistoryInner(@NotNull TextRange textRange, @NotNull EditorEx editor, boolean erase, boolean preserveMarkup) {
|
||||
final String result = super.addToHistoryInner(textRange, editor, erase, preserveMarkup);
|
||||
|
||||
GroovyShellCodeFragment codeFragment = (GroovyShellCodeFragment)myFile;
|
||||
@@ -73,6 +75,9 @@ public class GroovyShellConsoleImpl extends LanguageConsoleImpl {
|
||||
codeFragment.addVariable(((GrReferenceExpression)left).getReferenceName(), assignment.getRValue());
|
||||
}
|
||||
}
|
||||
else if (statement instanceof GrTypeDefinition) {
|
||||
codeFragment.addTypeDefinition(prepareTypeDefinition((GrTypeDefinition)statement));
|
||||
}
|
||||
}
|
||||
|
||||
PsiType scriptType = ((GroovyShellCodeFragment)myFile).getInferredScriptReturnType();
|
||||
@@ -83,7 +88,15 @@ public class GroovyShellConsoleImpl extends LanguageConsoleImpl {
|
||||
return result;
|
||||
}
|
||||
|
||||
private GrClosableBlock generateClosure(GrMethod method) {
|
||||
@NotNull
|
||||
private GrTypeDefinition prepareTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
|
||||
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
|
||||
GroovyFile file = factory.createGroovyFile("", false, myFile);
|
||||
return (GrTypeDefinition)file.add(typeDefinition);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private GrClosableBlock generateClosure(@NotNull GrMethod method) {
|
||||
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user