示例#1
0
        public void NotifyAnalysisComplete(IDocumentAnalysis analysis)
        {
            lock (_syncObj) {
                if (analysis.Version < Analysis.Version)
                {
                    return;
                }

                Analysis    = analysis;
                GlobalScope = analysis.GlobalScope;

                // Derived classes can override OnAnalysisComplete if they want
                // to perform additional actions on the completed analysis such
                // as declare additional variables, etc.
                OnAnalysisComplete();
                ContentState = State.Analyzed;

                if (ModuleType != ModuleType.User)
                {
                    _buffer.Reset(_buffer.Version, string.Empty);
                }
            }

            // Do not report issues with libraries or stubs
            if (ModuleType == ModuleType.User)
            {
                _diagnosticsService?.Replace(Uri, analysis.Diagnostics, DiagnosticSource.Analysis);
            }

            NewAnalysis?.Invoke(this, EventArgs.Empty);
        }
 private void LoadContent(string content, int version)
 {
     if (ContentState < State.Loading)
     {
         try {
             content = content ?? LoadContent();
             _buffer.Reset(version, content);
             ContentState = State.Loaded;
         } catch (IOException) { } catch (UnauthorizedAccessException) { }
     }
 }
示例#3
0
        public void ResetDocumentBuffer()
        {
            var doc = new DocumentBuffer();

            doc.Reset(0, "");

            Assert.AreEqual("", doc.Text.ToString());

            doc.Update(new[] { new DocumentChangeSet(0, 1, new[] {
                    DocumentChange.Insert("text", SourceLocation.MinValue)
                }) });

            Assert.AreEqual("text", doc.Text.ToString());

            try {
                doc.Update(new[] { new DocumentChangeSet(1, 0, new[] {
                        DocumentChange.Delete(SourceLocation.MinValue, SourceLocation.MinValue.AddColumns(4))
                    }) });
                Assert.Fail("expected InvalidOperationException");
            } catch (InvalidOperationException) {
            }
            Assert.AreEqual("text", doc.Text.ToString());
            Assert.AreEqual(1, doc.Version);

            doc.Update(new[] { new DocumentChangeSet(1, 0, new[] {
                    new DocumentChange {
                        WholeBuffer = true
                    }
                }) });

            Assert.AreEqual("", doc.Text.ToString());
            Assert.AreEqual(0, doc.Version);
        }
        private void InitializeContent(string content, ModuleLoadOptions newOptions)
        {
            lock (AnalysisLock) {
                if (!_loaded)
                {
                    if (!newOptions.ShouldLoad())
                    {
                        return;
                    }
                    content = content ?? LoadContent(newOptions);
                    _buffer.Reset(0, content);
                    _loaded = true;
                }

                IsOpen     = (newOptions & ModuleLoadOptions.Open) == ModuleLoadOptions.Open;
                newOptions = newOptions | (IsOpen ? ModuleLoadOptions.Analyze : 0);

                var change        = (_options ^ newOptions);
                var startAnalysis = change.ShouldAnalyze() && _analysisTcs?.Task == null;
                var startParse    = change.ShouldParse() && _parsingTask == null;

                _options = newOptions;

                if (startAnalysis)
                {
                    _analysisTcs = new TaskCompletionSource <IDocumentAnalysis>();
                }

                if (startParse)
                {
                    Parse();
                }
            }
        }
示例#5
0
        public void ResetDocumentBuffer()
        {
            var doc = new DocumentBuffer();

            doc.Reset(0, string.Empty);
            Assert.AreEqual(string.Empty, doc.Text);

            doc.Update(new[] {
                DocumentChange.Insert("text", SourceLocation.MinValue)
            });

            Assert.AreEqual("text", doc.Text);
            Assert.AreEqual(1, doc.Version);

            doc.Reset(0, @"abcdef");

            Assert.AreEqual(@"abcdef", doc.Text);
            Assert.AreEqual(0, doc.Version);
        }
示例#6
0
        public void NewLines(string s, NewLineLocation[] expected)
        {
            var doc = new DocumentBuffer();

            doc.Reset(0, s);
            var nls = doc.GetNewLineLocations().ToArray();

            for (var i = 0; i < nls.Length; i++)
            {
                Assert.AreEqual(nls[i].Kind, expected[i].Kind);
                Assert.AreEqual(nls[i].EndIndex, expected[i].EndIndex);
            }
        }
