Ejemplo n.º 1
0
        public void TypeRefFinding()
        {
            var pcl = ResolutionTests.CreateDefCtxt(@"module modA;

class A(T)
{
	int n;
	static int prop;
	static A!float statA;
}

void main()
{
	auto a = new A!int();
	a.n;
	A.prop = 3;
	int b = A.prop + 4;
	A!double.statA.statA = new A!double();
}
");

            var res = TypeReferenceFinder.Scan(pcl.ParseCache[0]["modA"], pcl);

            //Assert.AreEqual(6, res.TypeMatches.Count);
            //TODO: Correct variable recognization
        }
Ejemplo n.º 2
0
        void RefreshMarkers()
        {
            TypeReferencesResult res = null;

            try
            {
                var ParseCache = DCodeCompletionSupport.EnumAvailableModules(Document);

                res = TypeReferenceFinder.Scan(SyntaxTree, ParseCache);

                RemoveMarkers(false);

                var txtDoc = Document.Editor.Document;

                DocumentLine curLine = null;
                int          ln      = -1;
                int          len     = 0;
                foreach (var id in res.TypeMatches)
                {
                    var loc = DeepASTVisitor.ExtractIdLocation(id, out len);
                    if (ln != loc.Line)
                    {
                        curLine = Document.Editor.GetLine(ln = loc.Line);
                    }

                    var m = new HighlightMarker(txtDoc, curLine, "keyword.semantic.type", loc.Column, TypeReferenceFinder.ExtractId(id));
                    txtDoc.AddMarker(curLine, m);
                    markers.Add(m);
                }
            }
            catch
            {
            }
        }
Ejemplo n.º 3
0
        public void TypeRefFinding()
        {
            var modA = DParser.ParseString(@"module modA;
class A(T)
{
	int n;
	static int prop;
	static A!float statA;
}

void main()
{
	auto a = new A!int();
	a.n;
	A.prop = 3;
	int b = A.prop + 4;
	A!double.statA.statA = new A!double();
}
");

            var ed = new EditorData {
                SyntaxTree = modA,
                ParseCache = new D_Parser.Misc.ParseCacheView(new RootPackage[0])
            };

            var res = TypeReferenceFinder.Scan(ed, null, -1);

            Assert.That(res.Count, Is.GreaterThan(6));
        }
Ejemplo n.º 4
0
        private HashSet <ITypeDefinition> GetStaticInitializationDependencies(JsClass c, int pass)
        {
            // Consider the following reference locations:
            // Pass 1: static init statements, static methods, instance methods, constructors
            // Pass 2: static init statements, static methods
            // Pass 3: static init statements only

            var result = new HashSet <ITypeDefinition>();

            switch (pass)
            {
            case 1:
                foreach (var r in c.InstanceMethods.Where(m => m.Definition != null).SelectMany(m => TypeReferenceFinder.Analyze(m.Definition)))
                {
                    result.Add(r);
                }
                foreach (var r in c.NamedConstructors.Where(m => m.Definition != null).SelectMany(m => TypeReferenceFinder.Analyze(m.Definition)))
                {
                    result.Add(r);
                }
                if (c.UnnamedConstructor != null)
                {
                    foreach (var r in TypeReferenceFinder.Analyze(c.UnnamedConstructor))
                    {
                        result.Add(r);
                    }
                }
                goto case 2;

            case 2:
                foreach (var r in c.StaticMethods.Where(m => m.Definition != null).SelectMany(m => TypeReferenceFinder.Analyze(m.Definition)))
                {
                    result.Add(r);
                }
                goto case 3;

            case 3:
                foreach (var r in TypeReferenceFinder.Analyze(c.StaticInitStatements))
                {
                    result.Add(r);
                }
                break;

            default:
                throw new ArgumentException("pass");
            }
            return(result);
        }
Ejemplo n.º 5
0
        public void CanAnalyzeExpression()
        {
            var asm = Common.CreateMockAssembly();
            var t1  = Common.CreateMockTypeDefinition("Type", asm);
            var t2  = Common.CreateMockTypeDefinition("Type", asm);
            var t3  = Common.CreateMockTypeDefinition("Type", asm);

            var expr = JsExpression.Add(new JsTypeReferenceExpression(t1), JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t1), "X"), JsExpression.Add(new JsTypeReferenceExpression(t2), new JsTypeReferenceExpression(t3))));

            var refs = TypeReferenceFinder.Analyze(expr);

            Assert.That(refs, Has.Count.EqualTo(3));
            Assert.That(refs.Contains(t1));
            Assert.That(refs.Contains(t2));
            Assert.That(refs.Contains(t3));
        }
