Example #1
0
        static void RetrieveRegions(ICompilationUnit cu, ICSharpCode.NRefactory.Parser.SpecialTracker tracker)
        {
            Stack <ICSharpCode.NRefactory.PreprocessingDirective> regionStartDirectives = new Stack <ICSharpCode.NRefactory.PreprocessingDirective>();

            foreach (ICSharpCode.NRefactory.PreprocessingDirective directive in tracker.CurrentSpecials.OfType <ICSharpCode.NRefactory.PreprocessingDirective>())
            {
                if (directive.Cmd.Equals("#region", StringComparison.OrdinalIgnoreCase))
                {
                    regionStartDirectives.Push(directive);
                }
                if (directive.Cmd.Equals("#end", StringComparison.OrdinalIgnoreCase)
                    // using StartsWith allows comments at end of line
                    // fixes http://community.sharpdevelop.net/forums/t/12252.aspx
                    && directive.Arg.StartsWith("region", StringComparison.OrdinalIgnoreCase) &&
                    regionStartDirectives.Any())
                {
                    ICSharpCode.NRefactory.PreprocessingDirective start = regionStartDirectives.Pop();
                    cu.FoldingRegions.Add(new FoldingRegion(start.Arg.TrimComments().Trim().Trim('"'), DomRegion.FromLocation(start.StartPosition, directive.EndPosition)));
                }
            }
        }
Example #2
0
        void RetrieveRegions(ICompilationUnit cu, ICSharpCode.NRefactory.Parser.SpecialTracker tracker)
        {
            for (int i = 0; i < tracker.CurrentSpecials.Count; ++i)
            {
                ICSharpCode.NRefactory.PreprocessingDirective directive = tracker.CurrentSpecials[i] as ICSharpCode.NRefactory.PreprocessingDirective;
                if (directive != null)
                {
                    if (directive.Cmd.Equals("#region", StringComparison.OrdinalIgnoreCase))
                    {
                        int deep = 1;
                        for (int j = i + 1; j < tracker.CurrentSpecials.Count; ++j)
                        {
                            ICSharpCode.NRefactory.PreprocessingDirective nextDirective = tracker.CurrentSpecials[j] as ICSharpCode.NRefactory.PreprocessingDirective;
                            if (nextDirective != null)
                            {
                                switch (nextDirective.Cmd.ToLowerInvariant())
                                {
                                case "#region":
                                    ++deep;
                                    break;

                                case "#end":
                                    if (nextDirective.Arg.Equals("region", StringComparison.OrdinalIgnoreCase))
                                    {
                                        --deep;
                                        if (deep == 0)
                                        {
                                            cu.FoldingRegions.Add(new FoldingRegion(directive.Arg.Trim('"'), DomRegion.FromLocation(directive.StartPosition, nextDirective.EndPosition)));
                                            goto end;
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                        end :;
                    }
                }
            }
        }
Example #3
0
        void RetrieveRegions(ICompilationUnit cu, ICSharpCode.NRefactory.Parser.SpecialTracker tracker)
        {
            for (int i = 0; i < tracker.CurrentSpecials.Count; ++i)
            {
                ICSharpCode.NRefactory.PreprocessingDirective directive = tracker.CurrentSpecials[i] as ICSharpCode.NRefactory.PreprocessingDirective;
                if (directive != null)
                {
                    if (directive.Cmd == "#region")
                    {
                        int deep = 1;
                        for (int j = i + 1; j < tracker.CurrentSpecials.Count; ++j)
                        {
                            ICSharpCode.NRefactory.PreprocessingDirective nextDirective = tracker.CurrentSpecials[j] as ICSharpCode.NRefactory.PreprocessingDirective;
                            if (nextDirective != null)
                            {
                                switch (nextDirective.Cmd)
                                {
                                case "#region":
                                    ++deep;
                                    break;

                                case "#endregion":
                                    --deep;
                                    if (deep == 0)
                                    {
                                        cu.FoldingRegions.Add(new FoldingRegion(directive.Arg.Trim(), DomRegion.FromLocation(directive.StartPosition, nextDirective.EndPosition)));
                                        goto end;
                                    }
                                    break;
                                }
                            }
                        }
                        end :;
                    }
                }
            }
        }
Example #4
0
			public object Visit (ICSharpCode.NRefactory.PreprocessingDirective directive, object data)
			{
				DomLocation loc = new DomLocation (directive.StartPosition.Line, directive.StartPosition.Column);
				switch (directive.Cmd) {
				case "#if":
					directive.Expression.AcceptVisitor (visitor, null);
					conditionalRegions.Push (new ConditionalRegion (visitor.Text));
					visitor.Reset ();
					ifBlocks.Push (directive);
					ConditionalRegion.Start = loc;
					break;
				case "#elif":
					CloseConditionBlock (new DomLocation (directive.LastLineEnd.Line, directive.LastLineEnd.Column));
					directive.Expression.AcceptVisitor (visitor, null);
					if (ConditionalRegion != null)
						ConditionalRegion.ConditionBlocks.Add (new ConditionBlock (visitor.Text, loc));
					visitor.Reset ();
					//						elifBlocks.Add (directive);
					break;
				case "#else":
					CloseConditionBlock (new DomLocation (directive.LastLineEnd.Line, directive.LastLineEnd.Column));
					if (ConditionalRegion != null)
						ConditionalRegion.ElseBlock = new DomRegion (loc, DomLocation.Empty);
					//						elseBlock = directive;
					break;
				case "#endif":
					DomLocation endLoc = new DomLocation (directive.LastLineEnd.Line, directive.LastLineEnd.Column);
					CloseConditionBlock (endLoc);
					if (ConditionalRegion != null && !ConditionalRegion.ElseBlock.Start.IsEmpty)
						ConditionalRegion.ElseBlock = new DomRegion (ConditionalRegion.ElseBlock.Start, endLoc);
					AddCurRegion (directive.EndPosition);
					if (ifBlocks.Count > 0) {
						ICSharpCode.NRefactory.PreprocessingDirective ifBlock = ifBlocks.Pop ();
						DomRegion dr = new DomRegion (ifBlock.StartPosition.Line, ifBlock.StartPosition.Column, directive.EndPosition.Line, directive.EndPosition.Column);
						result.Add (new FoldingRegion ("#if " + ifBlock.Arg.Trim (), dr, FoldType.UserRegion, false));
						foreach (ICSharpCode.NRefactory.PreprocessingDirective d in elifBlocks) {
							dr.Start = new DomLocation (d.StartPosition.Line, d.StartPosition.Column);
							result.Add (new FoldingRegion ("#elif " + ifBlock.Arg.Trim (), dr, FoldType.UserRegion, false));
						}
						if (elseBlock != null) {
							dr.Start = new DomLocation (elseBlock.StartPosition.Line, elseBlock.StartPosition.Column);
							result.Add (new FoldingRegion ("#else", dr, FoldType.UserRegion, false));
						}
					}
					elseBlock = null;
					break;
				case "#define":
					result.Add (new PreProcessorDefine (directive.Arg, loc));
					break;
				case "#region":
					regions.Push (directive);
					break;
				case "#endregion":
					if (regions.Count > 0) {
						ICSharpCode.NRefactory.PreprocessingDirective start = regions.Pop ();
						DomRegion dr = new DomRegion (start.StartPosition.Line, start.StartPosition.Column, directive.EndPosition.Line, directive.EndPosition.Column);
						result.Add (new FoldingRegion (start.Arg, dr, FoldType.UserRegion, true));
					}
					break;
				}
				return null;
			}