Java Code Examples for com.intellij.openapi.vfs.newvfs.impl.StubVirtualFile

The following examples show how to use com.intellij.openapi.vfs.newvfs.impl.StubVirtualFile. These examples are extracted from open source projects. 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
@SuppressWarnings("ConstantConditions")
@Override
public boolean isMyFileType(@NotNull final VirtualFile file) {
    if (file instanceof StubVirtualFile) {
        return false; // Helps New -> File get correct file type
    }

    return recursionGuard.doPreventingRecursion(GUARD_ID, true, () -> {
        if (file.isValid()) {
            final String extension = file.getExtension();
            if ("yml".equalsIgnoreCase(extension) || "yaml".equalsIgnoreCase(extension)) {
                try (InputStream inputStream = file.getInputStream()) {
                    final byte[] bytes = new byte[BYTES_TO_READ];
                    final int n = inputStream.read(bytes, 0, BYTES_TO_READ);
                    return n > 0 && isKubernetesYaml(bytes);
                } catch (final IOException e) {
                    logger.info("Error while determining file type.", e);
                }
            }
        }
        return false;
    });
}
 
Example 2
public void testCompileTargetsInSelectedEditor() throws Throwable {
  String projectFolderPath = "examples/tests/scala/org/pantsbuild/example";
  doImport(projectFolderPath);
  String projectRelativePath = projectFolderPath + "/hello/greet/Greeting.java";
  VirtualFile virtualFile = new StubVirtualFile() {
    @NotNull
    @Override
    public String getPath() {
      return new File(getProjectFolder(), projectRelativePath).getAbsolutePath();
    }
  };
  AnActionEvent event = AnActionEvent.createFromDataContext(
    "",
    null,
    makeDataContext(myProject, virtualFile)
  );

  new CopyPathRelativeToBuildRootAction().actionPerformed(event);
  assertEquals(projectRelativePath, Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).getTransferData(DataFlavor.stringFlavor));
}
 
Example 3
Source Project: consulo   Source File: FileTypeManagerImpl.java    License: Apache License 2.0 6 votes vote down vote up
@Override
@Nonnull
public FileType getFileTypeByFile(@Nonnull VirtualFile file, @Nullable byte[] content) {
  //FileType overriddenFileType = FileTypeOverrider.EP_NAME.computeSafeIfAny((overrider) -> overrider.getOverriddenFileType(file));
  //if (overriddenFileType != null) {
  //  return overriddenFileType;
  //}

  FileType fileType = getByFile(file);
  if (!(file instanceof StubVirtualFile)) {
    if (fileType == null) {
      return getOrDetectFromContent(file, content);
    }
    if (mightBeReplacedByDetectedFileType(fileType)) {
      FileType detectedFromContent = getOrDetectFromContent(file, content);
      if (detectedFromContent != UnknownFileType.INSTANCE && detectedFromContent != PlainTextFileType.INSTANCE) {
        return detectedFromContent;
      }
    }
  }
  return ObjectUtils.notNull(fileType, UnknownFileType.INSTANCE);
}
 
Example 4
private void createVirtualFile(String path) {
  VirtualFile stub =
      new StubVirtualFile() {
        @Override
        public boolean isValid() {
          return true;
        }
      };
  when(mockFileSystem.findFileByIoFile(new File(path))).thenReturn(stub);
}
 
Example 5
Source Project: consulo   Source File: FileTypeManagerImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean isFileOfType(@Nonnull VirtualFile file, @Nonnull FileType type) {
  if (mightBeReplacedByDetectedFileType(type) || type.equals(UnknownFileType.INSTANCE)) {
    // a file has unknown file type if none of file type detectors matched it
    // for plain text file type, we run file type detection based on content

    return file.getFileType().equals(type);
  }

  if (file instanceof LightVirtualFile) {
    FileType assignedFileType = ((LightVirtualFile)file).getAssignedFileType();
    if (assignedFileType != null) {
      return type.equals(assignedFileType);
    }
  }

  //FileType overriddenFileType = FileTypeOverrider.EP_NAME.computeSafeIfAny((overrider) -> overrider.getOverriddenFileType(file));
  //if (overriddenFileType != null) {
  //  return overriddenFileType.equals(type);
  //}

  if (type instanceof FileTypeIdentifiableByVirtualFile && ((FileTypeIdentifiableByVirtualFile)type).isMyFileType(file)) {
    return true;
  }

  FileType fileTypeByFileName = getFileTypeByFileName(file.getNameSequence());
  if (fileTypeByFileName == type) {
    return true;
  }
  if (fileTypeByFileName != UnknownFileType.INSTANCE) {
    return false;
  }
  if (file instanceof StubVirtualFile || !isDetectable(file)) {
    return false;
  }

  FileType detectedFromContentFileType = file.getUserData(DETECTED_FROM_CONTENT_FILE_TYPE_KEY);
  if (detectedFromContentFileType != null) {
    return detectedFromContentFileType.equals(type);
  }

  Collection<FileTypeDetector> detectors = getDetectorsForType(type);
  if (detectors != null || !myUntypedFileTypeDetectors.isEmpty()) {
    Iterable<FileTypeDetector> applicableDetectors = detectors != null ? ContainerUtil.concat(detectors, myUntypedFileTypeDetectors) : myUntypedFileTypeDetectors;

    try {
      FileType detectedType = detectFromContent(file, null, applicableDetectors);
      if (detectedType != UnknownFileType.INSTANCE && detectedType != PlainTextFileType.INSTANCE) {
        cacheAutoDetectedFileType(file, detectedType);
      }
      if (detectedType.equals(type)) {
        return true;
      }
    }
    catch (IOException ignored) {
    }
  }

  return false;
}