Пример #1
0
        private bool TryGetPath(out string path)
        {
            int position = TextView.Caret.Position.BufferPosition.Position;

            path = null;

            ElementNode   element = null;
            AttributeNode attr    = null;

            _tree.GetPositionElement(position, out element, out attr);

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

            attr = element.GetAttribute("src") ?? element.GetAttribute("href");

            if (attr != null)
            {
                path = attr.Value;
                return(true);
            }

            return(false);
        }
Пример #2
0
        protected override bool Execute(VSConstants.VSStd2KCmdID commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (_tree == null || _broker.IsCompletionActive(TextView) || !IsValidTextBuffer() || !WESettings.Instance.Html.EnableEnterFormat)
            {
                return(false);
            }

            int              position = TextView.Caret.Position.BufferPosition.Position;
            SnapshotPoint    point    = new SnapshotPoint(TextView.TextBuffer.CurrentSnapshot, position);
            IWpfTextViewLine line     = TextView.GetTextViewLineContainingBufferPosition(point);

            ElementNode   element = null;
            AttributeNode attr    = null;

            _tree.GetPositionElement(position, out element, out attr);

            if (element == null ||
                _tree.IsDirty ||
                element.Parent == null ||
                element.StartTag.Contains(position) ||
                line.End.Position == position || // caret at end of line (TODO: add ignore whitespace logic)
                TextView.TextBuffer.CurrentSnapshot.GetText(element.InnerRange.Start, element.InnerRange.Length).Trim().Length == 0)
            {
                return(false);
            }

            UpdateTextBuffer(element);

            return(false);
        }
Пример #3
0
        private bool TryGetClassName(out string className)
        {
            int position = TextView.Caret.Position.BufferPosition.Position;

            className = null;

            ElementNode   element = null;
            AttributeNode attr    = null;

            _tree.GetPositionElement(position, out element, out attr);

            if (attr == null || attr.Name != "class")
            {
                return(false);
            }

            int beginning = position - attr.ValueRangeUnquoted.Start;
            int start     = attr.Value.LastIndexOf(' ', beginning) + 1;
            int length    = attr.Value.IndexOf(' ', start) - start;

            if (length < 0)
            {
                length = attr.ValueRangeUnquoted.Length - start;
            }

            className = attr.Value.Substring(start, length);

            return(true);
        }
Пример #4
0
        private ElementNode NodeAtCaret(HtmlEditorTree tree)
        {
            int start = _view.Caret.Position.BufferPosition.Position;

            ElementNode   tag  = null;
            AttributeNode attr = null;

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

            return(tag);
        }
Пример #5
0
        /// <summary>
        /// Finds a single property value at a given position in the tree.
        /// </summary>
        /// <param name="buffer">Any HTML editor tree</param>
        /// <param name="position">The position in the buffer</param>
        /// <param name="attributeNames">One or more HTML attribute names, such as 'class', 'id', 'src' etc.</param>
        /// <returns>A single value matching the position in the tree</returns>
        public static string GetSinglePropertyValue(HtmlEditorTree tree, int position, params string[] attributeNames)
        {
            ElementNode element = null;
            AttributeNode attr = null;

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

            if (attr == null || !attributeNames.Contains(attr.Name, StringComparer.OrdinalIgnoreCase))
                return null;

            int beginning = position - attr.ValueRangeUnquoted.Start;
            int start = attr.Value.LastIndexOf(' ', beginning) + 1;
            int length = attr.Value.IndexOf(' ', start) - start;

            if (length < 0)
                length = attr.ValueRangeUnquoted.Length - start;

            return attr.Value.Substring(start, length);
        }
Пример #6
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);
        }
Пример #7
0
        /// <summary>
        /// Finds a single property value at a given position in the tree.
        /// </summary>
        /// <param name="buffer">Any HTML editor tree</param>
        /// <param name="position">The position in the buffer</param>
        /// <param name="attributeNames">One or more HTML attribute names, such as 'class', 'id', 'src' etc.</param>
        /// <returns>A single value matching the position in the tree</returns>
        public static string GetSinglePropertyValue(HtmlEditorTree tree, int position, params string[] attributeNames)
        {
            tree.GetPositionElement(position, out ElementNode element, out AttributeNode attr);

            if (attr == null || !attributeNames.Contains(attr.Name, StringComparer.OrdinalIgnoreCase))
            {
                return(null);
            }

            int beginning = position - attr.ValueRangeUnquoted.Start;
            int start     = attr.Value.LastIndexOf(' ', beginning) + 1;
            int length    = attr.Value.IndexOf(' ', start) - start;

            if (length < 0)
            {
                length = attr.ValueRangeUnquoted.Length - start;
            }

            return(attr.Value.Substring(start, length));
        }
        protected override bool Execute(uint commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            int              position = TextView.Caret.Position.BufferPosition.Position;
            SnapshotPoint    point    = new SnapshotPoint(TextView.TextBuffer.CurrentSnapshot, position);
            IWpfTextViewLine line     = TextView.GetTextViewLineContainingBufferPosition(point);

            ElementNode   element = null;
            AttributeNode attr    = null;

            _tree.GetPositionElement(position, out element, out attr);

            if (element == null ||
                line.End.Position == position || // caret at end of line (TODO: add ignore whitespace logic)
                TextView.TextBuffer.CurrentSnapshot.GetText(element.InnerRange.Start, element.InnerRange.Length).Trim().Length == 0)
            {
                return(false);
            }

            UpdateTextBuffer(element, position);

            return(false);
        }
Пример #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.FromTextView(session.TextView).HtmlEditorTree;

            ElementNode   node = null;
            AttributeNode attr = null;

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

            if (attr == null || (attr.Name != "href" && attr.Name != "src"))
            {
                return;
            }

            string url = GetFileName(attr.Value.Trim('\'', '"').TrimStart('~'));

            if (!string.IsNullOrEmpty(url))
            {
                applicableToSpan = session.TextView.TextBuffer.CurrentSnapshot.CreateTrackingSpan(point.Value.Position, 1, SpanTrackingMode.EdgeNegative);
                var image = CreateImage(url);
                if (image != null && image.Source != null)
                {
                    qiContent.Add(image);
                    qiContent.Add(Math.Round(image.Source.Width) + "x" + Math.Round(image.Source.Height));
                }
            }
        }
        private ElementNode NodeAtCaret(HtmlEditorTree tree)
        {
            int start = _view.Caret.Position.BufferPosition.Position;

            ElementNode tag = null;
            AttributeNode attr = null;

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

            return tag;
        }