Ejemplo n.º 1
0
        private void HandleBackgroundParseComplete(object sender, ParseResultEventArgs e)
        {
            AntlrParseResultEventArgs      antlrParseResultArgs = e as AntlrParseResultEventArgs;
            List <IEditorNavigationTarget> navigationTargets    = new List <IEditorNavigationTarget>();

            if (antlrParseResultArgs != null)
            {
                //// add the Global Scope type
                //{
                //    var name = "Global Scope";
                //    var navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Types);
                //    var span = new SnapshotSpan(e.Snapshot, new Span(0, e.Snapshot.Length));
                //    var seek = new SnapshotSpan(e.Snapshot, new Span(0, 0));
                //    var glyph = GetGlyph(StandardGlyphGroup.GlyphGroupNamespace, StandardGlyphItem.GlyphItemPublic);
                //    var target = new EditorNavigationTarget(name, navigationType, span, seek, glyph);
                //    navigationTargets.Add(target);
                //}

                IAstRuleReturnScope resultArgs = antlrParseResultArgs.Result as IAstRuleReturnScope;
                var    result      = resultArgs != null ? resultArgs.Tree as CommonTree : null;
                string packageName = string.Empty;

                if (result != null && result.Children != null)
                {
                    foreach (CommonTree child in result.Children)
                    {
                        if (child == null || string.IsNullOrEmpty(child.Text))
                        {
                            continue;
                        }

                        switch (child.Type)
                        {
                        case GoLexer.KW_PACKAGE:
                        {
                            packageName = ((CommonTree)child.Children[0]).Token.Text;
                            if (string.IsNullOrWhiteSpace(packageName))
                            {
                                continue;
                            }

                            var navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Types);
                            var startToken     = antlrParseResultArgs.Tokens[child.TokenStartIndex];
                            var stopToken      = antlrParseResultArgs.Tokens[child.TokenStopIndex];
                            //Span span = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                            //SnapshotSpan ruleSpan = new SnapshotSpan(e.Snapshot, span);
                            // applies to the whole file
                            var          span     = new SnapshotSpan(e.Snapshot, new Span(0, e.Snapshot.Length));
                            SnapshotSpan ruleSeek = new SnapshotSpan(e.Snapshot, new Span(((CommonTree)child.Children[0]).Token.StartIndex, 0));
                            var          glyph    = _provider.GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupModule, StandardGlyphItem.GlyphItemPublic);
                            navigationTargets.Add(new EditorNavigationTarget(packageName, navigationType, span, ruleSeek, glyph));
                        }
                        break;

                        case GoLexer.KW_TYPE:
                            // each child tree is a typeSpec, the root of which is an identifier that names the type
                            foreach (CommonTree typeSpec in child.Children)
                            {
                                var typeName = typeSpec.Token.Text;
                                if (string.IsNullOrWhiteSpace(typeName))
                                {
                                    continue;
                                }

                                for (ITree parent = typeSpec.Parent; parent != null; parent = parent.Parent)
                                {
                                    if (parent.Type == GoParser.TYPE_IDENTIFIER)
                                    {
                                        typeName = parent.Text + "." + typeName;
                                    }
                                }

                                if (!string.IsNullOrWhiteSpace(packageName))
                                {
                                    typeName = packageName + "." + typeName;
                                }

                                var          navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Types);
                                var          startToken     = antlrParseResultArgs.Tokens[typeSpec.TokenStartIndex];
                                var          stopToken      = antlrParseResultArgs.Tokens[typeSpec.TokenStopIndex];
                                Span         span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                                SnapshotSpan ruleSpan       = new SnapshotSpan(e.Snapshot, span);
                                SnapshotSpan ruleSeek       = new SnapshotSpan(e.Snapshot, new Span(typeSpec.Token.StartIndex, 0));
                                var          glyph          = _provider.GlyphService.GetGlyph(GetGlyphGroupForType(typeSpec), char.IsUpper(typeName[0]) ? StandardGlyphItem.GlyphItemPublic : StandardGlyphItem.GlyphItemPrivate);
                                navigationTargets.Add(new EditorNavigationTarget(typeName, navigationType, ruleSpan, ruleSeek, glyph));

                                if (typeSpec.ChildCount > 0 && typeSpec.Children[0].Type == GoLexer.KW_STRUCT && typeSpec.Children[0].ChildCount > 0)
                                {
                                    foreach (CommonTree fieldSpec in ((CommonTree)typeSpec.Children[0]).Children)
                                    {
                                        if (fieldSpec.Type != GoParser.FIELD_DECLARATION)
                                        {
                                            continue;
                                        }

                                        foreach (CommonTree fieldNameIdentifier in ((CommonTree)fieldSpec.GetFirstChildWithType(GoParser.AST_VARS)).Children)
                                        {
                                            string fieldName = fieldNameIdentifier.Text;
                                            navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Members);
                                            startToken     = antlrParseResultArgs.Tokens[fieldNameIdentifier.TokenStartIndex];
                                            stopToken      = antlrParseResultArgs.Tokens[fieldSpec.TokenStopIndex];
                                            span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                                            ruleSpan       = new SnapshotSpan(e.Snapshot, span);
                                            ruleSeek       = new SnapshotSpan(e.Snapshot, new Span(fieldNameIdentifier.Token.StartIndex, 0));
                                            glyph          = _provider.GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupField, char.IsUpper(fieldName[0]) ? StandardGlyphItem.GlyphItemPublic : StandardGlyphItem.GlyphItemPrivate);
                                            navigationTargets.Add(new EditorNavigationTarget(fieldName, navigationType, ruleSpan, ruleSeek, glyph));
                                        }
                                    }
                                }
                            }

                            break;

                        case GoLexer.KW_CONST:
                        case GoLexer.KW_VAR:
                            foreach (CommonTree spec in child.Children)
                            {
                                CommonTree decl = (CommonTree)spec.Children[0];
                                foreach (CommonTree nameToken in decl.Children)
                                {
                                    var name = nameToken.Token.Text;
                                    if (string.IsNullOrWhiteSpace(name))
                                    {
                                        continue;
                                    }

                                    var          navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Members);
                                    var          startToken     = antlrParseResultArgs.Tokens[nameToken.TokenStartIndex];
                                    var          stopToken      = antlrParseResultArgs.Tokens[nameToken.TokenStopIndex];
                                    Span         span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                                    SnapshotSpan ruleSpan       = new SnapshotSpan(e.Snapshot, span);
                                    SnapshotSpan ruleSeek       = new SnapshotSpan(e.Snapshot, new Span(nameToken.Token.StartIndex, 0));
                                    var          group          = (child.Type == GoLexer.KW_CONST) ? StandardGlyphGroup.GlyphGroupConstant : StandardGlyphGroup.GlyphGroupVariable;
                                    var          item           = char.IsUpper(name[0]) ? StandardGlyphItem.GlyphItemPublic : StandardGlyphItem.GlyphItemPrivate;
                                    var          glyph          = _provider.GlyphService.GetGlyph(group, item);
                                    navigationTargets.Add(new EditorNavigationTarget(name, navigationType, ruleSpan, ruleSeek, glyph));
                                }
                            }
                            break;

                        case GoLexer.KW_FUNC:
                        {
                            // the first child is either a receiver (method) or an identifier with the name of the function
                            var token = ((CommonTree)child.Children[0]).Token;
                            if (token.Type == GoLexer.METHOD_RECEIVER)
                            {
                                token = ((CommonTree)child.Children[1]).Token;
                            }

                            var functionName = token.Text;
                            if (string.IsNullOrWhiteSpace(functionName))
                            {
                                continue;
                            }

                            ITree receiver = child.GetFirstChildWithType(GoParser.METHOD_RECEIVER);
                            if (receiver != null)
                            {
                                string receiverName;
                                if (receiver.ChildCount >= 2)
                                {
                                    receiverName = receiver.GetChild(receiver.ChildCount - 2).Text;
                                }
                                else
                                {
                                    receiverName = "?";
                                }

                                functionName = receiverName + "." + functionName;
                            }

                            IEnumerable <string> args   = ProcessFunctionParameters(child);
                            string       sig            = string.Format("{0}({1})", functionName, string.Join(", ", args));
                            var          navigationType = EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Members);
                            var          startToken     = antlrParseResultArgs.Tokens[child.TokenStartIndex];
                            var          stopToken      = antlrParseResultArgs.Tokens[child.TokenStopIndex];
                            Span         span           = new Span(startToken.StartIndex, stopToken.StopIndex - startToken.StartIndex + 1);
                            SnapshotSpan ruleSpan       = new SnapshotSpan(e.Snapshot, span);
                            SnapshotSpan ruleSeek       = new SnapshotSpan(e.Snapshot, new Span(child.Token.StartIndex, 0));
                            var          glyph          = _provider.GlyphService.GetGlyph(StandardGlyphGroup.GlyphGroupMethod, char.IsUpper(functionName[0]) ? StandardGlyphItem.GlyphItemPublic : StandardGlyphItem.GlyphItemPrivate);
                            navigationTargets.Add(new EditorNavigationTarget(sig, navigationType, ruleSpan, ruleSeek, glyph));
                        }

                        break;

                        default:
                            continue;
                        }
                    }
                }
            }

            this._navigationTargets = navigationTargets;
            OnNavigationTargetsChanged(EventArgs.Empty);
        }
Ejemplo n.º 2
0
        public IEnumerable <IEditorNavigationType> GetNavigationTypes()
        {
            yield return(EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Types));

            yield return(EditorNavigationTypeRegistryService.GetEditorNavigationType(PredefinedEditorNavigationTypes.Members));
        }