mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Groovy shell: support commands
This commit is contained in:
@@ -363,6 +363,8 @@
|
||||
id="groovyBasic2ClassName" order="first, after liveTemplates"/>
|
||||
<completion.contributor language="Groovy" implementationClass="org.jetbrains.plugins.groovy.lang.completion.GroovyCompletionContributor"
|
||||
id="groovyBasic" order="before javaClassName"/>
|
||||
<completion.contributor language="Groovy" implementationClass="org.jetbrains.plugins.groovy.console.GroovyShellCompletionContributor"/>
|
||||
|
||||
<completion.contributor language="Groovy"
|
||||
implementationClass="org.jetbrains.plugins.groovy.lang.completion.GroovySmartCompletionContributor"/>
|
||||
<completion.contributor language="Groovy" implementationClass="org.jetbrains.plugins.groovy.lang.completion.GrMethodMergingContributor"
|
||||
|
||||
@@ -142,4 +142,12 @@ public class GroovyShellCodeFragment extends GroovyCodeFragment {
|
||||
public void addTypeDefinition(GrTypeDefinition typeDefinition) {
|
||||
myTypeDefinitions.put(typeDefinition.getName(), typeDefinition);
|
||||
}
|
||||
|
||||
public void clearVariables() {
|
||||
myVariables.clear();
|
||||
}
|
||||
|
||||
public void clearClasses() {
|
||||
myTypeDefinitions.clear();
|
||||
}
|
||||
}
|
||||
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright 2000-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.plugins.groovy.console;
|
||||
|
||||
import com.intellij.codeInsight.completion.CompletionContributor;
|
||||
import com.intellij.codeInsight.completion.CompletionParameters;
|
||||
import com.intellij.codeInsight.completion.CompletionResultSet;
|
||||
import com.intellij.codeInsight.lookup.LookupElementBuilder;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression;
|
||||
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
|
||||
|
||||
/**
|
||||
* Created by Max Medvedev on 9/8/13
|
||||
*/
|
||||
public class GroovyShellCompletionContributor extends CompletionContributor {
|
||||
@Override
|
||||
public void fillCompletionVariants(CompletionParameters parameters, CompletionResultSet result) {
|
||||
PsiFile file = parameters.getOriginalFile();
|
||||
if (!(file instanceof GroovyShellCodeFragment)) return;
|
||||
|
||||
PsiElement position = parameters.getPosition();
|
||||
PsiElement parent = position.getParent();
|
||||
|
||||
if (!(parent instanceof GrReferenceExpression && !((GrReferenceExpression)parent).isQualified())) return;
|
||||
|
||||
if (PsiUtil.isExpressionStatement(parent)) {
|
||||
addAllCommands(result);
|
||||
}
|
||||
else if (parent.getParent() instanceof GrCommandArgumentList) {
|
||||
PsiElement ppparent = parent.getParent().getParent();
|
||||
if (ppparent instanceof GrMethodCall && isFirstArg((GrMethodCall)ppparent, parent)) {
|
||||
GrExpression invokedExpression = ((GrMethodCall)ppparent).getInvokedExpression();
|
||||
if (invokedExpression instanceof GrReferenceExpression && !((GrReferenceExpression)invokedExpression).isQualified()) {
|
||||
String name = ((GrReferenceExpression)invokedExpression).getReferenceName();
|
||||
|
||||
if ("help".equals(name)) {
|
||||
addAllCommands(result);
|
||||
}
|
||||
else if ("show".equals(name)) {
|
||||
add(result, "classes");
|
||||
add(result, "imports");
|
||||
add(result, "preferences");
|
||||
add(result, "all");
|
||||
}
|
||||
else if ("purge".equals(name)) {
|
||||
add(result, "variables");
|
||||
add(result, "classes");
|
||||
add(result, "imports");
|
||||
add(result, "preferences");
|
||||
add(result, "all");
|
||||
}
|
||||
else if ("record".equals(name)) {
|
||||
add(result, "start");
|
||||
add(result, "stop");
|
||||
add(result, "status");
|
||||
}
|
||||
else if ("history".equals(name)) {
|
||||
add(result, "show");
|
||||
add(result, "recall");
|
||||
add(result, "flush");
|
||||
add(result, "clear");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void add(@NotNull CompletionResultSet result, @NotNull String name) {
|
||||
result.addElement(LookupElementBuilder.create(name));
|
||||
}
|
||||
|
||||
private static void addAllCommands(@NotNull CompletionResultSet result) {
|
||||
add(result, "help");
|
||||
add(result, "exit");
|
||||
add(result, "quit");
|
||||
add(result, "display");
|
||||
add(result, "clear");
|
||||
add(result, "show");
|
||||
add(result, "inspect");
|
||||
add(result, "purge");
|
||||
add(result, "edit");
|
||||
add(result, "load");
|
||||
add(result, "save");
|
||||
add(result, "record");
|
||||
add(result, "history");
|
||||
add(result, "alias");
|
||||
add(result, "set");
|
||||
}
|
||||
|
||||
private static boolean isFirstArg(@NotNull GrMethodCall ppparent, @NotNull PsiElement parent) {
|
||||
GroovyPsiElement[] arguments = ppparent.getArgumentList().getAllArguments();
|
||||
return arguments.length > 0 && arguments[0] == parent;
|
||||
}
|
||||
}
|
||||
+44
-10
@@ -58,34 +58,68 @@ public class GroovyShellConsoleImpl extends LanguageConsoleImpl {
|
||||
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;
|
||||
GrTopStatement[] definitions = codeFragment.getTopStatements();
|
||||
if ("purge variables".equals(result.trim())) {
|
||||
clearVariables();
|
||||
}
|
||||
else if ("purge classes".equals(result.trim())) {
|
||||
clearClasses();
|
||||
}
|
||||
else if ("purge imports".equals(result.trim())) {
|
||||
clearImports();
|
||||
}
|
||||
else if ("purge all".equals(result.trim())) {
|
||||
clearVariables();
|
||||
clearClasses();
|
||||
clearImports();
|
||||
}
|
||||
else {
|
||||
processCode();
|
||||
}
|
||||
|
||||
for (GrTopStatement statement : definitions) {
|
||||
return result;
|
||||
}
|
||||
|
||||
private void processCode() {
|
||||
for (GrTopStatement statement : getFile().getTopStatements()) {
|
||||
if (statement instanceof GrImportStatement) {
|
||||
codeFragment.addImportsFromString(importToString((GrImportStatement)statement));
|
||||
getFile().addImportsFromString(importToString((GrImportStatement)statement));
|
||||
}
|
||||
else if (statement instanceof GrMethod) {
|
||||
codeFragment.addVariable(((GrMethod)statement).getName(), generateClosure((GrMethod)statement));
|
||||
getFile().addVariable(((GrMethod)statement).getName(), generateClosure((GrMethod)statement));
|
||||
}
|
||||
else if (statement instanceof GrAssignmentExpression) {
|
||||
GrAssignmentExpression assignment = (GrAssignmentExpression)statement;
|
||||
GrExpression left = assignment.getLValue();
|
||||
if (left instanceof GrReferenceExpression && !((GrReferenceExpression)left).isQualified()) {
|
||||
codeFragment.addVariable(((GrReferenceExpression)left).getReferenceName(), assignment.getRValue());
|
||||
getFile().addVariable(((GrReferenceExpression)left).getReferenceName(), assignment.getRValue());
|
||||
}
|
||||
}
|
||||
else if (statement instanceof GrTypeDefinition) {
|
||||
codeFragment.addTypeDefinition(prepareTypeDefinition((GrTypeDefinition)statement));
|
||||
getFile().addTypeDefinition(prepareTypeDefinition((GrTypeDefinition)statement));
|
||||
}
|
||||
}
|
||||
|
||||
PsiType scriptType = ((GroovyShellCodeFragment)myFile).getInferredScriptReturnType();
|
||||
PsiType scriptType = getFile().getInferredScriptReturnType();
|
||||
if (scriptType != null) {
|
||||
codeFragment.addVariable("_", scriptType);
|
||||
getFile().addVariable("_", scriptType);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
@NotNull
|
||||
public GroovyShellCodeFragment getFile() {
|
||||
return (GroovyShellCodeFragment)myFile;
|
||||
}
|
||||
|
||||
private void clearVariables() {
|
||||
getFile().clearVariables();
|
||||
}
|
||||
|
||||
private void clearClasses() {
|
||||
getFile().clearClasses();
|
||||
}
|
||||
|
||||
private void clearImports() {
|
||||
getFile().clearImports();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
+5
@@ -285,4 +285,9 @@ public class GroovyCodeFragment extends GroovyFileImpl implements JavaCodeFragme
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void clearImports() {
|
||||
myPseudoImports.clear();
|
||||
myOnDemandImports.clear();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user