com.intellij.openapi.project.ProjectLocator Java Examples

The following examples show how to use com.intellij.openapi.project.ProjectLocator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaGetImportCandidatesHandler.java    From ijaas with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #2
Source File: JavaCompleteHandler.java    From ijaas with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #3
Source File: PersistentFSImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public VirtualFile createChildFile(Object requestor, @Nonnull VirtualFile parent, @Nonnull String file) throws IOException {
  getDelegate(parent).createChildFile(requestor, parent, file);
  processEvent(new VFileCreateEvent(requestor, parent, file, false, null, null, false, null));

  final VirtualFile child = parent.findChild(file);
  if (child == null) {
    throw new IOException("Cannot create child file '" + file + "' at " + parent.getPath());
  }
  if (child.getCharset().equals(StandardCharsets.UTF_8)) {
    Project project = ProjectLocator.getInstance().guessProjectForFile(child);
    EncodingManager encodingManager = project == null ? EncodingManager.getInstance() : EncodingProjectManager.getInstance(project);
    if (encodingManager.shouldAddBOMForNewUtf8File()) {
      child.setBOM(CharsetToolkit.UTF8_BOM);
    }
  }
  return child;
}
 
Example #4
Source File: EncodingManagerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * @param virtualFile
 * @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 ReadAction.compute(() -> {
    Charset charsetFromContent = LoadTextUtil.charsetFromContentOrNull(project, virtualFile, document.getImmutableCharSequence());
    if (charsetFromContent != null) {
      setCachedCharsetFromContent(charsetFromContent, null, document);
    }
    return charsetFromContent;
  });
}
 
Example #5
Source File: EncodingUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
static void saveIn(@Nonnull final Document document, final Editor editor, @Nonnull final VirtualFile virtualFile, @Nonnull 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;
  }

  EncodingProjectManagerImpl.suppressReloadDuring(() -> {
    EncodingManager.getInstance().setEncoding(virtualFile, charset);
    try {
      ApplicationManager.getApplication().runWriteAction((ThrowableComputable<Object, IOException>)() -> {
        virtualFile.setCharset(charset);
        LoadTextUtil.write(project, virtualFile, virtualFile, document.getText(), document.getModificationStamp());
        return null;
      });
    }
    catch (IOException io) {
      Messages.showErrorDialog(project, io.getMessage(), "Error Writing File");
    }
  });
}
 
Example #6
Source File: StubUpdatingIndex.java    From consulo with Apache License 2.0 6 votes vote down vote up
public static boolean canHaveStub(@Nonnull ProjectLocator projectLocator, @Nullable Project project, @Nonnull VirtualFile file) {
  FileType fileType = SubstitutedFileType.substituteFileType(file, file.getFileType(), project == null ? projectLocator.guessProjectForFile(file) : project);
  if (fileType instanceof LanguageFileType) {
    final Language l = ((LanguageFileType)fileType).getLanguage();
    final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l);
    if (parserDefinition == null) {
      return false;
    }

    final IFileElementType elementType = parserDefinition.getFileNodeType();
    if (elementType instanceof IStubFileElementType) {
      if (((IStubFileElementType)elementType).shouldBuildStubFor(file)) {
        return true;
      }
      FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();
      if (file instanceof NewVirtualFile &&
          fileBasedIndex instanceof FileBasedIndexImpl &&
          ((FileBasedIndexImpl)fileBasedIndex).getIndex(INDEX_ID).isIndexedStateForFile(((NewVirtualFile)file).getId(), file)) {
        return true;
      }
    }
  }
  final BinaryFileStubBuilder builder = BinaryFileStubBuilders.INSTANCE.forFileType(fileType);
  return builder != null && builder.acceptsFile(file);
}
 
Example #7
Source File: EmbeditorUtil.java    From neovim-intellij-complete with MIT License 5 votes vote down vote up
@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;
}
 
