@Nullable private Project findProject(String file) { LocalFileSystem localFileSystem = LocalFileSystem.getInstance(); ProjectLocator projectLocator = ProjectLocator.getInstance(); AtomicReference<Project> ret = new AtomicReference<>(); FileUtil.processFilesRecursively( new File(file), (f) -> { VirtualFile vf = localFileSystem.findFileByIoFile(f); if (vf != null) { ret.set(projectLocator.guessProjectForFile(vf)); return false; } return true; }); return ret.get(); }
@Nullable("returns null if charset set cannot be determined from content") Charset computeCharsetFromContent(@NotNull final VirtualFile virtualFile) { final Document document = FileDocumentManager.getInstance().getDocument(virtualFile); if (document == null) { return null; } Charset cached = EncodingManager.getInstance().getCachedCharsetFromContent(document); if (cached != null) { return cached; } final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile); return ApplicationManager.getApplication().runReadAction(new Computable<Charset>() { @Override public Charset compute() { Charset charsetFromContent = LoadTextUtil.charsetFromContentOrNull(project, virtualFile, document.getImmutableCharSequence()); if (charsetFromContent != null) { setCachedCharsetFromContent(charsetFromContent, null, document); } return charsetFromContent; } }); }
@Nullable @Override public PsiFile getPsiFile(@NotNull Document document) { final PsiFile psiFile = super.getPsiFile(document); if (myUnitTestMode) { final VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document); if (virtualFile != null && virtualFile.isValid()) { Collection<Project> projects = ProjectLocator.getInstance().getProjectsForFile(virtualFile); if (!projects.isEmpty() && !projects.contains(myProject)) { LOG.error("Trying to get PSI for an alien project. VirtualFile=" + virtualFile + ";\n myProject=" + myProject + ";\n projects returned: " + projects); } } } return psiFile; }
@Nullable("returns null if charset set cannot be determined from content") public Charset computeCharsetFromContent(@NotNull final VirtualFile virtualFile) { final Document document = FileDocumentManager.getInstance().getDocument(virtualFile); if (document == null) return null; final Charset cached = EncodingManager.getInstance().getCachedCharsetFromContent(document); if (cached != null) return cached; final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile); return ApplicationManager.getApplication().runReadAction(new Computable<Charset>() { @Override public Charset compute() { Charset charsetFromContent = LoadTextUtil.charsetFromContentOrNull(project, virtualFile, document.getText()); if (charsetFromContent != null) { setCachedCharsetFromContent(charsetFromContent, cached, document); } return charsetFromContent; } }); }
public static void saveIn(@NotNull final Document document, final Editor editor, @NotNull final VirtualFile virtualFile, @NotNull final Charset charset) { FileDocumentManager documentManager = FileDocumentManager.getInstance(); documentManager.saveDocument(document); final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile); boolean writable = project == null ? virtualFile.isWritable() : ReadonlyStatusHandler.ensureFilesWritable(project, virtualFile); if (!writable) { CommonRefactoringUtil.showErrorHint(project, editor, "Cannot save the file " + virtualFile.getPresentableUrl(), "Unable to Save", null); return; } // first, save the file in the new charset and then mark the file as having the correct encoding virtualFile.setCharset(charset); try { LoadTextUtil.write(project, virtualFile, virtualFile, document.getText(), document.getModificationStamp()); } catch (IOException io) { Messages.showErrorDialog(project, io.getMessage(), "Error Writing File"); } EncodingProjectManagerImpl.suppressReloadDuring(new Runnable() { @Override public void run() { EncodingManager.getInstance().setEncoding(virtualFile, charset); } }); }
@Nullable @Override public PsiFile getPsiFile(@NotNull Document document) { final PsiFile psiFile = super.getPsiFile(document); if (ApplicationManager.getApplication().isUnitTestMode()) { final VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document); if (virtualFile != null && virtualFile.isValid()) { Collection<Project> projects = ProjectLocator.getInstance().getProjectsForFile(virtualFile); LOG.assertTrue(projects.isEmpty() || projects.contains(myProject), "Trying to get PSI for an alien project. VirtualFile=" + virtualFile + ";\n myProject=" + myProject + ";\n projects returned: " + projects); } } return psiFile; }
/** * @return returns null if charset set cannot be determined from content */ @Nullable Charset computeCharsetFromContent(@Nonnull final VirtualFile virtualFile) { final Document document = FileDocumentManager.getInstance().getDocument(virtualFile); if (document == null) { return null; } Charset cached = EncodingManager.getInstance().getCachedCharsetFromContent(document); if (cached != null) { return cached; } final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile); return ApplicationManager.getApplication().runReadAction((Computable<Charset>)() -> { Charset charsetFromContent = LoadTextUtil.charsetFromContentOrNull(project, virtualFile, document.getImmutableCharSequence()); if (charsetFromContent != null) { setCachedCharsetFromContent(charsetFromContent, null, document); } return charsetFromContent; }); }
@Nullable @Override public PsiFile getPsiFile(@Nonnull Document document) { final PsiFile psiFile = super.getPsiFile(document); if (myUnitTestMode) { final VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document); if (virtualFile != null && virtualFile.isValid()) { Collection<Project> projects = ProjectLocator.getInstance().getProjectsForFile(virtualFile); if (!projects.isEmpty() && !projects.contains(myProject)) { LOG.error("Trying to get PSI for an alien project. VirtualFile=" + virtualFile + ";\n myProject=" + myProject + ";\n projects returned: " + projects); } } } return psiFile; }
@Nullable public static Pair<VirtualFile, Project> findByAbsolutePath(@NotNull String path) { File file = new File(FileUtil.toSystemDependentName(path)); if (file.exists()) { VirtualFile vFile = findVirtualFile(file); if (vFile != null) { Project project = ProjectLocator.getInstance().guessProjectForFile(vFile); if (project != null) { return Pair.create(vFile, project); } } } return null; }
public VcsDirtyScopeVfsListener() { myProjectLocator = ProjectLocator.getInstance(); myMessageBusConnection = ApplicationManager.getApplication().getMessageBus().connect(); myLock = new Object(); myQueue = new ArrayList<FileAndDirsCollector>(); myDirtReporter = new Runnable() { @Override public void run() { ArrayList<FileAndDirsCollector> list; synchronized (myLock) { list = new ArrayList<FileAndDirsCollector>(myQueue); myQueue.clear(); } Map<VcsDirtyScopeManager, Couple<HashSet<FilePath>>> map = new HashMap<VcsDirtyScopeManager, Couple<HashSet<FilePath>>>(); for (FileAndDirsCollector collector : list) { Map<VcsDirtyScopeManager, Couple<HashSet<FilePath>>> pairMap = collector.map; for (Map.Entry<VcsDirtyScopeManager, Couple<HashSet<FilePath>>> entry : pairMap .entrySet()) { final VcsDirtyScopeManager key = entry.getKey(); Couple<HashSet<FilePath>> existing = map.get(key); Couple<HashSet<FilePath>> value = entry.getValue(); if (existing != null) { existing.getFirst().addAll(value.getFirst()); existing.getSecond().addAll(value.getSecond()); } else { map.put(key, value); } } } new FileAndDirsCollector().markDirty(map); } }; myZipperUpdater = new ConstantZipperUpdater(300, Alarm.ThreadToUse.POOLED_THREAD, ApplicationManager.getApplication(), myDirtReporter); }
public void startOperation(@NotNull VirtualFile file) { if (!myIsInCommand) { // currently actions like "new project", "import project" (probably also others) are not performed under command myGuessedProject = ProjectLocator.getInstance().guessProjectForFile(file); if (myGuessedProject != null) { commandStarted(myGuessedProject); } } }
public VcsDirtyScopeVfsListener() { myProjectLocator = ProjectLocator.getInstance(); myMessageBusConnection = ApplicationManager.getApplication().getMessageBus().connect(); myLock = new Object(); myQueue = new ArrayList<FileAndDirsCollector>(); myDirtReporter = new Runnable() { @Override public void run() { ArrayList<FileAndDirsCollector> list; synchronized (myLock) { list = new ArrayList<FileAndDirsCollector>(myQueue); myQueue.clear(); } Map<VcsDirtyScopeManager, Pair<HashSet<FilePath>, HashSet<FilePath>>> map = new HashMap<VcsDirtyScopeManager, Pair<HashSet<FilePath>, HashSet<FilePath>>>(); for (FileAndDirsCollector collector : list) { Map<VcsDirtyScopeManager, Pair<HashSet<FilePath>, HashSet<FilePath>>> pairMap = collector.map; for (Map.Entry<VcsDirtyScopeManager, Pair<HashSet<FilePath>, HashSet<FilePath>>> entry : pairMap .entrySet()) { final VcsDirtyScopeManager key = entry.getKey(); Pair<HashSet<FilePath>, HashSet<FilePath>> existing = map.get(key); Pair<HashSet<FilePath>, HashSet<FilePath>> value = entry.getValue(); if (existing != null) { existing.getFirst().addAll(value.getFirst()); existing.getSecond().addAll(value.getSecond()); } else { map.put(key, value); } } } new FileAndDirsCollector().markDirty(map); } }; myZipperUpdater = new ConstantZipperUpdater(300, Alarm.ThreadToUse.POOLED_THREAD, ApplicationManager.getApplication(), myDirtReporter); }
@Nullable private static Pair<VirtualFile, Project> findByAbsolutePath(String path) { File file = new File(FileUtil.toSystemDependentName(path)); if (file.exists()) { VirtualFile vFile = findVirtualFile(file); if (vFile != null) { Project project = ProjectLocator.getInstance().guessProjectForFile(vFile); if (project != null) { return Pair.create(vFile, project); } } } return null; }
@Nullable private static Project getProject(Object[] args) { for (Object arg : args) { if (arg instanceof VirtualFile) { return ProjectLocator.getInstance().guessProjectForFile((VirtualFile) arg); } } return null; }
public boolean askReloadFromDisk(VirtualFile file, Document document) { String message = UIBundle.message("file.cache.conflict.message.text", file.getPresentableUrl()); final DialogBuilder builder = new DialogBuilder(); builder.setCenterPanel(new JLabel(message, Messages.getQuestionIcon(), SwingConstants.CENTER)); builder.addOkAction().setText(UIBundle.message("file.cache.conflict.load.fs.changes.button")); builder.addCancelAction().setText(UIBundle.message("file.cache.conflict.keep.memory.changes.button")); builder.addAction(new AbstractAction(UIBundle.message("file.cache.conflict.show.difference.button")) { @Override public void actionPerformed(ActionEvent e) { final ProjectEx project = (ProjectEx)ProjectLocator.getInstance().guessProjectForFile(file); FileType fileType = file.getFileType(); String fsContent = LoadTextUtil.loadText(file).toString(); DocumentContent content1 = DiffContentFactory.getInstance().create(project, fsContent, fileType); DocumentContent content2 = DiffContentFactory.getInstance().create(project, document, file); String title = UIBundle.message("file.cache.conflict.for.file.dialog.title", file.getPresentableUrl()); String title1 = UIBundle.message("file.cache.conflict.diff.content.file.system.content"); String title2 = UIBundle.message("file.cache.conflict.diff.content.memory.content"); DiffRequest request = new SimpleDiffRequest(title, content1, content2, title1, title2); request.putUserData(DiffUserDataKeys.GO_TO_SOURCE_DISABLE, true); DialogBuilder diffBuilder = new DialogBuilder(project); DiffRequestPanel diffPanel = DiffManager.getInstance().createRequestPanel(project, diffBuilder, diffBuilder.getWindow()); diffPanel.setRequest(request); diffBuilder.setCenterPanel(diffPanel.getComponent()); diffBuilder.setDimensionServiceKey("FileDocumentManager.FileCacheConflict"); diffBuilder.addOkAction().setText(UIBundle.message("file.cache.conflict.save.changes.button")); diffBuilder.addCancelAction(); diffBuilder.setTitle(title); if (diffBuilder.show() == DialogWrapper.OK_EXIT_CODE) { builder.getDialogWrapper().close(DialogWrapper.CANCEL_EXIT_CODE); } } }); builder.setTitle(UIBundle.message("file.cache.conflict.dialog.title")); builder.setButtonsAlignment(SwingConstants.CENTER); builder.setHelpId("reference.dialogs.fileCacheConflict"); return builder.show() == 0; }
@Nullable private static Project guessProject(final VirtualFile virtualFile) { return ProjectLocator.getInstance().guessProjectForFile(virtualFile); }
@Override public TemplateModel exec(List args) throws TemplateModelException { if (args.size() != 1) { throw new TemplateModelException("Wrong arguments"); } String artifact = ((TemplateScalarModel)args.get(0)).getAsString(); if (artifact.isEmpty()) { return TemplateBooleanModel.FALSE; } if (myParamMap.containsKey(TemplateMetadata.ATTR_DEPENDENCIES_LIST)) { Object listObject = myParamMap.get(TemplateMetadata.ATTR_DEPENDENCIES_LIST); if (listObject instanceof List) { @SuppressWarnings("unchecked") List<String> dependencyList = (List<String>)listObject; for (String dependency : dependencyList) { if (dependency.contains(artifact)) { return TemplateBooleanModel.TRUE; } } } } // Find the corresponding module, if any String modulePath = (String)myParamMap.get(TemplateMetadata.ATTR_PROJECT_OUT); if (modulePath != null) { VirtualFile file = LocalFileSystem.getInstance().findFileByIoFile(new File(modulePath.replace('/', File.separatorChar))); if (file != null) { Project project = ProjectLocator.getInstance().guessProjectForFile(file); if (project != null) { Module module = ModuleUtilCore.findModuleForFile(file, project); if (module != null) { AndroidFacet facet = AndroidFacet.getInstance(module); if (facet != null) { IdeaAndroidProject gradleProject = facet.getIdeaAndroidProject(); if (gradleProject != null) { return GradleUtil.dependsOn(gradleProject, artifact) ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE; } } } } } } // Creating a new module, so no existing dependencies: provide some defaults. This is really intended for appcompat-v7, // but since it depends on support-v4, we include it here (such that a query to see if support-v4 is installed in a newly // created project will return true since it will be by virtue of appcompat also being installed.) if (artifact.contains(SdkConstants.APPCOMPAT_LIB_ARTIFACT) || artifact.contains(SdkConstants.SUPPORT_LIB_ARTIFACT)) { // No dependencies: Base it off of the minApi and buildApi versions: // If building with Lollipop, and targeting anything earlier than Lollipop, use appcompat. // (Also use it if minApi is less than ICS.) Object buildApiObject = myParamMap.get(TemplateMetadata.ATTR_BUILD_API); Object minApiObject = myParamMap.get(TemplateMetadata.ATTR_MIN_API_LEVEL); if (buildApiObject instanceof Integer && minApiObject instanceof Integer) { int buildApi = (Integer)buildApiObject; int minApi = (Integer)minApiObject; return minApi >= 8 && ((buildApi >= 21 && minApi < 21) || minApi < 14) ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE; } } return TemplateBooleanModel.FALSE; }
@Override protected void setUp() throws Exception { super.setUp(); initApplication(); registerExtension(FileTypeFactory.FILE_TYPE_FACTORY_EP, new InternalFileTypeFactory()); registerExtensionPoint(FileDocumentSynchronizationVetoer.EP_NAME, FileDocumentSynchronizationVetoer.class); getApplication().registerService(CommandProcessor.class, new MyMockCommandProcessor()); getApplication().registerService(CodeStyleFacade.class, new DefaultCodeStyleFacade()); getApplication().registerService(ProjectLocator.class, new DefaultProjectLocator()); MockEditorFactory editorFactory = new MockEditorFactory(); getApplication().registerService(EditorFactory.class, editorFactory); final LanguageFileType[] fileType = {null}; getApplication().addComponent(FileTypeManager.class, new FileTypeManagerImpl(null, new MockSchemesManagerFactory()) { @NotNull @Override public FileType getFileTypeByFileName(@NotNull String fileName) { return fileType[0]; } @NotNull @Override public FileType getFileTypeByFile(@NotNull VirtualFile file) { return fileType[0]; } @NotNull @Override public FileType getFileTypeByExtension(@NotNull String extension) { return fileType[0]; } }); fileType[0] = StdFileTypes.JAVA; getApplication().getComponent(FileTypeManager.class); final VirtualFileManager virtualFileManager = EasyMock.createMock(VirtualFileManager.class); final ProjectManager projectManager = EasyMock.createMock(ProjectManager.class); myDocumentManager = new MyMockFileDocumentManager(virtualFileManager, projectManager); getApplication().registerService(FileDocumentManager.class, myDocumentManager); getApplication().registerService(DataManager.class, new DataManagerImpl()); }