public MainWindow() { InitializeComponent(); // 更新状态栏 CodeBox.SelectionChanged += (s, e) => { int lineIndex = CodeBox.GetLineIndexFromCharacterIndex(CodeBox.CaretIndex); LineIndexSBI.Content = lineIndex + 1; ColIndexSBI.Content = CodeBox.CaretIndex - CodeBox.GetCharacterIndexFromLineIndex(lineIndex) + 1; SelectionLengthSBI.Content = CodeBox.SelectionLength; }; CodeBox.DecorationScheme = CodeBoxControl.Decorations.DecorationSchemes.Java; }
/// <summary> /// 启动编译 /// </summary> private async void RunAsync() { StateSBI.Content = "编译中..."; if (parserAdapter == null) { parserAdapter = new ParserAdapter(); } var result = await parserAdapter.Compile(CodeBox.Text, Path.RULE_PATH, Path.CFG_PATH); FirstTableDataGrid.ItemsSource = result.First; FollowTableDataGrid.ItemsSource = result.Follow; ASTView.ItemsSource = new List <Node>() { result.Root }; ArithResultDataGrid.ItemsSource = result.ArithResult; ErrorDataGrid.ItemsSource = result.Errors; CodeBox.popup.ClearTokens(); foreach (var id in result.IDs) { int index = CodeBox.popup.tokens.BinarySearch(id); if (index < 0) { CodeBox.popup.tokens.Insert(~index, id); } } CodeBox.popup.RefreshList(); var pairDecoration = new CodeBoxControl.Decorations.PairDecoration() { DecorationType = CodeBoxControl.Decorations.EDecorationType.Underline, Brush = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)), }; foreach (var error in result.Errors) { int offset = CodeBox.GetCharacterIndexFromLineIndex(error.Row - 1) + error.Col - 1; pairDecoration.Pairs.Add(new CodeBoxControl.Pair(offset, error.WordLength)); } CodeBox.Decorations.Clear(); CodeBox.Decorations.Add(pairDecoration); CodeBox.InvalidateVisual(); StateSBI.Content = "就绪"; }
/// <summary> /// 注释多行 /// </summary> private void CommentMultiLines() { // 将选择区域修正成首行行头到末行行尾 int firstLine = CodeBox.GetLineIndexFromCharacterIndex(CodeBox.SelectionStart); int lastLine = CodeBox.GetLineIndexFromCharacterIndex(CodeBox.SelectionStart + CodeBox.SelectionLength); CodeBox.SelectionStart = CodeBox.GetCharacterIndexFromLineIndex(firstLine); CodeBox.SelectionLength = CodeBox.GetCharacterIndexFromLineIndex(lastLine) + CodeBox.GetLineLength(lastLine) - CodeBox.SelectionStart; string[] lines = CodeBox.SelectedText.Split('\n'); StringBuilder sb = new StringBuilder(); int i = 0; for (; i < lines.Length - 1; i++) { sb.Append("//").Append(lines[i]).Append('\n'); } sb.Append("//").Append(lines[i]); CodeBox.textChangedEnable = false; CodeBox.SelectedText = sb.ToString(); CodeBox.textChangedEnable = true; }
/// <summary> /// 注释单行 /// </summary> private void CommentOneLine() { int lineindex = CodeBox.GetLineIndexFromCharacterIndex(CodeBox.SelectionStart); int firstCharIndex = CodeBox.GetCharacterIndexFromLineIndex(lineindex); int lastCharIndex = firstCharIndex + CodeBox.GetLineLength(lineindex) - 1; // 判断是否选择了该行所有非空白字符 bool selectWholeLine = true; for (int i = firstCharIndex; i <= lastCharIndex; i++) { if (i >= CodeBox.SelectionStart && i < CodeBox.SelectionStart + CodeBox.SelectionLength) { continue; } else if (char.IsWhiteSpace(CodeBox.Text[i])) { continue; } else { selectWholeLine = false; break; } } CodeBox.textChangedEnable = false; if (selectWholeLine) { CodeBox.SelectedText = "//" + CodeBox.SelectedText; } else { CodeBox.SelectedText = "/*" + CodeBox.SelectedText + "*/"; } CodeBox.textChangedEnable = true; }