1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-13 05:44:11 +02:00
Beef/IDE/src/BookmarkManager.bf

189 lines
4.4 KiB
Beef
Raw Normal View History

2019-08-23 11:56:54 -07:00
using System;
using System.Collections;
2019-08-23 11:56:54 -07:00
using System.Text;
using System.Threading.Tasks;
using IDE.ui;
using System.Diagnostics;
namespace IDE
{
public class TrackedTextElement
{
public int mRefCount = 1;
public bool mIsDead;
public String mFileName ~ delete _;
public int32 mLineNum;
public int32 mColumn;
public bool mSnapToLineStart = true;
public int32 mMoveIdx; // Increments when we manually move an element (ie: a history location updating because the user types near the previous history location)
public virtual void Move(int wantLineNum, int wantColumn)
{
mLineNum = (int32)wantLineNum;
mColumn = (int32)wantColumn;
}
public virtual void Kill()
{
mIsDead = true;
Deref();
}
public void AddRef()
{
mRefCount++;
}
public void Deref()
{
if (--mRefCount == 0)
delete this;
}
public ~this()
{
Debug.Assert(mRefCount == 0);
}
}
public class Bookmark : TrackedTextElement
2022-06-08 20:11:39 +02:00
{
public String mTitle ~ delete _;
2019-08-23 11:56:54 -07:00
public String mNotes ~ delete _;
2022-06-08 20:11:39 +02:00
public bool mIsDisabled;
2019-08-23 11:56:54 -07:00
}
public class BookmarkManager
{
public List<Bookmark> mBookmarkList = new List<Bookmark>() ~
{
for (var bookmark in mBookmarkList)
bookmark.Kill();
delete _;
};
public int32 mBookmarkIdx;
2022-06-08 20:11:39 +02:00
private int32 _createdBookmarks;
2019-08-23 11:56:54 -07:00
2022-06-08 20:11:39 +02:00
public bool AllBookmarksDisabled
{
get
{
for (Bookmark b in mBookmarkList)
{
if (!b.mIsDisabled)
return false;
}
return true;
}
}
public Bookmark CreateBookmark(String fileName, int wantLineNum, int wantColumn, bool isDisabled = false, String title = null)
{
2019-08-23 11:56:54 -07:00
mBookmarkIdx = (int32)mBookmarkList.Count;
2022-06-08 20:11:39 +02:00
_createdBookmarks++;
2019-08-23 11:56:54 -07:00
Bookmark bookmark = new Bookmark();
bookmark.mFileName = new String(fileName);
bookmark.mLineNum = (int32)wantLineNum;
bookmark.mColumn = (int32)wantColumn;
2022-06-08 20:11:39 +02:00
if (title == null)
bookmark.mTitle = new $"Bookmark {_createdBookmarks++}";
else
bookmark.mTitle = new String(title);
bookmark.mIsDisabled = isDisabled;
2019-08-23 11:56:54 -07:00
mBookmarkList.Add(bookmark);
gApp.mDebugger.mBreakpointsChangedDelegate();
2022-06-08 20:11:39 +02:00
//gApp.mBookmarksPanel.UpdateBookmarks();
gApp.mBookmarksPanel.mBookmarksDirty = true;
2019-08-23 11:56:54 -07:00
return bookmark;
}
public void DeleteBookmark(Bookmark bookmark)
{
int idx = mBookmarkList.IndexOf(bookmark);
mBookmarkList.RemoveAt(idx);
if (mBookmarkIdx == idx)
mBookmarkIdx--;
if (mBookmarkIdx >= mBookmarkList.Count)
mBookmarkIdx = (int32)mBookmarkList.Count - 1;
gApp.mDebugger.mBreakpointsChangedDelegate();
bookmark.Kill();
2022-06-08 20:11:39 +02:00
//gApp.mBookmarksPanel.UpdateBookmarks();
gApp.mBookmarksPanel.mBookmarksDirty = true;
2019-08-23 11:56:54 -07:00
}
public void Clear()
{
for (var bookmark in mBookmarkList)
bookmark.Kill();
mBookmarkList.Clear();
mBookmarkIdx = 0;
gApp.mDebugger.mBreakpointsChangedDelegate();
2022-06-08 20:11:39 +02:00
//gApp.mBookmarksPanel.UpdateBookmarks();
gApp.mBookmarksPanel.mBookmarksDirty = true;
2019-08-23 11:56:54 -07:00
}
public void PrevBookmark()
{
if (mBookmarkList.Count == 0)
return;
2022-06-08 20:11:39 +02:00
int32 currentIdx = mBookmarkIdx;
Bookmark prevBookmark;
repeat
{
mBookmarkIdx++;
if (mBookmarkIdx >= mBookmarkList.Count)
mBookmarkIdx = 0;
prevBookmark = mBookmarkList[mBookmarkIdx];
}
// skip disabled bookmarks, stop when we reach starting point
while (prevBookmark.mIsDisabled && (currentIdx != mBookmarkIdx));
2019-08-23 11:56:54 -07:00
2022-06-08 20:11:39 +02:00
GotoBookmark(prevBookmark);
2019-08-23 11:56:54 -07:00
}
public void NextBookmark()
{
if (mBookmarkList.Count == 0)
return;
2022-06-08 20:11:39 +02:00
int32 currentIdx = mBookmarkIdx;
Bookmark nextBookmark;
2019-08-23 11:56:54 -07:00
2022-06-08 20:11:39 +02:00
repeat
{
mBookmarkIdx--;
if (mBookmarkIdx < 0)
mBookmarkIdx = (int32)mBookmarkList.Count - 1;
nextBookmark = mBookmarkList[mBookmarkIdx];
}
// skip disabled bookmarks, stop when we reach starting point
while (nextBookmark.mIsDisabled && (currentIdx != mBookmarkIdx));
GotoBookmark(nextBookmark);
2019-08-23 11:56:54 -07:00
}
2022-06-08 20:11:39 +02:00
public void GotoBookmark(Bookmark bookmark)
{
gApp.ShowSourceFileLocation(bookmark.mFileName, -1, -1, bookmark.mLineNum, bookmark.mColumn, LocatorType.Smart);
}
2019-08-23 11:56:54 -07:00
}
}