Ejemplo n.º 1
0
        public override int AddFoldingRegion(RefactorerContext ctx, IType cls, string regionName)
        {
            IEditableTextFile buffer = ctx.GetFile(cls.CompilationUnit.FileName);
            int    pos       = GetNewMethodPosition(buffer, cls);
            string eolMarker = Environment.NewLine;

            if (cls.SourceProject != null)
            {
                TextStylePolicy policy = cls.SourceProject.Policies.Get <TextStylePolicy> ();
                if (policy != null)
                {
                    eolMarker = policy.GetEolMarker();
                }
            }

            int line, col;

            buffer.GetLineColumnFromPosition(pos, out line, out col);

            string indent = buffer.GetText(buffer.GetPositionFromLineColumn(line, 1), pos);

            string pre  = "#region " + regionName + eolMarker;
            string post = indent + "#endregion" + eolMarker;

            buffer.InsertText(pos, pre + post);
            return(pos + pre.Length);
        }
Ejemplo n.º 2
0
        protected override int GetVariableNamePosition(IEditableTextFile file, LocalVariable var)
        {
            int begin = file.GetPositionFromLineColumn(var.Region.Start.Line, var.Region.Start.Column);
            int end   = file.GetPositionFromLineColumn(var.Region.Start.Line, var.Region.End.Column);

            if (begin == -1 || end == -1)
            {
                return(-1);
            }

            string txt = file.GetText(begin, end);

            int i = 0;             /* = txt.IndexOf ('=');
                                    * if (i == -1)
                                    * i = txt.Length;*/

            int pos = -1;

            do
            {
                i = pos = txt.IndexOf(var.Name, i);
            } while ((pos > 0 && !Char.IsLetter(file.GetCharAt(pos - 1))) &&
                     (pos + txt.Length + 1 < file.Length) && !Char.IsLetterOrDigit(file.GetCharAt(pos + txt.Length + 1))
                     );
            if (pos == -1)
            {
                return(-1);
            }

            return(begin + pos);
        }