Example #8
Source File: ChangeFileEncodingAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected boolean chosen(final Document document, final Editor editor, @Nullable final VirtualFile virtualFile, byte[] bytes, @Nonnull final Charset charset) {
  if (virtualFile == null) return false;
  String text = document.getText();
  EncodingUtil.Magic8 isSafeToConvert = EncodingUtil.isSafeToConvertTo(virtualFile, text, bytes, charset);
  EncodingUtil.Magic8 isSafeToReload = EncodingUtil.isSafeToReloadIn(virtualFile, text, bytes, charset);

  final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
  return changeTo(project, document, editor, virtualFile, charset, isSafeToConvert, isSafeToReload);
}
 
Example #9
Source File: PsiDocumentManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #10
Source File: MemoryDiskConflictResolver.java    From consulo with Apache License 2.0 4 votes vote down vote up
boolean askReloadFromDisk(VirtualFile file, Document document) {
  if (myConflictAppeared != null) {
    Throwable trace = myConflictAppeared;
    myConflictAppeared = null;
    throw new IllegalStateException("Unexpected memory-disk conflict in tests for " + file.getPath() + ", please use FileDocumentManager#reloadFromDisk or avoid VFS refresh", trace);
  }

  String message = UIBundle.message("file.cache.conflict.message.text", file.getPresentableUrl());

  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) {
      Project project = ProjectLocator.getInstance().guessProjectForFile(file);
      String fsContent = LoadTextUtil.loadText(file).toString();
      DocumentContent content1 = DiffContentFactory.getInstance().create(project, fsContent, file.getFileType());
      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;
}
 
Example #11
Source File: FileDocumentManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void doSaveDocumentInWriteAction(@Nonnull final Document document, @Nonnull final VirtualFile file) throws IOException {
  if (!file.isValid()) {
    removeFromUnsaved(document);
    return;
  }

  if (!file.equals(getFile(document))) {
    registerDocument(document, file);
  }

  boolean saveNeeded = false;
  IOException ioException = null;
  try {
    saveNeeded = isSaveNeeded(document, file);
  }
  catch (IOException e) {
    // in case of corrupted VFS try to stay consistent
    ioException = e;
  }
  if (!saveNeeded) {
    if (document instanceof DocumentEx) {
      ((DocumentEx)document).setModificationStamp(file.getModificationStamp());
    }
    removeFromUnsaved(document);
    updateModifiedProperty(file);
    if (ioException != null) throw ioException;
    return;
  }

  PomModelImpl.guardPsiModificationsIn(() -> {
    myMultiCaster.beforeDocumentSaving(document);
    LOG.assertTrue(file.isValid());

    String text = document.getText();
    String lineSeparator = getLineSeparator(document, file);
    if (!lineSeparator.equals("\n")) {
      text = StringUtil.convertLineSeparators(text, lineSeparator);
    }

    Project project = ProjectLocator.getInstance().guessProjectForFile(file);
    LoadTextUtil.write(project, file, this, text, document.getModificationStamp());

    myUnsavedDocuments.remove(document);
    LOG.assertTrue(!myUnsavedDocuments.contains(document));
    myTrailingSpacesStripper.clearLineModificationFlags(document);
  });
}
 
Example #12
Source File: EncodingManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
private static Project guessProject(@Nullable VirtualFile virtualFile) {
  return ProjectLocator.getInstance().guessProjectForFile(virtualFile);
}
 
Example #13
Source File: StubUpdatingIndex.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean canHaveStub(@Nonnull ProjectLocator projectLocator, @Nonnull VirtualFile file) {
  return canHaveStub(projectLocator, null, file);
}
 
Example #14
Source File: StubUpdatingIndex.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Inject
public StubUpdatingIndex(ProjectLocator projectLocator) {
  myProjectLocator = projectLocator;

  myInputFilter = (project, file) -> canHaveStub(myProjectLocator, project, file);
}