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另存为成功"); } }
/// <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; } }
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> /// <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; } }
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)); } } }