@Override public boolean canHandle(RunConfiguration configuration, Project project) { if (!(configuration instanceof JUnitConfiguration)) { return false; } else { JUnitConfiguration.Data data = ((JUnitConfiguration) configuration).getPersistentData(); String mainClassName = data.getMainClassName(); String methodName = data.getMethodName(); JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); PsiClass referencedClass = psiFacade.findClass(mainClassName, GlobalSearchScope.allScope(project)); PsiFile containingFile = referencedClass.getContainingFile(); FileEditorManager fileEditorManager = FileEditorManager.getInstance(project); if (methodName == null) { fileEditorManager.openFile(containingFile.getVirtualFile(), true); } else { PsiMethod[] methodsByName = referencedClass.findMethodsByName(methodName, false); fileEditorManager.openEditor( new OpenFileDescriptor( project, containingFile.getVirtualFile(), methodsByName[0].getTextOffset()), true); } return true; } }
public String getReportText() { final Element rootElement = new Element("root"); rootElement.setAttribute("isBackward", String.valueOf(!myForward)); final List<PsiFile> files = new ArrayList<PsiFile>(myDependencies.keySet()); Collections.sort( files, new Comparator<PsiFile>() { @Override public int compare(PsiFile f1, PsiFile f2) { final VirtualFile virtualFile1 = f1.getVirtualFile(); final VirtualFile virtualFile2 = f2.getVirtualFile(); if (virtualFile1 != null && virtualFile2 != null) { return virtualFile1.getPath().compareToIgnoreCase(virtualFile2.getPath()); } return 0; } }); for (PsiFile file : files) { final Element fileElement = new Element("file"); fileElement.setAttribute("path", file.getVirtualFile().getPath()); for (PsiFile dep : myDependencies.get(file)) { Element depElement = new Element("dependency"); depElement.setAttribute("path", dep.getVirtualFile().getPath()); fileElement.addContent(depElement); } rootElement.addContent(fileElement); } PathMacroManager.getInstance(myProject).collapsePaths(rootElement); return JDOMUtil.writeDocument(new Document(rootElement), SystemProperties.getLineSeparator()); }
private boolean checkClassUnderSources(final PsiElement element, final Project project) { final PsiFile file = element.getContainingFile(); if (file != null && file.getVirtualFile() != null) { final FileIndexFacade indexFacade = FileIndexFacade.getInstance(project); final VirtualFile vf = file.getVirtualFile(); return indexFacade.isInSource(vf) || indexFacade.isInSourceContent(vf); } return false; }
private boolean acceptedByFilters(@NotNull PsiFile file) { VirtualFile vFile = file.getVirtualFile(); if (vFile == null) { return false; } for (VirtualFileFilter filter : myFilters) { if (!filter.accept(file.getVirtualFile())) { return false; } } return true; }
protected void runOverEditor( @NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile psiFile) { final Document document = editor.getDocument(); if (!ReadonlyStatusHandler.ensureDocumentWritable(project, document)) return; final Runnable runnable = () -> { final int caretOffset = editor.getCaretModel().getOffset(); final int lineLength = getRightMargin(project); DartAnalysisServerService.getInstance().updateFilesContent(); DartAnalysisServerService.FormatResult formatResult = DartAnalysisServerService.getInstance() .edit_format(psiFile.getVirtualFile(), caretOffset, 0, lineLength); if (formatResult == null) { showHintLater(editor, DartBundle.message("dart.style.hint.failed"), true); LOG.warn("Unexpected response from edit_format, formatResult is null"); return; } final List<SourceEdit> edits = formatResult.getEdits(); if (edits == null || edits.size() == 0) { showHintLater(editor, DartBundle.message("dart.style.hint.already.good"), false); } else if (edits.size() == 1) { final String replacement = StringUtil.convertLineSeparators(edits.get(0).getReplacement()); document.replaceString(0, document.getTextLength(), replacement); final int offset = DartAnalysisServerService.getInstance() .getConvertedOffset(psiFile.getVirtualFile(), formatResult.getOffset()); editor.getCaretModel().moveToOffset(offset); showHintLater(editor, DartBundle.message("dart.style.hint.success"), false); } else { showHintLater(editor, DartBundle.message("dart.style.hint.failed"), true); LOG.warn( "Unexpected response from edit_format, formatResult.getEdits().size() = " + edits.size()); } }; ApplicationManager.getApplication() .runWriteAction( () -> CommandProcessor.getInstance() .executeCommand( project, runnable, DartBundle.message("dart.style.action.name"), null)); }
@Override @Nullable public GlobalSearchScope getSearchScope() { GlobalSearchScope scope = null; Module[] modules = getConvertContextModules(); if (modules.length != 0) { PsiFile file = getFile(); file = file.getOriginalFile(); VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile != null) { ProjectFileIndex fileIndex = ProjectRootManager.getInstance(file.getProject()).getFileIndex(); boolean tests = fileIndex.isInTestSourceContent(virtualFile); for (Module module : modules) { if (scope == null) { scope = module.getModuleRuntimeScope(tests); } else { scope = scope.union(module.getModuleRuntimeScope(tests)); } } } } return scope; // ??? scope == null ? GlobalSearchScope.allScope(getProject()) : scope; ??? }
@Override public void doCollectInformation(@NotNull ProgressIndicator progress) { if (ApplicationManager.getApplication().isUnitTestMode()) { return; } if (!WebEditorOptions.getInstance().isTagTreeHighlightingEnabled()) { return; } final PsiElement[] elements = BreadcrumbsXmlWrapper.getLinePsiElements( myEditor.getCaretModel().getOffset(), myFile.getVirtualFile(), myProject, myInfoProvider); if (elements == null || elements.length == 0) { return; } if (!XmlTagTreeHighlightingUtil.containsTagsWithSameName(elements)) { return; } for (int i = elements.length - 1; i >= 0; i--) { if (elements[i] instanceof XmlTag) { myPairsToHighlight.add(getTagRanges((XmlTag) elements[i])); } } }
public void execute(final String line, final int textEndOffset) { myResult = null; myInfo = parseExceptionLine(line); if (myInfo == null) { return; } myMethod = myInfo.getSecond().substring(line); final int lparenthIndex = myInfo.third.getStartOffset(); final int rparenthIndex = myInfo.third.getEndOffset(); final String fileAndLine = line.substring(lparenthIndex + 1, rparenthIndex).trim(); final int colonIndex = fileAndLine.lastIndexOf(':'); if (colonIndex < 0) return; final String lineString = fileAndLine.substring(colonIndex + 1); try { final int lineNumber = Integer.parseInt(lineString); myClass = findPositionClass(line); myFile = myClass == null ? null : (PsiFile) myClass.getContainingFile().getNavigationElement(); if (myFile == null) { // try find the file with the required name PsiFile[] files = PsiShortNamesCache.getInstance(myProject) .getFilesByName(fileAndLine.substring(0, colonIndex).trim()); if (files.length > 0) { myFile = files[0]; } } if (myFile == null) return; /* IDEADEV-4976: Some scramblers put something like SourceFile mock instead of real class name. final String filePath = fileAndLine.substring(0, colonIndex).replace('/', File.separatorChar); final int slashIndex = filePath.lastIndexOf(File.separatorChar); final String shortFileName = slashIndex < 0 ? filePath : filePath.substring(slashIndex + 1); if (!file.getName().equalsIgnoreCase(shortFileName)) return null; */ final int textStartOffset = textEndOffset - line.length(); final int highlightStartOffset = textStartOffset + lparenthIndex + 1; final int highlightEndOffset = textStartOffset + rparenthIndex; final VirtualFile virtualFile = myFile.getVirtualFile(); HyperlinkInfo linkInfo = new MyHyperlinkInfo(myProject, virtualFile, lineNumber); TextAttributes attributes = HYPERLINK_ATTRIBUTES.clone(); if (!ProjectRootManager.getInstance(myProject).getFileIndex().isInContent(virtualFile)) { Color color = UIUtil.getInactiveTextColor(); attributes.setForegroundColor(color); attributes.setEffectColor(color); } myResult = new Filter.Result(highlightStartOffset, highlightEndOffset, linkInfo, attributes); } catch (NumberFormatException e) { // } }
private void highlightInjectedSyntax( @NotNull PsiFile injectedPsi, @NotNull HighlightInfoHolder holder) { List<Trinity<IElementType, SmartPsiElementPointer<PsiLanguageInjectionHost>, TextRange>> tokens = InjectedLanguageUtil.getHighlightTokens(injectedPsi); if (tokens == null) return; final Language injectedLanguage = injectedPsi.getLanguage(); Project project = injectedPsi.getProject(); SyntaxHighlighter syntaxHighlighter = SyntaxHighlighterFactory.getSyntaxHighlighter( injectedLanguage, project, injectedPsi.getVirtualFile()); final TextAttributes defaultAttrs = myGlobalScheme.getAttributes(HighlighterColors.TEXT); for (Trinity<IElementType, SmartPsiElementPointer<PsiLanguageInjectionHost>, TextRange> token : tokens) { ProgressManager.checkCanceled(); IElementType tokenType = token.getFirst(); PsiLanguageInjectionHost injectionHost = token.getSecond().getElement(); if (injectionHost == null) continue; TextRange textRange = token.getThird(); TextAttributesKey[] keys = syntaxHighlighter.getTokenHighlights(tokenType); if (textRange.getLength() == 0) continue; TextRange annRange = textRange.shiftRight(injectionHost.getTextRange().getStartOffset()); // force attribute colors to override host' ones TextAttributes attributes = null; for (TextAttributesKey key : keys) { TextAttributes attrs2 = myGlobalScheme.getAttributes(key); if (attrs2 != null) { attributes = attributes == null ? attrs2 : TextAttributes.merge(attributes, attrs2); } } TextAttributes forcedAttributes; if (attributes == null || attributes.isEmpty() || attributes.equals(defaultAttrs)) { forcedAttributes = TextAttributes.ERASE_MARKER; } else { HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.INJECTED_LANGUAGE_FRAGMENT) .range(annRange) .textAttributes(TextAttributes.ERASE_MARKER) .createUnconditionally(); holder.add(info); forcedAttributes = new TextAttributes( attributes.getForegroundColor(), attributes.getBackgroundColor(), attributes.getEffectColor(), attributes.getEffectType(), attributes.getFontType()); } HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.INJECTED_LANGUAGE_FRAGMENT) .range(annRange) .textAttributes(forcedAttributes) .createUnconditionally(); holder.add(info); } }
public void testTransitivePathPackageDependencies() { myFixture.addFileToProject( "project1/pubspec.yaml", "name: project1\n" + "dependencies:\n" + " project2:\n" + " path: ../project2\n"); myFixture.addFileToProject( "project1/.packages", "project1:lib/\n" + "project2:../project2/lib/\n" + "project3:../project3/lib/\n"); myFixture.addFileToProject( "project2/pubspec.yaml", "name: project2\n" + "dependencies:\n" + " project1:\n" + " path: ../project1\n" + " project3:\n" + " path: ../project3\n"); myFixture.addFileToProject("project3/pubspec.yaml", "name: project3\n"); myFixture.addFileToProject("project3/.packages", "project3:lib/\n"); myFixture.addFileToProject("project2/lib/in_lib2.dart", "inLib2(){}"); myFixture.addFileToProject("project3/lib/in_lib3.dart", "inLib3(){}"); final PsiFile psiFile = myFixture.addFileToProject( "project1/lib/foo.dart", "import 'package:project2/in_lib2.dart';\n" + "import 'package:project3/in_lib3.dart';\n" + "main(){\n" + " inLib2<caret expected='project2/lib/in_lib2.dart -> inLib2'>();\n" + " inLib3<caret expected='project3/lib/in_lib3.dart -> inLib3'>();\n" + "}"); myFixture.openFileInEditor(psiFile.getVirtualFile()); doTest(myFixture); }
@Nullable @Override public String getLocationString() { if (!this.appendBundleLocation) { return this.locationString; } PsiFile psiFile = psiElement.getContainingFile(); if (psiFile == null) { return this.locationString; } String locationPathString = this.locationString; String bundleName = psiFile.getVirtualFile().getPath(); if (bundleName.contains("Bundle")) { bundleName = bundleName.substring(0, bundleName.lastIndexOf("Bundle")); if (bundleName.length() > 1 && bundleName.contains("/")) { return locationPathString + " " + bundleName.substring(bundleName.lastIndexOf("/") + 1, bundleName.length()) + "::" + psiFile.getName(); } } return locationPathString + " " + psiFile.getName(); }
public void testFilterOutImpossibleVariants() { PsiFile file = myFixture.addFileToProject( "Foo.java", "interface A {\n" + " void save();\n" + "}\n" + "interface B extends A {\n" + " void foo();\n" + "}\n" + "class X implements B {\n" + " public void foo() { }\n" + " public void save(){}\n" + "}\n" + "class Y implements A {\n" + " public void save(){}\n" + "}\n" + "class App {\n" + " private B b;\n" + " private void some() {\n" + " b.sa<caret>ve();\n" + " }\n" + "}"); myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); final PsiElement[] impls = getTargets(file); assertEquals(1, impls.length); final PsiElement method = impls[0]; assertTrue(method instanceof PsiMethod); final PsiClass aClass = ((PsiMethod) method).getContainingClass(); assertNotNull(aClass); assertEquals("X", aClass.getName()); }
public void testImplicitInheritance() { PsiFile file = myFixture.addFileToProject( "Foo.java", "interface PackContainer {\n" + " void foo();\n" + "}\n" + "interface PsiPackage extends PackContainer {}\n" + "class PsiPackageBase implements PackContainer {\n" + " public void foo() {}\n" + "}\n" + "class PsiPackageImpl extends PsiPackageBase implements PsiPackage {}\n" + "\n" + "class Foo {\n" + " class Bar {\n" + " void bar(PsiPackage i) {\n" + " i.fo<caret>o();\n" + " }\n" + " }\n" + "}"); myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); final PsiElement[] impls = getTargets(file); assertEquals(1, impls.length); final PsiElement method = impls[0]; assertTrue(method instanceof PsiMethod); final PsiClass aClass = ((PsiMethod) method).getContainingClass(); assertNotNull(aClass); assertEquals("PsiPackageBase", aClass.getName()); }
@Override public void actionPerformed(AnActionEvent event) { Presentation presentation = event.getPresentation(); DataContext dataContext = event.getDataContext(); Project project = CommonDataKeys.PROJECT.getData(dataContext); Editor editor = CommonDataKeys.EDITOR.getData(dataContext); if (project == null || editor == null) { presentation.setEnabled(false); return; } PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()); if (file == null || file.getVirtualFile() == null) { presentation.setEnabled(false); return; } boolean hasSelection = editor.getSelectionModel().hasSelection(); LayoutCodeDialog dialog = new LayoutCodeDialog(project, file, hasSelection, HELP_ID); dialog.show(); if (dialog.isOK()) { new FileInEditorProcessor(file, editor, dialog.getRunOptions()).processCode(); } }
public void testShowSelfNonAbstract() { // fails if groovy plugin is enabled: // org.jetbrains.plugins.groovy.codeInsight.JavaClsMethodElementEvaluator PsiFile file = myFixture.addFileToProject( "Foo.java", "public class Hello {\n" + " void foo(){}\n" + "\n" + " class A {\n" + " {\n" + " fo<caret>o();\n" + " }\n" + " }\n" + " class Hello1 extends Hello {\n" + " void foo() {}\n" + " }\n" + " class Hello2 extends Hello {\n" + " void foo() {}\n" + " }\n" + "}"); myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); final PsiElement[] impls = getTargets(file); assertEquals(3, impls.length); }
@NotNull public static Collection<PsiFileSystemItem> getAbsoluteTopLevelDirLocations( final @NotNull PsiFile file) { final VirtualFile virtualFile = file.getVirtualFile(); if (virtualFile == null) { return Collections.emptyList(); } final Project project = file.getProject(); PsiDirectory parent = file.getParent(); final Module module = ModuleUtil.findModuleForPsiElement(parent == null ? file : parent); if (module == null) { return Collections.emptyList(); } final FileReferenceHelper[] helpers = FileReferenceHelperRegistrar.getHelpers(); final ArrayList<PsiFileSystemItem> list = new ArrayList<PsiFileSystemItem>(); for (FileReferenceHelper helper : helpers) { if (helper.isMine(project, virtualFile)) { final Collection<PsiFileSystemItem> roots = helper.getRoots(module); for (PsiFileSystemItem root : roots) { LOG.assertTrue(root != null, "Helper " + helper + " produced a null root for " + file); } list.addAll(roots); } } if (list.size() == 0) { list.addAll(FileReferenceHelperRegistrar.getNotNullHelper(file).getRoots(module)); } return list; }
public PsiDirectory[] getSelectedDirectories() { final PsiElement[] elements = getSelectedPSIElements(); if (elements.length == 1) { final PsiElement element = elements[0]; if (element instanceof PsiDirectory) { return new PsiDirectory[] {(PsiDirectory) element}; } else if (element instanceof PsiDirectoryContainer) { return ((PsiDirectoryContainer) element).getDirectories(); } else { final PsiFile containingFile = element.getContainingFile(); if (containingFile != null) { final PsiDirectory psiDirectory = containingFile.getContainingDirectory(); if (psiDirectory != null) { return new PsiDirectory[] {psiDirectory}; } final VirtualFile file = containingFile.getVirtualFile(); if (file instanceof VirtualFileWindow) { final VirtualFile delegate = ((VirtualFileWindow) file).getDelegate(); final PsiFile delegatePsiFile = containingFile.getManager().findFile(delegate); if (delegatePsiFile != null && delegatePsiFile.getContainingDirectory() != null) { return new PsiDirectory[] {delegatePsiFile.getContainingDirectory()}; } } return PsiDirectory.EMPTY_ARRAY; } } } else { final DefaultMutableTreeNode selectedNode = getSelectedNode(); if (selectedNode != null) { return getSelectedDirectoriesInAmbiguousCase(selectedNode); } } return PsiDirectory.EMPTY_ARRAY; }
private static BundleManifest readProperties(PsiFile propertiesFile) { try { UTF8Properties properties = new UTF8Properties(); properties.load(new StringReader(propertiesFile.getText())); Map<String, String> map = ContainerUtil.newHashMap(); for (Object key : properties.keySet()) { String name = key.toString(); map.put(name, properties.getProperty(name)); } if (map.get(Constants.BUNDLE_SYMBOLICNAME) == null) { VirtualFile file = propertiesFile.getVirtualFile(); if (file != null) { if (!BndProjectImporter.BND_FILE.equals(file.getName())) { map.put( Constants.BUNDLE_SYMBOLICNAME, FileUtil.getNameWithoutExtension(file.getName())); } else if (file.getParent() != null) { map.put(Constants.BUNDLE_SYMBOLICNAME, file.getParent().getName()); } } } return new BundleManifest(map, propertiesFile); } catch (IOException ignored) { } catch (InvalidVirtualFileAccessException ignored) { } return null; }
public void testNoRecursiveImports() throws Exception { myFixture.addFileToProject("file2.dart", "inFile2(){}"); myFixture.addFileToProject("file1.dart", "import 'file2.dart'\n inFile1(){}"); myFixture.addFileToProject( "file.dart", "library fileLib;\n" + "import 'file1.dart';\n" + "part 'filePart1.dart';\n" + "part 'filePart2.dart';\n" + "inFile(){}"); myFixture.addFileToProject("filePart1.dart", "part of fileLib;\n" + "inFilePart1(){}"); final PsiFile file = myFixture.addFileToProject( "filePart2.dart", "part of fileLib;\n" + "inFilePart2(){\n" + " <caret expected='filePart1.dart -> inFilePart1'>inFilePart1()\n" + " <caret expected='filePart2.dart -> inFilePart2'>inFilePart2()\n" + " <caret expected='file.dart -> inFile'>inFile()\n" + " <caret expected='file1.dart -> inFile1'>inFile1()\n" + " <caret expected=''>inFile2()\n" + "}"); myFixture.openFileInEditor(file.getVirtualFile()); doTest(myFixture); }
public void testMethodReferences() { PsiFile file = myFixture.addFileToProject( "Foo.java", "interface I {void f();}\n" + "class A implements I { public void f(){}}\n" + "class B implements I { public void f(){}}\n" + "class C {\n" + " void foo(java.util.List<I> l) {l.stream().forEach(I::<caret>f);}" + "}"); myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); final PsiElement[] impls = getTargets(file); assertEquals(2, impls.length); // target are non-deterministic now Arrays.sort( impls, (o1, o2) -> { String name1 = ((PsiMethod) o1).getContainingClass().getName(); String name2 = ((PsiMethod) o2).getContainingClass().getName(); return StringUtil.compare(name1, name2, false); }); final PsiElement method = impls[0]; assertTrue(method instanceof PsiMethod); final PsiClass aClass = ((PsiMethod) method).getContainingClass(); assertNotNull(aClass); assertEquals("A", aClass.getName()); }
public void testTransitiveShowHide() throws Exception { myFixture.addFileToProject( "file1part.dart", "part of file1lib;\n" + "var foo1, foo2, foo3, foo4, foo5, foo6, foo7, foo8, foo9;"); myFixture.addFileToProject("file1.dart", "library file1lib;\n" + "part 'file1part.dart';"); myFixture.addFileToProject("file2.dart", "export 'file1.dart' show foo1, foo2, foo3, foo4;"); myFixture.addFileToProject("file3.dart", "export 'file1.dart' show foo5, foo6, foo7, foo8;"); myFixture.addFileToProject( "file4.dart", "export 'file2.dart' show foo1, foo2, foo3, foo5, foo6, foo7, foo8, foo9;"); myFixture.addFileToProject("file5.dart", "export 'file3.dart' hide foo7, foo8;"); myFixture.addFileToProject( "file.dart", "library filelib;\n" + "import 'file4.dart' hide foo3;\n" + "import 'file5.dart' show foo1, foo2, foo3, foo4, foo5, foo7, foo8, foo9;\n" + "part 'filepart.dart';"); final PsiFile file = myFixture.addFileToProject( "filepart.dart", "part of filelib;\n" + "main(){\n" + " <caret expected='file1part.dart -> foo1'>foo1;\n" + " <caret expected='file1part.dart -> foo2'>foo2;\n" + " <caret expected=''>foo3;\n" + " <caret expected=''>foo4;\n" + " <caret expected='file1part.dart -> foo5'>foo5;\n" + " <caret expected=''>foo6;\n" + " <caret expected=''>foo7;\n" + " <caret expected=''>foo8;\n" + " <caret expected=''>foo9;\n" + "}"); myFixture.openFileInEditor(file.getVirtualFile()); doTest(myFixture); }
public void testToStringOnUnqualified() { final PsiFile file = myFixture.addFileToProject( "Foo.java", "public class Fix {\n" + " {\n" + " <caret>toString();\n" + " }\n" + "}\n" + "class FixImpl1 extends Fix {\n" + " @Override\n" + " public String toString() {\n" + " return \"Impl1\";\n" + " }\n" + "}\n" + "class FixImpl2 extends Fix {\n" + " @Override\n" + " public String toString() {\n" + " return \"Impl2\";\n" + " }\n" + "}"); myFixture.configureFromExistingVirtualFile(file.getVirtualFile()); PlatformTestUtil.startPerformanceTest( getTestName(false), 150, () -> { PsiElement[] impls = getTargets(file); assertEquals(3, impls.length); }) .cpuBound() .usesAllCPUCores() .assertTiming(); }
@Nullable private Navigatable getSelectedNavigatable( final CommonProblemDescriptor descriptor, final PsiElement psiElement) { if (descriptor instanceof ProblemDescriptorBase) { Navigatable navigatable = ((ProblemDescriptorBase) descriptor).getNavigatable(); if (navigatable != null) { return navigatable; } } if (psiElement == null || !psiElement.isValid()) return null; PsiFile containingFile = psiElement.getContainingFile(); VirtualFile virtualFile = containingFile == null ? null : containingFile.getVirtualFile(); if (virtualFile != null) { int startOffset = psiElement.getTextOffset(); if (descriptor instanceof ProblemDescriptorBase) { final TextRange textRange = ((ProblemDescriptorBase) descriptor).getTextRangeForNavigation(); if (textRange != null) { if (virtualFile instanceof VirtualFileWindow) { virtualFile = ((VirtualFileWindow) virtualFile).getDelegate(); } startOffset = textRange.getStartOffset(); } } return new OpenFileDescriptor(myProject, virtualFile, startOffset); } return null; }
public List<HighlightInfo> runMainPasses( @NotNull PsiFile psiFile, @NotNull Document document, @NotNull final ProgressIndicator progress) { final List<HighlightInfo> result = new ArrayList<HighlightInfo>(); final VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile != null && !virtualFile.getFileType().isBinary()) { final List<TextEditorHighlightingPass> passes = TextEditorHighlightingPassRegistrarEx.getInstanceEx(myProject) .instantiateMainPasses(psiFile, document); Collections.sort( passes, new Comparator<TextEditorHighlightingPass>() { @Override public int compare(TextEditorHighlightingPass o1, TextEditorHighlightingPass o2) { if (o1 instanceof GeneralHighlightingPass) return -1; if (o2 instanceof GeneralHighlightingPass) return 1; return 0; } }); for (TextEditorHighlightingPass pass : passes) { pass.doCollectInformation(progress); result.addAll(pass.getInfos()); } } return result; }
public AddModuleDependencyFix( Module currentModule, VirtualFile classVFile, PsiClass[] classes, PsiReference reference) { final PsiElement psiElement = reference.getElement(); final Project project = psiElement.getProject(); final JavaPsiFacade facade = JavaPsiFacade.getInstance(project); final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex(); for (PsiClass aClass : classes) { if (!facade.getResolveHelper().isAccessible(aClass, psiElement, aClass)) continue; PsiFile psiFile = aClass.getContainingFile(); if (psiFile == null) continue; VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile == null) continue; final Module classModule = fileIndex.getModuleForFile(virtualFile); if (classModule != null && classModule != currentModule && !ModuleRootManager.getInstance(currentModule).isDependsOn(classModule)) { myModules.add(classModule); } } myCurrentModule = currentModule; myClassVFile = classVFile; myClasses = classes; myReference = reference; }
private boolean getStringToReplace( int textOffset, int textEndOffset, Document document, FindModel findModel, Ref<String> stringToReplace) throws FindManager.MalformedReplacementStringException { if (textOffset < 0 || textOffset >= document.getTextLength()) { return false; } if (textEndOffset < 0 || textOffset > document.getTextLength()) { return false; } FindManager findManager = FindManager.getInstance(myProject); final CharSequence foundString = document.getCharsSequence().subSequence(textOffset, textEndOffset); PsiFile file = PsiDocumentManager.getInstance(myProject).getPsiFile(document); FindResult findResult = findManager.findString( document.getCharsSequence(), textOffset, findModel, file != null ? file.getVirtualFile() : null); if (!findResult.isStringFound()) { return false; } stringToReplace.set( FindManager.getInstance(myProject) .getStringToReplace(foundString.toString(), findModel, textOffset, document.getText())); return true; }
@Override public PsiElement getTopLevelElement(PsiElement element) { PsiFile file = element.getContainingFile(); if (file == null || !(file instanceof JetFile)) return null; VirtualFile virtualFile = file.getVirtualFile(); if (!fileInRoots(virtualFile)) return file; PsiElement current = element; while (current != null) { if (isSelectable(current)) break; current = current.getParent(); } if (current instanceof JetFile) { List<JetDeclaration> declarations = ((JetFile) current).getDeclarations(); String nameWithoutExtension = virtualFile != null ? virtualFile.getNameWithoutExtension() : file.getName(); if (declarations.size() == 1 && declarations.get(0) instanceof JetClassOrObject && nameWithoutExtension.equals(declarations.get(0).getName())) { current = declarations.get(0); } } return current != null ? current : file; }
public XmlTagTreeHighlightingPass(@NotNull PsiFile file, @NotNull EditorEx editor) { super(file.getProject(), editor.getDocument(), true); myFile = file; myEditor = editor; final FileViewProvider viewProvider = file.getManager().findViewProvider(file.getVirtualFile()); myInfoProvider = BreadcrumbsXmlWrapper.findInfoProvider(viewProvider); }
private void updateEditorText() { disposeNonTextEditor(); final PsiElement elt = myElements[myIndex].getNavigationElement(); Project project = elt.getProject(); PsiFile psiFile = getContainingFile(elt); final VirtualFile vFile = psiFile.getVirtualFile(); if (vFile == null) return; final FileEditorProvider[] providers = FileEditorProviderManager.getInstance().getProviders(project, vFile); for (FileEditorProvider provider : providers) { if (provider instanceof TextEditorProvider) { updateTextElement(elt); myBinarySwitch.show(myViewingPanel, TEXT_PAGE_KEY); break; } else if (provider.accept(project, vFile)) { myCurrentNonTextEditorProvider = provider; myNonTextEditor = myCurrentNonTextEditorProvider.createEditor(project, vFile); myBinaryPanel.removeAll(); myBinaryPanel.add(myNonTextEditor.getComponent()); myBinarySwitch.show(myViewingPanel, BINARY_PAGE_KEY); break; } } }
public static void clearCaches( @NotNull PsiFile injected, @NotNull DocumentWindowImpl documentWindow) { VirtualFileWindowImpl virtualFile = (VirtualFileWindowImpl) injected.getVirtualFile(); PsiManagerEx psiManagerEx = (PsiManagerEx) injected.getManager(); if (psiManagerEx.getProject().isDisposed()) return; psiManagerEx.getFileManager().setViewProvider(virtualFile, null); PsiElement context = InjectedLanguageManager.getInstance(injected.getProject()).getInjectionHost(injected); PsiFile hostFile; if (context != null) { hostFile = context.getContainingFile(); } else { VirtualFile delegate = virtualFile.getDelegate(); hostFile = delegate.isValid() ? psiManagerEx.findFile(delegate) : null; } if (hostFile != null) { // modification of cachedInjectedDocuments must be under PsiLock synchronized (PsiLock.LOCK) { List<DocumentWindow> cachedInjectedDocuments = getCachedInjectedDocuments(hostFile); for (int i = cachedInjectedDocuments.size() - 1; i >= 0; i--) { DocumentWindow cachedInjectedDocument = cachedInjectedDocuments.get(i); if (cachedInjectedDocument == documentWindow) { cachedInjectedDocuments.remove(i); } } } } }