private void InsertSelectedWord() { try { SqlWordToken token = this.GetLastWordToken(true, true); ListViewItem item = this.lvWords.SelectedItems[0]; object tag = item.Tag; string selectedWord = item.SubItems[1].Text; int length = token.StartIndex == token.StopIndex ? 0 : token.StopIndex - token.StartIndex + 1; this.txtEditor.Select(token.StartIndex, length); string quotationValue = selectedWord; if (!(tag is FunctionSpecification)) { quotationValue = this.DbInterpreter.GetQuotedString(selectedWord); } this.txtEditor.SelectedText = quotationValue; this.SetWordListViewVisible(false); this.txtEditor.SelectionStart = this.txtEditor.SelectionStart; this.txtEditor.Focus(); } catch (Exception ex) { } }
private void SetWordColor(SqlWordToken token, bool keepCurrentPos = false) { Color color = Color.Black; if (token.Type == SqlWordTokenType.Keyword) { color = Color.Blue; } else if (token.Type == SqlWordTokenType.BuiltinFunction) { color = ColorTranslator.FromHtml("#FF00FF"); } else if (token.Type == SqlWordTokenType.String) { color = Color.Red; } else if (token.Type == SqlWordTokenType.Comment) { color = ColorTranslator.FromHtml("#008000"); } int start = this.txtEditor.SelectionStart; this.txtEditor.Select(token.StartIndex, token.StopIndex - token.StartIndex + 1); this.txtEditor.SelectionBackColor = this.txtEditor.BackColor; this.txtEditor.SelectionColor = color; this.txtEditor.SelectionStart = keepCurrentPos ? start : token.StopIndex + 1; this.txtEditor.SelectionLength = 0; }
private void ClearStyle(SqlWordToken token) { this.txtEditor.Select(token.StartIndex, token.StopIndex - token.StartIndex + 1); this.txtEditor.SelectionColor = Color.Black; this.txtEditor.SelectionStart = token.StopIndex + 1; this.txtEditor.SelectionLength = 0; }
private void ShowWordListByToken(SqlWordToken token) { if (string.IsNullOrEmpty(token.Text)) { this.SetWordListViewVisible(false); return; } SqlWordTokenType type = this.DetectTypeByWord(token.Text); var words = SqlWordFinder.FindWords(this.DatabaseType, token.Text, type); this.ShowWordList(words); }
private SqlWordToken GetLastWordToken(bool noAction = false, bool isInsert = false) { SqlWordToken token = null; int currentIndex = this.txtEditor.SelectionStart; int lineIndex = this.txtEditor.GetLineFromCharIndex(currentIndex); int lineFirstCharIndex = this.txtEditor.GetFirstCharIndexOfCurrentLine(); int index = currentIndex - 1; if (index < 0 || index > this.txtEditor.Text.Length - 1) { return(token); } token = new SqlWordToken(); bool isDot = false; if (this.txtEditor.Text[index] == '.') { isDot = true; if (isInsert) { token.StartIndex = token.StopIndex = this.txtEditor.SelectionStart; token.Text = "."; return(token); } index = index - 1; } token.StopIndex = index; string lineBefore = this.txtEditor.Text.Substring(lineFirstCharIndex, currentIndex - lineFirstCharIndex); bool isComment = false; if (lineBefore.Contains(this.DbInterpreter.CommentString)) { isComment = true; } string word = ""; if (!isComment) { List <char> chars = new List <char>(); string delimeterPattern = @"[ ,\.\r\n=]"; int i = -1; bool exited = false; for (i = index; i >= 0; i--) { char c = this.txtEditor.Text[i]; if (!Regex.IsMatch(c.ToString(), delimeterPattern)) { chars.Add(c); if (c == '\'') { break; } else if (c == '(') { if (chars.Count > 1) { chars.RemoveAt(chars.Count - 1); i++; } break; } } else { exited = true; break; } } if (i == -1) { i = 0; } chars.Reverse(); word = string.Join("", chars); token.Text = word; token.StartIndex = i + (exited ? 1 : 0); //if (token.StartIndex > token.StopIndex) //{ // token.StartIndex = token.StopIndex; //} if (word.Contains("'")) { int singQuotationCount = lineBefore.Count(item => item == '\''); bool isQuotationPaired = singQuotationCount % 2 == 0; if (isQuotationPaired && word.StartsWith("'")) { List <char> afterChars = new List <char>(); for (int j = currentIndex; j < this.txtEditor.Text.Length; j++) { char c = this.txtEditor.Text[j]; if (Regex.IsMatch(c.ToString(), delimeterPattern)) { break; } else { afterChars.Add(c); } } string afterWord = string.Join("", afterChars); if (afterWord.EndsWith("'") || (word == "'" && afterChars.Count == 0)) { token.Type = SqlWordTokenType.String; } else { token.StartIndex++; token.Text = token.Text.Substring(1); } } else if (!isQuotationPaired || (isQuotationPaired && word.EndsWith("'"))) { token.Type = SqlWordTokenType.String; } if (token.Type == SqlWordTokenType.String) { this.SetWordColor(token); return(token); } } } else { int firstIndexOfComment = lineFirstCharIndex + lineBefore.IndexOf(this.DbInterpreter.CommentString); token.StartIndex = firstIndexOfComment; token.StopIndex = lineFirstCharIndex + this.txtEditor.Lines[lineIndex].Length - 1; } if (!noAction) { if (this.keywords.Any(item => item.ToUpper() == word.ToUpper())) { token.Type = SqlWordTokenType.Keyword; this.SetWordColor(token); } else if (this.builtinFunctions.Any(item => item.Name.ToUpper() == word.ToUpper())) { token.Type = SqlWordTokenType.BuiltinFunction; this.SetWordColor(token); } else if (isComment) { token.Type = SqlWordTokenType.Comment; this.SetWordColor(token, true); } else { if (!isDot) { this.ClearStyle(token); } } } return(token); }
private void HandleKeyUpFoIntellisense(KeyEventArgs e) { if (e.KeyValue >= 112 && e.KeyValue <= 123) { this.SetWordListViewVisible(false); return; } if (e.KeyCode == Keys.Space) { this.ClearStyleForSpace(); this.SetWordListViewVisible(false); } SqlWordToken token = this.GetLastWordToken(); if (token == null || token.Text == null || token.Type != SqlWordTokenType.None) { this.SetWordListViewVisible(false); if (token != null) { if (token.Type != SqlWordTokenType.String && token.Text.Contains("'")) { this.ClearStyle(token); } } return; } if (this.panelWords.Visible) { if (this.lvWords.Tag is SqlWord word) { if (word.Type == SqlWordTokenType.Table) { string columnName = null; int index = this.txtEditor.SelectionStart; char c = this.txtEditor.Text[index - 1]; if (c != '.') { columnName = token.Text; } this.ShowTableColumns(word.Text, columnName); } else if (word.Type == SqlWordTokenType.Owner) { this.ShowDbObjects(token.Text); } return; } } if (e.KeyData == Keys.OemPeriod) { SqlWord word = this.FindWord(token.Text); if (word.Type == SqlWordTokenType.Table) { this.ShowTableColumns(word.Text); this.lvWords.Tag = word; } else if (word.Type == SqlWordTokenType.Owner) { this.ShowDbObjects(null, word.Text); this.lvWords.Tag = word; } } else if (e.KeyCode == Keys.Back) { if (this.panelWords.Visible) { if (!this.IsMatchWord(token.Text) || this.txtEditor.Text.Length == 0) { this.SetWordListViewVisible(false); } else { this.ShowWordListByToken(token); } } if (token.Text.Length > 0 && this.DbInterpreter.CommentString.Contains(token.Text.Last())) { int start = this.txtEditor.SelectionStart; int lineIndex = this.txtEditor.GetLineFromCharIndex(start); int stop = this.txtEditor.GetFirstCharIndexFromLine(lineIndex) + this.txtEditor.Lines[lineIndex].Length - 1; RichTextBoxHelper.Highlighting(this.txtEditor, this.DatabaseType, true, start, stop); } } else if (e.KeyValue < 48 || (e.KeyValue >= 58 && e.KeyValue <= 64) || (e.KeyValue >= 91 && e.KeyValue <= 96) || e.KeyValue > 122) { this.SetWordListViewVisible(false); } else { this.ShowWordListByToken(token); } }