//Process hot keys bool Query_DoProcessDialogKey(Keys keyData) { bool NoEcho = true, Echo = false; int CurPos; string TxtBef, TxtAft, CurrentWord; Token CurrentToken, LastToken; // Echo == true, then NoEcho == false #region Key shortcut processing switch (keyData) { case Keys.F5://Execute query if (BtnExecute.Enabled) BtnExecute_Click(null, null); return NoEcho; case Keys.Shift | Keys.F5://Stop execution if (BtnStop.Enabled) BtnStop_Click(null, null); return NoEcho; case Keys.Control | Keys.F5://Forced stop execution if (BtnExtremeStop.Enabled) BtnExtremeStop_Click(null, null); return NoEcho; case Keys.Control | Keys.C://Comment selection / Collapse outlining if (LastKeyPressed == (Keys.Control | Keys.K)) {//comment selection, or current line LastKeyPressed = Keys.Space;//clean the last key pressed aux var, to avoid wrong behavior if (Query.Enabled) BtnComment_Click(null, null); return NoEcho; } else if (LastKeyPressed == (Keys.Control | Keys.O)) {//collapse outlining LastKeyPressed = Keys.Space;//clean the last key pressed aux var, to avoid wrong behavior if (Query.Enabled) collapseToolStripMenuItem_Click(null, null); return NoEcho; } return Echo; case Keys.Control | Keys.U://Uncomment selection / to lower case the selection if (LastKeyPressed == (Keys.Control | Keys.K)) { LastKeyPressed = Keys.Space;//clean the last key pressed aux var, to avoid wrong behavior if (Query.Enabled) BtnUncomment_Click(null, null); return NoEcho; } else {//to lower case the selection toLowerCaseToolStripMenuItem1_Click(null, null); } return NoEcho; case Keys.Control | Keys.Shift | Keys.U://to upper case selection toUpperCseToolStripMenuItem_Click(null, null); break; case Keys.Control | Keys.F://Open search dialog BtnSearch_Click(null, null); return NoEcho; case Keys.Control | Keys.F3: string lookFor; if (Query.ActiveTextAreaControl.TextArea.SelectionManager.HasSomethingSelected) { lookFor = Query.ActiveTextAreaControl.TextArea.SelectionManager.SelectedText; } else { Token selToken; TokenList tokens = Query.Text.GetTokens(); selToken = tokens.GetTokenAtOffset(Query.CurrentOffset()); lookFor = selToken.Text; } if (_findForm == null) _findForm = new SearchAndReplace(); if (!String.IsNullOrEmpty(lookFor) && lookFor.Trim(' ', '\t', '\r', '\n').Length > 0) { _findForm.SetSearchString(lookFor); FindNext(true, false, String.Format("Specified text: {0}, was not found.", _findForm.LookFor)); return NoEcho; } break; case Keys.F3://Search next (forward) if (_findForm != null && !String.IsNullOrEmpty(_findForm.LookFor) && _findForm.LookFor.Trim(' ', '\t', '\r', '\n').Length > 0) { FindNext(true, false, String.Format("Specified text: {0}, was not found.", _findForm.LookFor)); } return NoEcho; case Keys.Shift | Keys.F3://Search next (Backward) if (_findForm != null && !String.IsNullOrEmpty(_findForm.LookFor) && _findForm.LookFor.Trim(' ', '\t', '\r', '\n').Length > 0) { FindNext(true, true, String.Format("Specified text: {0}, was not found.", _findForm.LookFor)); } return NoEcho; case Keys.F2://Toggle bookmark BtnBookmark_Click(null, null); return NoEcho; case Keys.Shift | Keys.F2://Go to previous bookmark BtnPrevious_Click(null, null); return NoEcho; case Keys.Control | Keys.F2://Go to next bookmark BtnNext_Click(null, null); return NoEcho; case Keys.Control | Keys.Shift | Keys.F2://Clear bookmarks BtnClearBookmarks_Click(null, null); return NoEcho; case Keys.Control | Keys.G://Save to file BtnSave_Click(null, null); return NoEcho; case Keys.Control | Keys.L://Load file BtnLoad_Click(null, null); return NoEcho; case Keys.Control | Keys.W://Hide/show results tab BtnShowHideResults_Click(null, null); return NoEcho; case Keys.F12://go to definition Token selToken2; TokenList tokens2 = Query.Text.GetTokens(); selToken2 = tokens2.GetTokenAtOffset(Query.CurrentOffset()); if (selToken2 != null && selToken2.Type == TokenType.WORD) { ISqlObject Obj = DataProvider.IsSqlObject(selToken2.Text); if (Obj != null) { Parent.AddQueryForm(Obj.Name, Obj.Script, DataProvider); } } return NoEcho; case Keys.Control | Keys.E://Expand outlining if (LastKeyPressed == (Keys.Control | Keys.O)) { LastKeyPressed = Keys.Space;//clean the last key pressed aux var, to avoid wrong behavior if (Query.Enabled) expandToolStripMenuItem_Click(null, null); return NoEcho; } return NoEcho; case Keys.Control | Keys.T://Toggle outlining if (LastKeyPressed == (Keys.Control | Keys.O)) { LastKeyPressed = Keys.Space; //clean the last key pressed aux var, to avoid wrong behavior if (Query.Enabled) toggleToolStripMenuItem_Click(null, null); return NoEcho; } else { break; } } #endregion CurPos = Query.CurrentOffset(); TxtBef = Query.Document.GetText(0, CurPos); TxtAft = Query.Document.GetText(CurPos, Query.Text.Length - CurPos); #region Autocomplete/intellisense code if (IsIntellisenseOn) { #region Code to handle the key pressed if the "intellisense" is active switch (keyData) { case Keys.Back: AutocompleteDialog.Close(); if (!String.IsNullOrEmpty(CurrentFilterString)) { CurrentFilterString = CurrentFilterString.Remove(CurrentFilterString.Length - 1); CancelAutoCompleteClosure = true; ShowIntellisense(CurrentFilterString, GetAliasesAndAuxiliarTables(Query.Text), curFiltering); } return NoEcho; case Keys.Escape: AutocompleteDialog.Close(); curFiltering = FilteringType.None; break; case Keys.OemMinus: CurrentFilterString += "-"; ShowIntellisense(CurrentFilterString, GetAliasesAndAuxiliarTables(Query.Text), curFiltering); return Echo; case Keys.OemMinus | Keys.Shift: CurrentFilterString += "_"; ShowIntellisense(CurrentFilterString, GetAliasesAndAuxiliarTables(Query.Text), curFiltering); return Echo; case (Keys)65601: case Keys.A: case (Keys)65602: case Keys.B: case (Keys)65603: case Keys.C: case (Keys)65604: case Keys.D: case (Keys)65605: case Keys.E: case (Keys)65606: case Keys.F: case (Keys)65607: case Keys.G: case (Keys)65608: case Keys.H: case (Keys)65609: case Keys.I: case (Keys)65610: case Keys.J: case (Keys)65611: case Keys.K: case (Keys)65612: case Keys.L: case (Keys)65613: case Keys.M: case (Keys)65614: case Keys.N: case (Keys)65615: case Keys.O: case (Keys)65616: case Keys.P: case (Keys)65617: case Keys.Q: case (Keys)65618: case Keys.R: case (Keys)65619: case Keys.S: case (Keys)65620: case Keys.T: case (Keys)65621: case Keys.U: case (Keys)65622: case Keys.V: case (Keys)65623: case Keys.W: case (Keys)65624: case Keys.X: case (Keys)65625: case Keys.Y: case (Keys)65626: case Keys.Z: case Keys.NumPad0: case Keys.NumPad1: case Keys.NumPad2: case Keys.NumPad3: case Keys.NumPad4: case Keys.NumPad5: case Keys.NumPad6: case Keys.NumPad7: case Keys.NumPad8: case Keys.NumPad9: case System.Windows.Forms.Keys.LButton | System.Windows.Forms.Keys.ShiftKey | System.Windows.Forms.Keys.Space: case System.Windows.Forms.Keys.RButton | System.Windows.Forms.Keys.ShiftKey | System.Windows.Forms.Keys.Space: case System.Windows.Forms.Keys.LButton | System.Windows.Forms.Keys.RButton | System.Windows.Forms.Keys.ShiftKey | System.Windows.Forms.Keys.Space: case System.Windows.Forms.Keys.MButton | System.Windows.Forms.Keys.ShiftKey | System.Windows.Forms.Keys.Space: case System.Windows.Forms.Keys.LButton | System.Windows.Forms.Keys.MButton | System.Windows.Forms.Keys.ShiftKey | System.Windows.Forms.Keys.Space: case System.Windows.Forms.Keys.RButton | System.Windows.Forms.Keys.MButton | System.Windows.Forms.Keys.ShiftKey | System.Windows.Forms.Keys.Space: case System.Windows.Forms.Keys.LButton | System.Windows.Forms.Keys.RButton | System.Windows.Forms.Keys.MButton | System.Windows.Forms.Keys.ShiftKey | System.Windows.Forms.Keys.Space: case System.Windows.Forms.Keys.Back | System.Windows.Forms.Keys.ShiftKey | System.Windows.Forms.Keys.Space: case System.Windows.Forms.Keys.LButton | System.Windows.Forms.Keys.Back | System.Windows.Forms.Keys.ShiftKey | System.Windows.Forms.Keys.Space: case System.Windows.Forms.Keys.ShiftKey | System.Windows.Forms.Keys.Space: CurrentFilterString += ((char)keyData).ToString(); ShowIntellisense(CurrentFilterString, GetAliasesAndAuxiliarTables(Query.Text), curFiltering); return Echo; case Keys.Delete: case Keys.Left: case Keys.Right: return NoEcho; default: if (IsIntellisenseOn) return Echo; else return NoEcho; } #endregion } else { LastKeyPressed = keyData; #region Code to handle the key pressed if the intellisense is inactive switch (keyData) { case Keys.Control | Keys.OemPeriod://ctrl + . , Show autocomplete case Keys.Space | Keys.Control://ctrl + space, Show autocomplete CurrentToken = TxtBef.GetLastToken(); if (CurrentToken.Type == TokenType.EMPTYSPACE || CurrentToken.Type == TokenType.COMMA) { CurrentWord = ""; } else { CurrentWord = CurrentToken.Text; } CurrentFilterString = CurrentWord; AutoCompleteStartOffset = CurPos - CurrentWord.Length; curFiltering = FilteringType.Smart; ShowIntellisense(CurrentWord, GetAliasesAndAuxiliarTables(Query.Text), curFiltering); return NoEcho; case Keys.T | Keys.Control://ctrl + T , Show autocomplete for "field" items CurrentToken = TxtBef.GetLastToken(); if (CurrentToken.Type == TokenType.EMPTYSPACE || CurrentToken.Type == TokenType.COMMA) { CurrentWord = ""; } else { CurrentWord = CurrentToken.Text; } CurrentFilterString = CurrentWord; AutoCompleteStartOffset = CurPos - CurrentWord.Length; curFiltering = FilteringType.FieldItem; ShowIntellisense(CurrentWord, GetAliasesAndAuxiliarTables(Query.Text), curFiltering); return NoEcho; case Keys.Space://if the text before is an @ or an #, show posible vars or temp tables LastToken = TxtBef.GetLastToken(); if (LastToken.Text == "@") { TokenList Tokens = Query.Text.GetTokens(); List<string> Helper = new List<string>(); foreach (Token t in Tokens.List.Where(X => X.Type == TokenType.VARIABLE)) { string buff = t.Text.Contains(".") ? t.Text.Split('.')[0] : t.Text; if (!Helper.Contains(buff, StringComparer.CurrentCultureIgnoreCase)) Helper.Add(buff); } if (Helper.Count > 0) { ObjectSelector Os = new ObjectSelector("Variables detected on script", "Select variable", Helper); if (Os.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Query.InsertString(Os.SelectedObject.Length > 0 && Os.SelectedObject.StartsWith("@") ? Os.SelectedObject.Substring(1) : Os.SelectedObject); return NoEcho; } else { return Echo; } } return Echo; } else if (LastToken.Text == "#") { TokenList Tokens = Query.Text.GetTokens(); List<string> Helper = new List<string>(); foreach (Token t in Tokens.List.Where(X => X.Type == TokenType.TEMPTABLE)) { string buff = t.Text.Contains(".") ? t.Text.Split('.')[0] : t.Text; if (!Helper.Contains(buff, StringComparer.CurrentCultureIgnoreCase)) Helper.Add(buff); } if (Helper.Count > 0) { ObjectSelector Os = new ObjectSelector("Variables temp tables on script", "Select table", Helper); if (Os.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Query.InsertString(Os.SelectedObject.Length > 0 && Os.SelectedObject.StartsWith("#") ? Os.SelectedObject.Substring(1) : Os.SelectedObject); return NoEcho; } else { return Echo; } } return Echo; } else if (LastToken.Text.Equals("from", StringComparison.CurrentCultureIgnoreCase) || LastToken.Text.Equals("join", StringComparison.CurrentCultureIgnoreCase)) { Query.InsertString(" "); CurrentFilterString = ""; AutoCompleteStartOffset = CurPos + 1; curFiltering = FilteringType.FieldItem; ShowIntellisense("", GetAliasesAndAuxiliarTables(Query.Text), curFiltering); return NoEcho; } break; case Keys.OemPeriod: case Keys.Decimal://show Sql Object childs... if any Query.InsertString("."); CurPos = Query.CurrentOffset(); TxtBef = Query.Document.GetText(0, CurPos); TxtAft = Query.Document.GetText(CurPos, Query.Text.Length - CurPos); CurrentToken = TxtBef.GetLastToken(); if (CurrentToken.Type == TokenType.EMPTYSPACE || CurrentToken.Type == TokenType.COMMA) { CurrentWord = ""; } else { CurrentWord = CurrentToken.Text; } CurrentFilterString = CurrentWord; AutoCompleteStartOffset = CurPos - CurrentWord.Length; curFiltering = FilteringType.Smart; ShowIntellisense(CurrentWord, GetAliasesAndAuxiliarTables(Query.Text), curFiltering); return NoEcho; case Keys.S | Keys.Control://ctrl + s , show list of snippets List<string> SnippetsNames = new List<string>(); List<string> SnippetsValues = new List<string>(); foreach (Snippet sn in Parent.GetSnippetList()) { SnippetsNames.Add(sn.Name); SnippetsValues.Add(sn.ShortCut); } ObjectSelector SnSelector = new ObjectSelector("Snippets detected", "Select the snippet, to insert his shortcut", SnippetsValues, SnippetsNames, false, 1); if (SnSelector.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Query.InsertString(SnSelector.SelectedObject); return NoEcho; } else { return Echo; } break; case Keys.Tab://check if it is a request for a snippet, a variable or a temp table LastToken = TxtBef.GetLastToken(); if (LastToken.Text == "@") { TokenList Tokens = Query.Text.GetTokens(); List<string> Helper = new List<string>(); foreach (Token t in Tokens.List.Where(X => X.Type == TokenType.VARIABLE)) { string buff = t.Text.Contains(".") ? t.Text.Split('.')[0] : t.Text; if (!Helper.Contains(buff, StringComparer.CurrentCultureIgnoreCase)) Helper.Add(buff); } if (Helper.Count > 0) { ObjectSelector Os = new ObjectSelector("Variables detected on script", "Select variable", Helper); if (Os.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Query.InsertString(Os.SelectedObject.Length > 0 && Os.SelectedObject.StartsWith("@") ? Os.SelectedObject.Substring(1) : Os.SelectedObject); return NoEcho; } else { return Echo; } } return Echo; } else if (LastToken.Text == "#") { TokenList Tokens = Query.Text.GetTokens(); List<string> Helper = new List<string>(); foreach (Token t in Tokens.List.Where(X => X.Type == TokenType.TEMPTABLE)) { string buff = t.Text.Contains(".") ? t.Text.Split('.')[0] : t.Text; if (!Helper.Contains(buff, StringComparer.CurrentCultureIgnoreCase)) Helper.Add(buff); } if (Helper.Count > 0) { ObjectSelector Os = new ObjectSelector("Variables temp tables on script", "Select table", Helper); if (Os.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Query.InsertString(Os.SelectedObject.Length > 0 && Os.SelectedObject.StartsWith("#") ? Os.SelectedObject.Substring(1) : Os.SelectedObject); return NoEcho; } else { return Echo; } } return Echo; } else if (LastToken.Type == TokenType.WORD) {//check for snippet string SnippetScript = Parent.IsInsertSnippet(LastToken.Text); if (!String.IsNullOrEmpty(SnippetScript)) { int Offset = Query.CurrentOffset(); Query.SetSelectionByOffset(Offset - LastToken.Text.Length, Offset); InsertSnippet(SnippetScript); return NoEcho; } return Echo; } break; default: //this.Text = keyData.ToString(); return Echo; } #endregion } #endregion return Echo; }
private void InsertSnippet(string SnippetScript) { string ProcessedSnippet = SnippetScript; List<string> Objs; List<SnippetInnerObject> InnerObjects = new List<SnippetInnerObject>(); if (ProcessedSnippet.IndexOf("$OBJ:") > 0) { Objs = DataProvider.DbObjects.Where(X => X.Kind != ObjectType.Schema && X.Kind != ObjectType.Alias).Select(X => X.Schema + "." + X.Name).ToList(); while (ProcessedSnippet.IndexOf("$OBJ:") > 0) { try { int start = ProcessedSnippet.IndexOf("$OBJ:"); int length = (ProcessedSnippet.IndexOf('$', start + 1) - start) + 1; if (length <= 0 || ProcessedSnippet.IndexOf('$', start + 1) == -1) { MessageBox.Show("Error while processing the snippet, check sintax(not found ending $ for object)", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } string Id = ProcessedSnippet.Substring(start, length); Id = Id.Trim('$').Split(':')[1]; if (String.IsNullOrEmpty(Id) || Id.Trim().Length == 0) { MessageBox.Show("Error while processing the snippet, check sintax(not found object id for object)", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } SnippetInnerObject Obj; ObjectSelector Os = new ObjectSelector("Objects on the current database connection", "Select a database object", Objs, false, 5); if (Os.ShowDialog() == System.Windows.Forms.DialogResult.OK) { Obj = new SnippetInnerObject() { Id = Id, Object = DataProvider.DbObjects.Find (X => String.Format("{0}.{1}", X.Schema, X.Name).Equals(Os.SelectedObject) && X.Kind != ObjectType.Schema && X.Kind != ObjectType.Alias ) }; switch (Obj.Object.Kind) { default: case ObjectType.Table: case ObjectType.View: case ObjectType.TableFunction: if (Obj.Id.StartsWith("na", StringComparison.CurrentCultureIgnoreCase)) {//this object must be managed without an alias, so we use <schema>.<object name> as an alias Obj.Alias = String.Format("{0}.{1}", Obj.Object.Schema, Obj.Object.Name); } else { Obj.Alias = Obj.Object.Name.GetUpperCasedLetters(2).GetAsSentence();//camel cased objects FTW!!! if (String.IsNullOrEmpty(Obj.Alias)) Obj.Alias = Obj.Object.Name.Substring(0, 2);//if not camel cased object, get the first 2 letters in the name if (InnerObjects.Any(X => X.Alias.Equals(Obj.Alias)))//making sure this alias is unique Obj.Alias += InnerObjects.Count + 1; } break; case ObjectType.Procedure: case ObjectType.ScalarFunction: Obj.Alias = ""; break; } if (String.IsNullOrEmpty(Obj.Alias) || Obj.Alias.Contains(".")) { ProcessedSnippet = ProcessedSnippet.Replace(Obj.Name, String.Format("{0}.{1}", Obj.Object.Schema, Obj.Object.Name)); } else { ProcessedSnippet = ProcessedSnippet.Replace(Obj.Name, String.Format("{0}.{1} AS {2}", Obj.Object.Schema, Obj.Object.Name, Obj.Alias)); } InnerObjects.Add(Obj); //check for all fields ProcessedSnippet = ProcessSnippetChilds(Obj, ProcessedSnippet, "Fields", true); //check for field selector ProcessedSnippet = ProcessSnippetChilds(Obj, ProcessedSnippet, "Fields", false); //check for all Params ProcessedSnippet = ProcessSnippetChilds(Obj, ProcessedSnippet, "Parameters", true); //check for param selector ProcessedSnippet = ProcessSnippetChilds(Obj, ProcessedSnippet, "Parameters", false); } else {//cancelled the snippet processing return; } } catch (Exception) { MessageBox.Show("Error while processing the snippet, check sintax", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } } Query.InsertString(ProcessedSnippet); }