structural search: resolve elements from idea open api inside groovy script (resolve and very basic completion now works)

This commit is contained in:
Dmitry Batkovich
2017-10-03 15:44:01 +03:00
parent b80a89e36d
commit 05a4a74695
4 changed files with 76 additions and 19 deletions
@@ -2,5 +2,6 @@
<extensions defaultExtensionNs="com.intellij">
<structuralsearch.profile implementation="com.intellij.structuralsearch.JavaStructuralSearchProfile"/>
<structuralsearch.matchPredicateProvider implementation="com.intellij.structuralsearch.impl.matcher.JavaMatchPredicateProvider"/>
<java.elementFinder implementation="com.intellij.structuralsearch.IdeaOpenApiClassFinder"/>
</extensions>
</idea-plugin>
@@ -0,0 +1,41 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.structuralsearch;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.NonClasspathClassFinder;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.structuralsearch.plugin.util.StructuralSearchScriptScope;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class IdeaOpenApiClassFinder extends NonClasspathClassFinder {
public IdeaOpenApiClassFinder(@NotNull Project project) {
super(project);
}
@Override
protected List<VirtualFile> calcClassRoots() {
LocalFileSystem lfs = LocalFileSystem.getInstance();
return Stream.of(PsiClass.class, PsiElement.class)
.map(PathManager::getJarPathForClass)
.filter(Objects::nonNull)
.map(File::new)
.map(lfs::refreshAndFindFileByIoFile)
.collect(Collectors.toList());
}
@Override
public PsiClass findClass(@NotNull String qualifiedName, @NotNull GlobalSearchScope scope) {
return !(scope instanceof StructuralSearchScriptScope) ? null : super.findClass(qualifiedName, scope);
}
}
@@ -1,24 +1,11 @@
/*
* Copyright 2000-2017 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.
*/
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.structuralsearch.plugin.ui;
import com.intellij.codeInsight.template.impl.Variable;
import com.intellij.find.impl.RegExHelpPopup;
import com.intellij.icons.AllIcons;
import com.intellij.ide.highlighter.HighlighterFactory;
import com.intellij.lang.Language;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
@@ -43,6 +30,7 @@ import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.psi.PsiCodeFragment;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
@@ -51,6 +39,7 @@ import com.intellij.structuralsearch.impl.matcher.predicates.ScriptLog;
import com.intellij.structuralsearch.impl.matcher.predicates.ScriptSupport;
import com.intellij.structuralsearch.plugin.replace.ReplaceOptions;
import com.intellij.structuralsearch.plugin.replace.ui.ReplaceConfiguration;
import com.intellij.structuralsearch.plugin.util.StructuralSearchScriptScope;
import com.intellij.ui.EditorTextField;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.containers.ContainerUtil;
@@ -65,10 +54,8 @@ import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.HashSet;
import java.util.*;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
@@ -633,8 +620,23 @@ class EditVarConstraintsDialog extends DialogWrapper {
}
Editor createEditor(final Project project, final String text, final String fileName) {
Language groovy = Language.findLanguageByID("Groovy");
Document doc = null;
final FileType fileType = getFileType(fileName);
final Document doc = createDocument(fileName, fileType, text);
if (groovy != null) {
// there is no right way to create a code fragment for generic language, so we use this hole since we need extend resolve scope
for (StructuralSearchProfile profile : StructuralSearchProfile.EP_NAME.getExtensions()) {
if (profile.isMyLanguage(groovy)) {
PsiCodeFragment fragment = Objects.requireNonNull(profile.createCodeFragment(project, text, null));
fragment.forceResolveScope(new StructuralSearchScriptScope(myProject));
doc = PsiDocumentManager.getInstance(project).getDocument(fragment);
break;
}
}
}
if (doc == null) {
doc = createDocument(fileName, fileType, text);
}
final Editor editor = EditorFactory.getInstance().createEditor(doc, project);
((EditorEx)editor).setEmbeddedIntoDialogWrapper(true);
@@ -0,0 +1,13 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.structuralsearch.plugin.util;
import com.intellij.openapi.project.Project;
import com.intellij.psi.search.DelegatingGlobalSearchScope;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
public class StructuralSearchScriptScope extends DelegatingGlobalSearchScope{
public StructuralSearchScriptScope(@NotNull Project project) {
super(GlobalSearchScope.everythingScope(project));
}
}