use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-plugins by JetBrains.
the class PubspecYamlUtil method getPubspecYamlInfo.
@Nullable
private static Map<String, Object> getPubspecYamlInfo(@NotNull final VirtualFile pubspecYamlFile) {
// do not use Yaml plugin here - IntelliJ IDEA Community Edition doesn't contain it.
Pair<Long, Map<String, Object>> data = pubspecYamlFile.getUserData(MOD_STAMP_TO_PUBSPEC_NAME);
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
final Document cachedDocument = documentManager.getCachedDocument(pubspecYamlFile);
final Long currentTimestamp = cachedDocument != null ? cachedDocument.getModificationStamp() : pubspecYamlFile.getModificationCount();
final Long cachedTimestamp = data == null ? null : data.first;
if (cachedTimestamp == null || !cachedTimestamp.equals(currentTimestamp)) {
data = null;
pubspecYamlFile.putUserData(MOD_STAMP_TO_PUBSPEC_NAME, null);
try {
final Map<String, Object> pubspecYamlInfo;
if (cachedDocument != null) {
pubspecYamlInfo = loadPubspecYamlInfo(cachedDocument.getText());
} else {
pubspecYamlInfo = loadPubspecYamlInfo(VfsUtilCore.loadText(pubspecYamlFile));
}
if (pubspecYamlInfo != null) {
data = Pair.create(currentTimestamp, pubspecYamlInfo);
pubspecYamlFile.putUserData(MOD_STAMP_TO_PUBSPEC_NAME, data);
}
} catch (IOException ignored) {
/* unlucky */
}
}
return data == null ? null : data.second;
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class IdeaGateway method doCreateEntry.
@Nullable
private Entry doCreateEntry(@NotNull VirtualFile file, boolean forDeletion) {
if (!file.isDirectory()) {
if (!isVersioned(file))
return null;
Pair<StoredContent, Long> contentAndStamps;
if (forDeletion) {
FileDocumentManager m = FileDocumentManager.getInstance();
// should not try to load document
Document d = m.isFileModified(file) ? m.getCachedDocument(file) : null;
contentAndStamps = acquireAndClearCurrentContent(file, d);
} else {
contentAndStamps = getActualContentNoAcquire(file);
}
return doCreateFileEntry(file, contentAndStamps);
}
DirectoryEntry newDir = null;
if (file instanceof VirtualFileSystemEntry) {
int nameId = ((VirtualFileSystemEntry) file).getNameId();
if (nameId > 0) {
newDir = new DirectoryEntry(nameId);
}
}
if (newDir == null) {
newDir = new DirectoryEntry(file.getName());
}
doCreateChildren(newDir, iterateDBChildren(file), forDeletion);
if (!isVersioned(file) && newDir.getChildren().isEmpty())
return null;
return newDir;
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class LossyEncodingInspection method isGoodCharset.
// check if file was loaded in correct encoding
// returns true if text converted with charset is equals to the bytes currently on disk
private static boolean isGoodCharset(@NotNull VirtualFile virtualFile, @NotNull Charset charset) {
FileDocumentManager documentManager = FileDocumentManager.getInstance();
Document document = documentManager.getDocument(virtualFile);
if (document == null)
return true;
byte[] loadedBytes;
byte[] bytesToSave;
try {
loadedBytes = virtualFile.contentsToByteArray();
bytesToSave = new String(loadedBytes, charset).getBytes(charset);
} catch (Exception e) {
return true;
}
byte[] bom = virtualFile.getBOM();
if (bom != null && !ArrayUtil.startsWith(bytesToSave, bom)) {
// for 2-byte encodings String.getBytes(Charset) adds BOM automatically
bytesToSave = ArrayUtil.mergeArrays(bom, bytesToSave);
}
boolean equals = Arrays.equals(bytesToSave, loadedBytes);
if (!equals && LOG.isDebugEnabled()) {
try {
String tempDir = FileUtil.getTempDirectory();
FileUtil.writeToFile(new File(tempDir, "lossy-bytes-to-save"), bytesToSave);
FileUtil.writeToFile(new File(tempDir, "lossy-loaded-bytes"), loadedBytes);
LOG.debug("lossy bytes dumped into " + tempDir);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return equals;
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class XXXXX method processKeyTyped.
private boolean processKeyTyped(char c) {
// [vova] This is patch for Mac OS X. Under Mac "input methods"
// is handled before our EventQueue consume upcoming KeyEvents.
IdeEventQueue queue = IdeEventQueue.getInstance();
if (queue.shouldNotTypeInEditor() || ProgressManager.getInstance().hasModalProgressIndicator()) {
return false;
}
FileDocumentManager manager = FileDocumentManager.getInstance();
final VirtualFile file = manager.getFile(myDocument);
if (file != null && !file.isValid()) {
return false;
}
ActionManagerEx actionManager = ActionManagerEx.getInstanceEx();
DataContext dataContext = getDataContext();
actionManager.fireBeforeEditorTyping(c, dataContext);
MacUIUtil.hideCursor();
EditorActionManager.getInstance().getTypedAction().actionPerformed(this, c, dataContext);
return true;
}
use of com.intellij.openapi.fileEditor.FileDocumentManager in project intellij-community by JetBrains.
the class DocumentImpl method beforeChangedUpdate.
private void beforeChangedUpdate(DocumentEvent event) {
Application app = ApplicationManager.getApplication();
if (app != null) {
FileDocumentManager manager = FileDocumentManager.getInstance();
VirtualFile file = manager.getFile(this);
if (file != null && !file.isValid()) {
LOG.error("File of this document has been deleted.");
}
}
assertInsideCommand();
// initialize line set to track changed lines
getLineSet();
if (!ShutDownTracker.isShutdownHookRunning()) {
DocumentListener[] listeners = getListeners();
for (int i = listeners.length - 1; i >= 0; i--) {
try {
listeners[i].beforeDocumentChange(event);
} catch (Throwable e) {
LOG.error(e);
}
}
}
myEventsHandling = true;
}
Aggregations