Ejemplo n.º 3
0
        public override IClass RenameClass(RefactorerContext ctx, IClass cls, string newName)
        {
            IEditableTextFile file = ctx.GetFile(cls.Region.FileName);

            if (file == null)
            {
                return(null);
            }

            int    pos1 = file.GetPositionFromLineColumn(cls.Region.BeginLine, cls.Region.BeginColumn);
            int    pos2 = file.GetPositionFromLineColumn(cls.Region.EndLine, cls.Region.EndColumn);
            string txt  = file.GetText(pos1, pos2);

            Regex targetExp = new Regex(@"\sclass\s*(" + cls.Name + @")\s", RegexOptions.Multiline);
            Match match     = targetExp.Match(" " + txt + " ");

            if (!match.Success)
            {
                return(null);
            }

            int pos = pos1 + match.Groups [1].Index - 1;

            file.DeleteText(pos, cls.Name.Length);
            file.InsertText(pos, newName);

            return(GetGeneratedClass(ctx, file, cls));
        }
		protected override int GetMemberNamePosition (IEditableTextFile file, IMember member)
		{
			int begin = file.GetPositionFromLineColumn (member.BodyRegion.Start.Line, member.BodyRegion.Start.Column);
			int end = file.GetPositionFromLineColumn (member.BodyRegion.End.Line, member.BodyRegion.End.Column);
			
			if (begin == -1 || end == -1)
				return -1;
			
			string txt = file.GetText (begin, end);
			string name = member.Name;
			int len = txt.Length;
			int pos = -1;
			if (member is IField) {
				// Fields are different because multiple fields can be declared
				// in the same region and might even reference each other
				// e.g. "public int fu, bar = 1, baz = bar;"
				do {
					if ((pos = txt.IndexOf (member.Name, pos + 1)) == -1)
						return -1;
				} while (!IsMatchedField (txt, member.Name, pos));
				
				return begin + pos;
			} else if (member is IMethod) {
				if ((len = txt.IndexOf ('(')) == -1)
					return -1;
				
				if (((IMethod) member).IsConstructor)
					name = member.DeclaringType.Name;
			} else if (member is IEvent) {
				// no variables to change
			} else if (member is IProperty) {
				if (((IProperty)member).IsIndexer && (len = txt.IndexOf ('[')) == -1)
					return -1;
			} else {
				return -1;
			}
			
			if ((pos = txt.LastIndexOf (name, len)) == -1)
				return -1;
			
			return begin + pos;
		}
		protected override int GetParameterNamePosition (IEditableTextFile file, IParameter param)
		{
			IMember member = param.DeclaringMember;
			int begin = file.GetPositionFromLineColumn (member.BodyRegion.Start.Line, member.BodyRegion.Start.Column);
			int end = file.GetPositionFromLineColumn (member.BodyRegion.End.Line, member.BodyRegion.End.Column);
			
			if (begin == -1 || end == -1)
				return -1;
			
			string txt = file.GetText (begin, end);
			int open, close, i, j;
			char obrace, cbrace;
			
			if (member is IProperty) { // indexer
				obrace = '[';
				cbrace = ']';
			} else {
				obrace = '(';
				cbrace = ')';
			}
			
			if ((open = txt.IndexOf (obrace)) == -1)
				return -1;
			
			if ((close = txt.LastIndexOf (cbrace)) == -1)
				return -1;
			
			open++;
			
			while (open < close) {
				if ((i = txt.IndexOf (param.Name, open)) == -1)
					return -1;
				
				if (!Char.IsWhiteSpace (txt[i - 1]))
					return -1;
				
				j = i + param.Name.Length;
				if (j == close || Char.IsWhiteSpace (txt[j]) || txt[j] == ',')
					return begin + i;
				
				if ((open = txt.IndexOf (',', i)) == -1)
					return -1;
				
				open++;
			}
			
			return -1;
		}
		protected override int GetVariableNamePosition (IEditableTextFile file, LocalVariable var)
		{
			int begin = file.GetPositionFromLineColumn (var.Region.Start.Line, var.Region.Start.Column);
			int end = file.GetPositionFromLineColumn (var.Region.Start.Line, var.Region.End.Column);
			
			if (begin == -1 || end == -1)
				return -1;
			
			string txt = file.GetText (begin, end);
			
			int i = 0; /* = txt.IndexOf ('=');
			if (i == -1)
				i = txt.Length;*/
			
			int pos = -1;
			do {
				i = pos = txt.IndexOf (var.Name, i);
			} while ( (pos > 0 && !Char.IsLetter (file.GetCharAt (pos - 1))) &&
			          (pos + txt.Length + 1 < file.Length )&& !Char.IsLetterOrDigit (file.GetCharAt (pos + txt.Length + 1))
			         );
			if (pos == -1)
				return -1;
			
			return begin + pos;
		}
Ejemplo n.º 7
0
        protected override int GetMemberNamePosition(IEditableTextFile file, IMember member)
        {
            int begin = file.GetPositionFromLineColumn(member.BodyRegion.Start.Line, member.BodyRegion.Start.Column);
            int end   = file.GetPositionFromLineColumn(member.BodyRegion.End.Line, member.BodyRegion.End.Column);

            if (begin == -1 || end == -1)
            {
                return(-1);
            }

            string txt  = file.GetText(begin, end);
            string name = member.Name;
            int    len  = txt.Length;
            int    pos  = -1;

            if (member is IField)
            {
                // Fields are different because multiple fields can be declared
                // in the same region and might even reference each other
                // e.g. "public int fu, bar = 1, baz = bar;"
                do
                {
                    if ((pos = txt.IndexOf(member.Name, pos + 1)) == -1)
                    {
                        return(-1);
                    }
                } while (!IsMatchedField(txt, member.Name, pos));

                return(begin + pos);
            }
            else if (member is IMethod)
            {
                if ((len = txt.IndexOf('(')) == -1)
                {
                    return(-1);
                }

                if (((IMethod)member).IsConstructor)
                {
                    name = member.DeclaringType.Name;
                }
            }
            else if (member is IEvent)
            {
                // no variables to change
            }
            else if (member is IProperty)
            {
                if (((IProperty)member).IsIndexer && (len = txt.IndexOf('[')) == -1)
                {
                    return(-1);
                }
            }
            else
            {
                return(-1);
            }

            if ((pos = txt.LastIndexOf(name, len)) == -1)
            {
                return(-1);
            }

            return(begin + pos);
        }
