Exemplo n.º 1
0
        public override void ListScriptIntellisenseItems(string fileName, string text, string[] lines, Point caretPosition, ScriptIntellisense intellisense)
        {
            var session = _Session;

            if (session == null)
            {
                return;
            }

            var ident = GetIdent() ?? string.Empty;

            var completions = session.GetCompletions(ident);

            if (completions == null)
            {
                return;
            }

            var dictVariables = new Dictionary <string, int>();

            try
            {
                var values = session.GetBoundValues();
                foreach (var value in values)
                {
                    if (!string.IsNullOrWhiteSpace(value.Name))
                    {
                        if (dictVariables.ContainsKey(value.Name))
                        {
                            dictVariables[value.Name]++;
                        }
                        else
                        {
                            dictVariables.Add(value.Name, 1);
                        }
                    }
                }
            }
            catch (Exception)
            {
                //Do nothing
            }

            foreach (var completion in completions.Distinct().OrderBy(str => str, StringLogicalComparer.DefaultComparer))
            {
                if (string.IsNullOrWhiteSpace(completion))
                {
                    continue;
                }

                //printf does not work correctly
#pragma warning disable CRRSP06 // A misspelled word has been found
                if (completion.Contains("printf", StringComparison.CurrentCultureIgnoreCase))
#pragma warning restore CRRSP06 // A misspelled word has been found
                {
                    continue;
                }

                bool isVariable = dictVariables.ContainsKey(completion);

                var item = new ScriptIntellisenseItem()
                {
                    ItemType = isVariable ? ScriptIntellisenseItem.IntellisenseItemType.Variable : ScriptIntellisenseItem.IntellisenseItemType.Property,
                    Caption  = completion,
                    Value    = completion
                };

                intellisense.Items.Add(item);
            }


            string GetIdent()
            {
                var line = lines[caretPosition.Y];

                if (line == null)
                {
                    return(null);
                }
                line = line[..Math.Min(line.Length, caretPosition.X)];
        public virtual void ListScriptIntellisenseItems(string text, string[] lines, Point caretPosition, ScriptIntellisense intellisense)
        {
            intellisense.UsePeriodInIntellisense = true;

            var              script             = new SqlScript(text);
            string           lastConnectionName = null;
            SqlScriptCommand command            = null;

            for (int i = 0; i < script.Commands.Count; i++)
            {
                if (script.Commands[i].StartLineNumber - 1 > caretPosition.Y)
                {
                    break;
                }

                command            = script.Commands[i];
                lastConnectionName = command.GetConnectionName();
            }

            if (string.IsNullOrWhiteSpace(lastConnectionName) && _SelectedDbConnection == null)
            {
                return;
            }

            var itemCaptions = new Dictionary <(ScriptIntellisenseItem.IntellisenseItemType, string), int>();

            Connection conn = null;

            try
            {
                if (string.IsNullOrWhiteSpace(lastConnectionName))
                {
                    conn = _SelectedDbConnection.Factory.CreateConnection(_SelectedDbConnection.ConnectionString);
                }
                else
                {
                    var connections = DBConnections.LoadConnections();
                    var dbConn      = connections.FindConnection(lastConnectionName);
                    if (dbConn != null)
                    {
                        conn = dbConn.GetConnectionFactory()?.CreateConnection(dbConn.ConnectionString);
                    }
                }

                if (conn == null)
                {
                    return;
                }

                try
                {
                    conn.Open();
                }
                catch (Exception)
                {
                    return;
                }

                var    parts   = new List <string>();
                var    aliases = new StringNoCaseDictionary <TableAlias>();
                string dbName  = conn.DbConnection.Database;

                if (!string.IsNullOrWhiteSpace(command?.Text))
                {
                    var reAlias     = new Regex(@"(?i)\b(?:from|join)\s+(?:(?<Database>(\w+|\[.*?]\]|`.*?`|"".*?"")\.)?(?:(?<Schema>(?:\w+|\[.*?]\]|`.*?`|"".*?""))\.))?(?<Table>(?:\w+|\[.*?\]|`.*?`|"".*?""))[ \t]+(?<Alias>\w+)");
                    var collAliases = reAlias.Matches(command.Text);

                    if ((collAliases?.Count ?? 0) > 0)
                    {
                        foreach (Match matchAlias in collAliases)
                        {
                            if (!matchAlias.Success)
                            {
                                continue;
                            }

                            string alias         = matchAlias.Groups["Alias"].Value;
                            string aliasDatabase = Utils.NullString(Utils.UnquoteString(matchAlias.Groups["Database"].Value));
                            string aliasSchema   = Utils.NullString(Utils.UnquoteString(matchAlias.Groups["Schema"].Value));
                            string aliasTable    = Utils.NullString(Utils.UnquoteString(matchAlias.Groups["Table"].Value));

                            if (string.IsNullOrWhiteSpace(alias) || string.Compare(alias, "where", true) == 0 ||
                                string.Compare(alias, "order", true) == 0 || string.Compare(alias, "join", true) == 0 ||
                                string.Compare(alias, "left", true) == 0 || string.Compare(alias, "right", true) == 0 ||
                                string.Compare(alias, "inner", true) == 0 || string.Compare(alias, "outer", true) == 0 ||
                                string.Compare(alias, "full", true) == 0)
                            {
                                continue;
                            }

                            if (!string.IsNullOrWhiteSpace(aliasTable) && !aliases.ContainsKey(alias))
                            {
                                aliases.Add(alias, new TableAlias()
                                {
                                    Alias = alias, Database = aliasDatabase, Schema = aliasSchema, Table = aliasTable
                                });
                            }
                        }
                    }
                }

                string line = caretPosition.Y < (lines?.Length ?? 0) ? lines[caretPosition.Y] : null;
                if (!string.IsNullOrWhiteSpace(line))
                {
                    if (caretPosition.X < line.Length)
                    {
                        line = line.Substring(0, caretPosition.X);
                    }

                    var reParts    = new Regex(@"((?<Part>(\w+|\[.*?]\]|`.*?`|"".*?""))\.)*([""`[]?(?<LastPart>\w*))$");
                    var matchParts = reParts.Match(line);
                    if (matchParts.Success)
                    {
                        foreach (Capture capture in matchParts.Groups["Part"].Captures)
                        {
                            parts.Add(capture.Value);
                        }
                    }
                }

                if (parts.Count == 0)
                {
                    AddTablesAndProcedures(intellisense, itemCaptions, conn.DbConnection, null, null);
                }
                else if (parts.Count == 1 && !string.IsNullOrWhiteSpace(parts[0]))
                {
                    var part0 = Utils.UnquoteString(parts[0]);
                    if (!string.IsNullOrWhiteSpace(part0))
                    {
                        AddTablesAndProcedures(intellisense, itemCaptions, conn.DbConnection, dbName, part0);
                        AddTablesAndProcedures(intellisense, itemCaptions, conn.DbConnection, part0, null);

                        AddTableColumns(intellisense, itemCaptions, conn.DbConnection, dbName, null, part0);

                        if (aliases.ContainsKey(part0))
                        {
                            var tableAlias = aliases[part0];
                            AddTableColumns(intellisense, itemCaptions, conn.DbConnection, tableAlias.Database ?? dbName, tableAlias.Schema, tableAlias.Table);
                        }
                    }
                }
                else if (parts.Count == 2 && !string.IsNullOrWhiteSpace(parts[0]))
                {
                    AddTablesAndProcedures(intellisense, itemCaptions, conn.DbConnection, Utils.UnquoteString(parts[0]), Utils.UnquoteString(parts[1]));
                }
                else
                {
                    AddTablesAndProcedures(intellisense, itemCaptions, conn.DbConnection, null, null);
                }
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
Exemplo n.º 3
0
        public ScriptEditorPopupForm(SyntaxBoxControl editor, ScriptIntellisense intellisense)
        {
            InitializeComponent();

            _Editor       = editor;
            _Intellisense = intellisense;

            if (_Intellisense.Help != null && (_Intellisense.Help.SupportsHelp || _Intellisense.Help.SupportsOnlineHelp))
            {
                if (_Intellisense.Help.SupportsHelp && _Intellisense.Help.SupportsOnlineHelp)
                {
                    lblComments.Text = "Press<b>F1</b> for more help, <b>F2</b> for online help";
                }
                else if (_Intellisense.Help.SupportsHelp)
                {
                    lblComments.Text = "Press<b>F1</b> for more help";
                }
                else if (_Intellisense.Help.SupportsOnlineHelp)
                {
                    lblComments.Text = "Press <b>F2</b> for online help";
                }
            }
            else
            {
                lblComments.Visible = false;
            }


            _StartSearchPosition = new TextPoint()
            {
                X = _Editor.Caret.Position.X,
                Y = _Editor.Caret.Position.Y
            };

            _StartSearchPosition.X = FindStartSymbol(_Editor.Caret.CurrentRow.Text, Math.Max(_StartSearchPosition.X - 1, 0),
                                                     intellisense.UsePeriodInIntellisense, out _LastPartStart);

            _Intellisense.Items.Sort((x, y) =>
            {
                if (x.DisplayOrder != y.DisplayOrder)
                {
                    return(x.DisplayOrder.CompareTo(y.DisplayOrder));
                }
                if (x.IsMandatory.CompareTo(y.IsMandatory) != 0)
                {
                    return(-x.IsMandatory.CompareTo(y.IsMandatory));
                }
                if (x.Position.HasValue && !y.Position.HasValue)
                {
                    return(-1);
                }
                if (!x.Position.HasValue && y.Position.HasValue)
                {
                    return(1);
                }
                if (x.Position.HasValue && y.Position.HasValue && x.Position.Value.CompareTo(y.Position.Value) != 0)
                {
                    return(x.Position.Value.CompareTo(y.Position.Value));
                }
                return(StringLogicalComparer.Compare(x.Caption, y.Caption));
            });
            Grid.DataSource = _Intellisense.Items;

            lblDescription.DataBindings.Add(nameof(lblDescription.Text), _Intellisense.Items, nameof(ScriptIntellisenseItem.Description));

            _Editor.CaretChange += new EventHandler(Editor_CaretChange);
        }
 static void AddTablesAndProcedures(ScriptIntellisense intellisense, Dictionary <(ScriptIntellisenseItem.IntellisenseItemType, string), int> itemCaptions,