private void FixCursorPosition(bool back) { if (UHelper.IsChinesePart(Coder.CodeManager.GetCodeData(mCursor.Row), mCursor.Col)) { if (back) { mCursor.Col--; } else { mCursor.Col++; } } if (mCursor.Col < mRenderStartCol) { mRenderStartCol = mCursor.Col; } if (mCursor.Col >= mRenderStartCol + mRenderSize.Col) { mRenderStartCol = mCursor.Col - mRenderSize.Col + 1; } if (mCursor.Row < mRenderStartRow) { mRenderStartRow = mCursor.Row; } if (mCursor.Row > mRenderStartRow + mRenderSize.Row) { mRenderStartRow = mCursor.Row - mRenderSize.Row + 1; } }
/// <summary> /// 删除指定位置的一个字符 /// </summary> /// <param name="pos">开始位置</param> public void RemoveCharacter(URank pos) { if (pos.Row < 0 || pos.Row >= mCodeData.Count) { throw new Exception("RemoveCharacter : Row Invalid {" + pos.Row.ToString() + "}"); } if (pos.Col < 0 || pos.Col >= mCodeData[pos.Row].Count) { throw new Exception("RemoveCharacter : Col Invalid {" + pos.Col.ToString() + "}"); } if (UHelper.IsChinesePart(mCodeData[pos.Row].ToArray(), pos.Col)) { mCodeData[pos.Row].RemoveRange(pos.Col - 1, 2); mCoder.SetCursorPos(new URank(pos.Row, pos.Col - 2)); } else { mCodeData[pos.Row].RemoveAt(pos.Col); mCoder.SetCursorPos(new URank(pos.Row, pos.Col)); } mCodeLines[pos.Row] = mParser.ParseLine(pos.Row); }
private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e) { if (mCurrFile == string.Empty) { return; } saveFileDialog1.Filter = "文本文件(*.txt)|*.txt"; saveFileDialog1.FileName = string.Empty; saveFileDialog1.Title = "另存为"; if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { StreamWriter fp = new StreamWriter(saveFileDialog1.FileName, false, Encoding.Default); mCurrFile = saveFileDialog1.FileName; int len = coder.CodeManager.GetCodeCount(); for (int i = 0; i < len; i++) { fp.WriteLine(UHelper.GetStringByBytes(coder.CodeManager.GetCodeData(i))); } fp.Close(); fp.Dispose(); MessageBox.Show(saveFileDialog1.FileName + "\r\n另存为成功"); } }
private void RenderCodeCut(IRenderer renderer, float x, float y, UCodeCut cut) { try { if (cut.CutType == UCutType.Space || cut.CutType == UCutType.NewLine) { return; } renderer.DrawText(x, y, cut.Data, cut.CutType.CutColor); if (cut.CutType == UCutType.Error) { renderer.DrawDash( new UPoint((int)x, (int)(y + renderer.CharHeight)), new UPoint((int)(x + UHelper.GetAbsoluteLength(cut.Data) * renderer.CharWidth), (int)(y + renderer.CharHeight)), UColor.Red); } } catch (Exception ex) { throw ex; } }
/// <summary> /// 返回指定位置指定长度的字符串 /// </summary> /// <param name="pos"></param> /// <param name="len"></param> /// <returns></returns> public string GetCodeString(URank pos, int len) { if (pos.Row < 0 || pos.Row >= mCodeData.Count) { throw new Exception("GetCodeString : Row Invalid {" + pos.Row.ToString() + "}"); } if (pos.Col < 0 || pos.Col + len >= mCodeData[pos.Row].Count) { throw new Exception("GetCodeString : Col Invalid {" + pos.Col.ToString() + "}"); } return(UHelper.GetStringByBytes(mCodeData[pos.Row].GetRange(pos.Col, len).ToArray())); }
/// <summary> /// 返回指定区域的字符串 /// </summary> /// <param name="start">开始位置</param> /// <param name="end">结束位置</param> /// <returns></returns> public string GetCodeString(URank start, URank end) { try { StringBuilder sb = new StringBuilder(); List <byte> lineByte = new List <byte>(); int startRow = start.Row; int startCol = start.Col; int endRow = end.Row; int endCol = end.Col; if (start == end) { return(GetCodeString(start, endCol - startCol)); } // 复制第一行 int len = GetCodeLength(startRow); for (int i = startCol; i < len; i++) { lineByte.Add(GetCodeByte(new URank(startRow, i))); } sb.Append(UHelper.GetStringByBytes(lineByte.ToArray())); // 复制中间部分 for (int i = startRow + 1; i < endRow; i++) { sb.Append(UHelper.GetStringByBytes(mCodeData[i].ToArray())); } lineByte.Clear(); // 复制最后一行 for (int i = 0; i < endCol; i++) { lineByte.Add(GetCodeByte(new URank(endRow, i))); } sb.Append(UHelper.GetStringByBytes(lineByte.ToArray())); return(sb.ToString()); } catch (Exception ex) { throw ex; } }
/// <summary> /// 选中当前匹配项 /// </summary> public void Selected() { if (mMatchingResult.Count > 0) { string inputString = Coder.CodeManager.GetCodeCut(new URank(Coder.CursorRow, Coder.CursorCol - 1)).Data; ReplaceStringCommand rsc = new ReplaceStringCommand( Coder.CodeManager, new URank(Coder.CursorRow, Coder.CursorCol - UHelper.GetAbsoluteLength(inputString)), UHelper.GetAbsoluteLength(inputString), mMatchingResult[mSelectIndex]); Coder.CodeManager.Execute(rsc); Reset(); } }
/// <summary> /// 在指定位置插入一个字符串 /// </summary> /// <param name="pos">开始位置</param> /// <param name="text">要插入的字符串</param> public void InsertString(URank pos, string str) { if (pos.Row < 0 || pos.Row >= mCodeData.Count) { throw new Exception("InsertString : Row Invalid {" + pos.Row.ToString() + "}"); } if (pos.Col < 0 || pos.Col >= mCodeData[pos.Row].Count) { throw new Exception("InsertString : Col Invalid {" + pos.Col.ToString() + "}"); } mCodeData[pos.Row].InsertRange(pos.Col, UHelper.GetBytesByString(str)); mCodeLines[pos.Row] = mParser.ParseLine(pos.Row); mCoder.SetCursorPos(new URank(pos.Row, pos.Col + UHelper.GetAbsoluteLength(str))); }
public void AddCut(UCodeCut cut) { int len = UHelper.GetAbsoluteLength(cut.Data); if (HasVisibleCharacter == false && cut.CutType == UCutType.Space) { VisibleStartCol = VisibleStartCol + len;// UHelper.GetAbsoluteLength(cut.Data); } else { if (cut.CutType != UCutType.NewLine) { HasVisibleCharacter = true; } } mCutStartColList.Add(mCutStartCol); mCuts.Add(cut); mCutStartCol += len;// UHelper.GetAbsoluteLength(cut.Data); }
/// <summary> /// 载入文件 /// </summary> /// <param name="filePath">文件路径</param> public void LoadFile(string filePath) { try { mCodeData.Clear(); GC.Collect(); StreamReader fp = new StreamReader(filePath, Encoding.Default); string line = string.Empty; while (!fp.EndOfStream) { line = fp.ReadLine(); if (line == string.Empty || line[line.Length - 1] != '\n') { line += '\n'; } List <byte> lineData = new List <byte>(); lineData.AddRange(UHelper.TanslateString(line)); mCodeData.Add(lineData); } fp.Close(); fp.Dispose(); if (mCodeData.Count == 0) { List <byte> oneLine = new List <byte>(); oneLine.Add(UConfig.NewLine); mCodeData.Add(oneLine); } } catch (Exception ex) { throw ex; } }
public InsertRangeStringCommand(UCodeManager codeManager, URank start, string insertString) { this.mCodeManager = codeManager; this.mStart = start; this.mEnd = start; this.mInsertString = insertString; byte[] data = UHelper.TanslateString(insertString); for (int i = 0; i < data.Length; i++) { if (data[i] == UConfig.NewLine) { this.mEnd.Row++; this.mEnd.Col = 0; continue; } this.mEnd.Col++; } }
private void 保存SToolStripMenuItem_Click(object sender, EventArgs e) { if (mCurrFile == string.Empty) { return; } StreamWriter fp = new StreamWriter(mCurrFile, false, Encoding.Default); int len = coder.CodeManager.GetCodeCount(); for (int i = 0; i < len; i++) { fp.WriteLine(UHelper.GetStringByBytes(coder.CodeManager.GetCodeData(i))); } fp.Close(); fp.Dispose(); MessageBox.Show(mCurrFile + "\r\n保存成功"); }
/// <summary> /// 插入多行字符串 /// </summary> /// <param name="start">开始位置</param> /// <param name="rangeStr">字符串</param> public void InsertRangeString(URank start, string rangeStr) { int row = start.Row; int col = start.Col; byte[] data = UHelper.TanslateString(rangeStr); for (int i = 0; i < data.Length; i++) { mCodeData[row].Insert(col, data[i]); col++; if (data[i] == UConfig.NewLine) { // 遇到换行符,首先剪切之后的数据到下一行 List <byte> newLine = new List <byte>(); for (int n = col; n < mCodeData[row].Count; n++) { newLine.Add(mCodeData[row][n]); } mCodeData[row].RemoveRange(col, mCodeData[row].Count - col); mCodeLines[row] = mParser.ParseLine(row); row++; col = 0; mCodeData.Insert(row, newLine); mCodeLines.Insert(row, new UCodeLine()); mCodeLines[row] = mParser.ParseLine(row); continue; } } mCodeLines[row] = mParser.ParseLine(row); mCoder.SetCursorPos(new URank(row, col)); }
private void RenderCodeLine(IRenderer renderer, float x, float y, UCodeLine line) { try { float renderX = x; int count = line.GetCutCount(); for (int i = 0; i < count; i++) { UCodeCut cut = line.GetCutByIndex(i); RenderCodeCut(renderer, renderX, y, cut); renderX = renderX + UHelper.GetAbsoluteLength(cut.Data) * renderer.CharWidth; } } catch (Exception ex) { throw ex; } }
/// <summary> /// 返回下一个代码切块 /// </summary> /// <returns></returns> public UCodeCut GetNextCut() { try { UCodeCut cut = new UCodeCut(); List <byte> cutData = new List <byte>(); byte b; UCutType currType = UCutType.None; bool inString = false; // 是否解析字符串 if (EndOfCode == true) { cut.CutType = UCutType.End; return(cut); } while (!EndOfCode) { b = GetNextByte(); cutData.Add(b); #region UCutType.None if (currType == UCutType.None) { if (b == UConfig.Space) { currType = UCutType.Space; continue; } if (b == UConfig.Tab) { currType = UCutType.Tab; break; } if (b == UConfig.DoubleQuote) { currType = UCutType.String; inString = true; continue; } if (b == UConfig.NewLine) { currType = UCutType.NewLine; // 跳过回车符 if (PeekNextByte() == UConfig.Enter) { GetNextByte(); } break; } if (b == UConfig.BackSlash) { if (PeekNextByte() == UConfig.BackSlash) { currType = UCutType.Annotation; continue; } } if (UHelper.IsSymbol(b)) { currType = UCutType.Symbol; break; } if (UHelper.IsCharacter(b)) { currType = UCutType.Normal; continue; } if (UHelper.IsDigit(b)) { currType = UCutType.Digit; continue; } } #endregion #region UCutType.Normal if (currType == UCutType.Normal) { if (UHelper.IsCutEnd(b)) { BackToLastByte(); cutData.RemoveAt(cutData.Count - 1); break; } } #endregion #region UCutType.String if (currType == UCutType.String) { if (b == UConfig.NewLine) { BackToLastByte(); currType = UCutType.Normal; break; } if (b == UConfig.DoubleQuote) { inString = false; break; } if (b == UConfig.Slash) { // 添加 \ 后的字符 if (inString) { //ch = (char)GetNextChar(); //cutData.Add((byte)ch); cutData.Add(GetNextByte()); continue; } } } #endregion #region UCutType.Space if (currType == UCutType.Space) { if (b != UConfig.Space) { BackToLastByte(); cutData.RemoveAt(cutData.Count - 1); break; } } #endregion #region UCutType.Digit if (currType == UCutType.Digit) { if (UHelper.IsCutEnd(b)) { BackToLastByte(); cutData.RemoveAt(cutData.Count - 1); break; } if (!UHelper.IsDigit(b)) { BackToLastByte(); cutData.RemoveAt(cutData.Count - 1); break; } } #endregion #region UCutType.Annotation if (currType == UCutType.Annotation) { if (b == UConfig.NewLine) { BackToLastByte(); cutData.RemoveAt(cutData.Count - 1); break; } } #endregion } cut.CutType = currType; // 替换Tab为Space if (currType == UCutType.Tab) { cut.Data = UConfig.TabString; } else { cut.Data = UHelper.GetStringByBytes(cutData.ToArray()); } return(ParseCodeCutType(cut)); } catch (Exception ex) { throw ex; } }
// 分析cut的类型 private UCodeCut ParseCodeCutType(UCodeCut cut) { // 如果上一个Cut是错误的,之后都是错误 if (LastCut1.CutType == UCutType.Error) { cut.CutType = UCutType.Error; return(cut); } // 如果是普通的一段文本,判断是否为关键字 if (cut.CutType == UCutType.Normal) { if (UHelper.IsKeyWord(cut.Data)) { cut.CutType = UCutType.KeyWord; // 新版:设置不同关键字的颜色 cut.CutType.CutColor = UHelper.GetKeyWordColor(cut.Data); } } // 如果是以关键字开头,空格结尾,那么检查是否为类定义符 if (LastCut2.CutType == UCutType.KeyWord && LastCut1.CutType == UCutType.Space) { // 类的定义 if (LastCut2.Data == UConfig.ClassString) { cut.CutType = UCutType.ClassName; foreach (string str in UConfig.KeyWords) { if (str == cut.Data) { cut.CutType = UCutType.Error; break; } } } } else if (LastCut1.CutType == UCutType.Space) { // 函数名和变量的定义 foreach (string str in UConfig.VariableType) { if (str == LastCut2.Data) { if (PeekNextByte() == (byte)'(') { cut.CutType = UCutType.FunctionName; } else { cut.CutType = UCutType.VariableName; } foreach (string s in UConfig.KeyWords) { if (s == cut.Data) { cut.CutType = UCutType.Error; break; } } break; } } } // 引用类型,直接赋值是地址 //LastCut2 = LastCut1; //LastCut1 = cut; LastCut2 = new UCodeCut(LastCut1); LastCut1 = new UCodeCut(cut); return(cut); }
public void KeyDown(UKeyEvents e) { if (mIsBlankWindow) { return; } switch (e.KeyCode) { case UKeys.Left: mCodeRenderer.DecreaseCursorCol(); mIntelligentSence.Reset(); return; case UKeys.Right: mCodeRenderer.IncreaseCursorCol(); mIntelligentSence.Reset(); return; case UKeys.Up: if (mIntelligentSence.HasMatching) { mIntelligentSence.DecreaseIndex(); } else { mCodeRenderer.DecreaseCursorRow(); } return; case UKeys.Down: if (mIntelligentSence.HasMatching) { mIntelligentSence.IncreaseIndex(); } else { mCodeRenderer.IncreaseCursorRow(); } return; case UKeys.Back: // 删除键是删除鼠标前一个位置的字符 if (mSelection.HasSelection) { RemoveSelection(); return; } if (mCodeRenderer.CursorCol == 0) // 在行首删除,合并当上一行 { if (mCodeRenderer.CursorRow > 0) { RemoveNewlineByteCommand rnc = new RemoveNewlineByteCommand( mCodeManager, new URank(mCodeRenderer.CursorRow - 1, mCodeManager.GetCodeLength(mCodeRenderer.CursorRow - 1) - 1)); mCodeManager.Execute(rnc); } } else { RemoveCharacterCommand rcc = new RemoveCharacterCommand( mCodeManager, new URank(mCodeRenderer.CursorRow, mCodeRenderer.CursorCol - 1)); mCodeManager.Execute(rcc); } return; case UKeys.Space: { // 这里不要return,留到下面处理space添加一个空格 if (mIntelligentSence.HasMatching) { mIntelligentSence.Selected(); } else if (mSelection.HasSelection) { RemoveSelection(); } } break; case UKeys.Enter: { if (mIntelligentSence.HasMatching) { mIntelligentSence.Selected(); return; } URank pos = mCodeRenderer.GetCursorPos(); InsertNewLineByteCommand inc = new InsertNewLineByteCommand(mCodeManager, pos); mCodeManager.Execute(inc); // 缩进处理 // 计算新的一行需要额外填充的空格 // 从上一行的可见字符开始 int padSpaceCount = mCodeManager.GetCodeLine(pos.Row).VisibleStartCol; // 按回车后,检查是否是在“{”或“}”后按的回车 if (pos.Col - 1 >= 0) { byte lastByte = mCodeManager.GetCodeData(pos.Row)[pos.Col - 1]; // 最后一个字符是回车,所以应该减一 // 如果是左括号“{” if (lastByte == UConfig.LBracket) { padSpaceCount += UConfig.TabNumberOfSpace; } } if (padSpaceCount > 0) { // 如果只是在一行的空白部分中间按下回车 // 那么padSpaceCount = 上一行的空白部分 // 并且新的一行本身就包含了一部分 // 加起来刚好 byte[] spaceBytes = new byte[padSpaceCount]; for (int i = 0; i < padSpaceCount; i++) { spaceBytes[i] = UConfig.Space; } InsertStringCommand isc = new InsertStringCommand(mCodeManager, new URank(pos.Row + 1, 0), UHelper.GetStringByBytes(spaceBytes)); mCodeManager.Execute(isc); } return; } case UKeys.A: if (e.Ctrl) { SelectAll(); return; } break; case UKeys.C: if (e.Ctrl) { Copy(); return; } break; case UKeys.V: if (e.Ctrl) { Paste(); return; } break; case UKeys.X: if (e.Ctrl) { Cut(); return; } break; case UKeys.Y: if (e.Ctrl) { mCodeManager.Redo(); return; } break; case UKeys.Z: if (e.Ctrl) { mCodeManager.Undo(); return; } break; default: break; } char ch = UHelper.ParseKey(e); if (ch == (char)0) { return; } if (mSelection.HasSelection) { RemoveSelection(); } InsertCharacterCommand iccd = new InsertCharacterCommand(mCodeManager, mCodeRenderer.GetCursorPos(), (byte)ch); mCodeManager.Execute(iccd); // 如果输入的字符是“}”,那么缩进一个Tab // 处理输入字符“{”,是在按回车处理 if ((byte)ch == UConfig.RBracket) { URank pos = mCodeRenderer.GetCursorPos(); int visibleStartCol = mCodeManager.GetCodeLine(pos.Row).VisibleStartCol; if (visibleStartCol >= UConfig.TabNumberOfSpace) { // 缩进一个Tab,先找到最靠近第一个可见字符的空白字符,然后删除往前的空格 RemoveStringCommand rsc = new RemoveStringCommand( mCodeManager, new URank(pos.Row, visibleStartCol - UConfig.TabNumberOfSpace), UConfig.TabNumberOfSpace); mCodeManager.Execute(rsc); mCodeRenderer.SetCursorPos( new URank( pos.Row, mCodeManager.GetCodeLength(pos.Row) - 1)); } } }
public void Undo() { mCodeManager.RemoveString(mPosition, UHelper.GetAbsoluteLength(mInsertString)); }