Ejemplo n.º 6
0
        protected override string Process(
            EditorData editorData, Tuple <int, bool> args)
        {
            int  endLine      = args.Item1;
            bool resolveTypes = args.Item2;

            try
            {
                var invalidCodeRegions       = new List <ISyntaxRegion>();
                var textLocationsToHighlight =
                    TypeReferenceFinder.Scan(editorData, editorData.CancelToken, resolveTypes, invalidCodeRegions);
                return(TextLocationsToIdentifierSpans(textLocationsToHighlight));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);                 // Log the error
                return(null);
            }
        }
Ejemplo n.º 7
0
        public void CanAnalyzeStatements()
        {
            var asm = Common.CreateMockAssembly();
            var t1  = Common.CreateMockTypeDefinition("Type", asm);
            var t2  = Common.CreateMockTypeDefinition("Type", asm);
            var t3  = Common.CreateMockTypeDefinition("Type", asm);

            var ast = new JsStatement[] {
                JsStatement.If(JsExpression.Member(new JsTypeReferenceExpression(t1), "X"), JsStatement.Block(
                                   JsExpression.Add(new JsTypeReferenceExpression(t2), new JsTypeReferenceExpression(t3))
                                   ),
                               null),
                JsExpression.Add(JsExpression.Number(1), new JsTypeReferenceExpression(t1)),
            };

            var refs = TypeReferenceFinder.Analyze(ast);

            Assert.That(refs, Has.Count.EqualTo(3));
            Assert.That(refs.Contains(t1));
            Assert.That(refs.Contains(t2));
            Assert.That(refs.Contains(t3));
        }
