2019-05-02 22:45:40 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2019 faddenSoft
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2019-07-20 20:28:10 +00:00
|
|
|
|
namespace SourceGen {
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Maintains a record of interesting places we've been.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class NavStack {
|
2019-06-22 18:27:21 +00:00
|
|
|
|
// It's tempting to use a single stack, and just move a cursor up and down. However,
|
|
|
|
|
// that doesn't quite work. We always want to record where you came from, so we're
|
|
|
|
|
// pushing locations on when moving both forward and backward.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
//
|
2019-06-22 18:27:21 +00:00
|
|
|
|
// If you move backward and then jump somewhere else, we want to discard the list of
|
|
|
|
|
// previously-recorded forward places.
|
|
|
|
|
//
|
|
|
|
|
// Jumping to Notes is a little different from jumping to anything else, because we
|
2019-10-10 18:57:36 +00:00
|
|
|
|
// want to select the entire note rather than the code at the associated offset. This
|
2019-06-22 18:27:21 +00:00
|
|
|
|
// is especially important when moving upward through the file, or the note will be
|
|
|
|
|
// off the top of the screen.
|
|
|
|
|
|
|
|
|
|
// TODO(someday): change the back button to a pop-up list of locations (like the way
|
|
|
|
|
// VS 2017 does it).
|
|
|
|
|
|
2021-10-07 19:39:08 +00:00
|
|
|
|
public enum GoToMode {
|
|
|
|
|
Unknown = 0,
|
|
|
|
|
JumpToCodeData, // destination is first byte of code or data at target offset
|
|
|
|
|
JumpToNote, // destination is Note at target offset
|
|
|
|
|
JumpToAdjIndex, // destination is first line at target offset plus LineDelta
|
|
|
|
|
JumpToArStart, // destination is arstart at target offset.
|
|
|
|
|
JumpToArEnd, // destination is arend at target offset
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-22 18:27:21 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Holds enough information to get us back where we were, in style.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Location {
|
2019-10-10 18:57:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Code/data offset. May be less than zero for equates.
|
|
|
|
|
/// </summary>
|
2019-06-22 18:27:21 +00:00
|
|
|
|
public int Offset { get; set; }
|
2019-10-10 18:57:36 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Number of lines between the first line at the specified offset, and the
|
2021-10-07 19:39:08 +00:00
|
|
|
|
/// line we actually want to land on. Used when Mode=JumpToAdjIndex.
|
2019-10-10 18:57:36 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// It's possible this line no longer exists. Easiest test is to compare the
|
|
|
|
|
/// offset of the unadjusted line to the offset of the adjusted line. If not
|
|
|
|
|
/// equal, ignore the delta.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public int LineDelta { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-07 19:39:08 +00:00
|
|
|
|
/// Specifies interesting things to find at the target offset.
|
2019-10-10 18:57:36 +00:00
|
|
|
|
/// </summary>
|
2021-10-07 19:39:08 +00:00
|
|
|
|
public GoToMode Mode { get; set; }
|
2019-06-22 18:27:21 +00:00
|
|
|
|
|
2021-10-07 19:39:08 +00:00
|
|
|
|
public Location(int offset, int lineDelta, GoToMode mode) {
|
2019-06-22 18:27:21 +00:00
|
|
|
|
Offset = offset;
|
2019-10-10 18:57:36 +00:00
|
|
|
|
LineDelta = lineDelta;
|
2021-10-07 19:39:08 +00:00
|
|
|
|
Mode = mode;
|
|
|
|
|
|
|
|
|
|
//if (lineDelta != 0 && mode != GoToMode.JumpToAdjIndex) {
|
|
|
|
|
// Debug.WriteLine("HEY: lineDelta=" + lineDelta + " mode=" + mode);
|
|
|
|
|
//}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
2019-06-22 18:27:21 +00:00
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
public override string ToString() {
|
2021-10-07 19:39:08 +00:00
|
|
|
|
return string.Format("[+{0:x6},{1},{2}]", Offset, LineDelta, Mode);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-22 18:27:21 +00:00
|
|
|
|
public static bool operator ==(Location a, Location b) {
|
|
|
|
|
if (ReferenceEquals(a, b)) {
|
|
|
|
|
return true; // same object, or both null
|
|
|
|
|
}
|
|
|
|
|
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) {
|
|
|
|
|
return false; // one is null
|
|
|
|
|
}
|
2021-10-07 19:39:08 +00:00
|
|
|
|
return a.Offset == b.Offset && a.LineDelta == b.LineDelta && a.Mode == b.Mode;
|
2019-06-22 18:27:21 +00:00
|
|
|
|
}
|
|
|
|
|
public static bool operator !=(Location a, Location b) {
|
|
|
|
|
return !(a == b);
|
|
|
|
|
}
|
|
|
|
|
public override bool Equals(object obj) {
|
|
|
|
|
return obj is Location && this == (Location)obj;
|
|
|
|
|
}
|
|
|
|
|
public override int GetHashCode() {
|
2021-10-07 19:39:08 +00:00
|
|
|
|
return Offset + (LineDelta * 1000000) + ((int)Mode << 24);
|
2019-06-22 18:27:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-06-22 18:27:21 +00:00
|
|
|
|
// Location stacks.
|
|
|
|
|
private List<Location> mBackStack = new List<Location>();
|
|
|
|
|
private List<Location> mFwdStack = new List<Location>();
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public NavStack() { }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// True if there is an opportunity to pop backward.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool HasBackward {
|
|
|
|
|
get {
|
2019-06-22 18:27:21 +00:00
|
|
|
|
return mBackStack.Count > 0;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// True if there is an opportunity to push forward.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool HasForward {
|
|
|
|
|
get {
|
2019-06-22 18:27:21 +00:00
|
|
|
|
return mFwdStack.Count > 0;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-06-22 18:27:21 +00:00
|
|
|
|
/// Clears the stacks.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public void Clear() {
|
2019-06-22 18:27:21 +00:00
|
|
|
|
mBackStack.Clear();
|
|
|
|
|
mFwdStack.Clear();
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-06-22 18:27:21 +00:00
|
|
|
|
/// Pushes a new entry onto the back stack. Clears the forward stack.
|
|
|
|
|
///
|
|
|
|
|
/// If the same entry is already at the top of the stack, the entry will not be added.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
2019-06-22 18:27:21 +00:00
|
|
|
|
/// <param name="curLoc">Current location.</param>
|
|
|
|
|
public void Push(Location curLoc) {
|
|
|
|
|
if (mBackStack.Count > 0 && mBackStack[mBackStack.Count - 1] == curLoc) {
|
|
|
|
|
Debug.WriteLine("Not re-pushing " + curLoc);
|
|
|
|
|
return;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
2019-06-22 18:27:21 +00:00
|
|
|
|
|
|
|
|
|
mBackStack.Add(curLoc);
|
|
|
|
|
mFwdStack.Clear();
|
|
|
|
|
|
|
|
|
|
//Debug.WriteLine("Stack now: " + this);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-06-22 18:27:21 +00:00
|
|
|
|
/// Pops the top element from the back stack, and pushes the current position
|
|
|
|
|
/// onto the forward stack.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
2019-06-22 18:27:21 +00:00
|
|
|
|
/// <param name="fromLoc">Current location.</param>
|
|
|
|
|
/// <returns>The location to move to.</returns>
|
|
|
|
|
public Location MoveBackward(Location fromLoc) {
|
|
|
|
|
if (mBackStack.Count == 0) {
|
|
|
|
|
throw new Exception("Stack is empty");
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
2019-06-22 18:27:21 +00:00
|
|
|
|
Location toLoc = mBackStack[mBackStack.Count - 1];
|
|
|
|
|
mBackStack.RemoveAt(mBackStack.Count - 1);
|
|
|
|
|
mFwdStack.Add(fromLoc);
|
|
|
|
|
return toLoc;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-06-22 18:27:21 +00:00
|
|
|
|
/// Pops the top element from the forward stack, and pushes the current position
|
|
|
|
|
/// onto the back stack.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
2019-06-22 18:27:21 +00:00
|
|
|
|
/// <param name="fromLoc">Current location.</param>
|
|
|
|
|
/// <returns>The location to move to.</returns>
|
|
|
|
|
public Location MoveForward(Location fromLoc) {
|
|
|
|
|
if (mFwdStack.Count == 0) {
|
|
|
|
|
throw new Exception("Stack is empty");
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
2019-06-22 18:27:21 +00:00
|
|
|
|
Location toLoc = mFwdStack[mFwdStack.Count - 1];
|
|
|
|
|
mFwdStack.RemoveAt(mFwdStack.Count - 1);
|
|
|
|
|
mBackStack.Add(fromLoc);
|
|
|
|
|
return toLoc;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2019-06-22 18:27:21 +00:00
|
|
|
|
sb.Append("Back:");
|
|
|
|
|
foreach (Location loc in mBackStack) {
|
|
|
|
|
sb.Append(loc);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
2019-06-22 18:27:21 +00:00
|
|
|
|
sb.Append(" Fwd:");
|
|
|
|
|
foreach (Location loc in mFwdStack) {
|
|
|
|
|
sb.Append(loc);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|