示例#7
0
        public void BasicDocumentBuffer()
        {
            var doc = new DocumentBuffer();

            doc.Reset(0, @"def f(x):
    return

def g(y):
    return y * 2
");

            doc.Update(new DocumentChangeSet(0, 1, new[] {
                // We *should* batch adjacent insertions, but we should also
                // work fine even if we don't. Note that the insertion point
                // tracks backwards with previous insertions in the same version.
                // If each of these were in its own array, the location would
                // have to change for each.
                DocumentChange.Insert(")", new SourceLocation(2, 11)),
                DocumentChange.Insert("x", new SourceLocation(2, 11)),
                DocumentChange.Insert("(", new SourceLocation(2, 11)),
                DocumentChange.Insert("g", new SourceLocation(2, 11)),
                DocumentChange.Insert(" ", new SourceLocation(2, 11))
            }));

            doc.Text.Should().Contain("return g(x)");
            Assert.AreEqual(1, doc.Version);

            doc.Update(new[] {
                new DocumentChangeSet(1, 2, new [] {
                    DocumentChange.Delete(new SourceLocation(2, 14), new SourceLocation(2, 15))
                }),
                new DocumentChangeSet(2, 3, new [] {
                    DocumentChange.Insert("x * 2", new SourceLocation(2, 14))
                })
            });

            doc.Text.Should().Contain("return g(x * 2)");

            doc.Update(new DocumentChangeSet(3, 4, new[] {
                DocumentChange.Replace(new SourceLocation(2, 18), new SourceLocation(2, 19), "300")
            }));

            doc.Text.Should().Contain("return g(x * 300)");

            doc.Update(new DocumentChangeSet(4, 5, new[] {
                // Changes are out of order, but we should fix that automatically
                DocumentChange.Delete(new SourceLocation(2, 13), new SourceLocation(2, 22)),
                DocumentChange.Insert("#", new SourceLocation(2, 7))
            }));
            doc.Text.Should().Contain("re#turn g");
        }
示例#8
0
        public void InsertTopToBottom()
        {
            var doc = new DocumentBuffer();

            doc.Reset(0, @"linelinelineline");
            doc.Update(new[] {
                DocumentChange.Insert("\n", new SourceLocation(1, 1)),
                DocumentChange.Insert("1\n", new SourceLocation(2, 5)),
                DocumentChange.Insert("2\r", new SourceLocation(3, 5)),
                DocumentChange.Insert("3\r\n", new SourceLocation(4, 5)),
                DocumentChange.Insert("4\r\n", new SourceLocation(5, 5)),
            });
            Assert.AreEqual("\nline1\nline2\rline3\r\nline4\r\n", doc.Text);
        }
示例#9
0
        public void SequentialChanges()
        {
            var doc = new DocumentBuffer();

            doc.Reset(0, @"
line1
line2
line3
line4
");
            doc.Update(new[] {
                DocumentChange.Delete(new SourceSpan(2, 5, 3, 5)),
                DocumentChange.Delete(new SourceSpan(3, 5, 4, 5))
            });
            Assert.AreEqual(@"
line2
line4
", doc.Text);
        }
示例#10
0
        public void InsertMultipleDisjoint()
        {
            var doc = new DocumentBuffer();

            doc.Reset(0, @"
line
line
line
line
");
            doc.Update(new[] {
                DocumentChange.Insert("4", new SourceLocation(5, 5)),
                DocumentChange.Insert("3", new SourceLocation(4, 5)),
                DocumentChange.Insert("2", new SourceLocation(3, 5)),
                DocumentChange.Insert("1", new SourceLocation(2, 5)),
            });
            Assert.AreEqual(@"
line1
line2
line3
line4
", doc.Text);
        }
示例#11
0
        public void DeleteMultipleDisjoint()
        {
            var doc = new DocumentBuffer();

            doc.Reset(0, @"
line1
line2
line3
line4
");
            doc.Update(new[] {
                DocumentChange.Delete(new SourceSpan(5, 5, 5, 6)),
                DocumentChange.Delete(new SourceSpan(4, 5, 4, 6)),
                DocumentChange.Delete(new SourceSpan(3, 5, 3, 6)),
                DocumentChange.Delete(new SourceSpan(2, 5, 2, 6))
            });
            Assert.AreEqual(@"
line
line
line
line
", doc.Text);
        }
        public void ReplaceAllDocumentBuffer()
        {
            var doc = new DocumentBuffer();

            doc.Reset(0, string.Empty);
            Assert.AreEqual(string.Empty, doc.Text);

            doc.Update(new[] {
                DocumentChange.ReplaceAll("text")
            });

            Assert.AreEqual("text", doc.Text);
            Assert.AreEqual(1, doc.Version);

            doc.Update(new[] {
                DocumentChange.ReplaceAll("abcdef")
            });

            Assert.AreEqual(@"abcdef", doc.Text);
            Assert.AreEqual(2, doc.Version);

            doc.Update(new[] {
                DocumentChange.Insert("text", SourceLocation.MinValue),
                DocumentChange.ReplaceAll("1234")
            });

            Assert.AreEqual(@"1234", doc.Text);
            Assert.AreEqual(3, doc.Version);

            doc.Update(new[] {
                DocumentChange.ReplaceAll("1234"),
                DocumentChange.Insert("text", SourceLocation.MinValue)
            });

            Assert.AreEqual(@"text1234", doc.Text);
            Assert.AreEqual(4, doc.Version);
        }