Advertisement
GlobalAccessSoftware

Search and Replace Module Alpha Part-2

Oct 3rd, 2019
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.76 KB | None | 0 0
  1.  
  2. #region Top Part
  3. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  4. // SmesaSearch2.cs from SmesaSearch1
  5. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  6. // v5.3.4.28 22-July SmesaSearch.cs rockin'
  7. // -=[ Design Revision 4 ]=- My Custom Type.
  8. // v5.3.4.29 25-Jul-2019 Spelling things out
  9. // Class{in-Class{in-Class}}; Custom Types:
  10. // v5.3.5.30 30-July ReCoded4 WorkList2
  11. // v5.3.5.31 03-Aug ReCoded4 WorkList2
  12. // v5.4.5.32 07-Aug-2019 Search & Replace.
  13. // v5.4.5.34 16-Aug Migrating New Code Here.
  14. // v5.4.6.36 29-Aug Work down testwells &
  15. // BreakPoints, Clean and Polish++
  16. // v5.4.6.37 14-Sep GetFileData2();
  17. // re-re-Designed &
  18. // v5.4.6.38 16-Sep GetFileData2 re-Written.
  19. // v5.4.7.39 24-Sep-2019 Settling down after
  20. // some evolution during tracing & testing.
  21. // v5.4.7.40 01-Oct-2019 Heavy Hacking S&R.
  22. //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  23.  
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Collections.ObjectModel;
  27.  
  28. // Class Aliases
  29. using System.Drawing;
  30. using System.IO;
  31. using System.Windows.Forms;
  32. using MyCustomLibrary;
  33. using MCD = MyCustomLibrary.Dialogs;
  34. using MMF = MyCustomLibrary.MemoryMapper;
  35. using MCT = MyCustomLibrary.MyToolbox;
  36. using MSF = MyCustomLibrary.SearchForm;
  37. //using MST = MyCustomLibrary.MySearchType;
  38.  
  39. // Property Aliases
  40. using SMR = MyEditor.Properties.Resources;
  41. using SMS = MyEditor.Properties.Settings;
  42.  
  43. #endregion Pop Tart
  44.  
  45.  
  46. namespace MyEditor
  47. {
  48.   public partial class Smesa //  <-==<<
  49.   {
  50. #region GetFileData Method Group
  51.  
  52.     /// <summary> strings to Custom TYPE;
  53.     /// called from the loop body above.
  54.     /// So each record can have multiple
  55.     /// points, or hits, (Therein) i.e.
  56.     /// Occurances of the searchString.
  57.     /// DON'T FORGET TO REMEMBER: EATs
  58.     /// Empty Line Between Saved Records.
  59.     /// Comes setup except for the Files.
  60.     /// reWrite GetFileData and simplify
  61.     /// v5.4.6.38 14-Sep-2019 -JpE-
  62.     /// Okay, so, this...
  63.     /// Method Group written bottom up
  64.     /// Logical inside to outside whereas
  65.     /// Starting from here down is most
  66.     /// readable codewise & Design-Wise.
  67.     /// Structurably, Architecturally:
  68.     /// Our Custom Type(s) & Composits
  69.     /// of other Custom "SearchTypes.cs"
  70.     /// Confered and tied into this also.
  71.     /// The WorkList2 Layer:
  72.     /// </summary>
  73.     /// <param name="bools"> </param>
  74.     /// <param name="texts"> </param>
  75.     /// <param name="readStrings"></param>
  76.     /// <returns></returns>
  77.     static WorkList2 GetFileData(
  78.       IList<string> bools,
  79.       IList<string> texts,
  80.       IList<string> readStrings)
  81.     {
  82.       return
  83.         new WorkList2(
  84.           GetFiles(readStrings)) // <-===<<
  85.           {
  86.             CaseCheck = Convert
  87.               .ToBoolean(bools[0]),
  88.  
  89.             WordCheck = Convert
  90.               .ToBoolean(bools[1]),
  91.  
  92.             FindThis  = texts[0],
  93.             Change2   = texts[1],
  94.           };
  95.     }
  96.     // ><><><><><><><><><><><><><><><><>
  97.  
  98.     static int RedNum{ get; set; }// <-===<<
  99.  
  100.     /// <summary> The Files Layer
  101.     /// Remember that readStrings[0] & [1]
  102.     /// have already been used to define
  103.     /// both CheckBoxes [0] & Search &
  104.     /// Replace strings [1] So, offset by 2.
  105.     /// </summary>
  106.     /// <param name="redStrings"></param>
  107.     /// <returns></returns>
  108.     static Collection<Files>
  109.       GetFiles(IList<string> redStrings)
  110.     {
  111.       RedNum = 1;
  112.       var files = new Collection<Files>();
  113.  
  114.       while (++RedNum < redStrings.Count)
  115.       {
  116.         var r = redStrings[RedNum];
  117.  
  118.         if (string.IsNullOrEmpty(r)
  119.           || r.Equals(MCT.Special)
  120.           ) continue;
  121.  
  122.         var  test = GetFile(redStrings);
  123.         if ( test != null
  124.           && test.Line != null
  125.           && test.Line.Count > 0
  126.           ) files.Add(test);
  127.       }
  128.       return files;
  129.     }
  130.  
  131.     static Files GetFile(
  132.       IList<string> redstrs)
  133.     {
  134.       var named = redstrs[RedNum];
  135.       var lines = GetLines(redstrs);
  136.  
  137.       if (lines == null
  138.         || lines.Count < 1
  139.         || lines[0].Equals(null)
  140.         ) return null;
  141.  
  142.       var t  = new Files(lines)
  143.       { Name = named };
  144.  
  145.       return t;
  146.     }
  147.     // ><><><><><><><><><><><><><><><><><><>
  148.  
  149.     /// <summary> The Lines Layer  (Plural)
  150.     /// </summary>
  151.     /// <param name="readStrings"></param>
  152.     /// <returns></returns>
  153.     static Collection<Lines>
  154.       GetLines(IList<string> readStrings)
  155.     {
  156.       var lines = new Collection<Lines>();
  157.  
  158.       while (++RedNum < readStrings.Count)
  159.       {
  160.         var r = readStrings[RedNum--];
  161.  
  162.         if (string.IsNullOrEmpty(r)
  163.           || r.Equals(MCT.Special)
  164.           )
  165.           break;
  166.  
  167.         var test = GetLine(readStrings);
  168.         if ( test != null
  169.           && test.Star != null
  170.           && test.Star.Count > 0)
  171.             lines.Add(test);
  172.         else
  173.           break;
  174.       }
  175.       return lines;
  176.     }
  177.  
  178.     /// <summary> The Line Layer (Singular)
  179.     /// </summary>
  180.     /// <param name="redStrings"></param>
  181.     /// <returns></returns>
  182.     static Lines GetLine(
  183.       IList<string> redStrings)
  184.     {
  185.       var hits = GetHits(redStrings);
  186.       if (hits.Count< 2) return null;
  187.  
  188.       var l2 = hits[0];
  189.       hits.RemoveAt(0);
  190.  
  191.       var line = new Lines(hits)
  192.       { LineNumber = l2 };
  193.  
  194.       return line;
  195.     } // ><><><><><><><><><><><><><><><><><>
  196.  
  197.     /// <summary> The "hits" Layer (inner)
  198.     /// The so-called X Coordinates of
  199.     /// the scheme, The Start Position
  200.     /// of The FOUND string, allowing
  201.     /// for more than 1 per line, etc.
  202.     /// </summary>
  203.     /// <param name="redStrings"></param>
  204.     /// <returns></returns>
  205.     static Collection<int> GetHits(
  206.       IList<string> redStrings)
  207.     {
  208.       var hits = new Collection<int>();
  209.  
  210.       while (++RedNum < redStrings.Count)
  211.       {
  212.         var red = redStrings[RedNum];
  213.  
  214.         if (string.IsNullOrEmpty(red)
  215.           || red.Equals(MCT.Special)
  216.           || !red.Contains(","))
  217.         { --RedNum; break; }
  218.        
  219.         var ints =
  220.           redStrings[RedNum].Split(',');
  221.         if (ints.Length < 2) return null;
  222.  
  223.         foreach (var n in ints)
  224.         {
  225.           try { hits.Add(
  226.             Convert.ToInt32(n)); }
  227.           catch (FormatException) { break; }
  228.           catch(OverflowException){ break; }
  229.         }
  230.       }
  231.       return hits;
  232.     }
  233. #endregion GetFileData Methods reWritten
  234.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  235.  
  236.  
  237. #region Search & Replace Tracking
  238.  
  239.     void FindNextMatch()
  240.     {
  241.       // testwell &/or FindNextMatch
  242.       Tracker =
  243.         GetRtbCoords( // & Adjust WorkList.File.Lines to active RTB.Lines really fast!
  244.           Track2Next()); // <-= Take Guesses... ^
  245.  
  246.       SetSelectedText();
  247.     }
  248.  
  249.     // Highlight this Find in Editor.
  250.     void SetSelectedText()
  251.     {
  252.       // testwell SetSelected();
  253.       var tab = GetTabNum(
  254.         WorkList.File[Tracker.File].Name);//
  255.       var rtb = GetRtB(tab);
  256.  
  257.       if (tab < 0 || rtb == null
  258.         || rtb.Lines.Length < 1) return;
  259.  
  260.       rtb.SelectionStart =
  261.         rtb.GetCharIndexFromPosition(
  262.           new Point(
  263.  
  264.           WorkList // Y
  265.             .File[Tracker.File]
  266.             .Line[Tracker.Line]
  267.             .LineNumber,
  268.  
  269.           WorkList // X
  270.             .File[Tracker.File]
  271.             .Line[Tracker.Line]
  272.             .Star[Tracker.Star]));
  273.  
  274.       rtb.SelectionLength =
  275.         WorkList.FindThis.Length;
  276.     }
  277.  
  278.  
  279.     /**************************************/
  280.     //halt: Heavy Construction, Bridge Out!
  281.     /**************************************/
  282.  
  283.    
  284.     // Actual Tracking adjusts WorkList
  285.     // Lines and Starts, to actual real
  286.     // time numerations when needed.
  287.     // Updates WorkList.Files[x].Line
  288.     // & Star(ts), to rtb.Lines real fast.
  289.     // Uses a lot of existing code so that
  290.     // is a definite perk & good news
  291.     // for testing, as that's proven code.
  292.     P3D GetRtbCoords(P3D p)
  293.     {
  294.       // testwell design GRC
  295.       var rtb = GetRtB(GetTabNum(
  296.         WorkList.File[p.File].Name));
  297.       var lns =
  298.         WorkList.File[p.File].Line;
  299.  
  300.       // hack: Evolve in Trace.
  301.       RedNum  = -1; // As indexer.
  302.       var cls = GetLines(rtb.Lines);
  303.       if (cls == null // Collection of Lines
  304.         ||cls.Count < 1
  305.         ||cls.Equals(lns)) return p;
  306.       var fil = new Files(cls); // 1 file.
  307.       var pos =
  308.         WorkList.File.IndexOf(fil);
  309.         WorkList.File.RemoveAt(pos);
  310.         WorkList.File.Insert(pos, fil);
  311.       //develop: calibration ^ may be needed.
  312.       TrackTo = GetTrackMax();
  313.       return AdjustTracker(fil);
  314.     }
  315.  
  316.     // Now align Tracker with new
  317.     // WorkList.File[p.File].Lines.
  318.     P3D AdjustTracker(Files fls) // 1 file.
  319.     {
  320.       // design hack testwell AdjustTracker
  321.       var p3 = new P3D();
  322.  
  323.       foreach (var ln in fls.Line)
  324.       {
  325.         var p1 = p3.Line;
  326.         var nm = ln.LineNumber;
  327.         var st = ln.Star;
  328.  
  329.         wait what?
  330.  
  331.       }
  332.  
  333.       return p3;
  334.     }
  335.  
  336.     // Set Tracker to the NEXT
  337.     // Matching Block's P3D location ahead.
  338.     // (if any) REM: All Zero on FindFirst();
  339.     P3D Track2Next()
  340.     {
  341.       // testwell design Track2Next()
  342.       var trakWas = Tracker;
  343.       var trackTo = new P3D();
  344.  
  345.       // hack Track2Next(next find)
  346.       return trackTo;
  347.     }
  348.     /*  Design Note:
  349.        **************
  350.        Okay, so it needs to use Tracker to
  351.        keep track of where we were in the
  352.        WorkList, and increment to the next
  353.        record in the current WorkList on
  354.        demand from "remote" commands.
  355.      */
  356.  
  357.     RichTextBox GetRtB(int tab)
  358.     {
  359.       return
  360.         TabControl.TabPages[tab]
  361.         .Controls[_index] as RichTextBox;
  362.     }
  363.  
  364.     static int GetTabNum(string fn)
  365.     {
  366.       // testwell GetTabNum();
  367.       if (string.IsNullOrEmpty(fn)
  368.         || !SMS.Default
  369.           .FileNames.Contains(fn)
  370.         || !File.Exists(fn)) return -1;
  371.  
  372.       return
  373.         SMS.Default
  374.           .FileNames.IndexOf(fn);
  375.     }
  376.  
  377.     // Ta-da! Easy with Custom Types!
  378.     void ReplaceSelection()
  379.     {
  380.       // testwell ReplaceCurrentSelection
  381.       var rtb = GetRtB(GetTabNum(WorkList
  382.         .File[Tracker.File].Name));
  383.       if (rtb == null || rtb.Lines == null
  384.         ||rtb.Lines.Length < 1) return;
  385.       var tst = rtb.Lines[Tracker.Line];
  386.       if (!tst.Contains(
  387.         WorkList.FindThis)) return;
  388.  
  389.       // halt what do these contain after?
  390.       var len = WorkList.FindThis.Length;
  391.       var fst = tst.Substring(
  392.         0, Tracker.Star + len);
  393.       var lst = tst.Substring(
  394.         Tracker.Star + len);
  395.  
  396.       // Evolve as needed.
  397.       fst = fst.Replace(WorkList
  398.         .FindThis, WorkList.Change2);
  399.  
  400.       rtb.Lines[Tracker.Line] = fst + lst;
  401.     }
  402. #endregion Search And Replace Tracking
  403.     //\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
  404.  
  405.  
  406.   }
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement