hasPackage && fileIndex.isInSource(sourceFile) ? fileIndex.getSourceRootForFile(sourceFile) :
fileIndex.isInContent(sourceFile) ? fileIndex.getContentRootForFile(sourceFile) :
getFirstElement(hasPackage && sourceRoots.length > 0? sourceRoots : contentRoots);
if (virtualRoot == null) {
fail(project, sourceFile, "Unable to guess target source root");
throw new ProcessCanceledException();
}
try {
boolean newGenRoot = !fileIndex.isInSourceContent(virtualRoot);
final String relativePath = (hasPackage && newGenRoot? "gen." + targetPackage :
hasPackage ? targetPackage :
newGenRoot ? "gen" : "").replace('.', '/');
if (relativePath.isEmpty()) {
return virtualRoot;
}
else {
VirtualFile result = new WriteAction<VirtualFile>() {
@Override
protected void run(@NotNull Result<VirtualFile> result) throws Throwable {
result.setResult(VfsUtil.createDirectoryIfMissing(virtualRoot, relativePath));
}
}.execute().throwException().getResultObject();
LocalFileSystem.getInstance().refreshFiles(Collections.singleton(result));
return returnRoot && newGenRoot? ObjectUtils.assertNotNull(virtualRoot.findChild("gen")) :
returnRoot ? virtualRoot : result;
}
}
catch (Exception ex) {
fail(project, sourceFile, ex.getMessage());
throw new ProcessCanceledException();
}
}