Merge remote-tracking branch 'origin/master'

This commit is contained in:
Vladimir.Orlov
2016-11-01 19:50:11 +03:00
33 changed files with 705 additions and 328 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
<component name="libraryTable">
<library name="SceneBuilderKit">
<CLASSES>
<root url="jar://$PROJECT_DIR$/plugins/javaFX/lib/SceneBuilderKit-8.1.1.jar!/" />
<root url="jar://$PROJECT_DIR$/plugins/javaFX/lib/SceneBuilderKit-8.2.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
@@ -15,6 +15,7 @@
*/
package org.jetbrains.jps.backwardRefs;
import com.intellij.openapi.util.LowMemoryWatcher;
import com.intellij.util.Function;
import com.intellij.util.SystemProperties;
import com.intellij.util.containers.ContainerUtil;
@@ -34,7 +35,10 @@ import org.jetbrains.jps.model.java.compiler.JavaCompilers;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import static com.sun.tools.javac.code.Flags.PRIVATE;
@@ -45,10 +49,23 @@ public class BackwardReferenceIndexWriter {
private final CompilerBackwardReferenceIndex myIndex;
private final boolean myRebuild;
private final LowMemoryWatcher myMemWatcher;
private final Object myCloseLock = new Object();
private boolean myClosed;
private BackwardReferenceIndexWriter(CompilerBackwardReferenceIndex index, boolean rebuild) {
myIndex = index;
myRebuild = rebuild;
myMemWatcher = LowMemoryWatcher.register(new Runnable() {
@Override
public void run() {
synchronized (myCloseLock) {
if (!myClosed) {
myIndex.flush();
}
}
}
});
}
public static void closeIfNeed() {
@@ -94,10 +111,6 @@ public class BackwardReferenceIndexWriter {
return SystemProperties.getBooleanProperty(PROP_KEY, false);
}
void close() {
myIndex.close();
}
synchronized LightRef.JavaLightClassRef asClassUsage(Symbol name) {
return new LightRef.JavaLightClassRef(myIndex.getByteSeqEum().enumerate(bytes(name)));
}
@@ -207,6 +220,14 @@ public class BackwardReferenceIndexWriter {
}
}
private void close() {
synchronized (myCloseLock) {
myClosed = true;
myMemWatcher.stop();
myIndex.close();
}
}
private void directlyWriteHierarchyIndices(CompilerBackwardReferenceIndex.LightDefinition classId, LightRef.JavaLightClassRef[] superIds) {
for (LightRef.JavaLightClassRef superId : superIds) {
myIndex.getBackwardHierarchyMap().put(superId, classId);
@@ -203,6 +203,17 @@ public class CompilerBackwardReferenceIndex {
}
}
void flush() {
myBackwardHierarchyMap.flush(false);
myBackwardReferenceMap.flush(false);
myBackwardClassDefinitionMap.flush(false);
myHierarchyMap.flush(false);
myReferenceMap.flush(false);
myClassDefinitionMap.flush(false);
myNameEnumerator.force();
myFilePathEnumerator.force();
}
public static void removeIndexFiles(File buildDir) {
final File indexDir = getIndexDir(buildDir);
if (indexDir.exists()) {
@@ -40,8 +40,8 @@ import static com.intellij.util.concurrency.QueueProcessor.ThreadToUse;
*/
@SomeQueue
public class BackgroundTaskQueue {
@NotNull private final String myTitle;
@NotNull private final QueueProcessor<TaskData> myProcessor;
@NotNull protected final String myTitle;
@NotNull protected final QueueProcessor<TaskData> myProcessor;
@NotNull private final Object TEST_TASK_LOCK = new Object();
private volatile boolean myForceAsyncInTests = false;
@@ -110,10 +110,10 @@ public class BackgroundTaskQueue {
}
}
private interface TaskData extends Consumer<Runnable> {
protected interface TaskData extends Consumer<Runnable> {
}
private class BackgroundableTaskData implements TaskData {
protected class BackgroundableTaskData implements TaskData {
@NotNull private final Task.Backgroundable myTask;
@Nullable private final ModalityState myModalityState;
@Nullable private final ProgressIndicator myIndicator;
@@ -84,6 +84,8 @@ public class WelcomeBalloonLayoutImpl extends BalloonLayoutImpl {
}
private void addToPopup(@NotNull BalloonImpl balloon, @NotNull BalloonLayoutData layoutData) {
balloon.traceDispose(false);
layoutData.doLayout = this::layoutPopup;
layoutData.configuration = layoutData.configuration.replace(JBUI.scale(myPopupBalloon == null ? 7 : 5), JBUI.scale(12));
@@ -57,6 +57,8 @@
<completion.contributor language="TEXT" implementationClass="com.intellij.openapi.vcs.CommitCompletionContributor" id="commitCompletion"
order="first, before liveTemplates"/>
<completion.contributor language="TEXT" implementationClass="com.intellij.openapi.vcs.changes.ChangeListCompletionContributor"
order="first"/>
<vcs.fileStatusProvider implementation="com.intellij.openapi.vcs.changes.conflicts.ChangelistConflictFileStatusProvider"/>
<writingAccessProvider implementation="com.intellij.openapi.vcs.changes.conflicts.ChangelistConflictAccessProvider"/>
@@ -33,6 +33,7 @@ public class GeneralIdBasedToSMTRunnerEventsConvertor extends GeneralTestEventsP
private final HashMap<String, Node> myNodeByIdMap = new HashMap<>();
private final Set<Node> myRunningTestNodes = ContainerUtil.newHashSet();
private final Set<Node> myRunningSuiteNodes = ContainerUtil.newHashSet();
private final SMTestProxy.SMRootTestProxy myTestsRootProxy;
private final Node myTestsRootNode;
@@ -90,6 +91,7 @@ public class GeneralIdBasedToSMTRunnerEventsConvertor extends GeneralTestEventsP
}
myNodeByIdMap.clear();
myRunningTestNodes.clear();
myRunningSuiteNodes.clear();
fireOnTestingFinished(myTestsRootProxy);
});
@@ -349,6 +351,7 @@ public class GeneralIdBasedToSMTRunnerEventsConvertor extends GeneralTestEventsP
}
}
myRunningTestNodes.clear();
myRunningSuiteNodes.clear();
myNodeByIdMap.clear();
});
}
@@ -360,6 +363,7 @@ public class GeneralIdBasedToSMTRunnerEventsConvertor extends GeneralTestEventsP
SMTestProxy proxy = node.getProxy();
proxy.setStarted();
if (proxy.isSuite()) {
myRunningSuiteNodes.add(node);
fireOnSuiteStarted(proxy);
} else {
myRunningTestNodes.add(lowestNode);
@@ -372,14 +376,18 @@ public class GeneralIdBasedToSMTRunnerEventsConvertor extends GeneralTestEventsP
private void terminateNode(@NotNull Node node, @NotNull State terminateState) {
node.setState(terminateState, this);
myRunningTestNodes.remove(node);
myRunningSuiteNodes.remove(node);
}
@NotNull
private Node findActiveNode() {
if (myRunningTestNodes.isEmpty()) {
return myTestsRootNode;
if (!myRunningTestNodes.isEmpty()) {
return myRunningTestNodes.iterator().next();
}
return myRunningTestNodes.iterator().next();
if (!myRunningSuiteNodes.isEmpty()) {
return myRunningSuiteNodes.iterator().next();
}
return myTestsRootNode;
}
@Override
@@ -15,17 +15,18 @@
*/
package com.intellij.openapi.vcs.actions;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.BackgroundTaskQueue;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.AbstractVcsHelper;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.util.Consumer;
import com.intellij.util.ThrowableConsumer;
import com.intellij.util.ThrowableRunnable;
import com.intellij.util.concurrency.QueueProcessor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@@ -34,24 +35,21 @@ import static com.intellij.util.containers.ContainerUtil.createLockFreeCopyOnWri
/**
* Provides common cancellation and error handling behaviour for group of dependent tasks.
*/
public class BackgroundTaskGroup {
public class BackgroundTaskGroup extends BackgroundTaskQueue {
private static final Logger LOG = Logger.getInstance(BackgroundTaskGroup.class);
@NotNull private final QueueProcessor<TaskData> myProcessor;
@NotNull private final List<VcsException> myExceptions = createLockFreeCopyOnWriteList();
@NotNull private final Project myProject;
@NotNull private final String myTitle;
public BackgroundTaskGroup(@NotNull Project project, @NotNull String title) {
super(project, title);
myProject = project;
myTitle = title;
myProcessor = new QueueProcessor<>(TaskData::consume, true, QueueProcessor.ThreadToUse.AWT, project.getDisposed());
}
private interface TaskData extends Consumer<Runnable> {
@Override
public void run(@NotNull Task.Backgroundable task, @Nullable ModalityState modalityState, @Nullable ProgressIndicator indicator) {
throw new UnsupportedOperationException();
}
public void runInBackground(@NotNull String title, @NotNull ThrowableConsumer<ProgressIndicator, VcsException> task) {
@@ -0,0 +1,53 @@
/*
* Copyright 2000-2016 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 com.intellij.openapi.vcs.changes;
import com.intellij.codeInsight.completion.CompletionContributor;
import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.codeInsight.completion.PlainPrefixMatcher;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.util.Key;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;
public class ChangeListCompletionContributor extends CompletionContributor implements DumbAware {
public static final Key<ComboBox<ChangeList>> COMBO_BOX_KEY = Key.create("CHANGELIST_COMBO_BOX");
@Override
public void fillCompletionVariants(@NotNull final CompletionParameters parameters, @NotNull final CompletionResultSet result) {
final PsiFile file = parameters.getOriginalFile();
final Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
if (document == null) return;
ComboBox comboBox = document.getUserData(COMBO_BOX_KEY);
if (comboBox == null) return;
final CompletionResultSet resultSet = result.withPrefixMatcher(new PlainPrefixMatcher(document.getText()));
for (int i = 0; i < comboBox.getItemCount(); i++) {
resultSet.addElement(LookupElementBuilder.create(comboBox.getItemAt(i)));
}
}
@Override
public boolean invokeAutoPopup(@NotNull PsiElement position, char typeChar) {
return true;
}
}
@@ -17,24 +17,21 @@ package com.intellij.openapi.vcs.changes.ui;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.editor.event.DocumentAdapter;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.fileTypes.PlainTextLanguage;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.changes.ChangeListCompletionContributor;
import com.intellij.openapi.vcs.VcsConfiguration;
import com.intellij.openapi.vcs.changes.ChangeList;
import com.intellij.openapi.vcs.changes.ChangeListManager;
import com.intellij.openapi.vcs.changes.LocalChangeList;
import com.intellij.openapi.vcs.changes.committed.CommittedChangeListRenderer;
import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.EditorTextField;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.ui.StringComboboxEditor;
import com.intellij.util.ArrayUtil;
import com.intellij.ui.*;
import com.intellij.util.NullableConsumer;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.CalledInAwt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -44,10 +41,6 @@ import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static com.intellij.codeInsight.completion.ComboEditorCompletionContributor.CONTINUE_RUN_COMPLETION;
public class ChangeListChooserPanel extends JPanel {
@@ -61,18 +54,18 @@ public class ChangeListChooserPanel extends JPanel {
public ChangeListChooserPanel(final Project project, @NotNull final NullableConsumer<String> okEnabledListener) {
super(new BorderLayout());
myProject = project;
myExistingListsCombo = new MyEditorComboBox(project);
myExistingListsCombo = new MyEditorComboBox();
myExistingListsCombo.setEditable(true);
myExistingListsCombo.setRenderer(new ColoredListCellRenderer<String>() {
myExistingListsCombo.setRenderer(new ColoredListCellRenderer<ChangeList>() {
@Override
protected void customizeCellRenderer(@NotNull JList<? extends String> list,
String value,
protected void customizeCellRenderer(@NotNull JList<? extends ChangeList> list,
ChangeList value,
int index,
boolean selected,
boolean hasFocus) {
if (value != null) {
String name = value;
LocalChangeList changeList = ChangeListManager.getInstance(myProject).findChangeList(name);
String name = value.getName();
int visibleWidth = getSize().width;
if (visibleWidth == 0) {
visibleWidth = MyEditorComboBox.PREF_WIDTH;
@@ -86,7 +79,7 @@ public class ChangeListChooserPanel extends JPanel {
name = truncated + " ..";
}
}
append(name, changeList != null && changeList.isDefault()
append(name, value instanceof LocalChangeList && ((LocalChangeList)value).isDefault()
? SimpleTextAttributes.REGULAR_BOLD_ATTRIBUTES
: SimpleTextAttributes.REGULAR_ATTRIBUTES);
}
@@ -141,20 +134,19 @@ public class ChangeListChooserPanel extends JPanel {
}
public void setChangeLists(Collection<? extends ChangeList> changeLists) {
List<String> changelistNames = ContainerUtil.map(changeLists, ChangeList::getName);
Collections.sort(changelistNames);
myExistingListsCombo.setModel(new DefaultComboBoxModel<>(ArrayUtil.toStringArray(changelistNames)));
myExistingListsCombo.setModel(new DefaultComboBoxModel<>(changeLists.toArray(new ChangeList[changeLists.size()])));
}
public void setSuggestedName(@NotNull String name) {
if (StringUtil.isEmptyOrSpaces(name)) return;
if (getExistingChangelistByName(name) != null) {
myExistingListsCombo.setSelectedItem(name);
LocalChangeList changelistByName = getExistingChangelistByName(name);
if (changelistByName != null) {
myExistingListsCombo.setSelectedItem(changelistByName);
}
else {
myNewNameSuggested = true;
if (VcsConfiguration.getInstance(myProject).PRESELECT_EXISTING_CHANGELIST) {
myExistingListsCombo.insertItemAt(name, 0);
myExistingListsCombo.insertItemAt(LocalChangeList.createEmptyChangeList(myProject, name), 0);
selectActiveChangeListIfExist();
}
else {
@@ -165,7 +157,7 @@ public class ChangeListChooserPanel extends JPanel {
}
private void selectActiveChangeListIfExist() {
myExistingListsCombo.setSelectedItem(ChangeListManager.getInstance(myProject).getDefaultChangeList().getName());
myExistingListsCombo.setSelectedItem(ChangeListManager.getInstance(myProject).getDefaultChangeList());
}
public void updateEnabled() {
@@ -210,7 +202,7 @@ public class ChangeListChooserPanel extends JPanel {
selectActiveChangeListIfExist();
}
else {
myExistingListsCombo.setSelectedItem(defaultSelection.getName());
myExistingListsCombo.setSelectedItem(defaultSelection);
}
updateDescription();
updateEnabled();
@@ -230,27 +222,33 @@ public class ChangeListChooserPanel extends JPanel {
}
public JComponent getPreferredFocusedComponent() {
return myExistingListsCombo;
return myExistingListsCombo.getEditorTextField();
}
private static class MyEditorComboBox extends ComboBox<String> {
private class MyEditorComboBox extends ComboBox<ChangeList> {
private static final int PREF_WIDTH = 200;
private final LanguageTextField myEditorTextField;
public MyEditorComboBox(Project project) {
public MyEditorComboBox() {
super(PREF_WIDTH);
setEditor(new StringComboboxEditor(project, FileTypes.PLAIN_TEXT, this) {
myEditorTextField = new LanguageTextField(PlainTextLanguage.INSTANCE, myProject, "");
JLabel label = new JLabel(" ");
myEditorTextField.addDocumentListener(new DocumentAdapter() {
@Override
protected void onEditorCreate(EditorEx editor) {
super.onEditorCreate(editor);
getDocument().putUserData(CONTINUE_RUN_COMPLETION, true);
public void documentChanged(DocumentEvent e) {
String changeListName = e.getDocument().getText();
label.setText(StringUtil.isEmptyOrSpaces(changeListName)
? " " : getExistingChangelistByName(changeListName) != null ? " Existing " : " New ");
}
});
ObjectUtils.assertNotNull(myEditorTextField.getDocument()).putUserData(ChangeListCompletionContributor.COMBO_BOX_KEY, this);
setEditor(new ComboBoxCompositeEditor<>(myEditorTextField, label));
}
@NotNull
private EditorTextField getEditorTextField() {
return ObjectUtils.assertNotNull((EditorTextField)getEditor().getEditorComponent());
return myEditorTextField;
}
}
}
@@ -17,7 +17,6 @@
package org.jetbrains.plugins.groovy.lang.parser;
import com.intellij.lang.ASTNode;
import com.intellij.psi.impl.source.tree.SharedImplUtil;
import com.intellij.psi.stubs.*;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NotNull;
@@ -37,7 +36,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMe
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameter;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList;
import org.jetbrains.plugins.groovy.lang.psi.impl.auxiliary.GrThrowsClauseImpl;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableDeclarationImpl;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.blocks.GrBlockImpl;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.blocks.GrClosableBlockImpl;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.blocks.GrOpenBlockImpl;
@@ -415,22 +413,11 @@ public interface GroovyElementTypes {
throw new UnsupportedOperationException("Not implemented");
}
};
EmptyStubElementType<GrVariableDeclaration> VARIABLE_DEFINITION =
new EmptyStubElementType<GrVariableDeclaration>("variable definitions", GroovyLanguage.INSTANCE) {
@Override
public GrVariableDeclaration createPsi(@NotNull EmptyStub stub) {
return new GrVariableDeclarationImpl(stub);
}
@Override
public boolean shouldCreateStub(ASTNode node) {
return SharedImplUtil.getParent(node) instanceof GrTypeDefinitionBody;
}
};
GrVariableDeclarationElementType VARIABLE_DEFINITION = new GrVariableDeclarationElementType();
GroovyElementType TUPLE_DECLARATION = new GroovyElementType("tuple declaration");
GroovyElementType TUPLE_EXPRESSION = new GroovyElementType("tuple expression");
GroovyElementType VARIABLE = new GroovyElementType("assigned variable");
GrVariableElementType VARIABLE = new GrVariableElementType();
//modifiers
GrStubElementType<GrModifierListStub, GrModifierList> MODIFIERS = new GrModifierListElementType("modifier list");
@@ -47,5 +47,5 @@ public interface GroovyFile extends GroovyFileBase {
PsiType getInferredScriptReturnType();
@NotNull
GrVariableDeclaration[] getAnnotatedScriptDeclarations();
GrVariableDeclaration[] getScriptDeclarations(boolean topLevel);
}
@@ -537,12 +537,8 @@ public class GroovyFileImpl extends GroovyFileBaseImpl implements GroovyFile {
@NotNull
@Override
public GrVariableDeclaration[] getAnnotatedScriptDeclarations() {
StubElement<?> stub = getStub();
if (stub instanceof GrFileStub) {
return ((GrFileStub)stub).getAnnotatedScriptDeclarations();
}
return PsiImplUtil.getAnnotatedScriptDesclarations(this);
public GrVariableDeclaration[] getScriptDeclarations(boolean topLevel) {
return PsiImplUtilKt.getScriptDeclarations(this, topLevel);
}
@Override
@@ -36,7 +36,6 @@ import com.intellij.reference.SoftReference;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ReflectionUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.text.StringFactory;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -70,7 +69,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrI
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrReferenceList;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod;
@@ -1024,30 +1022,4 @@ public class PsiImplUtil {
substitutor, result.isAccessible(), result.isStaticsOK(),
result.isInvokedOnProperty(), result.isValidResult());
}
@NotNull
static GrVariableDeclaration[] getAnnotatedScriptDesclarations(@NotNull GroovyFile file) {
if (!file.isScript()) return GrVariableDeclaration.EMPTY_ARRAY;
List<GrVariableDeclaration> result = ContainerUtil.newArrayList();
file.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitVariableDeclaration(@NotNull GrVariableDeclaration declaration) {
if (declaration.getModifierList().getAnnotations().length > 0) {
result.add(declaration);
}
}
@Override
public void visitMethod(@NotNull GrMethod method) {
//skip methods
}
@Override
public void visitTypeDefinition(@NotNull GrTypeDefinition typeDefinition) {
//skip type defs
}
});
return result.toArray(GrVariableDeclaration.EMPTY_ARRAY);
}
}
@@ -23,6 +23,7 @@ import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.CachedValueProvider;
import com.intellij.psi.util.CachedValuesManager;
import com.intellij.psi.util.PsiModificationTracker;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ArrayFactory;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.ContainerUtil;
@@ -172,8 +173,9 @@ public class GrModifierListImpl extends GrStubElementBase<GrModifierListStub> im
public static boolean checkModifierProperty(@NotNull GrModifierList modifierList, @GrModifier.GrModifierConstant @NotNull String modifier) {
final PsiElement owner = modifierList.getParent();
if (owner instanceof GrVariableDeclaration && owner.getParent() instanceof GrTypeDefinitionBody) {
PsiElement pParent = owner.getParent().getParent();
PsiElement parent = PsiTreeUtil.getStubOrPsiParent(owner);
if (owner instanceof GrVariableDeclaration && parent instanceof GrTypeDefinitionBody) {
PsiElement pParent = parent.getParent();
if (!modifierList.hasExplicitVisibilityModifiers()) { //properties are backed by private fields
if (!(pParent instanceof GrTypeDefinition && GrTraitUtil.isInterface((GrTypeDefinition)pParent))) {
if (modifier.equals(GrModifier.PRIVATE)) return true;
@@ -194,11 +196,11 @@ public class GrModifierListImpl extends GrStubElementBase<GrModifierListStub> im
}
}
if (owner instanceof GrMethod && owner.getParent() instanceof GrTypeDefinitionBody) {
PsiElement parent = owner.getParent().getParent();
if (parent instanceof GrTypeDefinition && ((GrTypeDefinition)parent).isInterface()) {
if (owner instanceof GrMethod && parent instanceof GrTypeDefinitionBody) {
PsiElement pParent = parent.getParent();
if (pParent instanceof GrTypeDefinition && ((GrTypeDefinition)pParent).isInterface()) {
if (GrModifier.ABSTRACT.equals(modifier)) return true;
if (!((GrTypeDefinition)parent).isTrait() && GrModifier.PUBLIC.equals(modifier)) return true;
if (!((GrTypeDefinition)pParent).isTrait() && GrModifier.PUBLIC.equals(modifier)) return true;
}
}
@@ -208,7 +210,7 @@ public class GrModifierListImpl extends GrStubElementBase<GrModifierListStub> im
if (modifier.equals(GrModifier.PUBLIC)) {
if (owner instanceof GrPackageDefinition) return false;
if (owner instanceof GrVariableDeclaration && !(owner.getParent() instanceof GrTypeDefinitionBody) || owner instanceof GrVariable) {
if (owner instanceof GrVariableDeclaration && !(parent instanceof GrTypeDefinitionBody) || owner instanceof GrVariable) {
return false;
}
//groovy type definitions and methods are public by default
@@ -0,0 +1,73 @@
/*
* Copyright 2000-2016 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.lang.psi.impl
import com.intellij.openapi.util.Key
import com.intellij.psi.stubs.StubElement
import com.intellij.psi.util.CachedValueProvider.Result
import com.intellij.psi.util.CachedValuesManager
import com.intellij.psi.util.ParameterizedCachedValue
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes
import org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod
internal fun GroovyFileImpl.getScriptDeclarations(topLevel: Boolean): Array<out GrVariableDeclaration> {
val tree = stubTree ?: return collectScriptDeclarations(topLevel)
return if (topLevel) {
val root: StubElement<*> = tree.root
root.getChildrenByType(GroovyElementTypes.VARIABLE_DEFINITION, GrVariableDeclaration.EMPTY_ARRAY)
}
else {
tree.plainList.filter {
it.stubType === GroovyElementTypes.VARIABLE_DEFINITION
}.map {
it.psi as GrVariableDeclaration
}.toTypedArray()
}
}
private val key = Key.create<ParameterizedCachedValue<Array<GrVariableDeclaration>, Boolean>>("groovy.variable.declarations")
private fun GroovyFileImpl.collectScriptDeclarations(topLevel: Boolean): Array<GrVariableDeclaration> {
val provider = { it: Boolean ->
Result.create(doCollectScriptDeclarations(it), this)
}
return CachedValuesManager.getManager(project).getParameterizedCachedValue(this, key, provider, false, topLevel)
}
private fun GroovyFileImpl.doCollectScriptDeclarations(topLevel: Boolean): Array<GrVariableDeclaration> {
val result = mutableListOf<GrVariableDeclaration>()
accept(object : GroovyRecursiveElementVisitor() {
var withinMethod = false
override fun visitTypeDefinition(typeDefinition: GrTypeDefinition) = Unit
override fun visitMethod(method: GrMethod) {
withinMethod = true
super.visitMethod(method)
withinMethod = false
}
override fun visitVariableDeclaration(variableDeclaration: GrVariableDeclaration) {
if (!withinMethod || !topLevel) {
result.add(variableDeclaration)
}
}
})
return result.toTypedArray()
}
@@ -24,7 +24,6 @@ import com.intellij.psi.presentation.java.JavaPresentationUtil;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.stubs.IStubElementType;
import com.intellij.ui.LayeredIcon;
import com.intellij.util.Function;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.containers.ContainerUtil;
import icons.JetgroovyIcons;
@@ -43,7 +42,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrNamedArgumentSearc
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
import org.jetbrains.plugins.groovy.lang.psi.dataFlow.types.TypeInferenceHelper;
import org.jetbrains.plugins.groovy.lang.psi.stubs.GrFieldStub;
import org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.GrVariableEnhancer;
@@ -77,21 +75,6 @@ public class GrFieldImpl extends GrVariableBaseImpl<GrFieldStub> implements GrFi
visitor.visitField(this);
}
@Override
public GrTypeElement getTypeElementGroovy() {
final GrFieldStub stub = getStub();
if (stub != null) {
final String typeText = stub.getTypeText();
if (typeText == null) {
return null;
}
return GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(typeText, this);
}
return super.getTypeElementGroovy();
}
public String toString() {
return "Field";
}
@@ -203,16 +186,6 @@ public class GrFieldImpl extends GrVariableBaseImpl<GrFieldStub> implements GrFi
return PsiImplUtil.getMemberUseScope(this);
}
@NotNull
@Override
public String getName() {
final GrFieldStub stub = getStub();
if (stub != null) {
return stub.getName();
}
return super.getName();
}
@Override
public ItemPresentation getPresentation() {
return JavaPresentationUtil.getFieldPresentation(this);
@@ -26,7 +26,6 @@ import com.intellij.psi.impl.source.tree.LeafPsiElement;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.stubs.IStubElementType;
import com.intellij.psi.stubs.StubElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
@@ -49,6 +48,7 @@ import org.jetbrains.plugins.groovy.lang.psi.impl.GrStubElementBase;
import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyPsiElementImpl;
import org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil;
import org.jetbrains.plugins.groovy.lang.psi.stubs.GrVariableStubBase;
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
import javax.swing.*;
@@ -56,12 +56,12 @@ import javax.swing.*;
/**
* @author ilyas
*/
public abstract class GrVariableBaseImpl<T extends StubElement> extends GrStubElementBase<T> implements GrVariable {
public abstract class GrVariableBaseImpl<T extends GrVariableStubBase> extends GrStubElementBase<T> implements GrVariable, StubBasedPsiElement<T> {
public static final Logger LOG = Logger.getInstance("org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableImpl");
private static final RecursionGuard ourGuard = RecursionManager.createGuard("grVariableInitializer");
public GrVariableBaseImpl(ASTNode node) {
protected GrVariableBaseImpl(ASTNode node) {
super(node);
}
@@ -131,11 +131,17 @@ public abstract class GrVariableBaseImpl<T extends StubElement> extends GrStubEl
@Override
@Nullable
public GrTypeElement getTypeElementGroovy() {
T stub = getStub();
if (stub != null) {
return stub.getTypeElement();
}
PsiElement parent = getParent();
if (parent instanceof GrVariableDeclaration) {
return ((GrVariableDeclaration)parent).getTypeElementGroovyForVariable(this);
}
return null;
return findChildByClass(GrTypeElement.class);
}
@Override
@@ -266,6 +272,13 @@ public abstract class GrVariableBaseImpl<T extends StubElement> extends GrStubEl
@Override
@NotNull
public String getName() {
T stub = getStub();
if (stub != null) {
String name = stub.getName();
if (name != null) {
return name;
}
}
return PsiImplUtil.getName(this);
}
@@ -291,11 +304,7 @@ public abstract class GrVariableBaseImpl<T extends StubElement> extends GrStubEl
@Nullable
private GrVariableDeclaration getDeclaration() {
PsiElement parent = getParent();
if (parent instanceof GrVariableDeclaration) {
return (GrVariableDeclaration)parent;
}
return null;
return getStubOrPsiParentOfType(GrVariableDeclaration.class);
}
@Override
@@ -22,7 +22,6 @@ import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.scope.ElementClassHint;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.stubs.EmptyStub;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -46,6 +45,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
import org.jetbrains.plugins.groovy.lang.psi.impl.GrStubElementBase;
import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyPsiElementImpl;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil;
import org.jetbrains.plugins.groovy.lang.psi.stubs.GrVariableDeclarationStub;
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil;
@@ -55,14 +55,16 @@ import java.util.List;
/**
* @author: Dmitry.Krasilschikov
*/
public class GrVariableDeclarationImpl extends GrStubElementBase<EmptyStub> implements GrVariableDeclaration, StubBasedPsiElement<EmptyStub> {
public class GrVariableDeclarationImpl extends GrStubElementBase<GrVariableDeclarationStub>
implements GrVariableDeclaration, StubBasedPsiElement<GrVariableDeclarationStub> {
private static final Logger LOG = Logger.getInstance(GrVariableDeclarationImpl.class);
public GrVariableDeclarationImpl(@NotNull ASTNode node) {
super(node);
}
public GrVariableDeclarationImpl(EmptyStub stub) {
public GrVariableDeclarationImpl(@NotNull GrVariableDeclarationStub stub) {
super(stub, GroovyElementTypes.VARIABLE_DEFINITION);
}
@@ -159,6 +161,10 @@ public class GrVariableDeclarationImpl extends GrStubElementBase<EmptyStub> impl
@Override
@Nullable
public GrTypeElement getTypeElementGroovy() {
GrVariableDeclarationStub stub = getStub();
if (stub != null) {
return stub.getTypeElement();
}
if (isTuple()) return null;
return findChildByClass(GrTypeElement.class);
}
@@ -19,23 +19,28 @@ package org.jetbrains.plugins.groovy.lang.psi.impl.statements;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.stubs.StubElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrScriptField;
import org.jetbrains.plugins.groovy.lang.psi.stubs.GrVariableStub;
import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil;
/**
* @author Dmitry.Krasilschikov
* @date 11.04.2007
*/
public class GrVariableImpl extends GrVariableBaseImpl<StubElement> implements GrVariable {
public class GrVariableImpl extends GrVariableBaseImpl<GrVariableStub> implements GrVariable {
public GrVariableImpl(@NotNull ASTNode node) {
super(node);
}
public GrVariableImpl(@NotNull GrVariableStub stub) {
super(stub, GroovyElementTypes.VARIABLE);
}
@Override
public void accept(GroovyElementVisitor visitor) {
visitor.visitVariable(this);
@@ -52,7 +52,7 @@ import org.jetbrains.plugins.groovy.lang.psi.typeEnhancers.GrVariableEnhancer;
/**
* @author: Dmitry.Krasilschikov
*/
public class GrParameterImpl extends GrVariableBaseImpl<GrParameterStub> implements GrParameter, StubBasedPsiElement<GrParameterStub> {
public class GrParameterImpl extends GrVariableBaseImpl<GrParameterStub> implements GrParameter {
public GrParameterImpl(@NotNull ASTNode node) {
super(node);
}
@@ -161,29 +161,13 @@ public class GrParameterImpl extends GrVariableBaseImpl<GrParameterStub> impleme
JavaCodeStyleManager.getInstance(getProject()).shortenClassReferences(newTypeElement);
}
@Override
@Nullable
public GrTypeElement getTypeElementGroovy() {
final GrParameterStub stub = getStub();
if (stub != null) {
final String typeText = stub.getTypeText();
if (typeText == null) {
return null;
}
return GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(typeText, this);
}
return findChildByClass(GrTypeElement.class);
}
@Override
public boolean isOptional() {
final GrParameterStub stub = getStub();
if (stub != null) {
return GrParameterStub.hasInitializer(stub.getFlags());
}
return getInitializerGroovy() != null;
}
@@ -215,20 +199,10 @@ public class GrParameterImpl extends GrVariableBaseImpl<GrParameterStub> impleme
return new LocalSearchScope(scope);
}
@NotNull
@Override
public String getName() {
final GrParameterStub stub = getStub();
if (stub != null) {
return stub.getName();
}
return super.getName();
}
@Override
@NotNull
public GrModifierList getModifierList() {
return getStubOrPsiChild(GroovyElementTypes.MODIFIERS);
return getRequiredStubOrPsiChild(GroovyElementTypes.MODIFIERS);
}
@Override
@@ -17,8 +17,6 @@ package org.jetbrains.plugins.groovy.lang.psi.stubs;
import com.intellij.psi.impl.PsiImplUtil;
import com.intellij.psi.stubs.IStubElementType;
import com.intellij.psi.stubs.NamedStub;
import com.intellij.psi.stubs.StubBase;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NotNull;
@@ -29,39 +27,25 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrEn
/**
* @author ilyas
*/
public class GrFieldStub extends StubBase<GrField> implements NamedStub<GrField> {
public class GrFieldStub extends GrVariableStubBase<GrField> {
public static final byte IS_PROPERTY = 0x01;
public static final byte IS_ENUM_CONSTANT = 0x02;
public static final byte IS_DEPRECATED_BY_DOC_TAG = 0x04;
private final byte myFlags;
private final StringRef myName;
private final String[] myAnnotations;
private final String[] myNamedParameters;
private final String myTypeText;
private final byte myFlags;
public GrFieldStub(StubElement parent,
StringRef name,
final String[] annotations,
String[] namedParameters,
@NotNull IStubElementType elemType,
byte flags, @Nullable String typeText) {
super(parent, elemType);
myName = name;
myAnnotations = annotations;
@Nullable StringRef name,
@NotNull final String[] annotations,
@NotNull String[] namedParameters,
@NotNull IStubElementType elemType,
byte flags,
@Nullable String typeText) {
super(parent, elemType, name, annotations, typeText);
myNamedParameters = namedParameters;
myFlags = flags;
myTypeText = typeText;
}
@Override
@NotNull
public String getName() {
return StringRef.toString(myName);
}
public String[] getAnnotations() {
return myAnnotations;
}
@NotNull
@@ -81,11 +65,6 @@ public class GrFieldStub extends StubBase<GrField> implements NamedStub<GrField>
return myFlags;
}
@Nullable
public String getTypeText() {
return myTypeText;
}
public static byte buildFlags(GrField field) {
byte f = 0;
if (field instanceof GrEnumConstant) {
@@ -97,7 +76,7 @@ public class GrFieldStub extends StubBase<GrField> implements NamedStub<GrField>
}
if (PsiImplUtil.isDeprecatedByDocTag(field)) {
f|= IS_DEPRECATED_BY_DOC_TAG;
f |= IS_DEPRECATED_BY_DOC_TAG;
}
return f;
}
@@ -17,15 +17,11 @@ package org.jetbrains.plugins.groovy.lang.psi.stubs;
import com.intellij.psi.stubs.PsiFileStubImpl;
import com.intellij.psi.tree.IStubFileElementType;
import com.intellij.reference.SoftReference;
import com.intellij.util.ArrayUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.parser.GroovyParserDefinition;
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.GrVariableDeclaration;
import org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition;
/**
@@ -35,9 +31,6 @@ public class GrFileStub extends PsiFileStubImpl<GroovyFile> {
private final @NotNull String[] myAnnotations;
private final StringRef myName;
private final boolean isScript;
private final @NotNull String[] myDeclarationStrings;
private SoftReference<GrVariableDeclaration[]> myDeclarations;
public GrFileStub(GroovyFile file) {
super(file);
@@ -50,19 +43,13 @@ public class GrFileStub extends PsiFileStubImpl<GroovyFile> {
else {
myAnnotations = ArrayUtil.EMPTY_STRING_ARRAY;
}
myDeclarationStrings = ContainerUtil.map(
file.getAnnotatedScriptDeclarations(),
declaration -> declaration.getText(),
ArrayUtil.EMPTY_STRING_ARRAY
);
}
public GrFileStub(StringRef name, boolean isScript, @NotNull String[] annotations, @NotNull String[] declarationStrings) {
public GrFileStub(StringRef name, boolean isScript, @NotNull String[] annotations) {
super(null);
myName = name;
this.isScript = isScript;
myAnnotations = annotations;
myDeclarationStrings = declarationStrings;
}
@NotNull
@@ -83,28 +70,4 @@ public class GrFileStub extends PsiFileStubImpl<GroovyFile> {
public String[] getAnnotations() {
return myAnnotations;
}
@NotNull
public String[] getDeclarationStrings() {
return myDeclarationStrings;
}
@NotNull
public GrVariableDeclaration[] getAnnotatedScriptDeclarations() {
String[] declarationStrings = getDeclarationStrings();
if (declarationStrings.length == 0) return GrVariableDeclaration.EMPTY_ARRAY;
GrVariableDeclaration[] declarations = SoftReference.dereference(myDeclarations);
if (declarations == null) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
GroovyFile psi = getPsi();
declarations = ContainerUtil.map(
declarationStrings,
declarationString -> factory.createVariableDeclarationFromText(declarationString, psi),
GrVariableDeclaration.EMPTY_ARRAY
);
myDeclarations = new SoftReference<>(declarations);
}
return declarations;
}
}
@@ -15,50 +15,30 @@
*/
package org.jetbrains.plugins.groovy.lang.psi.stubs;
import com.intellij.psi.stubs.NamedStub;
import com.intellij.psi.stubs.StubBase;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.BitUtil;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter;
/**
* @author peter
*/
public class GrParameterStub extends StubBase<GrParameter> implements NamedStub<GrParameter> {
private final StringRef myName;
private final String[] myAnnotations;
private final String myTypeText;
public class GrParameterStub extends GrVariableStubBase<GrParameter> {
private final int myFlags;
public GrParameterStub(StubElement parent,
StringRef name,
final String[] annotations,
String typeText,
@NotNull StringRef name,
@NotNull final String[] annotations,
@Nullable String typeText,
int flags) {
super(parent, GroovyElementTypes.PARAMETER);
myName = name;
myAnnotations = annotations;
myTypeText = typeText;
super(parent, GroovyElementTypes.PARAMETER, name, annotations, typeText);
myFlags = flags;
}
@Override
@NotNull
public String getName() {
return StringRef.toString(myName);
}
public String[] getAnnotations() {
return myAnnotations;
}
public String getTypeText() {
return myTypeText;
}
public int getFlags() {
return myFlags;
}
@@ -0,0 +1,56 @@
/*
* Copyright 2000-2016 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.lang.psi.stubs;
import com.intellij.psi.stubs.StubBase;
import com.intellij.psi.stubs.StubElement;
import com.intellij.reference.SoftReference;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
public class GrVariableDeclarationStub extends StubBase<GrVariableDeclaration> {
private final @Nullable String myTypeString;
private SoftReference<GrTypeElement> myTypeElement;
public GrVariableDeclarationStub(StubElement parent, @Nullable String typeString) {
super(parent, GroovyElementTypes.VARIABLE_DEFINITION);
myTypeString = typeString;
}
@Nullable
public String getTypeString() {
return myTypeString;
}
@Nullable
public GrTypeElement getTypeElement() {
String typeString = getTypeString();
if (typeString == null) return null;
GrTypeElement typeElement = SoftReference.dereference(myTypeElement);
if (typeElement == null) {
typeElement = GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(typeString, getPsi());
myTypeElement = new SoftReference<>(typeElement);
}
return typeElement;
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 2000-2016 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.lang.psi.stubs;
import com.intellij.psi.stubs.IStubElementType;
import com.intellij.psi.stubs.StubElement;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
public class GrVariableStub extends GrVariableStubBase<GrVariable> {
public GrVariableStub(StubElement parent,
IStubElementType elementType,
@Nullable StringRef ref,
@NotNull String[] annotations,
@Nullable String text) {
super(parent, elementType, ref, annotations, text);
}
}
@@ -0,0 +1,78 @@
/*
* Copyright 2000-2016 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.lang.psi.stubs;
import com.intellij.psi.stubs.IStubElementType;
import com.intellij.psi.stubs.NamedStub;
import com.intellij.psi.stubs.StubBase;
import com.intellij.psi.stubs.StubElement;
import com.intellij.reference.SoftReference;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
public abstract class GrVariableStubBase<V extends GrVariable> extends StubBase<V> implements NamedStub<V> {
private final @Nullable StringRef myNameRef;
private final @NotNull String[] myAnnotations;
private final @Nullable String myTypeText;
private SoftReference<GrTypeElement> myTypeElement;
protected GrVariableStubBase(StubElement parent,
IStubElementType elementType,
@Nullable StringRef ref,
@NotNull String[] annotations,
@Nullable String text) {
super(parent, elementType);
myNameRef = ref;
myAnnotations = annotations;
myTypeText = text;
}
@Nullable
@Override
public String getName() {
return StringRef.toString(myNameRef);
}
@NotNull
public String[] getAnnotations() {
return myAnnotations;
}
@Nullable
public String getTypeText() {
return myTypeText;
}
@Nullable
public GrTypeElement getTypeElement() {
String typeText = getTypeText();
if (typeText == null) return null;
GrTypeElement typeElement = SoftReference.dereference(myTypeElement);
if (typeElement == null) {
typeElement = GroovyPsiElementFactory.getInstance(getProject()).createTypeElement(typeText, getPsi());
myTypeElement = new SoftReference<>(typeElement);
}
return typeElement;
}
}
@@ -23,7 +23,6 @@ import com.intellij.psi.StubBuilder;
import com.intellij.psi.stubs.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.IStubFileElementType;
import com.intellij.util.ArrayUtil;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
@@ -41,7 +40,7 @@ import java.io.IOException;
* @author ilyas
*/
public class GrStubFileElementType extends IStubFileElementType<GrFileStub> {
public static final int STUB_VERSION = 33;
public static final int STUB_VERSION = 35;
public GrStubFileElementType(Language language) {
super(language);
@@ -100,9 +99,6 @@ public class GrStubFileElementType extends IStubFileElementType<GrFileStub> {
dataStream.writeName(stub.getName().toString());
dataStream.writeBoolean(stub.isScript());
GrStubUtils.writeStringArray(dataStream, stub.getAnnotations());
if (stub.isScript()) {
GrStubUtils.writeStringArray(dataStream, stub.getDeclarationStrings());
}
}
@NotNull
@@ -111,14 +107,7 @@ public class GrStubFileElementType extends IStubFileElementType<GrFileStub> {
StringRef name = dataStream.readName();
boolean isScript = dataStream.readBoolean();
String[] annotations = GrStubUtils.readStringArray(dataStream);
String[] declarationStrings;
if (isScript) {
declarationStrings = GrStubUtils.readStringArray(dataStream);
}
else {
declarationStrings = ArrayUtil.EMPTY_STRING_ARRAY;
}
return new GrFileStub(name, isScript, annotations, declarationStrings);
return new GrFileStub(name, isScript, annotations);
}
@Override
@@ -0,0 +1,87 @@
/*
* Copyright 2000-2016 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.lang.psi.stubs.elements;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.impl.source.tree.SharedImplUtil;
import com.intellij.psi.stubs.StubElement;
import com.intellij.psi.stubs.StubInputStream;
import com.intellij.psi.stubs.StubOutputStream;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableDeclarationImpl;
import org.jetbrains.plugins.groovy.lang.psi.stubs.GrStubUtils;
import org.jetbrains.plugins.groovy.lang.psi.stubs.GrVariableDeclarationStub;
import java.io.IOException;
public class GrVariableDeclarationElementType extends GrStubElementType<GrVariableDeclarationStub, GrVariableDeclaration> {
public GrVariableDeclarationElementType() {
super("variable definitions");
}
@Override
public void serialize(@NotNull GrVariableDeclarationStub stub, @NotNull StubOutputStream dataStream) throws IOException {
GrStubUtils.writeNullableString(dataStream, stub.getTypeString());
}
@NotNull
@Override
public GrVariableDeclarationStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
return new GrVariableDeclarationStub(
parentStub,
GrStubUtils.readNullableString(dataStream)
);
}
@Override
public GrVariableDeclaration createPsi(@NotNull GrVariableDeclarationStub stub) {
return new GrVariableDeclarationImpl(stub);
}
@NotNull
@Override
public GrVariableDeclarationStub createStub(@NotNull GrVariableDeclaration psi, StubElement parentStub) {
return new GrVariableDeclarationStub(parentStub, GrStubUtils.getTypeText(psi.getTypeElementGroovy()));
}
@Override
public boolean shouldCreateStub(ASTNode node) {
PsiElement parent = SharedImplUtil.getParent(node);
if (parent instanceof GrTypeDefinitionBody) {
// store fields
return true;
}
if (PsiTreeUtil.getParentOfType(parent, GrTypeDefinition.class) != null) {
// do not store variable declarations within classes, as they are not scripts
return false;
}
PsiElement psi = node.getPsi();
if (!(psi instanceof GrVariableDeclaration) || ((GrVariableDeclaration)psi).getModifierList().getRawAnnotations().length == 0) {
// store only annotated declarations
return false;
}
PsiFile file = psi.getContainingFile();
return file instanceof GroovyFile && ((GroovyFile)file).isScript();
}
}
@@ -0,0 +1,78 @@
/*
* Copyright 2000-2016 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.lang.psi.stubs.elements;
import com.intellij.lang.ASTNode;
import com.intellij.psi.stubs.StubElement;
import com.intellij.psi.stubs.StubInputStream;
import com.intellij.psi.stubs.StubOutputStream;
import com.intellij.util.io.StringRef;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.GrVariableImpl;
import org.jetbrains.plugins.groovy.lang.psi.stubs.GrStubUtils;
import org.jetbrains.plugins.groovy.lang.psi.stubs.GrVariableStub;
import java.io.IOException;
public final class GrVariableElementType extends GrStubElementType<GrVariableStub, GrVariable> {
public GrVariableElementType() {
super("assigned variable");
}
@Override
public void serialize(@NotNull GrVariableStub stub, @NotNull StubOutputStream dataStream) throws IOException {
dataStream.writeName(stub.getName());
GrStubUtils.writeStringArray(dataStream, stub.getAnnotations());
GrStubUtils.writeNullableString(dataStream, stub.getTypeText());
}
@Override
public GrVariable createPsi(@NotNull GrVariableStub stub) {
return new GrVariableImpl(stub);
}
@NotNull
@Override
public GrVariableStub createStub(@NotNull GrVariable psi, StubElement parentStub) {
return new GrVariableStub(
parentStub,
this,
StringRef.fromNullableString(psi.getName()),
GrStubUtils.getAnnotationNames(psi),
GrStubUtils.getTypeText(psi.getTypeElementGroovy())
);
}
@NotNull
@Override
public GrVariableStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
final StringRef name = dataStream.readName();
final String[] annNames = GrStubUtils.readStringArray(dataStream);
final String typeText = GrStubUtils.readNullableString(dataStream);
return new GrVariableStub(parentStub, this, name, annNames, typeText);
}
@Override
public boolean shouldCreateStub(ASTNode node) {
ASTNode parent = node.getTreeParent();
return parent != null &&
parent.getElementType() == GroovyElementTypes.VARIABLE_DEFINITION &&
GroovyElementTypes.VARIABLE_DEFINITION.shouldCreateStub(parent);
}
}
@@ -15,7 +15,6 @@
*/
package org.jetbrains.plugins.groovy.transformations.impl;
import com.intellij.openapi.util.Ref;
import com.intellij.psi.PsiArrayType;
import com.intellij.psi.PsiClassType;
import com.intellij.psi.PsiModifier;
@@ -28,9 +27,6 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.GroovyLanguage;
import org.jetbrains.plugins.groovy.dsl.GroovyDslFileIndex;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration;
import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil;
@@ -84,16 +80,11 @@ public class BaseScriptTransformationSupport implements AstTransformationSupport
private static PsiClassType doGetSuperClassType(GroovyScriptClass scriptClass) {
GrVariableDeclaration declaration = findDeclaration(scriptClass.getContainingFile());
if (declaration != null) {
GrModifierList modifierList = declaration.getModifierList();
if (modifierList.findAnnotation(GroovyCommonClassNames.GROOVY_TRANSFORM_BASE_SCRIPT) != null) {
GrTypeElement typeElement = declaration.getTypeElementGroovy();
if (typeElement != null) {
PsiType type = typeElement.getType();
if (type instanceof PsiClassType) {
return (PsiClassType)type;
}
GrTypeElement typeElement = declaration.getTypeElementGroovy();
if (typeElement != null) {
PsiType type = typeElement.getType();
if (type instanceof PsiClassType) {
return (PsiClassType)type;
}
}
}
@@ -105,24 +96,11 @@ public class BaseScriptTransformationSupport implements AstTransformationSupport
if (!hasNameInFile(file, "BaseScript")) {
return null;
}
final Ref<GrVariableDeclaration> ref = Ref.create();
file.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitVariableDeclaration(@NotNull GrVariableDeclaration variableDeclaration) {
super.visitVariableDeclaration(variableDeclaration);
if (variableDeclaration.getModifierList().findAnnotation(GroovyCommonClassNames.GROOVY_TRANSFORM_BASE_SCRIPT) != null) {
ref.set(variableDeclaration);
}
for (GrVariableDeclaration declaration : file.getScriptDeclarations(false)) {
if (declaration.getModifierList().findAnnotation(GroovyCommonClassNames.GROOVY_TRANSFORM_BASE_SCRIPT) != null) {
return declaration;
}
@Override
public void visitElement(@NotNull GroovyPsiElement element) {
if (ref.isNull()) {
super.visitElement(element);
}
}
});
return ref.get();
}
return null;
}
}
@@ -34,7 +34,7 @@ public class FieldScriptTransformationSupport implements AstTransformationSuppor
if (!hasNameInFile(scriptClass.getContainingFile(), "Field")) {
return;
}
for (GrVariableDeclaration declaration : scriptClass.getContainingFile().getAnnotatedScriptDeclarations()) {
for (GrVariableDeclaration declaration : scriptClass.getContainingFile().getScriptDeclarations(true)) {
if (declaration.getModifierList().findAnnotation(GroovyCommonClassNames.GROOVY_TRANSFORM_FIELD) != null) {
for (GrVariable variable : declaration.getVariables()) {
context.addField(new GrScriptField(variable, scriptClass));
@@ -0,0 +1,65 @@
/*
* Copyright 2000-2016 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.transformations
import com.intellij.psi.PsiClass
import com.intellij.psi.util.InheritanceUtil
import com.intellij.testFramework.LightProjectDescriptor
import groovy.transform.CompileStatic
import org.jetbrains.plugins.groovy.GroovyLightProjectDescriptor
import org.jetbrains.plugins.groovy.LightGroovyTestCase
import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyFileImpl
import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass
@CompileStatic
class BaseScriptTransformationSupportTest extends LightGroovyTestCase {
LightProjectDescriptor projectDescriptor = GroovyLightProjectDescriptor.GROOVY_LATEST
@Override
void setUp() throws Exception {
super.setUp()
fixture.addFileToProject 'base.groovy', 'abstract class MyBaseScript extends Script {}'
}
private void doTest(String text) {
def file = fixture.addFileToProject('Zzz.groovy', """\
import groovy.transform.BaseScript
$text
""") as GroovyFileImpl
def clazz = fixture.findClass('Zzz')
assert clazz instanceof GroovyScriptClass
assert InheritanceUtil.isInheritor(clazz as PsiClass, 'MyBaseScript')
assert !file.contentsLoaded
}
void 'test top level'() {
doTest '@BaseScript MyBaseScript hello'
}
void 'test script block level'() {
doTest 'if (true) @BaseScript MyBaseScript hello'
}
void 'test within method'() {
doTest '''\
def foo() {
@BaseScript MyBaseScript hello
}
'''
}
}