Exemplo n.º 1
0
        public static LocationPreviewItem Create(VsProjectAnalyzer analyzer, FilePreviewItem parent, LocationInfo locationInfo, VariableType type)
        {
            Debug.Assert(locationInfo.StartColumn >= 1, "Invalid location info (Column)");
            Debug.Assert(locationInfo.StartLine >= 1, "Invalid location info (Line)");

            var origName = parent?.Engine?.OriginalName;

            if (string.IsNullOrEmpty(origName))
            {
                return(null);
            }

            var analysis = analyzer.GetAnalysisEntryFromUri(locationInfo.DocumentUri) ??
                           analyzer.GetAnalysisEntryFromPath(locationInfo.FilePath);

            if (analysis == null)
            {
                return(null);
            }

            var text = analysis.GetLine(locationInfo.StartLine);

            if (string.IsNullOrEmpty(text))
            {
                return(null);
            }

            int start, length;

            if (!GetSpan(text, origName, locationInfo, out start, out length))
            {
                // Name does not match exactly, so we might be renaming a prefixed name
                var prefix = parent.Engine.PrivatePrefix;
                if (string.IsNullOrEmpty(prefix))
                {
                    // No prefix available, so don't rename this
                    return(null);
                }

                var newName = parent.Engine.Request.Name;
                if (string.IsNullOrEmpty(newName))
                {
                    // No incoming name
                    Debug.Fail("No incoming name");
                    return(null);
                }

                if (!GetSpanWithPrefix(text, origName, locationInfo, prefix, newName, out start, out length))
                {
                    // Not renaming a prefixed name
                    return(null);
                }
            }

            if (start < 0 || length <= 0)
            {
                Debug.Fail("Expected valid span");
                return(null);
            }

            var trimText = text.TrimStart(_whitespace);

            return(new LocationPreviewItem(
                       parent,
                       trimText,
                       locationInfo.StartLine,
                       start + 1,
                       new Span(start - (text.Length - trimText.Length), length)
                       ));
        }
Exemplo n.º 2
0
        public LocationPreviewItem(VsProjectAnalyzer analyzer, FilePreviewItem parent, LocationInfo locationInfo, VariableType type)
        {
            Debug.Assert(locationInfo.StartColumn >= 1, "Invalid location info (Column)");
            Debug.Assert(locationInfo.StartLine >= 1, "Invalid location info (Line)");
            _parent = parent;
            Type    = type;
            _text   = string.Empty;

            var origName = _parent?.Engine?.OriginalName;

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

            var analysis = analyzer.GetAnalysisEntryFromUri(locationInfo.DocumentUri) ??
                           analyzer.GetAnalysisEntryFromPath(locationInfo.FilePath);

            if (analysis == null)
            {
                return;
            }

            var text = analysis.GetLine(locationInfo.StartLine);

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

            int start, length;

            if (!GetSpan(text, origName, locationInfo, out start, out length))
            {
                // Name does not match exactly, so we should be renaming a prefixed name
                var prefix = parent.Engine.PrivatePrefix;
                if (string.IsNullOrEmpty(prefix))
                {
                    // No prefix available, so fail
                    Debug.Fail("Failed to find '{0}' in '{1}' because we had no private prefix".FormatInvariant(origName, text));
                    return;
                }

                var newName = parent.Engine.Request.Name;
                if (string.IsNullOrEmpty(newName))
                {
                    // No incoming name
                    Debug.Fail("No incoming name");
                    return;
                }

                if (!GetSpanWithPrefix(text, origName, locationInfo, "_" + prefix, newName, out start, out length))
                {
                    // Not renaming a prefixed name
                    Debug.Fail("Failed to find '{0}' in '{1}'".FormatInvariant(origName, text));
                    return;
                }
            }

            if (start < 0 || length <= 0)
            {
                Debug.Fail("Expected valid span");
                return;
            }

            _text  = text.TrimStart(_whitespace);
            Line   = locationInfo.StartLine;
            Column = start + 1;
            _span  = new Span(start - (text.Length - _text.Length), length);
        }