示例#1
0
        private bool HandleElement()
        {
            HtmlEditorDocument document = HtmlEditorDocument.TryFromTextView(_view);

            if (document == null)
            {
                return(false);
            }

            var tree = document.HtmlEditorTree;

            int position = _view.Caret.Position.BufferPosition.Position;

            ElementNode   tag  = null;
            AttributeNode attr = null;

            tree.GetPositionElement(position, out tag, out attr);

            if (tag != null && (tag.EndTag != null || tag.IsSelfClosing()))
            {
                int start = tag.Start;
                int end   = tag.End;

                Update(start, end);
                return(true);
            }

            return(false);
        }
示例#2
0
        public HtmlGoToDefinition(IVsTextView adapter, IWpfTextView textView)
            : base(adapter, textView, VSConstants.VSStd97CmdID.GotoDefn)
        {
            HtmlEditorDocument document = HtmlEditorDocument.TryFromTextView(textView);

            _tree = document == null ? null : document.HtmlEditorTree;
        }
示例#3
0
        public HtmlFindAllReferences(IVsTextView adapter, IWpfTextView textView)
            : base(adapter, textView, VSConstants.VSStd97CmdID.FindReferences)
        {
            HtmlEditorDocument document = HtmlEditorDocument.TryFromTextView(textView);

            _tree = document == null ? null : document.HtmlEditorTree;
        }
示例#4
0
        public EnterFormat(IVsTextView adapter, IWpfTextView textView, IEditorFormatterProvider formatterProvider, ICompletionBroker broker)
            : base(adapter, textView, VSConstants.VSStd2KCmdID.RETURN)
        {
            HtmlEditorDocument document = HtmlEditorDocument.TryFromTextView(textView);

            _tree      = document == null ? null : document.HtmlEditorTree;
            _formatter = formatterProvider.CreateRangeFormatter();
            _broker    = broker;
        }
示例#5
0
        public DirectiveGoToDefinition(IVsTextView adapter, IWpfTextView textView, DTE dte, INgHierarchyProvider ngHierarchyProvider)
            : base(adapter, textView, VSConstants.VSStd97CmdID.GotoDefn)
        {
            HtmlEditorDocument document = HtmlEditorDocument.TryFromTextView(textView);

            this.tree = document == null ? null : document.HtmlEditorTree;
            this.dte  = dte;
            this.ngHierarchyProvider = ngHierarchyProvider;
        }
示例#6
0
        protected override bool Execute(FormattingCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            HtmlEditorDocument document = HtmlEditorDocument.TryFromTextView(_view);

            if (document == null)
            {
                return(false);
            }

            var tree = document.HtmlEditorTree;

            int start = _view.Selection.Start.Position.Position;
            int end   = _view.Selection.End.Position.Position;

            ElementNode   tag  = null;
            AttributeNode attr = null;

            tree.GetPositionElement(start, out tag, out attr);

            if (tag == null)
            {
                return(false);
            }

            if (attr != null)
            {
                SelectAttribute(start, end, attr, tag);
            }
            else if (tag.EndTag != null && tag.StartTag.End == start && tag.EndTag.Start == end)
            {
                Select(tag.Start, tag.OuterRange.Length);
            }
            else if (tag.Children.Count > 1 && tag.Children[0].Start == start && tag.Children.Last().End == end)
            {
                Select(tag.InnerRange.Start, tag.InnerRange.Length);
            }
            else if (tag.EndTag != null && tag.Children.Count > 1 && tag.StartTag.Start < start && tag.EndTag.End > end)
            {
                Select(tag.Children[0].Start, tag.Children.Last().End - tag.Children[0].Start);
            }
            else if (tag.EndTag != null && tag.StartTag.Start < start && tag.EndTag.End > end)
            {
                Select(tag.InnerRange.Start, tag.InnerRange.Length);
            }
            else if (tag.IsSelfClosing() && tag.Start < start && tag.End > end)
            {
                Select(tag.Start, tag.OuterRange.Length);
            }
            else if (tag.Parent != null)
            {
                Select(tag.Parent.Start, tag.Parent.OuterRange.Length);
            }

            return(true);
        }
