OSDN Git Service

関連付けからの起動もできるようにした
[fooeditengine/FooEditEngine.git] / Core / Controller.cs
index 0801be4..fb5f39f 100644 (file)
@@ -38,7 +38,13 @@ namespace FooEditEngine
     /// </summary>
     public enum IndentMode
     {
+        /// <summary>
+        /// タブ
+        /// </summary>
         Tab,
+        /// <summary>
+        /// スペース
+        /// </summary>
         Space,
     }
 
@@ -55,9 +61,8 @@ namespace FooEditEngine
             this.Document = doc;
             this.View = view;
             this.View.render.ChangedRenderResource += render_ChangedRenderResource;
-            this.View.PerformLayouted += View_LineBreakChanged;
             this.View.PageBoundChanged += View_PageBoundChanged;
-            this.Document.Clear();
+            //this.Document.Clear();
         }
 
         public Document Document
@@ -74,6 +79,8 @@ namespace FooEditEngine
                     this._Document.Update -= Document_Update;
                     this._Document.StatusUpdate -= Document_StatusChanged;
                     this._Document.SelectionChanged -= Document_SelectionChanged;
+                    this._Document.PerformLayouted -= View_LineBreakChanged;
+                    this._Document.CaretChanged -= _Document_CaretChanged;
                 }
 
                 this._Document = value;
@@ -81,20 +88,32 @@ namespace FooEditEngine
                 this._Document.Update += new DocumentUpdateEventHandler(Document_Update);
                 this._Document.StatusUpdate += Document_StatusChanged;
                 this._Document.SelectionChanged += Document_SelectionChanged;
+                this._Document.PerformLayouted += View_LineBreakChanged;
+                this._Document.CaretChanged += _Document_CaretChanged;
             }
         }
 
+        private void _Document_CaretChanged(object sender, EventArgs e)
+        {
+            TextPoint pos = this.Document.CaretPostion;
+            this.JumpCaret(pos.row, pos.col);
+        }
+
         private void Document_SelectionChanged(object sender, EventArgs e)
         {
             if (this.IsReverseSelect())
             {
-                this.Document.SelectGrippers.BottomRight.MoveByIndex(this.View, this.SelectionStart);
-                this.Document.SelectGrippers.BottomLeft.MoveByIndex(this.View, this.SelectionStart + this.SelectionLength);
+                if (this.Document.SelectGrippers.BottomRight.Enabled)
+                    this.Document.SelectGrippers.BottomRight.MoveByIndex(this.View, this.SelectionStart);
+                if (this.Document.SelectGrippers.BottomLeft.Enabled)
+                    this.Document.SelectGrippers.BottomLeft.MoveByIndex(this.View, this.SelectionStart + this.SelectionLength);
             }
             else
             {
-                this.Document.SelectGrippers.BottomLeft.MoveByIndex(this.View, this.SelectionStart);
-                this.Document.SelectGrippers.BottomRight.MoveByIndex(this.View, this.SelectionStart + this.SelectionLength);
+                if (this.Document.SelectGrippers.BottomLeft.Enabled)
+                    this.Document.SelectGrippers.BottomLeft.MoveByIndex(this.View, this.SelectionStart);
+                if (this.Document.SelectGrippers.BottomRight.Enabled)
+                    this.Document.SelectGrippers.BottomRight.MoveByIndex(this.View, this.SelectionStart + this.SelectionLength);
             }
         }
 
@@ -209,9 +228,7 @@ namespace FooEditEngine
                 else
                     return false;
             }
-            this.Document.Lock();
             this.Document.Replace(this.SelectionStart, this.SelectionLength, result.ToString());
-            this.Document.UnLock();
             return true;
         }
 
@@ -235,9 +252,7 @@ namespace FooEditEngine
                 else
                     i++;
             }
-            this.Document.Lock();
             this.Document.Replace(this.SelectionStart, this.SelectionLength, result.ToString());
-            this.Document.UnLock();
         }
 
         /// <summary>
@@ -275,7 +290,7 @@ namespace FooEditEngine
         {
             foreach(int id in this.Document.Markers.IDs)
             {
-                foreach (Marker m in this.Document.GetMarkers(index, id))
+                foreach (Marker m in this.Document.GetMarkers(id, index))
                 {
                     if (m.hilight == type)
                         return true;
@@ -560,9 +575,7 @@ namespace FooEditEngine
             if (this.Document[index] == Document.NewLine)
                 next = index + 1;
 
-            this.Document.Lock();
-            this.Document.Replace(index, next - index, "");
-            this.Document.UnLock();
+            this.Document.Replace(index, next - index, "", true);
         }
 
         public bool IsRectInsertMode()
@@ -615,9 +628,7 @@ namespace FooEditEngine
                 newIndex--;
             }
 
-            this.Document.Lock();
-            this.Document.Replace(newIndex, oldIndex - newIndex, "");
-            this.Document.UnLock();
+            this.Document.Replace(newIndex, oldIndex - newIndex, "", true);
         }
 
         /// <summary>
@@ -688,9 +699,7 @@ namespace FooEditEngine
                 if (foldingData != null && !foldingData.Expand && index > foldingData.Start && index <= foldingData.End)
                     index = foldingData.End + 1;
             }
-            this.Document.Lock();
-            this.Document.Replace(index, length, str);
-            this.Document.UnLock();
+            this.Document.Replace(index, length, str, true);
         }
 
         /// <summary>
@@ -724,11 +733,23 @@ namespace FooEditEngine
         /// JumpCaretで移動した位置からキャレットを移動し、選択状態にする
         /// </summary>
         /// <param name="tp"></param>
