Esempio n. 1
0
        public static Project GetContainingProject(this IWpfTextView textView)
        {
            var fileName = textView.GetFilePath();

            if (string.IsNullOrWhiteSpace(fileName))
            {
                return(null);
            }
            var dte2     = (DTE2)Package.GetGlobalService(typeof(SDTE));
            var projItem = dte2?.Solution.FindProjectItem(fileName);

            return(projItem?.ContainingProject);
        }
        public void TextViewCreated(IWpfTextView textView)
        {
            if (_serverUIContext.IsActive)
            {
                return;
            }

            if (textView == null)
            {
                throw new ArgumentNullException(nameof(textView));
            }

            var filePath = textView.GetFilePath();

            _projectService.TrackOpenDocument(filePath);

            var target = new XamlOleCommandTarget(textView, (IComponentModel)_serviceProvider.GetService(typeof(SComponentModel)));

            target.AttachToVsTextView();
        }
        /// <summary>
        /// Called whenever a new view is created.  
        /// This method checks to see if it is a custom dictionary, and if it is, it gets attached.
        /// </summary>
        /// <param name="wpfTextView"></param>
        public void RegisterTextBuffer(IWpfTextView wpfTextView)
        {
            // If this isn't the dictionary, we don't care.
            var filePath = wpfTextView.GetFilePath();
            var fileName = Path.GetFileName(filePath);
            if (String.Compare(fileName, DictionaryFileName, StringComparison.InvariantCultureIgnoreCase) != 0)
                return;

            // Get the dictionary;
            var spellingDictionaryService = GetSpellingDictionaryService(fileName);

            // Do we have a project dictionary?
            var projectSpellingDictionaryService = spellingDictionaryService as IProjectSpecificDictionary;

            // Assign the buffer
            if (projectSpellingDictionaryService != null)
                projectSpellingDictionaryService.ConnectBufferListener(wpfTextView.TextBuffer);
        }
        public override void PostprocessMouseRightButtonDown(MouseButtonEventArgs e)
        {
            if (_view == null)
            {
                return;
            }

            if (GoToDefinitionCommand.Instance == null ||
                FindAllReferencesCommand.Instance == null ||
                RenameCommand.Instance == null ||
                Reformat.ReformatCommand.Instance == null)
            {
                return;
            }

            // Whack any old values that cursor points to.
            GoToDefinitionCommand.Instance.Enabled        = false;
            GoToDefinitionCommand.Instance.Visible        = false;
            GoToDefinitionCommand.Instance.Symbol         = default(SnapshotSpan);
            GoToDefinitionCommand.Instance.Classification = default(string);
            GoToDefinitionCommand.Instance.View           = default(ITextView);

            FindAllReferencesCommand.Instance.Enabled        = false;
            FindAllReferencesCommand.Instance.Visible        = false;
            FindAllReferencesCommand.Instance.Symbol         = default(SnapshotSpan);
            FindAllReferencesCommand.Instance.Classification = default(string);
            FindAllReferencesCommand.Instance.View           = default(ITextView);

            RenameCommand.Instance.Enabled        = false;
            RenameCommand.Instance.Visible        = false;
            RenameCommand.Instance.Symbol         = default(SnapshotSpan);
            RenameCommand.Instance.Classification = default(string);
            RenameCommand.Instance.View           = default(ITextView);

            Reformat.ReformatCommand.Instance.Enabled = false;
            Reformat.ReformatCommand.Instance.Visible = false;
            RenameCommand.Instance.View = default(ITextView);

            var fp = _view.GetFilePath();

            if (fp != null)
            {
                string suffix = System.IO.Path.GetExtension(fp);
                if (suffix.Equals(".g4") || suffix.Equals(".G4"))
                {
                    GoToDefinitionCommand.Instance.Visible    = true;
                    FindAllReferencesCommand.Instance.Visible = true;
                    RenameCommand.Instance.Visible            = true;
                    Reformat.ReformatCommand.Instance.Enabled = true;
                    Reformat.ReformatCommand.Instance.Visible = true;
                }
            }

            // No matter what, say we handled event, otherwise we get all sorts of pop-up errors.
            e.Handled = true;

            // Find details of the Antlr symbol pointed to.
            _mouseDownAnchorPoint = RelativeToView(e.GetPosition(_view.VisualElement));
            System.Windows.Point point = _mouseDownAnchorPoint ?? default(System.Windows.Point);
            if (point == default(System.Windows.Point))
            {
                return;
            }
            SnapshotPoint?nullable_where = ConvertPointToBufferIndex(point);

            SnapshotPoint where = nullable_where ?? default(SnapshotPoint);
            if (where == default(SnapshotPoint))
            {
                return;
            }
            TextExtent   extent = _navigator.GetExtentOfWord(where);
            SnapshotSpan span   = extent.Span;

            //  Now, check for valid classification type.
            ClassificationSpan[] c1 = _aggregator.GetClassificationSpans(span).ToArray();
            foreach (ClassificationSpan classification in c1)
            {
                var name = classification.ClassificationType.Classification.ToLower();
                if (name == AntlrVSIX.Constants.ClassificationNameTerminal)
                {
                    GoToDefinitionCommand.Instance.Enabled        = true;
                    GoToDefinitionCommand.Instance.Visible        = true;
                    GoToDefinitionCommand.Instance.Symbol         = span;
                    GoToDefinitionCommand.Instance.Classification = AntlrVSIX.Constants.ClassificationNameTerminal;
                    GoToDefinitionCommand.Instance.View           = _view;

                    FindAllReferencesCommand.Instance.Enabled        = true;
                    FindAllReferencesCommand.Instance.Visible        = true;
                    FindAllReferencesCommand.Instance.Symbol         = span;
                    FindAllReferencesCommand.Instance.Classification = AntlrVSIX.Constants.ClassificationNameTerminal;
                    FindAllReferencesCommand.Instance.View           = _view;

                    RenameCommand.Instance.Enabled        = true;
                    RenameCommand.Instance.Visible        = true;
                    RenameCommand.Instance.Symbol         = span;
                    RenameCommand.Instance.Classification = AntlrVSIX.Constants.ClassificationNameTerminal;
                    RenameCommand.Instance.View           = _view;

                    Reformat.ReformatCommand.Instance.Enabled        = true;
                    Reformat.ReformatCommand.Instance.Visible        = true;
                    Reformat.ReformatCommand.Instance.Symbol         = span;
                    Reformat.ReformatCommand.Instance.Classification = AntlrVSIX.Constants.ClassificationNameTerminal;
                    Reformat.ReformatCommand.Instance.View           = _view;
                }
                else if (name == AntlrVSIX.Constants.ClassificationNameNonterminal)
                {
                    GoToDefinitionCommand.Instance.Enabled        = true;
                    GoToDefinitionCommand.Instance.Visible        = true;
                    GoToDefinitionCommand.Instance.Symbol         = span;
                    GoToDefinitionCommand.Instance.Classification = AntlrVSIX.Constants.ClassificationNameNonterminal;
                    GoToDefinitionCommand.Instance.View           = _view;

                    FindAllReferencesCommand.Instance.Enabled        = true;
                    FindAllReferencesCommand.Instance.Visible        = true;
                    FindAllReferencesCommand.Instance.Symbol         = span;
                    FindAllReferencesCommand.Instance.Classification = AntlrVSIX.Constants.ClassificationNameNonterminal;
                    FindAllReferencesCommand.Instance.View           = _view;

                    RenameCommand.Instance.Enabled        = true;
                    RenameCommand.Instance.Visible        = true;
                    RenameCommand.Instance.Symbol         = span;
                    RenameCommand.Instance.Classification = AntlrVSIX.Constants.ClassificationNameNonterminal;
                    RenameCommand.Instance.View           = _view;

                    Reformat.ReformatCommand.Instance.Enabled        = true;
                    Reformat.ReformatCommand.Instance.Visible        = true;
                    Reformat.ReformatCommand.Instance.Symbol         = span;
                    Reformat.ReformatCommand.Instance.Classification = AntlrVSIX.Constants.ClassificationNameNonterminal;
                    Reformat.ReformatCommand.Instance.View           = _view;
                }
                else if (name == AntlrVSIX.Constants.ClassificationNameLiteral)
                {
                    GoToDefinitionCommand.Instance.Enabled        = true;
                    GoToDefinitionCommand.Instance.Visible        = true;
                    GoToDefinitionCommand.Instance.Symbol         = span;
                    GoToDefinitionCommand.Instance.Classification = AntlrVSIX.Constants.ClassificationNameLiteral;
                    GoToDefinitionCommand.Instance.View           = _view;

                    FindAllReferencesCommand.Instance.Enabled        = true;
                    FindAllReferencesCommand.Instance.Visible        = true;
                    FindAllReferencesCommand.Instance.Symbol         = span;
                    FindAllReferencesCommand.Instance.Classification = AntlrVSIX.Constants.ClassificationNameLiteral;
                    FindAllReferencesCommand.Instance.View           = _view;

                    RenameCommand.Instance.Enabled        = true;
                    RenameCommand.Instance.Visible        = true;
                    RenameCommand.Instance.Symbol         = span;
                    RenameCommand.Instance.Classification = AntlrVSIX.Constants.ClassificationNameLiteral;
                    RenameCommand.Instance.View           = _view;

                    Reformat.ReformatCommand.Instance.Enabled        = true;
                    Reformat.ReformatCommand.Instance.Visible        = true;
                    Reformat.ReformatCommand.Instance.Symbol         = span;
                    Reformat.ReformatCommand.Instance.Classification = AntlrVSIX.Constants.ClassificationNameLiteral;
                    Reformat.ReformatCommand.Instance.View           = _view;
                }
            }
        }