示例#7
0
        public void UpdateSource(string innerHtml, string file, int position)
        {
            WebEssentialsPackage.DTE.ItemOperations.OpenFile(file);

            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
            {
                var view = ProjectHelpers.GetCurentTextView();
                var html = HtmlEditorDocument.TryFromTextView(view);

                if (html == null)
                {
                    return;
                }

                ElementNode element;
                AttributeNode attribute;

                view.Selection.Clear();
                html.HtmlEditorTree.GetPositionElement(position + 1, out element, out attribute);

                // HTML element
                if (element != null && element.Start == position)
                {
                    Span span   = new Span(element.InnerRange.Start, element.InnerRange.Length);
                    string text = html.TextBuffer.CurrentSnapshot.GetText(span);

                    if (text != innerHtml)
                    {
                        UpdateBuffer(innerHtml, html, span);
                    }
                }
                // ActionLink
                else if (element.Start != position)
                {
                    //@Html.ActionLink("Application name", "Index", "Home", null, new { @class = "brand" })
                    Span span = new Span(position, 100);

                    if (position + 100 < html.TextBuffer.CurrentSnapshot.Length)
                    {
                        string text = html.TextBuffer.CurrentSnapshot.GetText(span);
                        var result  = Regex.Replace(text, @"^html.actionlink\(""([^""]+)""", "Html.ActionLink(\"" + innerHtml + "\"", RegexOptions.IgnoreCase);

                        UpdateBuffer(result, html, span);
                    }
                }
            }), DispatcherPriority.ApplicationIdle, null);
        }
示例#8
0
        public void UpdateSource(string innerHtml, string file, int position)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            VsHelpers.DTE.ItemOperations.OpenFile(file);

            ThreadHelper.JoinableTaskFactory.Run(async() =>
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                Microsoft.VisualStudio.Text.Editor.IWpfTextView view = VsHelpers.GetCurentTextView();
                HtmlEditorDocument html = HtmlEditorDocument.TryFromTextView(view);

                if (html == null)
                {
                    return;
                }

                view.Selection.Clear();
                html.HtmlEditorTree.GetPositionElement(position + 1, out ElementNode element, out AttributeNode attribute);

                // HTML element
                if (element != null && element.Start == position)
                {
                    Span span   = new Span(element.InnerRange.Start, element.InnerRange.Length);
                    string text = html.TextBuffer.CurrentSnapshot.GetText(span);

                    if (text != innerHtml)
                    {
                        UpdateBuffer(innerHtml, html, span);
                    }
                }
                // ActionLink
                else if (element.Start != position)
                {
                    //@Html.ActionLink("Application name", "Index", "Home", null, new { @class = "brand" })
                    Span span = new Span(position, 100);

                    if (position + 100 < html.TextBuffer.CurrentSnapshot.Length)
                    {
                        string text   = html.TextBuffer.CurrentSnapshot.GetText(span);
                        string result = Regex.Replace(text, @"^html.actionlink\(""([^""]+)""", "Html.ActionLink(\"" + innerHtml + "\"", RegexOptions.IgnoreCase);

                        UpdateBuffer(result, html, span);
                    }
                }
            });
        }
示例#9
0
        public void AugmentQuickInfoSession(IQuickInfoSession session, IList <object> qiContent, out ITrackingSpan applicableToSpan)
        {
            applicableToSpan = null;

            SnapshotPoint?point = session.GetTriggerPoint(session.TextView.TextBuffer.CurrentSnapshot);

            if (!point.HasValue)
            {
                return;
            }

            HtmlEditorTree tree = HtmlEditorDocument.TryFromTextView(session.TextView).HtmlEditorTree;

            if (tree == null)
            {
                return;
            }

            ElementNode   node = null;
            AttributeNode attr = null;

            tree.GetPositionElement(point.Value.Position, out node, out attr);

            if (node == null || (!node.Name.Equals("img", StringComparison.OrdinalIgnoreCase) && !node.Name.Equals("source", StringComparison.OrdinalIgnoreCase)))
            {
                return;
            }
            if (attr == null || !attr.Name.Equals("src", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            string url = ImageQuickInfo.GetFullUrl(attr.Value, session.TextView.TextBuffer);

            if (string.IsNullOrEmpty(url))
            {
                return;
            }

            applicableToSpan = session.TextView.TextBuffer.CurrentSnapshot.CreateTrackingSpan(point.Value.Position, 1, SpanTrackingMode.EdgeNegative);

            ImageQuickInfo.AddImageContent(qiContent, url);
        }
示例#10
0
        private void FormatTag(ElementNode element)
        {
            try
            {
                var schemas = GetSchemas();

                element = GetFirstBlockParent(element, schemas);

                HtmlEditorDocument document = HtmlEditorDocument.TryFromTextView(TextView);

                if (document == null)
                {
                    return;
                }

                ITextBuffer  buffer = document.TextBuffer;
                SnapshotSpan span   = new SnapshotSpan(buffer.CurrentSnapshot, element.Start, element.Length);

                _formatter.FormatRange(TextView, buffer, span, true);
            }
            catch
            { }
        }