-        public void MoveCaretAndSelect(TextPoint tp)
+        /// <param name="alignWord">単語単位で選択するかどうか</param>
+        public void MoveCaretAndSelect(TextPoint tp,bool alignWord = false)
         {
+            TextPoint endSelectPostion = tp;
             int CaretPostion = this.View.GetIndexFromLayoutLine(tp);
+            if (alignWord)
+            {
+                if (this.IsReverseSelect())
+                    while (CaretPostion >= 0 && CaretPostion < this.Document.Length && !Util.IsWordSeparator(this.Document[CaretPostion])) CaretPostion--;
+                else
+                    while (CaretPostion < this.Document.Length && !Util.IsWordSeparator(this.Document[CaretPostion])) CaretPostion++;
+                if (CaretPostion < 0)
+                    CaretPostion = 0;
+                endSelectPostion = this.View.LayoutLines.GetTextPointFromIndex(CaretPostion);
+            }
             this.Document.Select(this.Document.AnchorIndex, CaretPostion - this.Document.AnchorIndex);
-            this.View.JumpCaret(tp.row, tp.col);
+            this.View.JumpCaret(endSelectPostion.row, endSelectPostion.col);
             this.View.AdjustCaretAndSrc();
         }
 
@@ -859,7 +880,7 @@ namespace FooEditEngine
                 return;
             int selectionStart = this.SelectionStart;
             string insertStr = this.IndentMode == IndentMode.Space ? this.GetIndentSpace(0) : "\t";
-            string text = this.RemoveLineHead(GetTextFromLineSelectArea(this.View.Selections), insertStr);
+            string text = this.RemoveLineHead(GetTextFromLineSelectArea(this.View.Selections), insertStr, insertStr.Length);
             this.RepleaceSelectionArea(this.View.Selections, text);
             this.Document.Select(selectionStart, text.Length);
         }
@@ -878,14 +899,14 @@ namespace FooEditEngine
             return output.ToString();
         }
 
-        public string RemoveLineHead(string s, string str)
+        public string RemoveLineHead(string s, string str,int remove_count)
         {
             string[] lines = s.Split(new string[] { Document.NewLine.ToString() }, StringSplitOptions.None);
             StringBuilder output = new StringBuilder();
             for (int i = 0; i < lines.Length; i++)
             {
                 if (lines[i].StartsWith(str))
-                    output.Append(lines[i].Substring(1) + Document.NewLine);
+                    output.Append(lines[i].Substring(remove_count) + Document.NewLine);
                 else if (i < lines.Length - 1)
                     output.Append(lines[i] + Document.NewLine);
             }
@@ -945,7 +966,7 @@ namespace FooEditEngine
                 throw new InvalidOperationException("");
 
             TextPoint nextPoint = this.GetTextPointAfterMoveLine(isMoveNext ? 1 : -1, this.Document.CaretPostion);
-            
+
             this.View.JumpCaret(nextPoint.row, nextPoint.col,false);
         }
 
@@ -1007,9 +1028,7 @@ namespace FooEditEngine
         private void ReplaceBeforeSelection(Selection sel, int removeLength, string insertStr)
         {
             sel = Util.NormalizeIMaker<Selection>(sel);
-            this.Document.Lock();
             this.Document.Replace(sel.start - removeLength, removeLength, insertStr);
-            this.Document.UnLock();
         }
 
         private void RepleaceSelectionArea(SelectCollection Selections, string value,bool updateInsertPoint = false)
@@ -1023,9 +1042,7 @@ namespace FooEditEngine
                 if (Selections.Count > 0)
                     sel = Util.NormalizeIMaker<Selection>(this.View.Selections.First());
 
-                this.Document.Lock();
                 this.Document.Replace(sel.start, sel.length, value);
-                this.Document.UnLock();
                 return;
             }
 
@@ -1040,8 +1057,6 @@ namespace FooEditEngine
             {
                 int i;
 
-                this.Document.Lock();
-
                 this.Document.UndoManager.BeginUndoGroup();
 
                 this.Document.FireUpdateEvent = false;
@@ -1070,15 +1085,11 @@ namespace FooEditEngine
                 this.Document.FireUpdateEvent = true;
 
                 this.Document.UndoManager.EndUndoGroup();
-
-                this.Document.UnLock();
             }
             else
             {
                 SelectCollection temp = new SelectCollection(this.View.Selections); //コピーしないとReplaceCommandを呼び出した段階で書き換えられてしまう
 
-                this.Document.Lock();
-
                 this.Document.UndoManager.BeginUndoGroup();
 
                 this.Document.FireUpdateEvent = false;
@@ -1113,8 +1124,6 @@ namespace FooEditEngine
                 this.Document.FireUpdateEvent = true;
 
                 this.Document.UndoManager.EndUndoGroup();
-
-                this.Document.UnLock();
             }
             this.JumpCaret(StartIndex);
             if (updateInsertPoint && newInsertPoint.Count > 0)
@@ -1171,7 +1180,7 @@ namespace FooEditEngine
         void View_PageBoundChanged(object sender, EventArgs e)
         {
             if (this.Document.LineBreak == LineBreakMethod.PageBound && this.View.PageBound.Width - this.View.LineBreakingMarginWidth > 0)
-                this.View.PerfomLayouts();
+                this.Document.PerformLayout();
             this.AdjustCaret();
         }
 
@@ -1180,7 +1189,7 @@ namespace FooEditEngine
             if (e.type == ResourceType.Font)
             {
                 if (this.Document.LineBreak == LineBreakMethod.PageBound)
-                    this.View.PerfomLayouts();
+                    this.Document.PerformLayout();
                 this.AdjustCaret();
             }
             if (e.type == ResourceType.InlineChar)