Ejemplo n.º 1
0
        public int GetStateForLineStart(int lineNum, ITextSnapshot snapshot, string fileName, ProbeAppSettings appSettings)
        {
            if (_states.Count <= lineNum)
            {
                if (_states.Count == 0)
                {
                    _states.Add(0);
                    if (lineNum == 0)
                    {
                        return(0);
                    }
                }

                var stateLineNum = _states.Count - 1;
                var state        = _states[stateLineNum];

                var fileStore = CodeModel.FileStore.GetOrCreateForTextBuffer(snapshot.TextBuffer);
                if (fileStore == null)
                {
                    return(0);
                }

                var model = fileStore.GetMostRecentModel(appSettings, fileName, snapshot, "GetStateForLine()");
                _snapshot = snapshot;

                var tokenInfo = new ProbeClassifierScanner.TokenInfo();

                while (stateLineNum < lineNum)
                {
                    var line = _snapshot.GetLineFromLineNumber(stateLineNum);
                    if (line == null)
                    {
                        break;
                    }

                    _scanner.SetSource(line.GetTextIncludingLineBreak(), line.Start.Position, _snapshot, model);
                    while (_scanner.ScanTokenAndProvideInfoAboutIt(tokenInfo, ref state))
                    {
                        ;
                    }

                    _states.Add(state);
                    stateLineNum++;
                }

                if (_states.Count <= lineNum)
                {
                    return(0);
                }
            }

            return(_states[lineNum]);
        }
Ejemplo n.º 2
0
        public int GetStateForPosition(int pos, ITextSnapshot snapshot, string fileName, ProbeAppSettings appSettings)
        {
            _snapshot = snapshot;

            var line         = _snapshot.GetLineFromPosition(pos);
            var state        = GetStateForLineStart(line.LineNumber, snapshot, fileName, appSettings);
            var lineStartPos = line.Start.Position;

            var fileStore = CodeModel.FileStore.GetOrCreateForTextBuffer(snapshot.TextBuffer);

            if (fileStore == null)
            {
                return(0);
            }

            var model = fileStore.GetMostRecentModel(appSettings, fileName, snapshot, "GetStateForPosition");

            if (lineStartPos <= pos)
            {
                var lineText = line.GetTextIncludingLineBreak();
                if (pos - lineStartPos < lineText.Length)
                {
                    lineText = lineText.Substring(0, pos - lineStartPos);
                }

                _scanner.SetSource(lineText, line.Start.Position, line.Snapshot, model);

                var tokenInfo = new ProbeClassifierScanner.TokenInfo();
                while (_scanner.ScanTokenAndProvideInfoAboutIt(tokenInfo, ref state))
                {
                    ;
                }
            }

            return(state);
        }
Ejemplo n.º 3
0
        public IList <ClassificationSpan> GetClassificationSpans(SnapshotSpan span)         // from IClassifier
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            _snapshot = span.Snapshot;

            var appSettings = ProbeEnvironment.CurrentAppSettings;
            var fileName    = VsTextUtil.TryGetDocumentFileName(span.Snapshot.TextBuffer);

            var tracker   = TextBufferStateTracker.GetTrackerForTextBuffer(span.Snapshot.TextBuffer);
            var spans     = new List <ClassificationSpan>();
            var state     = tracker.GetStateForPosition(span.Start.Position, span.Snapshot, fileName, appSettings);
            var tokenInfo = new ProbeClassifierScanner.TokenInfo();

            var fileStore = CodeModel.FileStore.GetOrCreateForTextBuffer(span.Snapshot.TextBuffer);

            if (fileStore == null)
            {
                return(new List <ClassificationSpan>());
            }

            var model = fileStore.GetMostRecentModel(appSettings, fileName, span.Snapshot, "GetClassificationSpans");

            _scanner.SetSource(span.GetText(), span.Start.Position, span.Snapshot, model);

            var disableDeadCode = ProbeToolsPackage.Instance.EditorOptions.DisableDeadCode;

            DisabledSectionTracker disabledSectionTracker = null;

            if (disableDeadCode)
            {
                disabledSectionTracker = new DisabledSectionTracker(model.DisabledSections);
                if (disabledSectionTracker.SetOffset(_scanner.PositionOffset))
                {
                    state |= State.Disabled;
                }
                else
                {
                    state &= ~State.Disabled;
                }
            }
            else
            {
                state &= ~State.Disabled;
            }

            while (_scanner.ScanTokenAndProvideInfoAboutIt(tokenInfo, ref state))
            {
                var classificationType = GetClassificationType(tokenInfo.Type);
                if (classificationType != null)
                {
                    spans.Add(new ClassificationSpan(new SnapshotSpan(_snapshot, new Span(span.Start.Position + tokenInfo.StartIndex, tokenInfo.Length)), classificationType));
                }

                if (disableDeadCode)
                {
                    if (disabledSectionTracker.Advance(_scanner.PositionOffset + _scanner.Position))
                    {
                        state |= State.Disabled;
                    }
                    else
                    {
                        state &= ~State.Disabled;
                    }
                }
                else
                {
                    state &= ~State.Disabled;
                }
            }

            return(spans);
        }