private void _updateCurrentWord(string currentWord) { if (currentWord == null || _textEditor.CaretOffset == 0 || _toIgnore.Any(p => currentWord.Contains(p))) { _renderer.CurrentResults.Clear(); return; } if (currentWord == ">") { int offsetPrevious = _textEditor.Text.LastIndexOf('<', _textEditor.CaretOffset); int offsetNext = _textEditor.Text.IndexOf('>', offsetPrevious + 1) + 1; if (offsetNext != _textEditor.CaretOffset || offsetNext <= offsetPrevious) { _renderer.CurrentResults.Clear(); return; } currentWord = _textEditor.Text.Substring(offsetPrevious, offsetNext - offsetPrevious); _currentWord = currentWord; } if (currentWord == "<") { int offsetNext = _textEditor.Text.IndexOf('>', _textEditor.CaretOffset); int offsetPrevious = _textEditor.Text.LastIndexOf('<', offsetNext - 1); if (offsetPrevious != _textEditor.CaretOffset || offsetNext <= offsetPrevious) { _renderer.CurrentResults.Clear(); return; } offsetNext++; currentWord = _textEditor.Text.Substring(offsetPrevious, offsetNext - offsetPrevious); _currentWord = currentWord; } new Thread(new ThreadStart(delegate { lock (_lock) { try { if (currentWord != _currentWord) return; Thread.Sleep(300); if (currentWord != _currentWord) return; Regex pattern = new Regex(Regex.Escape(currentWord), RegexOptions.Compiled); RegexSearchStrategy strategy = new RegexSearchStrategy(pattern, true); _textEditor.Dispatch(delegate { try { _renderer.CurrentResults.Clear(); if (!string.IsNullOrEmpty(currentWord)) { // We cast from ISearchResult to SearchResult; this is safe because we always use the built-in strategy foreach (SearchResult result in strategy.FindAll(_textArea.Document, 0, _textArea.Document.TextLength)) { _renderer.CurrentResults.Add(result); } } _textArea.TextView.InvalidateLayer(KnownLayer.Selection); } catch { } }); } catch (ArgumentException ex) { throw new SearchPatternException(ex.Message, ex); } } })).Start(); }
private void _textArea_TextEntering(object sender, TextCompositionEventArgs e) { try { if (e.Text.Length > 0 && _completionWindow != null) { if (!char.IsLetterOrDigit(e.Text[0]) && e.Text[0] != '_' && e.Text[0] != ' ') { string word = AvalonLoader.GetWholeWordAdv(_textEditor.TextArea.Document, _textEditor); var strategy = new RegexSearchStrategy(new Regex(word), true); if ((e.Text[0] != '\t' || e.Text[0] != '\n') && strategy.FindAll(_textEditor.Document, 0, _textEditor.Document.TextLength).Count() > 1) { _completionWindow.Close(); return; } string line = _getText(_textEditor.TextArea.Caret.Line); if (line.IndexOf('#') > -1) { _completionWindow.Close(); return; } _completionWindow.CompletionList.RequestInsertion(e); } else if (e.Text[0] == ' ') { _completionWindow.Close(); } } if (e.Text.Length > 0 && _completionWindow == null) { if (e.Text[0] == '\n') { int currentLine = _textEditor.TextArea.Caret.Line; DocumentLine docLine = _getLine(currentLine); string line = _getText(currentLine); int currentIndent = LineHelper.GetIndent(line); if (line.EndsWith(":") && _textEditor.CaretOffset >= docLine.EndOffset) { currentIndent++; } if (_textEditor.LineCount == currentLine) { _textEditor.Document.Insert(_textEditor.Document.TextLength, "\n" + LineHelper.GenerateIndent(currentIndent)); _textEditor.CaretOffset = _textEditor.Text.Length; } else { var position = _textEditor.CaretOffset; _textEditor.Document.Insert(_textEditor.CaretOffset, "\n" + LineHelper.GenerateIndent(currentIndent)); _textEditor.CaretOffset = position + ("\n" + LineHelper.GenerateIndent(currentIndent)).Length; } _textEditor.TextArea.Caret.BringCaretToView(); e.Handled = true; } } } catch { } }