private void GetCompleteStatement( ITextSnapshot snapshot, SnapshotPoint snapPos, out StringBuilder sbErrors, out ITree treeStmt) { string sql = snapshot.GetText(); treeStmt = null; sbErrors = new StringBuilder(); _position = snapPos.Position; _tokens = RemoveToken(sql, snapPos); if (_tokens.Count == 1 && _tokens.Get(0).Type == MySQL51Lexer.EOF) { return; } MySQL51Parser.program_return r = LanguageServiceUtil.ParseSql(sql, false, out sbErrors, _tokens); if (r == null) { return; } ITree t = r.Tree as ITree; treeStmt = t; // locate current statement's AST if (t.IsNil) { ITree tmp = FindStmt(t); if (tmp != null) { treeStmt = tmp; } } }
/// <summary> /// Removes a token using the enhanced token stream class. /// </summary> /// <param name="sql"></param> /// <param name="snapPos"></param> /// <returns></returns> private CommonTokenStream RemoveToken(string sql, SnapshotPoint snapPos) { Version ver = LanguageServiceUtil.GetVersion(LanguageServiceUtil.GetConnection().ServerVersion); TokenStreamRemovable tokens = LanguageServiceUtil.GetTokenStream(sql, ver); IToken tr = null; int position = snapPos.Position; tokens.Fill(); if (!char.IsWhiteSpace(snapPos.GetChar())) { foreach (IToken t in tokens.GetTokens()) { if ((t.StartIndex <= position) && (t.StopIndex >= position)) { tr = t; break; } } tokens.Remove(tr); } return(tokens); }
void ReParse() { ITextSnapshot newSnapshot = buffer.CurrentSnapshot; List <Region> newRegions = new List <Region>(); string sql = newSnapshot.GetText(); StringBuilder sb = new StringBuilder(); MySQL51Parser.statement_list_return r = LanguageServiceUtil.ParseSql(sql, false, out sb); if (sb.Length != 0) { return; } CommonTree ct = (CommonTree)r.Tree; foreach (ITree c in ct.Children) { newRegions.Add(new Region() { Level = 1, StartLine = c.Line, StartOffset = c.TokenStartIndex, EndLine = -1, EndOffset = c.TokenStopIndex, PartialParent = null }); } ITextSnapshot snap = this.snapshot; if (this.TagsChanged != null) { this.TagsChanged(this, new SnapshotSpanEventArgs( new SnapshotSpan(this.snapshot, Span.FromBounds(0, snap.Length)))); } }
void ICompletionSource.AugmentCompletionSession(ICompletionSession session, IList <CompletionSet> completionSets) { DbConnection connection = LanguageServiceUtil.GetConnection(); if (connection != null) { string database = LanguageServiceUtil.GetCurrentDatabase(); if (string.IsNullOrEmpty(database)) { database = connection.Database; } if (string.IsNullOrEmpty(database)) { database = "mysql"; } if (session.TextView.Caret.Position.BufferPosition.Position == 0) { return; } SnapshotPoint currentPoint = (session.TextView.Caret.Position.BufferPosition) - 1; ITextStructureNavigator navigator = _mSourceProvider.NavigatorService.GetTextStructureNavigator(_mTextBuffer); TextExtent extent = navigator.GetExtentOfWord(currentPoint); ITrackingSpan span = currentPoint.Snapshot.CreateTrackingSpan(extent.Span, SpanTrackingMode.EdgeInclusive); StringBuilder sbErrors; ITextSnapshot snapshot = currentPoint.Snapshot; ITextSnapshotLine line = currentPoint.GetContainingLine(); int position = currentPoint.Position; // Get starting token ITree t; GetCompleteStatement(snapshot, currentPoint, out sbErrors, out t); if ((snapshot.Length == 0) || (t == null)) { return; } string s = sbErrors.ToString(); Match m = new Regex(@"Expected (?<item>.*)\.").Match(s); string expectedToken = ""; if (m.Success) { expectedToken = m.Groups["item"].Value; } if (expectedToken == "table_factor" || expectedToken == "simple_table_ref_no_alias_existing") { _mCompList = new List <Completion>(); DataTable schema = connection.GetSchema("Tables", new string[] { null, database }); schema.Merge(connection.GetSchema("Views", new string[] { null, database })); string completionItem = null, completionItemUnq = null; foreach (DataRow row in schema.Rows) { completionItemUnq = row["TABLE_NAME"].ToString(); completionItem = string.Format("`{0}`", row["TABLE_NAME"].ToString()); _mCompList.Add(new Completion(completionItemUnq, completionItem, completionItem, null, null)); } completionSets.Add(new CompletionSet( "MySqlTokens", //the non-localized title of the tab "MySQL Tokens", //the display title of the tab FindTokenSpanAtPosition(session.GetTriggerPoint(_mTextBuffer), session), _mCompList, null)); } if (expectedToken == "proc_name") { _mCompList = new List <Completion>(); DataTable schema = connection.GetSchema("PROCEDURES WITH PARAMETERS", new string[] { null, database }); DataView vi = schema.DefaultView; vi.Sort = "specific_name asc"; string completionItem = null; string description = null; foreach (DataRowView row in vi) { if ("procedure".CompareTo(row["routine_type"].ToString().ToLower()) == 0) { completionItem = row["specific_name"].ToString(); description = string.Format("procedure {0}.{1}({2})", row["routine_schema"], row["specific_name"], row["ParameterList"]); _mCompList.Add(new Completion(completionItem, completionItem, description, null, null)); } } completionSets.Add(new CompletionSet( "MySqlTokens", //the non-localized title of the tab "MySQL Tokens", //the display title of the tab FindTokenSpanAtPosition(session.GetTriggerPoint(_mTextBuffer), session), _mCompList, null)); } else if (expectedToken == "column_name") { if (t != null) { if ((t.ChildCount != 0) || ((t is CommonErrorNode) && ((t as CommonErrorNode).Text.Equals("SELECT", StringComparison.CurrentCultureIgnoreCase)))) { List <TableWithAlias> tables = new List <TableWithAlias>(); ParserUtils.GetTables(t, tables); List <string> cols = GetColumns(connection, tables, database); CreateCompletionList(cols, session, completionSets); } } } } }
public void AugmentQuickInfoSession(IQuickInfoSession session, IList <object> qiContent, out ITrackingSpan applicableToSpan) { applicableToSpan = null; if (m_dictionary == null) { DbConnection connection = LanguageServiceUtil.GetConnection(); if (connection == null || string.IsNullOrEmpty(connection.Database)) { return; } LoadDictionary(connection); } // Map the trigger point down to our buffer. SnapshotPoint?subjectTriggerPoint = session.GetTriggerPoint(m_subjectBuffer.CurrentSnapshot); if (!subjectTriggerPoint.HasValue) { applicableToSpan = null; return; } ITextSnapshot currentSnapshot = subjectTriggerPoint.Value.Snapshot; SnapshotSpan querySpan = new SnapshotSpan(subjectTriggerPoint.Value, 0); //look for occurrences of our QuickInfo words in the span ITextStructureNavigator navigator = m_provider.NavigatorService.GetTextStructureNavigator(m_subjectBuffer); TextExtent extent = navigator.GetExtentOfWord(subjectTriggerPoint.Value); string searchText = extent.Span.GetText(); foreach (string key in m_dictionary.Keys) { if (key == searchText) { int foundIndex = 0; int span = querySpan.Start.Add(foundIndex).Position; applicableToSpan = currentSnapshot.CreateTrackingSpan ( span, Math.Min( span + currentSnapshot.Length - subjectTriggerPoint.Value.Position, currentSnapshot.Length - span), SpanTrackingMode.EdgeInclusive ); string value; m_dictionary.TryGetValue(key, out value); if (value != null) { qiContent.Add(value); } else { qiContent.Add(""); } return; } } applicableToSpan = null; }