public IEnumerable <ITagSpan <ClassificationTag> > GetTags(NormalizedSnapshotSpanCollection spans) { AntlrLanguageClient alc = AntlrLanguageClient.Instance; if (alc == null) { yield break; } string ffn = _buffer.GetFFN().Result; if (ffn == null) { yield break; } Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn); IVsTextView vstv = IVsTextViewExtensions.FindTextViewFor(ffn); foreach (SnapshotSpan curSpan in spans) { SnapshotPoint start = curSpan.Start; SnapshotPoint end = curSpan.End; string text = curSpan.GetText(); int curLocStart = start.Position; int curLocEnd = end.Position; SymbolInformation[] sorted_combined_tokens = alc.CMGetClassifiersSendServer(curLocStart, curLocEnd - 1, ffn); if (sorted_combined_tokens == null) { continue; } foreach (SymbolInformation r in sorted_combined_tokens) { int l = r.Location.Range.Start.Line; int c = r.Location.Range.Start.Character; int i = LanguageServer.Module.GetIndex(l, c, document); int l2 = r.Location.Range.End.Line; int c2 = r.Location.Range.End.Character; int i2 = LanguageServer.Module.GetIndex(l2, c2, document); int start_token_start = i; int end_token_end = i2; int type = (int)r.Kind; int length = end_token_end - start_token_start + 1; if (length < 0) { continue; } TagSpan <ClassificationTag> result = null; try { if (type >= 0) { // Make sure the length doesn't go past the end of the current span. if (start_token_start + length > curLocEnd) { var new_length = curLocEnd - start_token_start; if (new_length >= 0) { length = new_length; } } ITextSnapshot a = curSpan.Snapshot.TextBuffer.CurrentSnapshot; ITextSnapshot b = curSpan.Snapshot; SnapshotSpan tokenSpan = new SnapshotSpan( curSpan.Snapshot.TextBuffer.CurrentSnapshot, //curSpan.Snapshot, new Span(start_token_start, length)); result = new TagSpan <ClassificationTag>(tokenSpan, new ClassificationTag(_lsptype_to_classifiertype[type])); } } catch (Exception) { //Logger.Log.Notify(exception.StackTrace); } if (result != null) { yield return(result); } } } }
private void MenuItemCallback(object sender, EventArgs e, bool forward) { try { //////////////////////// /// Next rule. //////////////////////// IVsTextManager manager = ((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) as IVsTextManager; if (manager == null) { return; } manager.GetActiveView(1, null, out IVsTextView view); if (view == null) { return; } view.GetCaretPos(out int l, out int c); view.GetBuffer(out IVsTextLines buf); if (buf == null) { return; } IWpfTextView xxx = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view); ITextBuffer buffer = xxx.TextBuffer; string ffn = buffer.GetFFN().Result; if (ffn == null) { return; } Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(ffn); if (document == null) { return; } int pos = LanguageServer.Module.GetIndex(l, c, document); AntlrLanguageClient alc = AntlrLanguageClient.Instance; if (alc == null) { return; } int new_pos = alc.CMNextSymbolSendServer(ffn, pos, forward); if (new_pos < 0) { return; } List <IToken> where = new List <IToken>(); List <ParserDetails> where_details = new List <ParserDetails>(); IVsTextView vstv = IVsTextViewExtensions.FindTextViewFor(ffn); if (vstv == null) { return; } IWpfTextView wpftv = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(vstv); if (wpftv == null) { return; } vstv.GetLineAndColumn(new_pos, out int line_number, out int colum_number); ITextSnapshot cc = wpftv.TextBuffer.CurrentSnapshot; SnapshotSpan ss = new SnapshotSpan(cc, new_pos, 1); SnapshotPoint sp = ss.Start; // Put cursor on symbol. wpftv.Caret.MoveTo(sp); if (line_number > 0) { vstv.CenterLines(line_number - 1, 2); } else { vstv.CenterLines(line_number, 1); } } catch (Exception exception) { Logger.Log.Notify(exception.StackTrace); } }
public IEnumerable <ITagSpan <ClassificationTag> > GetTags(NormalizedSnapshotSpanCollection spans) { ThreadHelper.ThrowIfNotOnUIThread(); if (!Options.Option.GetBoolean("SemanticHighlighting")) { yield break; } // Checks to partially fix https://github.com/kaby76/AntlrVSIX/issues/31#. string ffn = _buffer.GetFFN(); if (ffn == null) { yield break; } var doc = Workspaces.Workspace.Instance.FindDocument(ffn); LanguageServer.IParserDescription _grammar_description = LanguageServer.ParserDescriptionFactory.Create(doc); if (_grammar_description == null) { yield break; } _ = IVsTextViewExtensions.FindTextViewFor(ffn); foreach (SnapshotSpan curSpan in spans) { SnapshotPoint start = curSpan.Start; SnapshotPoint end = curSpan.End; string text = curSpan.GetText(); int curLocStart = start.Position; int curLocEnd = end.Position; LanguageServer.CMClassifierInformation[] sorted_combined_tokens = AntlrLanguageClient.CMGetClassifiers(curLocStart, curLocEnd - 1, ffn); if (sorted_combined_tokens == null) { continue; } foreach (LanguageServer.CMClassifierInformation r in sorted_combined_tokens) { int type = (int)r.Kind; if (type < 0) { continue; } int i = r.start; int i2 = r.end; int length = i2 - i; if (length < 0) { continue; } TagSpan <ClassificationTag> result = null; try { ITextSnapshot a = curSpan.Snapshot.TextBuffer.CurrentSnapshot; ITextSnapshot b = curSpan.Snapshot; SnapshotSpan tokenSpan = new SnapshotSpan( curSpan.Snapshot.TextBuffer.CurrentSnapshot, //curSpan.Snapshot, new Span(i, length)); result = new TagSpan <ClassificationTag>(tokenSpan, new ClassificationTag(_to_classifiertype[type])); } catch (Exception) { //Logger.Log.Notify(exception.StackTrace); } if (result != null) { yield return(result); } } } }
private void MenuItemCallback(object sender, EventArgs e, bool visitor) { try { //////////////////////// /// Go to visitor. //////////////////////// IVsTextManager manager = ((IServiceProvider)ServiceProvider).GetService(typeof(VsTextManagerClass)) as IVsTextManager; if (manager == null) { return; } manager.GetActiveView(1, null, out IVsTextView view); if (view == null) { return; } view.GetCaretPos(out int l, out int c); view.GetBuffer(out IVsTextLines buf); if (buf == null) { return; } IWpfTextView xxx = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(view); ITextBuffer buffer = xxx.TextBuffer; string orig_ffn = buffer.GetFFN().Result; if (orig_ffn == null) { return; } Workspaces.Document document = Workspaces.Workspace.Instance.FindDocument(orig_ffn); if (document == null) { return; } int pos = LanguageServer.Module.GetIndex(l, c, document); AntlrLanguageClient alc = AntlrLanguageClient.Instance; if (alc == null) { return; } CMGotoResult symbol = null; if (visitor) { symbol = alc.CMGotoVisitorSendServer(orig_ffn, pos); } else { var is_enter = CtrlKeyState.GetStateForView(xxx).Enabled; symbol = alc.CMGotoListenerSendServer(orig_ffn, is_enter, pos); } if (symbol == null) { return; } { string class_file_path = symbol.TextDocument.LocalPath; int index = symbol.Start; // Open to this line in editor. IVsTextView vstv = IVsTextViewExtensions.FindTextViewFor(class_file_path); { IVsTextViewExtensions.ShowFrame(ServiceProvider, class_file_path); vstv = IVsTextViewExtensions.FindTextViewFor(class_file_path); } IWpfTextView wpftv = AntlrLanguageClient.AdaptersFactory.GetWpfTextView(vstv); if (wpftv == null) { return; } // Create new span in the appropriate view. ITextSnapshot cc = wpftv.TextBuffer.CurrentSnapshot; vstv.GetLineAndColumn(index, out int line_number, out int colum_number); // Put cursor on symbol. wpftv.Caret.MoveTo(new SnapshotPoint(cc, index)); // This sets cursor, bot does not center. // Center on cursor. //wpftv.Caret.EnsureVisible(); // This works, sort of. It moves the scroll bar, but it does not CENTER! Does not really work! if (line_number > 0) { vstv.CenterLines(line_number - 1, 2); } else { vstv.CenterLines(line_number, 1); } return; } } catch (Exception exception) { Logger.Log.Notify(exception.StackTrace); } }