Ejemplo n.º 8
0
        void updateTypeHighlightings()
        {
            if (guiDoc == null)
            {
                return;
            }
            var parsedDoc = guiDoc.GetDDocument();

            if (parsedDoc == null || parsedDoc.IsInvalid)
            {
                return;
            }

            RemoveOldTypeMarkers(false);

            var token = cancelTokenSource.Token;

            var ed = MonoDevelop.D.Resolver.DResolverWrapper.CreateEditorData(guiDoc);
            var invalidCodeRegions = new List <ISyntaxRegion>();

            try
            {
                Dictionary <int, Dictionary <ISyntaxRegion, byte> > textLocationsToHighlight;

                bool dontCancel = false;

                if (CompletionOptions.Instance.CompletionTimeout > 0)
                {
                    Task.Factory.StartNew(() =>
                    {
                        Thread.Sleep(CompletionOptions.Instance.CompletionTimeout);
                        if (!dontCancel)
                        {
                            cancelTokenSource.Cancel();
                        }
                    });
                }

                textLocationsToHighlight = TypeReferenceFinder.Scan(ed, token, invalidCodeRegions);

                dontCancel = true;

                foreach (var sr in invalidCodeRegions)
                {
                    if (sr != null)
                    {
                        invalidCodeRegionTree.Add(CreateCondHighlightSegment(sr));
                    }
                }

                int off, len;

                if (textLocationsToHighlight != null)
                {
                    foreach (var kv in textLocationsToHighlight)
                    {
                        if (token.IsCancellationRequested)
                        {
                            return;
                        }

                        var line = doc.GetLine(kv.Key);
                        if (line == null)
                        {
                            continue;
                        }
                        foreach (var kvv in kv.Value)
                        {
                            var sr    = kvv.Key;
                            var ident = "";
                            if (sr is INode)
                            {
                                var n        = sr as INode;
                                var nameLine = n.NameLocation.Line == kv.Key ? line : doc.GetLine(n.NameLocation.Line);
                                off = nameLine.Offset + n.NameLocation.Column - 1;
                                len = n.Name.Length;
                            }
                            else if (sr is TemplateParameter)
                            {
                                var tp = sr as TemplateParameter;
                                if (tp.NameLocation.IsEmpty)
                                {
                                    continue;
                                }
                                var nameLine = tp.NameLocation.Line == kv.Key ? line : doc.GetLine(tp.NameLocation.Line);
                                off = nameLine.Offset + tp.NameLocation.Column - 1;
                                len = tp.Name.Length;
                            }
                            else
                            {
                                var templ = sr as TemplateInstanceExpression;
                                if (templ != null)
                                {
                                    ident = templ.TemplateId;
                                }
                                GetIdentifier(ref sr);
                                off = line.Offset + sr.Location.Column - 1;
                                len = sr.EndLocation.Column - sr.Location.Column;
                            }

                            segmentMarkerTree.Add(new TypeIdSegmMarker(ident, off, len, kvv.Value));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingService.LogError("Error during semantic highlighting", ex);
            }

            if (!token.IsCancellationRequested)
            {
                ForceRedrawEditor();
            }
        }
Ejemplo n.º 9
0
        void updateTypeHighlightings()
        {
            if (guiDoc == null)
            {
                return;
            }
            var parsedDoc = guiDoc.ParsedDocument as Parser.ParsedDModule;

            if (parsedDoc == null || parsedDoc.IsInvalid)
            {
                return;
            }

            RemoveOldTypeMarkers(false);

            var token = cancelTokenSource.Token;

            var ed = MonoDevelop.D.Resolver.DResolverWrapper.CreateEditorData(guiDoc);
            var invalidCodeRegions = new List <ISyntaxRegion>();

            try
            {
                Dictionary <int, Dictionary <ISyntaxRegion, byte> > textLocationsToHighlight;

                textLocationsToHighlight = TypeReferenceFinder.Scan(ed, invalidCodeRegions, System.Diagnostics.Debugger.IsAttached ? -1 : 500);

                foreach (var sr in invalidCodeRegions)
                {
                    if (sr != null)
                    {
                        invalidCodeRegionTree.Add(CreateCondHighlightSegment(sr));
                    }
                }

                int off, len;

                if (textLocationsToHighlight != null)
                {
                    foreach (var kv in textLocationsToHighlight)
                    {
                        if (token.IsCancellationRequested)
                        {
                            return;
                        }

                        var line = doc.GetLine(kv.Key);
                        if (line == null)
                        {
                            continue;
                        }
                        foreach (var kvv in kv.Value)
                        {
                            var sr    = kvv.Key;
                            var ident = "";
                            if (sr is INode)
                            {
                                var n        = sr as INode;
                                var nameLine = n.NameLocation.Line == kv.Key ? line : doc.GetLine(n.NameLocation.Line);
                                off = nameLine.Offset + n.NameLocation.Column - 1;
                                len = n.Name.Length;
                            }
                            else if (sr is TemplateParameter)
                            {
                                var tp = sr as TemplateParameter;
                                if (tp.NameLocation.IsEmpty)
                                {
                                    continue;
                                }
                                var nameLine = tp.NameLocation.Line == kv.Key ? line : doc.GetLine(tp.NameLocation.Line);
                                off = nameLine.Offset + tp.NameLocation.Column - 1;
                                len = tp.Name.Length;
                            }
                            else
                            {
                                var templ = sr as TemplateInstanceExpression;
                                if (templ != null)
                                {
                                    ident = templ.TemplateId;
                                }
                                GetIdentifier(ref sr);
                                off = line.Offset + sr.Location.Column - 1;
                                len = sr.EndLocation.Column - sr.Location.Column;
                            }

                            segmentMarkerTree.Add(new TypeIdSegmMarker(ident, off, len, kvv.Value));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingService.LogError("Error during semantic highlighting", ex);
            }

            if (!token.IsCancellationRequested)
            {
                Ide.DispatchService.GuiDispatch(() =>
                {
                    if (guiDoc.Editor != null)
                    {
                        guiDoc.Editor.Parent.TextViewMargin.PurgeLayoutCache();
                        guiDoc.Editor.Parent.QueueDraw();
                    }
                });
            }
        }