Ejemplo n.º 8
0
        protected override int GetParameterNamePosition(IEditableTextFile file, IParameter param)
        {
            IMember member = param.DeclaringMember;
            int     begin  = file.GetPositionFromLineColumn(member.BodyRegion.Start.Line, member.BodyRegion.Start.Column);
            int     end    = file.GetPositionFromLineColumn(member.BodyRegion.End.Line, member.BodyRegion.End.Column);

            if (begin == -1 || end == -1)
            {
                return(-1);
            }

            string txt = file.GetText(begin, end);
            int    open, close, i, j;
            char   obrace, cbrace;

            if (member is IProperty)               // indexer
            {
                obrace = '[';
                cbrace = ']';
            }
            else
            {
                obrace = '(';
                cbrace = ')';
            }

            if ((open = txt.IndexOf(obrace)) == -1)
            {
                return(-1);
            }

            if ((close = txt.LastIndexOf(cbrace)) == -1)
            {
                return(-1);
            }

            open++;

            while (open < close)
            {
                if ((i = txt.IndexOf(param.Name, open)) == -1)
                {
                    return(-1);
                }

                if (!Char.IsWhiteSpace(txt[i - 1]))
                {
                    return(-1);
                }

                j = i + param.Name.Length;
                if (j == close || Char.IsWhiteSpace(txt[j]) || txt[j] == ',')
                {
                    return(begin + i);
                }

                if ((open = txt.IndexOf(',', i)) == -1)
                {
                    return(-1);
                }

                open++;
            }

            return(-1);
        }
Ejemplo n.º 9
0
		protected virtual int GetNewFieldPosition (IEditableTextFile buffer, IType cls)
		{
			cls = GetMainPart (cls);
			if (cls.FieldCount == 0) {
				int sp = buffer.GetPositionFromLineColumn (cls.BodyRegion.Start.Line, cls.BodyRegion.Start.Column);
				int ep = buffer.GetPositionFromLineColumn (cls.BodyRegion.End.Line, cls.BodyRegion.End.Column);
				string s = buffer.GetText (sp, ep);
				int i = s.IndexOf ('{');
				if (i == -1) return -1;
				string ind = GetLineIndent (buffer, cls.BodyRegion.Start.Line) ;
				int pos;
				if (cls.BodyRegion.Start.Line == cls.BodyRegion.End.Line) {
					buffer.InsertText (sp + i + 1, "\n" + ind);
					pos = sp + i + 2;
				} else  {
					pos = GetNextLine (buffer, sp + i + 1);
//					buffer.InsertText (pos, ind + "\n");
//					pos += ind.Length + 1;
				}
				return EnsurePositionIsNotInRegionsAndIndented (cls.SourceProject as Project, buffer, ind + "\t", pos);
			} else {
				IField f = cls.Fields.Last ();
				int pos = buffer.GetPositionFromLineColumn (f.Location.Line, f.Location.Column);
				string ind = GetLineIndent (buffer, f.Location.Line);
				if (cls.BodyRegion.Start.Line == cls.BodyRegion.End.Line) {
					int sp = buffer.GetPositionFromLineColumn (cls.BodyRegion.Start.Line, cls.BodyRegion.Start.Column);
					int ep = buffer.GetPositionFromLineColumn (cls.BodyRegion.End.Line, cls.BodyRegion.End.Column);
					string s = buffer.GetText (sp, ep);
					int i = s.IndexOf ('}');
					if (i == -1) return -1;
//					buffer.InsertText (sp + i, "\n" + ind + "\t\n" + ind);
					pos = sp + i + ind.Length + 2;
				} else {
					pos = GetNextLine (buffer, pos);
				}
//				buffer.InsertText (pos, ind);
				return EnsurePositionIsNotInRegionsAndIndented (cls.SourceProject as Project, buffer, ind, pos);
			}
		}