use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-plugins by JetBrains.
the class JSConditionalCompilationDefinitionsProviderImpl method getDefinitionsFromConfigFile.
private Collection<Pair<String, String>> getDefinitionsFromConfigFile(final String configFilePath) {
if (StringUtil.isEmptyOrSpaces(configFilePath))
return Collections.emptyList();
final VirtualFile configFile = LocalFileSystem.getInstance().findFileByPath(configFilePath);
if (configFile == null || configFile.isDirectory())
return Collections.emptyList();
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
final Document cachedDocument = documentManager.getCachedDocument(configFile);
final Long currentTimestamp = cachedDocument != null ? cachedDocument.getModificationStamp() : configFile.getModificationCount();
final Long cachedTimestamp = configFileToTimestamp.get(configFile);
if (cachedTimestamp == null || !cachedTimestamp.equals(currentTimestamp)) {
configFileToTimestamp.remove(configFile);
configFileToConditionalCompilerDefinitions.remove(configFile);
try {
final Element rootElement = cachedDocument == null ? JDOMUtil.load(configFile.getInputStream()) : JDOMUtil.load(cachedDocument.getCharsSequence());
final Collection<Pair<String, String>> result = new ArrayList<>();
if (rootElement.getName().equals(FlexCompilerConfigFileUtilBase.FLEX_CONFIG)) {
// noinspection unchecked
for (Element compilerElement : rootElement.getChildren(FlexCompilerConfigFileUtilBase.COMPILER, rootElement.getNamespace())) {
// noinspection unchecked
for (Element defineElement : compilerElement.getChildren(DEFINE, rootElement.getNamespace())) {
final String name = defineElement.getChildText(NAME, rootElement.getNamespace());
final String value = defineElement.getChildText(VALUE, rootElement.getNamespace());
if (!StringUtil.isEmpty(name) && value != null) {
result.add(Pair.create(name, value));
}
}
}
}
configFileToTimestamp.put(configFile, currentTimestamp);
configFileToConditionalCompilerDefinitions.put(configFile, result);
return result;
} catch (JDOMException ignored) {
/*ignore*/
} catch (IOException ignored) {
/*ignore*/
}
return Collections.emptyList();
} else {
return configFileToConditionalCompilerDefinitions.get(configFile);
}
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-plugins by JetBrains.
the class ComplexRenderAction method collectChanges.
private void collectChanges(List<DocumentFactoryManager.DocumentInfo> documentInfos, THashMap<ModuleInfo, List<LocalStyleHolder>> localStyleSources) {
final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
final DocumentFactoryManager documentFactoryManager = DocumentFactoryManager.getInstance();
final Client client = Client.getInstance();
for (Document document : documents) {
final VirtualFile file = fileDocumentManager.getFile(document);
if (file == null) {
continue;
}
boolean isMxml = JavaScriptSupportLoader.isFlexMxmFile(file);
if (isMxml || file.getFileType() == CssFileType.INSTANCE) {
if (!collectChangedLocalStyleSources(localStyleSources, file) && onlyStyle) {
// if onlyStyle and we didn't find changed local style sources, so, it is new style source - we must collect style sources for appropriate module
Project p = ProjectUtil.guessProjectForFile(file);
if (p != null) {
ModuleInfo info = client.getRegisteredModules().getNullableInfo(ModuleUtilCore.findModuleForFile(file, p));
if (info != null) {
localStyleSources.put(info, Collections.emptyList());
}
}
}
}
final DocumentFactoryManager.DocumentInfo info = isMxml ? documentFactoryManager.getNullableInfo(file) : null;
if (info == null) {
continue;
} else if (onlyStyle) {
info.documentModificationStamp = document.getModificationStamp();
continue;
}
if (info.documentModificationStamp == document.getModificationStamp()) {
continue;
}
final Project project = ProjectUtil.guessProjectForFile(file);
if (project == null) {
continue;
}
final Module module = ModuleUtilCore.findModuleForFile(file, project);
if (module == null) {
continue;
}
final XmlFile psiFile;
final AccessToken token = ReadAction.start();
try {
psiFile = (XmlFile) PsiDocumentManager.getInstance(project).getPsiFile(document);
if (psiFile == null) {
continue;
}
} finally {
token.finish();
}
if (client.updateDocumentFactory(info.getId(), module, psiFile, reportProblems)) {
info.documentModificationStamp = document.getModificationStamp();
documentInfos.add(info);
}
}
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class ChangeFileEncodingAction method checkEnabled.
private boolean checkEnabled(@NotNull VirtualFile virtualFile) {
if (allowDirectories && virtualFile.isDirectory())
return true;
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Document document = documentManager.getDocument(virtualFile);
if (document == null)
return false;
return EncodingUtil.checkCanConvert(virtualFile) == null || EncodingUtil.checkCanReload(virtualFile).second == null;
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class EncodingUtil method saveIn.
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;
}
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");
}
});
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class FileContentUtilCore method saveOrReload.
private static void saveOrReload(VirtualFile file, @NotNull Collection<VFilePropertyChangeEvent> events) {
if (file == null || file.isDirectory() || !file.isValid()) {
return;
}
FileDocumentManager documentManager = FileDocumentManager.getInstance();
if (documentManager.isFileModified(file)) {
Document document = documentManager.getDocument(file);
if (document != null) {
// this can be called e.g. in context of undo, so we shouldn't modify document
documentManager.saveDocumentAsIs(document);
}
}
events.add(new VFilePropertyChangeEvent(FORCE_RELOAD_REQUESTOR, file, VirtualFile.PROP_NAME, file.getName(), file.getName(), false));
}
Aggregations