Esempio n. 5
0
        public static void ResetMenus()
        {
            if (GoToDefinitionCommand.Instance == null ||
                FindAllReferencesCommand.Instance == null ||
                RenameCommand.Instance == null ||
                Reformat.ReformatCommand.Instance == null)
            {
                return;
            }

            IWpfTextView view = AntlrLanguagePackage.Instance.GetActiveView();

            if (view == null)
            {
                return;
            }

            // First, find out what this view is, and what the file is.
            ITextBuffer         buffer = view.TextBuffer;
            string              path   = buffer.GetFFN().Result;
            IGrammarDescription grammar_description = LanguageServer.GrammarDescriptionFactory.Create(path);

            // Whack any old values that cursor points to.
            GoToDefinitionCommand.Instance.Enabled    = false;
            FindAllReferencesCommand.Instance.Enabled = false;
            NextSymCommand.Instance.Enabled           = true;
            RenameCommand.Instance.Enabled            = false;
            ReformatCommand.Instance.Enabled          = false;
            GoToVisitorCommand.Instance.Enabled       = false;

            AntlrLanguagePackage.Instance.Span           = default(SnapshotSpan);
            AntlrLanguagePackage.Instance.Classification = default(string);
            AntlrLanguagePackage.Instance.View           = view;

            if (grammar_description == null)
            {
                return;
            }

            var fp = view.GetFilePath().Result;

            if (fp != null)
            {
                var gd = LanguageServer.GrammarDescriptionFactory.Create(fp);
                if (gd != null && gd.CanNextRule)
                {
                    NextSymCommand.Instance.Enabled = true;
                }
                if (gd != null && gd.CanReformat)
                {
                    Reformat.ReformatCommand.Instance.Enabled = true;
                }
            }
            else
            {
                return;
            }

            // Find details of the Antlr symbol pointed to.
            ITextCaret    car    = view.Caret;
            CaretPosition cp     = car.Position;
            SnapshotPoint bp     = cp.BufferPosition;
            TextExtent    extent = AntlrVSIX.Package.AntlrLanguagePackage.Instance.Navigator[view].GetExtentOfWord(bp);
            SnapshotSpan  span   = extent.Span;

            AntlrLanguagePackage.Instance.Span = span;

            HighlightTagger.Update(view, bp);

            //  Now, check for valid classification type.
            ClassificationSpan[] c1 = AntlrVSIX.Package.AntlrLanguagePackage.Instance.Aggregator[view].GetClassificationSpans(span).ToArray();
            foreach (ClassificationSpan classification in c1)
            {
                var name = classification.ClassificationType.Classification;
                if (!grammar_description.InverseMap.TryGetValue(name, out int type))
                {
                    continue;
                }
                if (grammar_description.CanFindAllRefs[type])
                {
                    FindAllReferencesCommand.Instance.Enabled = true;
                }
                if (grammar_description.CanRename[type])
                {
                    RenameCommand.Instance.Enabled = true;
                }
                if (grammar_description.CanGotodef[type])
                {
                    GoToDefinitionCommand.Instance.Enabled = true;
                }
                if (grammar_description.CanGotovisitor[type])
                {
                    GoToVisitorCommand.Instance.Enabled = true;
                }
                if (grammar_description.CanReformat)
                {
                    ReformatCommand.Instance.Enabled = true;
                }
            }
        }