use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class MvcModuleStructureSynchronizer method initComponent.
@Override
public void initComponent() {
final MessageBusConnection connection = myProject.getMessageBus().connect();
connection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootListener() {
@Override
public void rootsChanged(ModuleRootEvent event) {
myModificationTracker.incModificationCount();
queue(SyncAction.SyncLibrariesInPluginsModule, myProject);
queue(SyncAction.UpgradeFramework, myProject);
queue(SyncAction.CreateAppStructureIfNeeded, myProject);
queue(SyncAction.UpdateProjectStructure, myProject);
queue(SyncAction.EnsureRunConfigurationExists, myProject);
updateProjectViewVisibility();
}
});
connection.subscribe(ProjectTopics.MODULES, new ModuleListener() {
@Override
public void moduleAdded(@NotNull Project project, @NotNull Module module) {
queue(SyncAction.UpdateProjectStructure, module);
queue(SyncAction.CreateAppStructureIfNeeded, module);
}
});
connection.subscribe(VirtualFileManager.VFS_CHANGES, new BulkVirtualFileListenerAdapter(new VirtualFileAdapter() {
@Override
public void fileCreated(@NotNull final VirtualFileEvent event) {
myModificationTracker.incModificationCount();
final VirtualFile file = event.getFile();
final String fileName = event.getFileName();
if (MvcModuleStructureUtil.APPLICATION_PROPERTIES.equals(fileName) || isApplicationDirectoryName(fileName)) {
queue(SyncAction.UpdateProjectStructure, file);
queue(SyncAction.EnsureRunConfigurationExists, file);
} else if (isLibDirectory(file) || isLibDirectory(event.getParent())) {
queue(SyncAction.UpdateProjectStructure, file);
} else {
if (!myProject.isInitialized())
return;
final Module module = ProjectRootManager.getInstance(myProject).getFileIndex().getModuleForFile(file);
if (module == null) {
// Maybe it is creation of a plugin in plugin directory.
if (file.isDirectory()) {
if (myPluginRoots.contains(file.getParent())) {
queue(SyncAction.UpdateProjectStructure, myProject);
return;
}
if (!myOutOfModuleDirectoryCreatedActionAdded) {
queue(SyncAction.OutOfModuleDirectoryCreated, myProject);
myOutOfModuleDirectoryCreatedActionAdded = true;
}
}
return;
}
if (!MvcConsole.isUpdatingVfsByConsoleProcess(module))
return;
final MvcFramework framework = MvcFramework.getInstance(module);
if (framework == null)
return;
if (framework.isToReformatOnCreation(file) || file.isDirectory()) {
ApplicationManager.getApplication().invokeLater(() -> {
if (!file.isValid())
return;
if (!framework.hasSupport(module))
return;
final List<VirtualFile> files = new ArrayList<>();
if (file.isDirectory()) {
ModuleRootManager.getInstance(module).getFileIndex().iterateContentUnderDirectory(file, new ContentIterator() {
@Override
public boolean processFile(VirtualFile fileOrDir) {
if (!fileOrDir.isDirectory() && framework.isToReformatOnCreation(fileOrDir)) {
files.add(file);
}
return true;
}
});
} else {
files.add(file);
}
PsiManager manager = PsiManager.getInstance(myProject);
for (VirtualFile virtualFile : files) {
PsiFile psiFile = manager.findFile(virtualFile);
if (psiFile != null) {
new ReformatCodeProcessor(myProject, psiFile, null, false).run();
}
}
}, module.getDisposed());
}
}
}
@Override
public void fileDeleted(@NotNull VirtualFileEvent event) {
myModificationTracker.incModificationCount();
final VirtualFile file = event.getFile();
if (isLibDirectory(file) || isLibDirectory(event.getParent())) {
queue(SyncAction.UpdateProjectStructure, file);
}
}
@Override
public void contentsChanged(@NotNull VirtualFileEvent event) {
final String fileName = event.getFileName();
if (MvcModuleStructureUtil.APPLICATION_PROPERTIES.equals(fileName)) {
queue(SyncAction.UpdateProjectStructure, event.getFile());
}
}
@Override
public void fileMoved(@NotNull VirtualFileMoveEvent event) {
myModificationTracker.incModificationCount();
}
@Override
public void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
if (VirtualFile.PROP_NAME.equals(event.getPropertyName())) {
myModificationTracker.incModificationCount();
}
}
}));
}
use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class MvcModuleStructureUtil method findApplicationProperties.
@Nullable
public static PropertiesFile findApplicationProperties(@NotNull Module module, MvcFramework framework) {
VirtualFile root = framework.findAppRoot(module);
if (root == null)
return null;
VirtualFile appChild = root.findChild(APPLICATION_PROPERTIES);
if (appChild == null || !appChild.isValid())
return null;
PsiManager manager = PsiManager.getInstance(module.getProject());
PsiFile psiFile = manager.findFile(appChild);
if (psiFile instanceof PropertiesFile) {
return (PropertiesFile) psiFile;
}
return null;
}
use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class FileAssociationsManagerImpl method getAssociationsFor.
public PsiFile[] getAssociationsFor(PsiFile file, FileType... fileTypes) {
final VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile == null)
return PsiFile.EMPTY_ARRAY;
for (VirtualFilePointer pointer : myAssociations.keySet()) {
if (pointer.isValid() && pointer.getUrl().equals(virtualFile.getUrl())) {
final VirtualFilePointerContainer container = myAssociations.get(pointer);
if (container != null) {
final VirtualFile[] files = container.getFiles();
final Set<PsiFile> list = new HashSet<>();
final PsiManager psiManager = PsiManager.getInstance(myProject);
for (VirtualFile assoc : files) {
final PsiFile psiFile = psiManager.findFile(assoc);
if (psiFile != null && (fileTypes.length == 0 || matchesFileType(psiFile, fileTypes))) {
list.add(psiFile);
}
}
return PsiUtilCore.toPsiFileArray(list);
} else {
return PsiFile.EMPTY_ARRAY;
}
}
}
return PsiFile.EMPTY_ARRAY;
}
use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class XmlChooseColorIntentionAction method chooseColor.
public static void chooseColor(JComponent editorComponent, PsiElement element) {
String caption = CodeInsightBundle.message("intention.color.chooser.dialog");
final XmlAttributeValue literal = PsiTreeUtil.getParentOfType(element, XmlAttributeValue.class, false);
if (literal == null)
return;
final String text = StringUtil.unquoteString(literal.getValue());
Color oldColor;
try {
oldColor = Color.decode(text);
} catch (NumberFormatException e) {
oldColor = JBColor.GRAY;
}
Color color = ColorChooser.chooseColor(editorComponent, caption, oldColor, true);
if (color == null)
return;
if (!Comparing.equal(color, oldColor)) {
if (!FileModificationService.getInstance().preparePsiElementForWrite(element))
return;
final String newText = "#" + ColorUtil.toHex(color);
final PsiManager manager = literal.getManager();
final XmlAttribute newAttribute = XmlElementFactory.getInstance(manager.getProject()).createAttribute("name", newText, element);
final Runnable replaceRunnable = () -> {
final XmlAttributeValue valueElement = newAttribute.getValueElement();
assert valueElement != null;
literal.replace(valueElement);
};
new WriteCommandAction(element.getProject(), caption) {
@Override
protected void run(@NotNull Result result) throws Throwable {
replaceRunnable.run();
}
}.execute();
}
}
use of com.intellij.psi.PsiManager in project intellij-community by JetBrains.
the class FetchExtResourceAction method fetchDtd.
private void fetchDtd(final Project project, final String dtdUrl, final String url, final ProgressIndicator indicator) throws IOException {
final String extResourcesPath = getExternalResourcesPath();
final File extResources = new File(extResourcesPath);
LOG.assertTrue(extResources.mkdirs() || extResources.exists(), extResources);
final PsiManager psiManager = PsiManager.getInstance(project);
ApplicationManager.getApplication().invokeAndWait(() -> {
WriteAction.run(() -> {
final String path = FileUtil.toSystemIndependentName(extResources.getAbsolutePath());
final VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(path);
LOG.assertTrue(vFile != null, path);
});
});
final List<String> downloadedResources = new LinkedList<>();
final List<String> resourceUrls = new LinkedList<>();
final IOException[] nestedException = new IOException[1];
try {
final String resPath = fetchOneFile(indicator, url, project, extResourcesPath, null);
if (resPath == null)
return;
resourceUrls.add(dtdUrl);
downloadedResources.add(resPath);
VirtualFile virtualFile = findFileByPath(resPath, dtdUrl);
Set<String> linksToProcess = new HashSet<>();
Set<String> processedLinks = new HashSet<>();
Map<String, String> baseUrls = new HashMap<>();
VirtualFile contextFile = virtualFile;
linksToProcess.addAll(extractEmbeddedFileReferences(virtualFile, null, psiManager, url));
while (!linksToProcess.isEmpty()) {
String s = linksToProcess.iterator().next();
linksToProcess.remove(s);
processedLinks.add(s);
final boolean absoluteUrl = s.startsWith(HTTP_PROTOCOL);
String resourceUrl;
if (absoluteUrl) {
resourceUrl = s;
} else {
String baseUrl = baseUrls.get(s);
if (baseUrl == null)
baseUrl = url;
resourceUrl = baseUrl.substring(0, baseUrl.lastIndexOf('/') + 1) + s;
}
String resourcePath;
String refname = s.substring(s.lastIndexOf('/') + 1);
if (absoluteUrl)
refname = Integer.toHexString(s.hashCode()) + "_" + refname;
try {
resourcePath = fetchOneFile(indicator, resourceUrl, project, extResourcesPath, refname);
} catch (IOException e) {
nestedException[0] = new FetchingResourceIOException(e, resourceUrl);
break;
}
if (resourcePath == null)
break;
virtualFile = findFileByPath(resourcePath, absoluteUrl ? s : null);
downloadedResources.add(resourcePath);
if (absoluteUrl) {
resourceUrls.add(s);
}
final Set<String> newLinks = extractEmbeddedFileReferences(virtualFile, contextFile, psiManager, resourceUrl);
for (String u : newLinks) {
baseUrls.put(u, resourceUrl);
if (!processedLinks.contains(u))
linksToProcess.add(u);
}
}
} catch (IOException ex) {
nestedException[0] = ex;
}
if (nestedException[0] != null) {
cleanup(resourceUrls, downloadedResources);
throw nestedException[0];
}
}
Aggregations