FileEditData.bf 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using IDE.ui;
  2. using System;
  3. using System.IO;
  4. using Beefy.utils;
  5. using System.Collections.Generic;
  6. using System.Security.Cryptography;
  7. using IDE.util;
  8. namespace IDE
  9. {
  10. [AllowDuplicates]
  11. public enum LineEndingKind
  12. {
  13. Unknown,
  14. Lf, // \n
  15. CrLf, // \r\n
  16. Cr, // \r
  17. #if BF_PLATFORM_WINDOWS
  18. Default = CrLf,
  19. #else
  20. Default = Lf,
  21. #endif
  22. }
  23. public class FileEditData
  24. {
  25. public int mRefCount = 1;
  26. public String mSavedContent ~ delete _;
  27. public IdSpan mSavedCharIdData = IdSpan() ~ _.Dispose();
  28. public List<ProjectSource> mProjectSources = new List<ProjectSource>() ~ delete _;
  29. public Event<Action> mEditWidgetCreatedEvent ~ _.Dispose();
  30. public LineEndingKind mLineEndingKind;
  31. public SourceEditWidget mEditWidget;
  32. public int32 mLastFileTextVersion;
  33. public bool mOwnsEditWidget;
  34. public String mFilePath ~ delete _;
  35. public String mQueuedContent ~ delete _;
  36. public bool mHadRefusedFileChange;
  37. public bool mFileDeleted;
  38. public MD5Hash mMD5Hash;
  39. public SHA256Hash mSHA256Hash;
  40. public this()
  41. {
  42. }
  43. public void SetSavedData(String savedContent, IdSpan savedIdSpan)
  44. {
  45. if (savedContent != null)
  46. {
  47. if (mSavedContent == null)
  48. mSavedContent = new String();
  49. mSavedContent.Set(savedContent);
  50. }
  51. else
  52. {
  53. delete mSavedContent;
  54. mSavedContent = null;
  55. }
  56. mSavedCharIdData.Dispose();
  57. mSavedCharIdData = savedIdSpan.Duplicate();
  58. }
  59. public bool HasTextChanged()
  60. {
  61. if (mEditWidget == null)
  62. return false;
  63. return mLastFileTextVersion != mEditWidget.Content.mData.mCurTextVersionId;
  64. }
  65. public bool Reload()
  66. {
  67. mHadRefusedFileChange = false;
  68. if (mEditWidget == null)
  69. {
  70. if (mQueuedContent != null)
  71. {
  72. var span = IdSpan.GetDefault((int32)mQueuedContent.Length);
  73. SetSavedData(mQueuedContent, span);
  74. span.Dispose();
  75. DeleteAndNullify!(mQueuedContent);
  76. }
  77. }
  78. else
  79. {
  80. var editWidgetContent = (SourceEditWidgetContent)mEditWidget.mEditWidgetContent;
  81. mFileDeleted = !editWidgetContent.Reload(mFilePath, mQueuedContent);
  82. /*if (editWidgetContent.mSourceViewPanel.LoadedHash.GetKind() == mLoadedHash.GetKind())
  83. editWidgetContent.mSourceViewPanel.LoadedHash = mLoadedHash;*/
  84. mLastFileTextVersion = mEditWidget.Content.mData.mCurTextVersionId;
  85. }
  86. return true;
  87. }
  88. public bool IsFileDeleted()
  89. {
  90. if (mFileDeleted) // Double check
  91. mFileDeleted = !File.Exists(mFilePath);
  92. return mFileDeleted;
  93. }
  94. public bool IsLocked()
  95. {
  96. if (mEditWidget != null)
  97. {
  98. if (let sourceEditWidgetContent = mEditWidget.mEditWidgetContent as SourceEditWidgetContent)
  99. {
  100. if (sourceEditWidgetContent.mSourceViewPanel != null)
  101. {
  102. if (sourceEditWidgetContent.mSourceViewPanel.IsReadOnly)
  103. return true;
  104. }
  105. }
  106. }
  107. for (var projectSource in mProjectSources)
  108. {
  109. if (projectSource.mProject.mLocked)
  110. return true;
  111. }
  112. return false;
  113. }
  114. public ~this()
  115. {
  116. if (mOwnsEditWidget)
  117. delete mEditWidget;
  118. }
  119. public void Ref()
  120. {
  121. mRefCount++;
  122. }
  123. public void Deref()
  124. {
  125. if (--mRefCount == 0)
  126. delete this;
  127. }
  128. public void BuildHash(StringView contents)
  129. {
  130. mMD5Hash = Security.Cryptography.MD5.Hash(.((uint8*)contents.Ptr, contents.Length));
  131. mSHA256Hash = Security.Cryptography.SHA256.Hash(.((uint8*)contents.Ptr, contents.Length));
  132. }
  133. public bool CheckHash(SourceHash sourceHash)
  134. {
  135. switch (sourceHash)
  136. {
  137. case .MD5(let hash): return hash == mMD5Hash;
  138. case .SHA256(let hash): return hash == mSHA256Hash;
  139. default: return false;
  140. }
  141. }
  142. }
  143. }